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