allow using CPPUNIT_ASSERT_EQUAL(int, wxFileOffset) too
[wxWidgets.git] / tests / streams / bstream.h
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"
14
15 ///////////////////////////////////////////////////////////////////////////////
16 // Some macros preventing us from typing too much ;-)
17 //
18
19 #define STREAM_TEST_NAME "Streams"
20 #define COMPOSE_TEST_NAME(Name) \
21 STREAM_TEST_NAME "." #Name
22 #define STREAM_REGISTER_SUB_SUITE(Name) \
23 extern CppUnit::Test* Get##Name##Suite(); \
24 suite->addTest(Get##Name##Suite())
25 #define STREAM_IMPLEMENT_SUB_REGISTRATION_ROUTINE(Name) \
26 CppUnit::Test* Get##Name##Suite() { return Name::suite(); }
27 #define STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(Name) \
28 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Name, COMPOSE_TEST_NAME(Name) ); \
29 STREAM_IMPLEMENT_SUB_REGISTRATION_ROUTINE( Name )
30
31
32 ///////////////////////////////////////////////////////////////////////////////
33 // Template class that implements a test for all base stream functions.
34 //
35
36 template <class TStreamIn, class TStreamOut> class BaseStreamTestCase : public CppUnit::TestCase
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()
48 {
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),
61 m_bSeekInvalidBeyondEnd(true),
62 m_bEofAtLastRead(true),
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;
71 }
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
111 char buf[10];
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
125 // Test and see what happens to the EOF when we
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 {
138 CPPUNIT_ASSERT_MESSAGE( "unexpected non-EOF stream error",
139 stream_in.IsOk() );
140
141 // Read, we move one byte along.
142 (void)stream_in.GetC();
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
153 }
154
155 // Check EOF stream state.
156 CPPUNIT_ASSERT_MESSAGE("EOF is not EOF?", stream_in.Eof());
157
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());
164 CPPUNIT_ASSERT_MESSAGE("Last error is not EOF while stream_in.Eof() is true", stream_in.GetLastError() == wxSTREAM_EOF);
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_EQUAL(5, stream_in.LastRead());
177 (void)stream_in.GetC();
178 CPPUNIT_ASSERT_EQUAL(1, stream_in.LastRead());
179 }
180
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
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();
199
200 CPPUNIT_ASSERT( stream_in.IsSeekable() );
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_EQUAL(2, stream_in.SeekI(2, wxFromStart));
207 CPPUNIT_ASSERT_EQUAL(4, stream_in.SeekI(2, wxFromCurrent));
208 // Not sure the following line is correct, so test it differently.
209 //CPPUNIT_ASSERT_EQUAL(stream_in.GetSize()-2, stream_in.SeekI(-2, wxFromEnd));
210 CPPUNIT_ASSERT(stream_in.SeekI(-2, wxFromEnd) != wxInvalidOffset);
211 // Go beyond the stream size.
212 CPPUNIT_ASSERT((stream_in.SeekI(10, wxFromCurrent) == wxInvalidOffset) == m_bSeekInvalidBeyondEnd);
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();
220
221 // this test shouldn't be used at all if the stream isn't seekable
222 CPPUNIT_ASSERT( stream_in.IsSeekable() );
223
224 CPPUNIT_ASSERT(!stream_in.Eof());
225
226 // Try to Get the location in the stream...
227 CPPUNIT_ASSERT_EQUAL(0, stream_in.TellI());
228 (void)stream_in.GetC();
229 CPPUNIT_ASSERT_EQUAL(1, stream_in.TellI());
230 if (!m_bSimpleTellITest)
231 {
232 wxFileOffset pos = stream_in.SeekI(5, wxFromStart);
233 CPPUNIT_ASSERT_EQUAL(pos, stream_in.TellI());
234 (void)stream_in.GetC();
235 CPPUNIT_ASSERT_EQUAL(6, stream_in.TellI());
236 pos = stream_in.SeekI(2, wxFromCurrent);
237 CPPUNIT_ASSERT_EQUAL(pos, stream_in.TellI());
238 pos = stream_in.SeekI(5, wxFromStart);
239 CPPUNIT_ASSERT_EQUAL(pos, stream_in.TellI());
240 }
241 }
242
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
250 while (stream_in.IsOk())
251 {
252 char peekChar = stream_in.Peek();
253 char getChar = stream_in.GetC();
254 if (stream_in.LastRead() == 1)
255 CPPUNIT_ASSERT_EQUAL(getChar, peekChar);
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());
265
266 const char *ungetstr = "test";
267 size_t ungetsize = stream_in.Ungetch(ungetstr, strlen(ungetstr) + 1);
268 if (ungetsize != 0)
269 {
270 CPPUNIT_ASSERT_EQUAL(strlen(ungetstr) + 1, ungetsize);
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_EQUAL(int('a'), stream_in.GetC());
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
292 const char *buf = "Some text";
293 const wxFileOffset len = strlen(buf);
294 for ( int i = 0; i < len; i++ )
295 stream_out.PutC(buf[i]);
296
297 if ( stream_out.IsSeekable() )
298 CPPUNIT_ASSERT_EQUAL(len, stream_out.TellO());
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.
308 const char *buf = "Some text";
309 const wxFileOffset len = strlen(buf);
310 (void)stream_out.Write(buf, len);
311 if ( stream_out.IsSeekable() )
312 CPPUNIT_ASSERT_EQUAL( len, stream_out.TellO() );
313
314 // Do the Stream version.
315 TStreamIn &stream_in = CreateInStream();
316 (void)stream_out.Write(stream_in);
317
318 if ( stream_out.IsSeekable() )
319 CPPUNIT_ASSERT(stream_out.TellO() > len);
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
328 const char *buf = "12345";
329 (void)stream_out.Write(buf, 5);
330 CPPUNIT_ASSERT_EQUAL(5, stream_out.LastWrite());
331 (void)stream_out.PutC('1');
332 CPPUNIT_ASSERT_EQUAL(1, stream_out.LastWrite());
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();
340
341 CPPUNIT_ASSERT( stream_out.IsSeekable() );
342
343 // First put some data in the stream, so it is not empty.
344 const char *buf = "1234567890";
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_EQUAL(2, stream_out.SeekO(2, wxFromStart));
351 CPPUNIT_ASSERT_EQUAL(4, stream_out.SeekO(2, wxFromCurrent));
352 // Not sure the following line is correct, so test it differently.
353 //CPPUNIT_ASSERT_EQUAL(stream_in.GetSize()-2, stream_out.SeekO(-2, wxFromEnd));
354 CPPUNIT_ASSERT(stream_out.SeekO(-2, wxFromEnd) != wxInvalidOffset);
355 // Go beyond the stream size.
356 CPPUNIT_ASSERT((stream_out.SeekO(10, wxFromCurrent) == wxInvalidOffset) == m_bSeekInvalidBeyondEnd);
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
365 // If this test is used, the stream must be seekable
366 CPPUNIT_ASSERT( stream_out.IsSeekable() );
367
368 // Try to Get the location in the stream...
369 CPPUNIT_ASSERT_EQUAL(0, stream_out.TellO());
370 (void)stream_out.PutC('1');
371 CPPUNIT_ASSERT_EQUAL(1, stream_out.TellO());
372 if (!m_bSimpleTellOTest)
373 {
374 // First put some extra data in the stream, so it's not empty.
375 const char *buf = "1234567890";
376 (void)stream_out.Write(buf, 10);
377
378 off_t pos = stream_out.SeekO(5, wxFromStart);
379 CPPUNIT_ASSERT_EQUAL(pos, stream_out.TellO());
380 (void)stream_out.PutC('1');
381 CPPUNIT_ASSERT_EQUAL(6, stream_out.TellO());
382 pos = stream_out.SeekO(2, wxFromCurrent);
383 CPPUNIT_ASSERT_EQUAL(pos, stream_out.TellO());
384 pos = stream_out.SeekO(5, wxFromStart);
385 CPPUNIT_ASSERT_EQUAL(pos, stream_out.TellO());
386 }
387 }
388
389 protected:
390 // Some tests can be configured... here you can find the config settings
391 bool m_bSimpleTellITest; // if true, no SeekI will be used by the TellI test.
392 // Default false.
393 bool m_bSimpleTellOTest; // if true, no SeekO will be used by the TellI test.
394 // Default false.
395 bool m_bSeekInvalidBeyondEnd; // if true a SeekI|O beyond the end of the stream should return wxInvalidOffset
396 // Default true.
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.
399 protected:
400 TStreamIn &CreateInStream()
401 {
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()
412 {
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 }
422
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()
433 {
434 if (m_pCurrentOut == NULL)
435 return;
436
437 CPPUNIT_ASSERT(m_pCurrentOut->Close());
438
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