]>
git.saurik.com Git - wxWidgets.git/blob - 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
6 // Copyright: (c) 2004 Hans Van Leemputten
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
10 #ifndef _WX_TESTBSTREAM_H__
11 #define _WX_TESTBSTREAM_H__
13 #include "wx/cppunit.h"
15 ///////////////////////////////////////////////////////////////////////////////
16 // Some macros preventing us from typing too much ;-)
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 )
32 WX_CPPUNIT_ALLOW_EQUALS_TO_INT(wxFileOffset
)
34 ///////////////////////////////////////////////////////////////////////////////
35 // Template class that implements a test for all base stream functions.
38 template <class TStreamIn
, class TStreamOut
> class BaseStreamTestCase
: public CppUnit::TestCase
41 typedef BaseStreamTestCase
<TStreamIn
, TStreamOut
> StreamTestCase
;
46 CleanupHelper(StreamTestCase
*value
)
51 m_pCleanup
->DeleteInStream();
52 m_pCleanup
->DeleteOutStream();
55 StreamTestCase
*m_pCleanup
;
57 friend class CleanupHelper
;
61 :m_bSimpleTellITest(false),
62 m_bSimpleTellOTest(false),
63 m_bSeekInvalidBeyondEnd(true),
64 m_bEofAtLastRead(true),
67 { /* Nothing extra */ }
68 virtual ~BaseStreamTestCase()
80 // Just try to perform a GetSize() on the input stream.
83 CleanupHelper
cleanup(this);
84 const TStreamIn
&stream_in
= CreateInStream();
85 CPPUNIT_ASSERT(!stream_in
.Eof());
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);
93 // Just try to perform a GetC() on the input stream.
96 CleanupHelper
cleanup(this);
97 TStreamIn
&stream_in
= CreateInStream();
98 CPPUNIT_ASSERT(!stream_in
.Eof());
100 // If no exception occurs the test is successful.
101 (void)stream_in
.GetC();
104 // Just try to perform a Read() on the input stream.
107 CleanupHelper
cleanup(this);
108 TStreamIn
&stream_in
= CreateInStream();
109 CPPUNIT_ASSERT(!stream_in
.Eof());
111 // Note: the input stream should at least be of min size +10!
114 (void)stream_in
.Read(buf
, 10);
116 CPPUNIT_ASSERT(!stream_in
.Eof());
117 CPPUNIT_ASSERT(stream_in
.IsOk());
119 // Test the stream version aswell.
120 TStreamOut
&stream_out
= CreateOutStream();
121 (void)stream_in
.Read(stream_out
);
123 // The output stream should have read the input stream till the end.
124 CPPUNIT_ASSERT(stream_in
.Eof());
127 // Test and see what happens to the EOF when we
128 // read after EOF was encountered.
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());
137 // Travel to the end of the stream.
138 while(!stream_in
.Eof())
140 CPPUNIT_ASSERT_MESSAGE( "unexpected non-EOF stream error",
143 // Read, we move one byte along.
144 (void)stream_in
.GetC();
146 // EOF behaviour is different in streams, disabled (for now?)
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()));
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()));
157 // Check EOF stream state.
158 CPPUNIT_ASSERT_MESSAGE("EOF is not EOF?", stream_in
.Eof());
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();
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
);
169 // Just try to perform a LastRead() on the input stream.
170 void Input_LastRead()
172 CleanupHelper
cleanup(this);
173 TStreamIn
&stream_in
= CreateInStream();
174 CPPUNIT_ASSERT(!stream_in
.Eof());
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());
185 CleanupHelper
cleanup(this);
186 TStreamIn
&stream_in
= CreateInStream();
188 CPPUNIT_ASSERT( stream_in
.CanRead() );
190 // read the entire contents
191 (void)stream_in
.Read(CreateOutStream());
193 CPPUNIT_ASSERT( !stream_in
.CanRead() );
196 // Just try to perform a SeekI() on the input stream.
199 CleanupHelper
cleanup(this);
200 TStreamIn
&stream_in
= CreateInStream();
202 CPPUNIT_ASSERT( stream_in
.IsSeekable() );
203 CPPUNIT_ASSERT(!stream_in
.Eof());
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
);
217 // Just try to perform a TellI() on the input stream.
220 CleanupHelper
cleanup(this);
221 TStreamIn
&stream_in
= CreateInStream();
223 // this test shouldn't be used at all if the stream isn't seekable
224 CPPUNIT_ASSERT( stream_in
.IsSeekable() );
226 CPPUNIT_ASSERT(!stream_in
.Eof());
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
)
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());
245 // Just try to perform a Peek() on the input stream.
248 CleanupHelper
cleanup(this);
249 TStreamIn
&stream_in
= CreateInStream();
251 // Test the full stream
252 while (stream_in
.IsOk())
254 char peekChar
= stream_in
.Peek();
255 char getChar
= stream_in
.GetC();
256 if (stream_in
.LastRead() == 1)
257 CPPUNIT_ASSERT_EQUAL(getChar
, peekChar
);
261 // Just try to perform a Ungetch() on the input stream.
264 CleanupHelper
cleanup(this);
265 TStreamIn
&stream_in
= CreateInStream();
266 CPPUNIT_ASSERT(!stream_in
.Eof());
268 const char *ungetstr
= "test";
269 size_t ungetsize
= stream_in
.Ungetch(ungetstr
, strlen(ungetstr
) + 1);
272 CPPUNIT_ASSERT_EQUAL(strlen(ungetstr
) + 1, ungetsize
);
274 (void)stream_in
.Read(buf
, ungetsize
);
275 CPPUNIT_ASSERT(strcmp(buf
, ungetstr
) == 0);
278 if (stream_in
.Ungetch('a'))
280 CPPUNIT_ASSERT_EQUAL(int('a'), stream_in
.GetC());
285 * Output stream tests.
288 // Just try to perform a PutC() on the output stream.
291 CleanupHelper
cleanup(this);
292 TStreamOut
&stream_out
= CreateOutStream();
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
]);
299 if ( stream_out
.IsSeekable() )
300 CPPUNIT_ASSERT_EQUAL(len
, stream_out
.TellO());
303 // Just try to perform a Write() on the output stream.
306 CleanupHelper
cleanup(this);
307 TStreamOut
&stream_out
= CreateOutStream();
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() );
316 // Do the Stream version.
317 TStreamIn
&stream_in
= CreateInStream();
318 (void)stream_out
.Write(stream_in
);
320 if ( stream_out
.IsSeekable() )
321 CPPUNIT_ASSERT(stream_out
.TellO() > len
);
324 // Just try to perform a LastWrite() on the output stream.
325 void Output_LastWrite()
327 CleanupHelper
cleanup(this);
328 TStreamOut
&stream_out
= CreateOutStream();
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());
337 // Just try to perform a SeekO() on the output stream.
340 CleanupHelper
cleanup(this);
341 TStreamOut
&stream_out
= CreateOutStream();
343 CPPUNIT_ASSERT( stream_out
.IsSeekable() );
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);
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
);
361 // Just try to perform a TellO() on the output stream.
364 CleanupHelper
cleanup(this);
365 TStreamOut
&stream_out
= CreateOutStream();
367 // If this test is used, the stream must be seekable
368 CPPUNIT_ASSERT( stream_out
.IsSeekable() );
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
)
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);
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());
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.
395 bool m_bSimpleTellOTest
; // if true, no SeekO will be used by the TellI test.
397 bool m_bSeekInvalidBeyondEnd
; // if true a SeekI|O beyond the end of the stream should return wxInvalidOffset
399 bool m_bEofAtLastRead
; // Does EOF occure at the moment the last byte is read or when read past the last byte.
402 TStreamIn
&CreateInStream()
406 wxFAIL_MSG(_T("Error in test case, the previouse input stream needs to be delete first!"));
409 m_pCurrentIn
= DoCreateInStream();
410 wxASSERT(m_pCurrentIn
!= NULL
);
411 return *m_pCurrentIn
;
413 TStreamOut
&CreateOutStream()
417 wxFAIL_MSG(_T("Error in test case, the previouse output stream needs to be delete first!"));
420 m_pCurrentOut
= DoCreateOutStream();
421 wxASSERT(m_pCurrentOut
!= NULL
);
422 return *m_pCurrentOut
;
425 void DeleteInStream()
427 if (m_pCurrentIn
== NULL
)
431 // Incase something extra needs to be done.
434 void DeleteOutStream()
436 if (m_pCurrentOut
== NULL
)
439 CPPUNIT_ASSERT(m_pCurrentOut
->Close());
441 delete m_pCurrentOut
;
442 m_pCurrentOut
= NULL
;
443 // Incase something extra needs to be done.
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 */ }
455 TStreamIn
*m_pCurrentIn
;
456 TStreamOut
*m_pCurrentOut
;