]>
Commit | Line | Data |
---|---|---|
e3f810a2 VS |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/streams/bstream.h | |
3 | // Purpose: Template class for testing base stream functions. | |
4 | // Author: Hans Van Leemputten | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) 2004 Hans Van Leemputten | |
526954c5 | 7 | // Licence: wxWindows licence |
e3f810a2 VS |
8 | /////////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | #ifndef _WX_TESTBSTREAM_H__ | |
11 | #define _WX_TESTBSTREAM_H__ | |
12 | ||
13 | #include "wx/cppunit.h" | |
e3f810a2 VS |
14 | |
15 | /////////////////////////////////////////////////////////////////////////////// | |
08776b09 | 16 | // Some macros preventing us from typing too much ;-) |
e3f810a2 VS |
17 | // |
18 | ||
19 | #define STREAM_TEST_NAME "Streams" | |
08776b09 VS |
20 | #define COMPOSE_TEST_NAME(Name) \ |
21 | STREAM_TEST_NAME "." #Name | |
e3f810a2 | 22 | #define STREAM_REGISTER_SUB_SUITE(Name) \ |
3e5f6c1c | 23 | extern CppUnit::Test* Get##Name##Suite(); \ |
e3f810a2 VS |
24 | suite->addTest(Get##Name##Suite()) |
25 | #define STREAM_IMPLEMENT_SUB_REGISTRATION_ROUTINE(Name) \ | |
3e5f6c1c | 26 | CppUnit::Test* Get##Name##Suite() { return Name::suite(); } |
e3f810a2 | 27 | #define STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(Name) \ |
08776b09 | 28 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Name, COMPOSE_TEST_NAME(Name) ); \ |
e3f810a2 VS |
29 | STREAM_IMPLEMENT_SUB_REGISTRATION_ROUTINE( Name ) |
30 | ||
e3f810a2 VS |
31 | /////////////////////////////////////////////////////////////////////////////// |
32 | // Template class that implements a test for all base stream functions. | |
33 | // | |
34 | ||
3e5f6c1c | 35 | template <class TStreamIn, class TStreamOut> class BaseStreamTestCase : public CppUnit::TestCase |
e3f810a2 VS |
36 | { |
37 | protected: | |
38 | typedef BaseStreamTestCase<TStreamIn, TStreamOut> StreamTestCase; | |
39 | ||
40 | class CleanupHelper | |
41 | { | |
42 | public: | |
43 | CleanupHelper(StreamTestCase *value) | |
44 | :m_pCleanup(value) | |
45 | {} | |
46 | ~CleanupHelper() | |
2d76b6d8 | 47 | { |
e3f810a2 VS |
48 | m_pCleanup->DeleteInStream(); |
49 | m_pCleanup->DeleteOutStream(); | |
50 | } | |
51 | private: | |
52 | StreamTestCase *m_pCleanup; | |
53 | }; | |
54 | friend class CleanupHelper; | |
55 | ||
56 | public: | |
57 | BaseStreamTestCase() | |
58 | :m_bSimpleTellITest(false), | |
59 | m_bSimpleTellOTest(false), | |
08776b09 | 60 | m_bSeekInvalidBeyondEnd(true), |
7735998c | 61 | m_bEofAtLastRead(true), |
e3f810a2 VS |
62 | m_pCurrentIn(NULL), |
63 | m_pCurrentOut(NULL) | |
64 | { /* Nothing extra */ } | |
65 | virtual ~BaseStreamTestCase() | |
66 | { | |
67 | // Prevent mem leaks! | |
68 | delete m_pCurrentIn; | |
69 | delete m_pCurrentOut; | |
2d76b6d8 | 70 | } |
e3f810a2 VS |
71 | |
72 | protected: | |
73 | /* | |
74 | * Input stream tests. | |
75 | */ | |
76 | ||
77 | // Just try to perform a GetSize() on the input stream. | |
78 | void Input_GetSize() | |
79 | { | |
80 | CleanupHelper cleanup(this); | |
81 | const TStreamIn &stream_in = CreateInStream(); | |
82 | CPPUNIT_ASSERT(!stream_in.Eof()); | |
83 | ||
84 | // Size should be greater then zero. | |
85 | // Note: streams not supporting this should register this test | |
86 | // with CPPUNIT_TEST_FAIL instead of CPPUNIT_TEST. | |
87 | CPPUNIT_ASSERT(stream_in.GetSize() != 0); | |
88 | } | |
89 | ||
90 | // Just try to perform a GetC() on the input stream. | |
91 | void Input_GetC() | |
92 | { | |
93 | CleanupHelper cleanup(this); | |
94 | TStreamIn &stream_in = CreateInStream(); | |
95 | CPPUNIT_ASSERT(!stream_in.Eof()); | |
96 | ||
97 | // If no exception occurs the test is successful. | |
98 | (void)stream_in.GetC(); | |
99 | } | |
100 | ||
101 | // Just try to perform a Read() on the input stream. | |
102 | void Input_Read() | |
103 | { | |
104 | CleanupHelper cleanup(this); | |
105 | TStreamIn &stream_in = CreateInStream(); | |
106 | CPPUNIT_ASSERT(!stream_in.Eof()); | |
107 | ||
108 | // Note: the input stream should at least be of min size +10! | |
109 | ||
2d76b6d8 | 110 | char buf[10]; |
e3f810a2 VS |
111 | (void)stream_in.Read(buf, 10); |
112 | ||
113 | CPPUNIT_ASSERT(!stream_in.Eof()); | |
114 | CPPUNIT_ASSERT(stream_in.IsOk()); | |
115 | ||
116 | // Test the stream version aswell. | |
117 | TStreamOut &stream_out = CreateOutStream(); | |
118 | (void)stream_in.Read(stream_out); | |
119 | ||
120 | // The output stream should have read the input stream till the end. | |
121 | CPPUNIT_ASSERT(stream_in.Eof()); | |
122 | } | |
123 | ||
2d76b6d8 | 124 | // Test and see what happens to the EOF when we |
e3f810a2 VS |
125 | // read after EOF was encountered. |
126 | void Input_Eof() | |
127 | { | |
128 | CleanupHelper cleanup(this); | |
129 | TStreamIn &stream_in = CreateInStream(); | |
130 | CPPUNIT_ASSERT(!stream_in.Eof()); | |
131 | // Double check to see if Eof it self doesn't changes the Eof status. | |
132 | CPPUNIT_ASSERT(!stream_in.Eof()); | |
133 | ||
134 | // Travel to the end of the stream. | |
135 | while(!stream_in.Eof()) | |
136 | { | |
bf451723 VZ |
137 | CPPUNIT_ASSERT_MESSAGE( "unexpected non-EOF stream error", |
138 | stream_in.IsOk() ); | |
139 | ||
e3f810a2 VS |
140 | // Read, we move one byte along. |
141 | (void)stream_in.GetC(); | |
7735998c VS |
142 | #if 0 |
143 | // EOF behaviour is different in streams, disabled (for now?) | |
144 | ||
145 | if (m_bEofAtLastRead) | |
146 | // EOF should only occure after the last successful get. | |
147 | CPPUNIT_ASSERT_MESSAGE("Eof is detected too late.", !(stream_in.LastRead() != 1 && stream_in.Eof())); | |
148 | else | |
149 | // EOF should only occure after a failed get. | |
150 | CPPUNIT_ASSERT_MESSAGE("Eof is detected too soon.", !(stream_in.LastRead() == 1 && stream_in.Eof())); | |
151 | #endif | |
e3f810a2 VS |
152 | } |
153 | ||
7735998c | 154 | // Check EOF stream state. |
2d76b6d8 | 155 | CPPUNIT_ASSERT_MESSAGE("EOF is not EOF?", stream_in.Eof()); |
7735998c | 156 | |
4c51a665 | 157 | // Ok we found the end, let's see if we can go past it. |
e3f810a2 VS |
158 | for (size_t i = 0; i < 100; i++) |
159 | (void)stream_in.GetC(); | |
160 | ||
161 | // Check for EOF correctness. | |
162 | CPPUNIT_ASSERT_MESSAGE("EOF is wrong when we read past EOF!", stream_in.Eof()); | |
7735998c | 163 | CPPUNIT_ASSERT_MESSAGE("Last error is not EOF while stream_in.Eof() is true", stream_in.GetLastError() == wxSTREAM_EOF); |
e3f810a2 VS |
164 | } |
165 | ||
166 | // Just try to perform a LastRead() on the input stream. | |
167 | void Input_LastRead() | |
168 | { | |
169 | CleanupHelper cleanup(this); | |
170 | TStreamIn &stream_in = CreateInStream(); | |
171 | CPPUNIT_ASSERT(!stream_in.Eof()); | |
172 | ||
173 | char buf[5]; | |
174 | (void)stream_in.Read(buf, 5); | |
91bde3ec | 175 | CPPUNIT_ASSERT_EQUAL(5, stream_in.LastRead()); |
e3f810a2 | 176 | (void)stream_in.GetC(); |
91bde3ec | 177 | CPPUNIT_ASSERT_EQUAL(1, stream_in.LastRead()); |
e3f810a2 VS |
178 | } |
179 | ||
2d76b6d8 VZ |
180 | void Input_CanRead() |
181 | { | |
182 | CleanupHelper cleanup(this); | |
183 | TStreamIn &stream_in = CreateInStream(); | |
184 | ||
185 | CPPUNIT_ASSERT( stream_in.CanRead() ); | |
186 | ||
187 | // read the entire contents | |
188 | (void)stream_in.Read(CreateOutStream()); | |
189 | ||
190 | CPPUNIT_ASSERT( !stream_in.CanRead() ); | |
191 | } | |
192 | ||
e3f810a2 VS |
193 | // Just try to perform a SeekI() on the input stream. |
194 | void Input_SeekI() | |
195 | { | |
196 | CleanupHelper cleanup(this); | |
197 | TStreamIn &stream_in = CreateInStream(); | |
8bd966d3 VZ |
198 | |
199 | CPPUNIT_ASSERT( stream_in.IsSeekable() ); | |
e3f810a2 VS |
200 | CPPUNIT_ASSERT(!stream_in.Eof()); |
201 | ||
202 | // Try to Seek in the stream... | |
203 | // Note: streams not supporting this should register this test | |
204 | // with CPPUNIT_TEST_FAIL instead of CPPUNIT_TEST. | |
a54b285e VZ |
205 | CPPUNIT_ASSERT_EQUAL(2, stream_in.SeekI(2, wxFromStart)); |
206 | CPPUNIT_ASSERT_EQUAL(4, stream_in.SeekI(2, wxFromCurrent)); | |
e3f810a2 | 207 | // Not sure the following line is correct, so test it differently. |
91bde3ec | 208 | //CPPUNIT_ASSERT_EQUAL(stream_in.GetSize()-2, stream_in.SeekI(-2, wxFromEnd)); |
e3f810a2 VS |
209 | CPPUNIT_ASSERT(stream_in.SeekI(-2, wxFromEnd) != wxInvalidOffset); |
210 | // Go beyond the stream size. | |
08776b09 | 211 | CPPUNIT_ASSERT((stream_in.SeekI(10, wxFromCurrent) == wxInvalidOffset) == m_bSeekInvalidBeyondEnd); |
e3f810a2 VS |
212 | } |
213 | ||
214 | // Just try to perform a TellI() on the input stream. | |
215 | void Input_TellI() | |
216 | { | |
217 | CleanupHelper cleanup(this); | |
218 | TStreamIn &stream_in = CreateInStream(); | |
8bd966d3 | 219 | |
e3f810a2 VS |
220 | CPPUNIT_ASSERT(!stream_in.Eof()); |
221 | ||
222 | // Try to Get the location in the stream... | |
91bde3ec | 223 | CPPUNIT_ASSERT_EQUAL(0, stream_in.TellI()); |
e3f810a2 | 224 | (void)stream_in.GetC(); |
91bde3ec | 225 | CPPUNIT_ASSERT_EQUAL(1, stream_in.TellI()); |
e3f810a2 VS |
226 | if (!m_bSimpleTellITest) |
227 | { | |
30984dea | 228 | wxFileOffset pos = stream_in.SeekI(5, wxFromStart); |
91bde3ec | 229 | CPPUNIT_ASSERT_EQUAL(pos, stream_in.TellI()); |
e3f810a2 | 230 | (void)stream_in.GetC(); |
91bde3ec | 231 | CPPUNIT_ASSERT_EQUAL(6, stream_in.TellI()); |
e3f810a2 | 232 | pos = stream_in.SeekI(2, wxFromCurrent); |
91bde3ec | 233 | CPPUNIT_ASSERT_EQUAL(pos, stream_in.TellI()); |
e3f810a2 | 234 | pos = stream_in.SeekI(5, wxFromStart); |
91bde3ec | 235 | CPPUNIT_ASSERT_EQUAL(pos, stream_in.TellI()); |
e3f810a2 VS |
236 | } |
237 | } | |
2d76b6d8 | 238 | |
e3f810a2 VS |
239 | // Just try to perform a Peek() on the input stream. |
240 | void Input_Peek() | |
241 | { | |
242 | CleanupHelper cleanup(this); | |
243 | TStreamIn &stream_in = CreateInStream(); | |
244 | ||
245 | // Test the full stream | |
08776b09 | 246 | while (stream_in.IsOk()) |
e3f810a2 | 247 | { |
e3f810a2 VS |
248 | char peekChar = stream_in.Peek(); |
249 | char getChar = stream_in.GetC(); | |
08776b09 | 250 | if (stream_in.LastRead() == 1) |
91bde3ec | 251 | CPPUNIT_ASSERT_EQUAL(getChar, peekChar); |
e3f810a2 VS |
252 | } |
253 | } | |
254 | ||
255 | // Just try to perform a Ungetch() on the input stream. | |
256 | void Input_Ungetch() | |
257 | { | |
258 | CleanupHelper cleanup(this); | |
259 | TStreamIn &stream_in = CreateInStream(); | |
260 | CPPUNIT_ASSERT(!stream_in.Eof()); | |
2d76b6d8 | 261 | |
e3f810a2 VS |
262 | const char *ungetstr = "test"; |
263 | size_t ungetsize = stream_in.Ungetch(ungetstr, strlen(ungetstr) + 1); | |
264 | if (ungetsize != 0) | |
265 | { | |
91bde3ec | 266 | CPPUNIT_ASSERT_EQUAL(strlen(ungetstr) + 1, ungetsize); |
e3f810a2 VS |
267 | char buf[10]; |
268 | (void)stream_in.Read(buf, ungetsize); | |
269 | CPPUNIT_ASSERT(strcmp(buf, ungetstr) == 0); | |
270 | } | |
271 | ||
272 | if (stream_in.Ungetch('a')) | |
273 | { | |
a54b285e | 274 | CPPUNIT_ASSERT_EQUAL(int('a'), stream_in.GetC()); |
e3f810a2 VS |
275 | } |
276 | } | |
277 | ||
278 | /* | |
279 | * Output stream tests. | |
280 | */ | |
281 | ||
282 | // Just try to perform a PutC() on the output stream. | |
283 | void Output_PutC() | |
284 | { | |
285 | CleanupHelper cleanup(this); | |
286 | TStreamOut &stream_out = CreateOutStream(); | |
287 | ||
8bd966d3 VZ |
288 | const char *buf = "Some text"; |
289 | const wxFileOffset len = strlen(buf); | |
290 | for ( int i = 0; i < len; i++ ) | |
e3f810a2 VS |
291 | stream_out.PutC(buf[i]); |
292 | ||
8bd966d3 VZ |
293 | if ( stream_out.IsSeekable() ) |
294 | CPPUNIT_ASSERT_EQUAL(len, stream_out.TellO()); | |
e3f810a2 VS |
295 | } |
296 | ||
297 | // Just try to perform a Write() on the output stream. | |
298 | void Output_Write() | |
299 | { | |
300 | CleanupHelper cleanup(this); | |
301 | TStreamOut &stream_out = CreateOutStream(); | |
302 | ||
303 | // Do the buffer version. | |
8bd966d3 VZ |
304 | const char *buf = "Some text"; |
305 | const wxFileOffset len = strlen(buf); | |
e3f810a2 | 306 | (void)stream_out.Write(buf, len); |
8bd966d3 VZ |
307 | if ( stream_out.IsSeekable() ) |
308 | CPPUNIT_ASSERT_EQUAL( len, stream_out.TellO() ); | |
e3f810a2 VS |
309 | |
310 | // Do the Stream version. | |
311 | TStreamIn &stream_in = CreateInStream(); | |
312 | (void)stream_out.Write(stream_in); | |
8bd966d3 VZ |
313 | |
314 | if ( stream_out.IsSeekable() ) | |
315 | CPPUNIT_ASSERT(stream_out.TellO() > len); | |
e3f810a2 VS |
316 | } |
317 | ||
318 | // Just try to perform a LastWrite() on the output stream. | |
319 | void Output_LastWrite() | |
320 | { | |
321 | CleanupHelper cleanup(this); | |
322 | TStreamOut &stream_out = CreateOutStream(); | |
323 | ||
8bd966d3 | 324 | const char *buf = "12345"; |
e3f810a2 | 325 | (void)stream_out.Write(buf, 5); |
91bde3ec | 326 | CPPUNIT_ASSERT_EQUAL(5, stream_out.LastWrite()); |
e3f810a2 | 327 | (void)stream_out.PutC('1'); |
91bde3ec | 328 | CPPUNIT_ASSERT_EQUAL(1, stream_out.LastWrite()); |
e3f810a2 VS |
329 | } |
330 | ||
331 | // Just try to perform a SeekO() on the output stream. | |
332 | void Output_SeekO() | |
333 | { | |
334 | CleanupHelper cleanup(this); | |
335 | TStreamOut &stream_out = CreateOutStream(); | |
2d76b6d8 | 336 | |
8bd966d3 VZ |
337 | CPPUNIT_ASSERT( stream_out.IsSeekable() ); |
338 | ||
e3f810a2 | 339 | // First put some data in the stream, so it is not empty. |
8bd966d3 | 340 | const char *buf = "1234567890"; |
e3f810a2 VS |
341 | (void)stream_out.Write(buf, 10); |
342 | ||
343 | // Try to Seek in the stream... | |
344 | // Note: streams not supporting this should register this test | |
345 | // with CPPUNIT_TEST_FAIL instead of CPPUNIT_TEST. | |
91bde3ec VZ |
346 | CPPUNIT_ASSERT_EQUAL(2, stream_out.SeekO(2, wxFromStart)); |
347 | CPPUNIT_ASSERT_EQUAL(4, stream_out.SeekO(2, wxFromCurrent)); | |
e3f810a2 | 348 | // Not sure the following line is correct, so test it differently. |
91bde3ec | 349 | //CPPUNIT_ASSERT_EQUAL(stream_in.GetSize()-2, stream_out.SeekO(-2, wxFromEnd)); |
e3f810a2 VS |
350 | CPPUNIT_ASSERT(stream_out.SeekO(-2, wxFromEnd) != wxInvalidOffset); |
351 | // Go beyond the stream size. | |
08776b09 | 352 | CPPUNIT_ASSERT((stream_out.SeekO(10, wxFromCurrent) == wxInvalidOffset) == m_bSeekInvalidBeyondEnd); |
e3f810a2 VS |
353 | } |
354 | ||
355 | // Just try to perform a TellO() on the output stream. | |
356 | void Output_TellO() | |
357 | { | |
358 | CleanupHelper cleanup(this); | |
359 | TStreamOut &stream_out = CreateOutStream(); | |
360 | ||
361 | // Try to Get the location in the stream... | |
91bde3ec | 362 | CPPUNIT_ASSERT_EQUAL(0, stream_out.TellO()); |
e3f810a2 | 363 | (void)stream_out.PutC('1'); |
91bde3ec | 364 | CPPUNIT_ASSERT_EQUAL(1, stream_out.TellO()); |
e3f810a2 VS |
365 | if (!m_bSimpleTellOTest) |
366 | { | |
367 | // First put some extra data in the stream, so it's not empty. | |
8bd966d3 | 368 | const char *buf = "1234567890"; |
e3f810a2 | 369 | (void)stream_out.Write(buf, 10); |
2d76b6d8 | 370 | |
9540df9c | 371 | wxFileOffset pos = stream_out.SeekO(5, wxFromStart); |
91bde3ec | 372 | CPPUNIT_ASSERT_EQUAL(pos, stream_out.TellO()); |
e3f810a2 | 373 | (void)stream_out.PutC('1'); |
91bde3ec | 374 | CPPUNIT_ASSERT_EQUAL(6, stream_out.TellO()); |
e3f810a2 | 375 | pos = stream_out.SeekO(2, wxFromCurrent); |
91bde3ec | 376 | CPPUNIT_ASSERT_EQUAL(pos, stream_out.TellO()); |
e3f810a2 | 377 | pos = stream_out.SeekO(5, wxFromStart); |
91bde3ec | 378 | CPPUNIT_ASSERT_EQUAL(pos, stream_out.TellO()); |
e3f810a2 VS |
379 | } |
380 | } | |
381 | ||
382 | protected: | |
383 | // Some tests can be configured... here you can find the config settings | |
2d76b6d8 | 384 | bool m_bSimpleTellITest; // if true, no SeekI will be used by the TellI test. |
e3f810a2 | 385 | // Default false. |
2d76b6d8 | 386 | bool m_bSimpleTellOTest; // if true, no SeekO will be used by the TellI test. |
e3f810a2 | 387 | // Default false. |
08776b09 VS |
388 | bool m_bSeekInvalidBeyondEnd; // if true a SeekI|O beyond the end of the stream should return wxInvalidOffset |
389 | // Default true. | |
7735998c VS |
390 | bool m_bEofAtLastRead; // Does EOF occure at the moment the last byte is read or when read past the last byte. |
391 | // Default true. | |
e3f810a2 VS |
392 | protected: |
393 | TStreamIn &CreateInStream() | |
2d76b6d8 | 394 | { |
e3f810a2 VS |
395 | if (m_pCurrentIn) |
396 | { | |
9a83f860 | 397 | wxFAIL_MSG(wxT("Error in test case, the previouse input stream needs to be delete first!")); |
e3f810a2 VS |
398 | } |
399 | ||
400 | m_pCurrentIn = DoCreateInStream(); | |
401 | wxASSERT(m_pCurrentIn != NULL); | |
402 | return *m_pCurrentIn; | |
403 | } | |
404 | TStreamOut &CreateOutStream() | |
2d76b6d8 | 405 | { |
e3f810a2 VS |
406 | if (m_pCurrentOut) |
407 | { | |
9a83f860 | 408 | wxFAIL_MSG(wxT("Error in test case, the previouse output stream needs to be delete first!")); |
e3f810a2 VS |
409 | } |
410 | ||
411 | m_pCurrentOut = DoCreateOutStream(); | |
412 | wxASSERT(m_pCurrentOut != NULL); | |
413 | return *m_pCurrentOut; | |
414 | } | |
2d76b6d8 | 415 | |
e3f810a2 VS |
416 | void DeleteInStream() |
417 | { | |
418 | if (m_pCurrentIn == NULL) | |
419 | return; | |
420 | delete m_pCurrentIn; | |
421 | m_pCurrentIn = NULL; | |
422 | // Incase something extra needs to be done. | |
423 | DoDeleteInStream(); | |
424 | } | |
425 | void DeleteOutStream() | |
2d76b6d8 | 426 | { |
e3f810a2 VS |
427 | if (m_pCurrentOut == NULL) |
428 | return; | |
2d76b6d8 | 429 | |
8f0ff178 | 430 | CPPUNIT_ASSERT(m_pCurrentOut->Close()); |
2d76b6d8 | 431 | |
e3f810a2 VS |
432 | delete m_pCurrentOut; |
433 | m_pCurrentOut = NULL; | |
434 | // Incase something extra needs to be done. | |
435 | DoDeleteOutStream(); | |
436 | } | |
437 | ||
438 | protected: | |
439 | // Items that need to be implemented by a derived class! | |
440 | virtual TStreamIn *DoCreateInStream() = 0; | |
441 | virtual TStreamOut *DoCreateOutStream() = 0; | |
442 | virtual void DoDeleteInStream() { /* Depends on the base class */ } | |
443 | virtual void DoDeleteOutStream() { /* Depends on the base class */ } | |
444 | ||
445 | private: | |
446 | TStreamIn *m_pCurrentIn; | |
447 | TStreamOut *m_pCurrentOut; | |
448 | }; | |
449 | ||
450 | #endif | |
451 | ||
452 |