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