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