]>
Commit | Line | Data |
---|---|---|
340da6ae VS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/streams/memstream.cpp | |
3 | // Purpose: Test wxMemoryInputStream/wxMemoryOutputStream | |
4 | // Author: Hans Van Leemputten | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) 2004 Hans Van Leemputten | |
526954c5 | 7 | // Licence: wxWindows licence |
340da6ae VS |
8 | /////////////////////////////////////////////////////////////////////////////// |
9 | ||
340da6ae | 10 | // For compilers that support precompilation, includes "wx/wx.h". |
8899b155 RN |
11 | // and "wx/cppunit.h" |
12 | #include "testprec.h" | |
340da6ae VS |
13 | |
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | // for all others, include the necessary headers | |
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/wx.h" | |
21 | #endif | |
22 | ||
340da6ae VS |
23 | #include "wx/mstream.h" |
24 | ||
25 | #include "bstream.h" | |
26 | ||
340da6ae VS |
27 | #define DATABUFFER_SIZE 256 |
28 | ||
29 | /////////////////////////////////////////////////////////////////////////////// | |
30 | // The test case | |
31 | // | |
32 | // Try to fully test wxMemoryInputStream and wxMemoryOutputStream | |
33 | ||
34 | class memStream : public BaseStreamTestCase<wxMemoryInputStream, wxMemoryOutputStream> | |
35 | { | |
36 | public: | |
37 | memStream(); | |
38 | virtual ~memStream(); | |
39 | ||
40 | CPPUNIT_TEST_SUITE(memStream); | |
41 | // Base class stream tests the memStream supports. | |
42 | CPPUNIT_TEST(Input_GetSize); | |
43 | CPPUNIT_TEST(Input_GetC); | |
44 | CPPUNIT_TEST(Input_Read); | |
45 | CPPUNIT_TEST(Input_Eof); | |
46 | CPPUNIT_TEST(Input_LastRead); | |
2d76b6d8 | 47 | CPPUNIT_TEST(Input_CanRead); |
340da6ae VS |
48 | CPPUNIT_TEST(Input_SeekI); |
49 | CPPUNIT_TEST(Input_TellI); | |
50 | CPPUNIT_TEST(Input_Peek); | |
51 | CPPUNIT_TEST(Input_Ungetch); | |
52 | ||
53 | CPPUNIT_TEST(Output_PutC); | |
54 | CPPUNIT_TEST(Output_Write); | |
55 | CPPUNIT_TEST(Output_LastWrite); | |
56 | CPPUNIT_TEST(Output_SeekO); | |
57 | CPPUNIT_TEST(Output_TellO); | |
58 | ||
59 | // Other test specific for Memory stream test case. | |
76447155 | 60 | CPPUNIT_TEST(Ctor_InFromIn); |
96461cc2 | 61 | CPPUNIT_TEST(Ctor_InFromOut); |
340da6ae VS |
62 | CPPUNIT_TEST_SUITE_END(); |
63 | ||
64 | protected: | |
65 | // Add own test here. | |
76447155 | 66 | void Ctor_InFromIn(); |
96461cc2 | 67 | void Ctor_InFromOut(); |
340da6ae VS |
68 | |
69 | private: | |
70 | const char *GetDataBuffer(); | |
71 | ||
72 | private: | |
73 | // Implement base class functions. | |
3e5f6c1c | 74 | virtual wxMemoryInputStream *DoCreateInStream(); |
340da6ae VS |
75 | virtual wxMemoryOutputStream *DoCreateOutStream(); |
76 | ||
77 | private: | |
78 | char m_DataBuffer[DATABUFFER_SIZE]; | |
79 | }; | |
80 | ||
81 | memStream::memStream() | |
82 | { | |
83 | // Init the data buffer. | |
84 | for (size_t i = 0; i < DATABUFFER_SIZE; i++) | |
85 | m_DataBuffer[i] = (i % 0xFF); | |
86 | } | |
87 | ||
88 | memStream::~memStream() | |
89 | { | |
90 | /* Nothing extra for now. */ | |
91 | } | |
92 | ||
93 | const char *memStream::GetDataBuffer() | |
94 | { | |
95 | return m_DataBuffer; | |
96 | } | |
97 | ||
3e5f6c1c WS |
98 | wxMemoryInputStream *memStream::DoCreateInStream() |
99 | { | |
340da6ae VS |
100 | wxMemoryInputStream *pMemInStream = new wxMemoryInputStream(GetDataBuffer(), DATABUFFER_SIZE); |
101 | CPPUNIT_ASSERT(pMemInStream->IsOk()); | |
102 | return pMemInStream; | |
103 | } | |
104 | wxMemoryOutputStream *memStream::DoCreateOutStream() | |
3e5f6c1c | 105 | { |
340da6ae VS |
106 | wxMemoryOutputStream *pMemOutStream = new wxMemoryOutputStream(); |
107 | CPPUNIT_ASSERT(pMemOutStream->IsOk()); | |
108 | return pMemOutStream; | |
109 | } | |
110 | ||
76447155 VZ |
111 | void memStream::Ctor_InFromIn() |
112 | { | |
113 | wxMemoryInputStream *pMemInStream1 = DoCreateInStream(); | |
114 | wxMemoryInputStream *pMemInStream2 = new wxMemoryInputStream(*pMemInStream1); | |
115 | CPPUNIT_ASSERT(pMemInStream2->IsOk()); | |
116 | CPPUNIT_ASSERT_EQUAL(pMemInStream1->GetLength(), pMemInStream2->GetLength()); | |
2d2dd913 | 117 | wxFileOffset len = pMemInStream2->GetLength(); |
76447155 VZ |
118 | char *dat = new char[len]; |
119 | pMemInStream2->Read(dat, len); | |
2d2dd913 | 120 | CPPUNIT_ASSERT_EQUAL(len, (wxFileOffset)pMemInStream2->LastRead()); |
76447155 VZ |
121 | wxStreamBuffer *buf = pMemInStream1->GetInputStreamBuffer(); |
122 | void *pIn = buf->GetBufferStart(); | |
123 | CPPUNIT_ASSERT(memcmp(pIn, dat, len) == 0); | |
124 | delete pMemInStream2; | |
125 | ||
2d2dd913 | 126 | wxFileOffset len2 = len / 2; |
76447155 VZ |
127 | CPPUNIT_ASSERT(len2); |
128 | CPPUNIT_ASSERT(pMemInStream1->SeekI(-len2, wxFromCurrent) != wxInvalidOffset); | |
129 | pIn = buf->GetBufferPos(); | |
130 | pMemInStream2 = new wxMemoryInputStream(*pMemInStream1, len2); | |
131 | CPPUNIT_ASSERT(pMemInStream2->IsOk()); | |
132 | CPPUNIT_ASSERT_EQUAL((wxFileOffset)len2, pMemInStream2->GetLength()); | |
133 | pMemInStream2->Read(dat, len2); | |
2d2dd913 | 134 | CPPUNIT_ASSERT_EQUAL(len2, (wxFileOffset)pMemInStream2->LastRead()); |
76447155 VZ |
135 | CPPUNIT_ASSERT(memcmp(pIn, dat, len2) == 0); |
136 | ||
137 | delete[] dat; | |
138 | delete pMemInStream2; | |
139 | delete pMemInStream1; | |
140 | } | |
141 | ||
96461cc2 VZ |
142 | void memStream::Ctor_InFromOut() |
143 | { | |
144 | wxMemoryOutputStream *pMemOutStream = DoCreateOutStream(); | |
145 | pMemOutStream->Write(GetDataBuffer(), DATABUFFER_SIZE); | |
146 | wxMemoryInputStream *pMemInStream = new wxMemoryInputStream(*pMemOutStream); | |
147 | CPPUNIT_ASSERT(pMemInStream->IsOk()); | |
76447155 VZ |
148 | CPPUNIT_ASSERT_EQUAL(pMemInStream->GetLength(), pMemOutStream->GetLength()); |
149 | size_t len = pMemInStream->GetLength(); | |
96461cc2 VZ |
150 | wxStreamBuffer *in = pMemInStream->GetInputStreamBuffer(); |
151 | wxStreamBuffer *out = pMemOutStream->GetOutputStreamBuffer(); | |
152 | void *pIn = in->GetBufferStart(); | |
153 | void *pOut = out->GetBufferStart(); | |
154 | CPPUNIT_ASSERT(pIn != pOut); | |
155 | CPPUNIT_ASSERT(memcmp(pIn, pOut, len) == 0); | |
76447155 VZ |
156 | delete pMemInStream; |
157 | delete pMemOutStream; | |
96461cc2 | 158 | } |
340da6ae VS |
159 | |
160 | // Register the stream sub suite, by using some stream helper macro. | |
161 | // Note: Don't forget to connect it to the base suite (See: bstream.cpp => StreamCase::suite()) | |
162 | STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(memStream) |