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