]> git.saurik.com Git - wxWidgets.git/blame - tests/streams/bstream.h
Added wxPropertyGrid::SetSortFunction(); moved Sort() and SortChildren() to wxPropert...
[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
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
b2eabfe8
VZ
32WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxFileOffset)
33
e3f810a2
VS
34///////////////////////////////////////////////////////////////////////////////
35// Template class that implements a test for all base stream functions.
36//
37
3e5f6c1c 38template <class TStreamIn, class TStreamOut> class BaseStreamTestCase : public CppUnit::TestCase
e3f810a2
VS
39{
40protected:
41 typedef BaseStreamTestCase<TStreamIn, TStreamOut> StreamTestCase;
42
43 class CleanupHelper
44 {
45 public:
46 CleanupHelper(StreamTestCase *value)
47 :m_pCleanup(value)
48 {}
49 ~CleanupHelper()
2d76b6d8 50 {
e3f810a2
VS
51 m_pCleanup->DeleteInStream();
52 m_pCleanup->DeleteOutStream();
53 }
54 private:
55 StreamTestCase *m_pCleanup;
56 };
57 friend class CleanupHelper;
58
59public:
60 BaseStreamTestCase()
61 :m_bSimpleTellITest(false),
62 m_bSimpleTellOTest(false),
08776b09 63 m_bSeekInvalidBeyondEnd(true),
7735998c 64 m_bEofAtLastRead(true),
e3f810a2
VS
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;
2d76b6d8 73 }
e3f810a2
VS
74
75protected:
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
2d76b6d8 113 char buf[10];
e3f810a2
VS
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
2d76b6d8 127 // Test and see what happens to the EOF when we
e3f810a2
VS
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 {
bf451723
VZ
140 CPPUNIT_ASSERT_MESSAGE( "unexpected non-EOF stream error",
141 stream_in.IsOk() );
142
e3f810a2
VS
143 // Read, we move one byte along.
144 (void)stream_in.GetC();
7735998c
VS
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
e3f810a2
VS
155 }
156
7735998c 157 // Check EOF stream state.
2d76b6d8 158 CPPUNIT_ASSERT_MESSAGE("EOF is not EOF?", stream_in.Eof());
7735998c 159
e3f810a2
VS
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());
7735998c 166 CPPUNIT_ASSERT_MESSAGE("Last error is not EOF while stream_in.Eof() is true", stream_in.GetLastError() == wxSTREAM_EOF);
e3f810a2
VS
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);
91bde3ec 178 CPPUNIT_ASSERT_EQUAL(5, stream_in.LastRead());
e3f810a2 179 (void)stream_in.GetC();
91bde3ec 180 CPPUNIT_ASSERT_EQUAL(1, stream_in.LastRead());
e3f810a2
VS
181 }
182
2d76b6d8
VZ
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
e3f810a2
VS
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();
8bd966d3
VZ
201
202 CPPUNIT_ASSERT( stream_in.IsSeekable() );
e3f810a2
VS
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.
a54b285e
VZ
208 CPPUNIT_ASSERT_EQUAL(2, stream_in.SeekI(2, wxFromStart));
209 CPPUNIT_ASSERT_EQUAL(4, stream_in.SeekI(2, wxFromCurrent));
e3f810a2 210 // Not sure the following line is correct, so test it differently.
91bde3ec 211 //CPPUNIT_ASSERT_EQUAL(stream_in.GetSize()-2, stream_in.SeekI(-2, wxFromEnd));
e3f810a2
VS
212 CPPUNIT_ASSERT(stream_in.SeekI(-2, wxFromEnd) != wxInvalidOffset);
213 // Go beyond the stream size.
08776b09 214 CPPUNIT_ASSERT((stream_in.SeekI(10, wxFromCurrent) == wxInvalidOffset) == m_bSeekInvalidBeyondEnd);
e3f810a2
VS
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();
8bd966d3
VZ
222
223 // this test shouldn't be used at all if the stream isn't seekable
224 CPPUNIT_ASSERT( stream_in.IsSeekable() );
225
e3f810a2
VS
226 CPPUNIT_ASSERT(!stream_in.Eof());
227
228 // Try to Get the location in the stream...
91bde3ec 229 CPPUNIT_ASSERT_EQUAL(0, stream_in.TellI());
e3f810a2 230 (void)stream_in.GetC();
91bde3ec 231 CPPUNIT_ASSERT_EQUAL(1, stream_in.TellI());
e3f810a2
VS
232 if (!m_bSimpleTellITest)
233 {
30984dea 234 wxFileOffset pos = stream_in.SeekI(5, wxFromStart);
91bde3ec 235 CPPUNIT_ASSERT_EQUAL(pos, stream_in.TellI());
e3f810a2 236 (void)stream_in.GetC();
91bde3ec 237 CPPUNIT_ASSERT_EQUAL(6, stream_in.TellI());
e3f810a2 238 pos = stream_in.SeekI(2, wxFromCurrent);
91bde3ec 239 CPPUNIT_ASSERT_EQUAL(pos, stream_in.TellI());
e3f810a2 240 pos = stream_in.SeekI(5, wxFromStart);
91bde3ec 241 CPPUNIT_ASSERT_EQUAL(pos, stream_in.TellI());
e3f810a2
VS
242 }
243 }
2d76b6d8 244
e3f810a2
VS
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
08776b09 252 while (stream_in.IsOk())
e3f810a2 253 {
e3f810a2
VS
254 char peekChar = stream_in.Peek();
255 char getChar = stream_in.GetC();
08776b09 256 if (stream_in.LastRead() == 1)
91bde3ec 257 CPPUNIT_ASSERT_EQUAL(getChar, peekChar);
e3f810a2
VS
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());
2d76b6d8 267
e3f810a2
VS
268 const char *ungetstr = "test";
269 size_t ungetsize = stream_in.Ungetch(ungetstr, strlen(ungetstr) + 1);
270 if (ungetsize != 0)
271 {
91bde3ec 272 CPPUNIT_ASSERT_EQUAL(strlen(ungetstr) + 1, ungetsize);
e3f810a2
VS
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 {
a54b285e 280 CPPUNIT_ASSERT_EQUAL(int('a'), stream_in.GetC());
e3f810a2
VS
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
8bd966d3
VZ
294 const char *buf = "Some text";
295 const wxFileOffset len = strlen(buf);
296 for ( int i = 0; i < len; i++ )
e3f810a2
VS
297 stream_out.PutC(buf[i]);
298
8bd966d3
VZ
299 if ( stream_out.IsSeekable() )
300 CPPUNIT_ASSERT_EQUAL(len, stream_out.TellO());
e3f810a2
VS
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.
8bd966d3
VZ
310 const char *buf = "Some text";
311 const wxFileOffset len = strlen(buf);
e3f810a2 312 (void)stream_out.Write(buf, len);
8bd966d3
VZ
313 if ( stream_out.IsSeekable() )
314 CPPUNIT_ASSERT_EQUAL( len, stream_out.TellO() );
e3f810a2
VS
315
316 // Do the Stream version.
317 TStreamIn &stream_in = CreateInStream();
318 (void)stream_out.Write(stream_in);
8bd966d3
VZ
319
320 if ( stream_out.IsSeekable() )
321 CPPUNIT_ASSERT(stream_out.TellO() > len);
e3f810a2
VS
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
8bd966d3 330 const char *buf = "12345";
e3f810a2 331 (void)stream_out.Write(buf, 5);
91bde3ec 332 CPPUNIT_ASSERT_EQUAL(5, stream_out.LastWrite());
e3f810a2 333 (void)stream_out.PutC('1');
91bde3ec 334 CPPUNIT_ASSERT_EQUAL(1, stream_out.LastWrite());
e3f810a2
VS
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();
2d76b6d8 342
8bd966d3
VZ
343 CPPUNIT_ASSERT( stream_out.IsSeekable() );
344
e3f810a2 345 // First put some data in the stream, so it is not empty.
8bd966d3 346 const char *buf = "1234567890";
e3f810a2
VS
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.
91bde3ec
VZ
352 CPPUNIT_ASSERT_EQUAL(2, stream_out.SeekO(2, wxFromStart));
353 CPPUNIT_ASSERT_EQUAL(4, stream_out.SeekO(2, wxFromCurrent));
e3f810a2 354 // Not sure the following line is correct, so test it differently.
91bde3ec 355 //CPPUNIT_ASSERT_EQUAL(stream_in.GetSize()-2, stream_out.SeekO(-2, wxFromEnd));
e3f810a2
VS
356 CPPUNIT_ASSERT(stream_out.SeekO(-2, wxFromEnd) != wxInvalidOffset);
357 // Go beyond the stream size.
08776b09 358 CPPUNIT_ASSERT((stream_out.SeekO(10, wxFromCurrent) == wxInvalidOffset) == m_bSeekInvalidBeyondEnd);
e3f810a2
VS
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
8bd966d3
VZ
367 // If this test is used, the stream must be seekable
368 CPPUNIT_ASSERT( stream_out.IsSeekable() );
369
e3f810a2 370 // Try to Get the location in the stream...
91bde3ec 371 CPPUNIT_ASSERT_EQUAL(0, stream_out.TellO());
e3f810a2 372 (void)stream_out.PutC('1');
91bde3ec 373 CPPUNIT_ASSERT_EQUAL(1, stream_out.TellO());
e3f810a2
VS
374 if (!m_bSimpleTellOTest)
375 {
376 // First put some extra data in the stream, so it's not empty.
8bd966d3 377 const char *buf = "1234567890";
e3f810a2 378 (void)stream_out.Write(buf, 10);
2d76b6d8 379
9540df9c 380 wxFileOffset pos = stream_out.SeekO(5, wxFromStart);
91bde3ec 381 CPPUNIT_ASSERT_EQUAL(pos, stream_out.TellO());
e3f810a2 382 (void)stream_out.PutC('1');
91bde3ec 383 CPPUNIT_ASSERT_EQUAL(6, stream_out.TellO());
e3f810a2 384 pos = stream_out.SeekO(2, wxFromCurrent);
91bde3ec 385 CPPUNIT_ASSERT_EQUAL(pos, stream_out.TellO());
e3f810a2 386 pos = stream_out.SeekO(5, wxFromStart);
91bde3ec 387 CPPUNIT_ASSERT_EQUAL(pos, stream_out.TellO());
e3f810a2
VS
388 }
389 }
390
391protected:
392 // Some tests can be configured... here you can find the config settings
2d76b6d8 393 bool m_bSimpleTellITest; // if true, no SeekI will be used by the TellI test.
e3f810a2 394 // Default false.
2d76b6d8 395 bool m_bSimpleTellOTest; // if true, no SeekO will be used by the TellI test.
e3f810a2 396 // Default false.
08776b09
VS
397 bool m_bSeekInvalidBeyondEnd; // if true a SeekI|O beyond the end of the stream should return wxInvalidOffset
398 // Default true.
7735998c
VS
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.
e3f810a2
VS
401protected:
402 TStreamIn &CreateInStream()
2d76b6d8 403 {
e3f810a2
VS
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()
2d76b6d8 414 {
e3f810a2
VS
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 }
2d76b6d8 424
e3f810a2
VS
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()
2d76b6d8 435 {
e3f810a2
VS
436 if (m_pCurrentOut == NULL)
437 return;
2d76b6d8 438
8f0ff178 439 CPPUNIT_ASSERT(m_pCurrentOut->Close());
2d76b6d8 440
e3f810a2
VS
441 delete m_pCurrentOut;
442 m_pCurrentOut = NULL;
443 // Incase something extra needs to be done.
444 DoDeleteOutStream();
445 }
446
447protected:
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
454private:
455 TStreamIn *m_pCurrentIn;
456 TStreamOut *m_pCurrentOut;
457};
458
459#endif
460
461