]> git.saurik.com Git - wxWidgets.git/blame - tests/streams/bstream.h
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / tests / streams / bstream.h
CommitLineData
e3f810a2
VS
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/streams/bstream.h
3// Purpose: Template class for testing base stream functions.
4// Author: Hans Van Leemputten
e3f810a2 5// Copyright: (c) 2004 Hans Van Leemputten
526954c5 6// Licence: wxWindows licence
e3f810a2
VS
7///////////////////////////////////////////////////////////////////////////////
8
9#ifndef _WX_TESTBSTREAM_H__
10#define _WX_TESTBSTREAM_H__
11
12#include "wx/cppunit.h"
e3f810a2
VS
13
14///////////////////////////////////////////////////////////////////////////////
08776b09 15// Some macros preventing us from typing too much ;-)
e3f810a2
VS
16//
17
18#define STREAM_TEST_NAME "Streams"
08776b09
VS
19#define COMPOSE_TEST_NAME(Name) \
20 STREAM_TEST_NAME "." #Name
e3f810a2 21#define STREAM_REGISTER_SUB_SUITE(Name) \
3e5f6c1c 22 extern CppUnit::Test* Get##Name##Suite(); \
e3f810a2
VS
23 suite->addTest(Get##Name##Suite())
24#define STREAM_IMPLEMENT_SUB_REGISTRATION_ROUTINE(Name) \
3e5f6c1c 25 CppUnit::Test* Get##Name##Suite() { return Name::suite(); }
e3f810a2 26#define STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(Name) \
08776b09 27 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Name, COMPOSE_TEST_NAME(Name) ); \
e3f810a2
VS
28 STREAM_IMPLEMENT_SUB_REGISTRATION_ROUTINE( Name )
29
e3f810a2
VS
30///////////////////////////////////////////////////////////////////////////////
31// Template class that implements a test for all base stream functions.
32//
33
3e5f6c1c 34template <class TStreamIn, class TStreamOut> class BaseStreamTestCase : public CppUnit::TestCase
e3f810a2
VS
35{
36protected:
37 typedef BaseStreamTestCase<TStreamIn, TStreamOut> StreamTestCase;
38
39 class CleanupHelper
40 {
41 public:
42 CleanupHelper(StreamTestCase *value)
43 :m_pCleanup(value)
44 {}
45 ~CleanupHelper()
2d76b6d8 46 {
e3f810a2
VS
47 m_pCleanup->DeleteInStream();
48 m_pCleanup->DeleteOutStream();
49 }
50 private:
51 StreamTestCase *m_pCleanup;
52 };
53 friend class CleanupHelper;
54
55public:
56 BaseStreamTestCase()
57 :m_bSimpleTellITest(false),
58 m_bSimpleTellOTest(false),
08776b09 59 m_bSeekInvalidBeyondEnd(true),
7735998c 60 m_bEofAtLastRead(true),
e3f810a2
VS
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;
2d76b6d8 69 }
e3f810a2
VS
70
71protected:
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
d13b34d3 83 // Size should be greater than zero.
e3f810a2
VS
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
2d76b6d8 109 char buf[10];
e3f810a2
VS
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
2d76b6d8 123 // Test and see what happens to the EOF when we
e3f810a2
VS
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 {
bf451723
VZ
136 CPPUNIT_ASSERT_MESSAGE( "unexpected non-EOF stream error",
137 stream_in.IsOk() );
138
e3f810a2
VS
139 // Read, we move one byte along.
140 (void)stream_in.GetC();
7735998c
VS
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
e3f810a2
VS
151 }
152
7735998c 153 // Check EOF stream state.
2d76b6d8 154 CPPUNIT_ASSERT_MESSAGE("EOF is not EOF?", stream_in.Eof());
7735998c 155
4c51a665 156 // Ok we found the end, let's see if we can go past it.
e3f810a2
VS
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());
7735998c 162 CPPUNIT_ASSERT_MESSAGE("Last error is not EOF while stream_in.Eof() is true", stream_in.GetLastError() == wxSTREAM_EOF);
e3f810a2
VS
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);
91bde3ec 174 CPPUNIT_ASSERT_EQUAL(5, stream_in.LastRead());
e3f810a2 175 (void)stream_in.GetC();
91bde3ec 176 CPPUNIT_ASSERT_EQUAL(1, stream_in.LastRead());
e3f810a2
VS
177 }
178
2d76b6d8
VZ
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
e3f810a2
VS
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();
8bd966d3
VZ
197
198 CPPUNIT_ASSERT( stream_in.IsSeekable() );
e3f810a2
VS
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.
a54b285e
VZ
204 CPPUNIT_ASSERT_EQUAL(2, stream_in.SeekI(2, wxFromStart));
205 CPPUNIT_ASSERT_EQUAL(4, stream_in.SeekI(2, wxFromCurrent));
e3f810a2 206 // Not sure the following line is correct, so test it differently.
91bde3ec 207 //CPPUNIT_ASSERT_EQUAL(stream_in.GetSize()-2, stream_in.SeekI(-2, wxFromEnd));
e3f810a2
VS
208 CPPUNIT_ASSERT(stream_in.SeekI(-2, wxFromEnd) != wxInvalidOffset);
209 // Go beyond the stream size.
08776b09 210 CPPUNIT_ASSERT((stream_in.SeekI(10, wxFromCurrent) == wxInvalidOffset) == m_bSeekInvalidBeyondEnd);
e3f810a2
VS
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();
8bd966d3 218
e3f810a2
VS
219 CPPUNIT_ASSERT(!stream_in.Eof());
220
221 // Try to Get the location in the stream...
91bde3ec 222 CPPUNIT_ASSERT_EQUAL(0, stream_in.TellI());
e3f810a2 223 (void)stream_in.GetC();
91bde3ec 224 CPPUNIT_ASSERT_EQUAL(1, stream_in.TellI());
e3f810a2
VS
225 if (!m_bSimpleTellITest)
226 {
30984dea 227 wxFileOffset pos = stream_in.SeekI(5, wxFromStart);
91bde3ec 228 CPPUNIT_ASSERT_EQUAL(pos, stream_in.TellI());
e3f810a2 229 (void)stream_in.GetC();
91bde3ec 230 CPPUNIT_ASSERT_EQUAL(6, stream_in.TellI());
e3f810a2 231 pos = stream_in.SeekI(2, wxFromCurrent);
91bde3ec 232 CPPUNIT_ASSERT_EQUAL(pos, stream_in.TellI());
e3f810a2 233 pos = stream_in.SeekI(5, wxFromStart);
91bde3ec 234 CPPUNIT_ASSERT_EQUAL(pos, stream_in.TellI());
e3f810a2
VS
235 }
236 }
2d76b6d8 237
e3f810a2
VS
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
08776b09 245 while (stream_in.IsOk())
e3f810a2 246 {
e3f810a2
VS
247 char peekChar = stream_in.Peek();
248 char getChar = stream_in.GetC();
08776b09 249 if (stream_in.LastRead() == 1)
91bde3ec 250 CPPUNIT_ASSERT_EQUAL(getChar, peekChar);
e3f810a2
VS
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());
2d76b6d8 260
e3f810a2
VS
261 const char *ungetstr = "test";
262 size_t ungetsize = stream_in.Ungetch(ungetstr, strlen(ungetstr) + 1);
263 if (ungetsize != 0)
264 {
91bde3ec 265 CPPUNIT_ASSERT_EQUAL(strlen(ungetstr) + 1, ungetsize);
e3f810a2
VS
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 {
a54b285e 273 CPPUNIT_ASSERT_EQUAL(int('a'), stream_in.GetC());
e3f810a2
VS
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
8bd966d3
VZ
287 const char *buf = "Some text";
288 const wxFileOffset len = strlen(buf);
289 for ( int i = 0; i < len; i++ )
e3f810a2
VS
290 stream_out.PutC(buf[i]);
291
8bd966d3
VZ
292 if ( stream_out.IsSeekable() )
293 CPPUNIT_ASSERT_EQUAL(len, stream_out.TellO());
e3f810a2
VS
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.
8bd966d3
VZ
303 const char *buf = "Some text";
304 const wxFileOffset len = strlen(buf);
e3f810a2 305 (void)stream_out.Write(buf, len);
8bd966d3
VZ
306 if ( stream_out.IsSeekable() )
307 CPPUNIT_ASSERT_EQUAL( len, stream_out.TellO() );
e3f810a2
VS
308
309 // Do the Stream version.
310 TStreamIn &stream_in = CreateInStream();
311 (void)stream_out.Write(stream_in);
8bd966d3
VZ
312
313 if ( stream_out.IsSeekable() )
314 CPPUNIT_ASSERT(stream_out.TellO() > len);
e3f810a2
VS
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
8bd966d3 323 const char *buf = "12345";
e3f810a2 324 (void)stream_out.Write(buf, 5);
91bde3ec 325 CPPUNIT_ASSERT_EQUAL(5, stream_out.LastWrite());
e3f810a2 326 (void)stream_out.PutC('1');
91bde3ec 327 CPPUNIT_ASSERT_EQUAL(1, stream_out.LastWrite());
e3f810a2
VS
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();
2d76b6d8 335
8bd966d3
VZ
336 CPPUNIT_ASSERT( stream_out.IsSeekable() );
337
e3f810a2 338 // First put some data in the stream, so it is not empty.
8bd966d3 339 const char *buf = "1234567890";
e3f810a2
VS
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.
91bde3ec
VZ
345 CPPUNIT_ASSERT_EQUAL(2, stream_out.SeekO(2, wxFromStart));
346 CPPUNIT_ASSERT_EQUAL(4, stream_out.SeekO(2, wxFromCurrent));
e3f810a2 347 // Not sure the following line is correct, so test it differently.
91bde3ec 348 //CPPUNIT_ASSERT_EQUAL(stream_in.GetSize()-2, stream_out.SeekO(-2, wxFromEnd));
e3f810a2
VS
349 CPPUNIT_ASSERT(stream_out.SeekO(-2, wxFromEnd) != wxInvalidOffset);
350 // Go beyond the stream size.
08776b09 351 CPPUNIT_ASSERT((stream_out.SeekO(10, wxFromCurrent) == wxInvalidOffset) == m_bSeekInvalidBeyondEnd);
e3f810a2
VS
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...
91bde3ec 361 CPPUNIT_ASSERT_EQUAL(0, stream_out.TellO());
e3f810a2 362 (void)stream_out.PutC('1');
91bde3ec 363 CPPUNIT_ASSERT_EQUAL(1, stream_out.TellO());
e3f810a2
VS
364 if (!m_bSimpleTellOTest)
365 {
366 // First put some extra data in the stream, so it's not empty.
8bd966d3 367 const char *buf = "1234567890";
e3f810a2 368 (void)stream_out.Write(buf, 10);
2d76b6d8 369
9540df9c 370 wxFileOffset pos = stream_out.SeekO(5, wxFromStart);
91bde3ec 371 CPPUNIT_ASSERT_EQUAL(pos, stream_out.TellO());
e3f810a2 372 (void)stream_out.PutC('1');
91bde3ec 373 CPPUNIT_ASSERT_EQUAL(6, stream_out.TellO());
e3f810a2 374 pos = stream_out.SeekO(2, wxFromCurrent);
91bde3ec 375 CPPUNIT_ASSERT_EQUAL(pos, stream_out.TellO());
e3f810a2 376 pos = stream_out.SeekO(5, wxFromStart);
91bde3ec 377 CPPUNIT_ASSERT_EQUAL(pos, stream_out.TellO());
e3f810a2
VS
378 }
379 }
380
381protected:
382 // Some tests can be configured... here you can find the config settings
2d76b6d8 383 bool m_bSimpleTellITest; // if true, no SeekI will be used by the TellI test.
e3f810a2 384 // Default false.
2d76b6d8 385 bool m_bSimpleTellOTest; // if true, no SeekO will be used by the TellI test.
e3f810a2 386 // Default false.
08776b09
VS
387 bool m_bSeekInvalidBeyondEnd; // if true a SeekI|O beyond the end of the stream should return wxInvalidOffset
388 // Default true.
7735998c
VS
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.
e3f810a2
VS
391protected:
392 TStreamIn &CreateInStream()
2d76b6d8 393 {
e3f810a2
VS
394 if (m_pCurrentIn)
395 {
9a83f860 396 wxFAIL_MSG(wxT("Error in test case, the previouse input stream needs to be delete first!"));
e3f810a2
VS
397 }
398
399 m_pCurrentIn = DoCreateInStream();
400 wxASSERT(m_pCurrentIn != NULL);
401 return *m_pCurrentIn;
402 }
403 TStreamOut &CreateOutStream()
2d76b6d8 404 {
e3f810a2
VS
405 if (m_pCurrentOut)
406 {
9a83f860 407 wxFAIL_MSG(wxT("Error in test case, the previouse output stream needs to be delete first!"));
e3f810a2
VS
408 }
409
410 m_pCurrentOut = DoCreateOutStream();
411 wxASSERT(m_pCurrentOut != NULL);
412 return *m_pCurrentOut;
413 }
2d76b6d8 414
e3f810a2
VS
415 void DeleteInStream()
416 {
417 if (m_pCurrentIn == NULL)
418 return;
419 delete m_pCurrentIn;
420 m_pCurrentIn = NULL;
d13b34d3 421 // In case something extra needs to be done.
e3f810a2
VS
422 DoDeleteInStream();
423 }
424 void DeleteOutStream()
2d76b6d8 425 {
e3f810a2
VS
426 if (m_pCurrentOut == NULL)
427 return;
2d76b6d8 428
8f0ff178 429 CPPUNIT_ASSERT(m_pCurrentOut->Close());
2d76b6d8 430
e3f810a2
VS
431 delete m_pCurrentOut;
432 m_pCurrentOut = NULL;
d13b34d3 433 // In case something extra needs to be done.
e3f810a2
VS
434 DoDeleteOutStream();
435 }
436
437protected:
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
444private:
445 TStreamIn *m_pCurrentIn;
446 TStreamOut *m_pCurrentOut;
447};
448
449#endif
450
451