]>
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 ///////////////////////////////////////////////////////////////////////////////
33 // Template class that implements a test for all base stream functions.
36 template <class TStreamIn
, class TStreamOut
> class BaseStreamTestCase
: public CppUnit::TestCase
39 typedef BaseStreamTestCase
<TStreamIn
, TStreamOut
> StreamTestCase
;
44 CleanupHelper(StreamTestCase
*value
)
49 m_pCleanup
->DeleteInStream();
50 m_pCleanup
->DeleteOutStream();
53 StreamTestCase
*m_pCleanup
;
55 friend class CleanupHelper
;
59 :m_bSimpleTellITest(false),
60 m_bSimpleTellOTest(false),
61 m_bSeekInvalidBeyondEnd(true),
62 m_bEofAtLastRead(true),
65 { /* Nothing extra */ }
66 virtual ~BaseStreamTestCase()
78 // Just try to perform a GetSize() on the input stream.
81 CleanupHelper
cleanup(this);
82 const TStreamIn
&stream_in
= CreateInStream();
83 CPPUNIT_ASSERT(!stream_in
.Eof());
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);
91 // Just try to perform a GetC() on the input stream.
94 CleanupHelper
cleanup(this);
95 TStreamIn
&stream_in
= CreateInStream();
96 CPPUNIT_ASSERT(!stream_in
.Eof());
98 // If no exception occurs the test is successful.
99 (void)stream_in
.GetC();
102 // Just try to perform a Read() on the input stream.
105 CleanupHelper
cleanup(this);
106 TStreamIn
&stream_in
= CreateInStream();
107 CPPUNIT_ASSERT(!stream_in
.Eof());
109 // Note: the input stream should at least be of min size +10!
112 (void)stream_in
.Read(buf
, 10);
114 CPPUNIT_ASSERT(!stream_in
.Eof());
115 CPPUNIT_ASSERT(stream_in
.IsOk());
117 // Test the stream version aswell.
118 TStreamOut
&stream_out
= CreateOutStream();
119 (void)stream_in
.Read(stream_out
);
121 // The output stream should have read the input stream till the end.
122 CPPUNIT_ASSERT(stream_in
.Eof());
125 // Test and see what happens to the EOF when we
126 // read after EOF was encountered.
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());
135 // Travel to the end of the stream.
136 while(!stream_in
.Eof())
138 CPPUNIT_ASSERT_MESSAGE( "unexpected non-EOF stream error",
141 // Read, we move one byte along.
142 (void)stream_in
.GetC();
144 // EOF behaviour is different in streams, disabled (for now?)
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()));
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()));
155 // Check EOF stream state.
156 CPPUNIT_ASSERT_MESSAGE("EOF is not EOF?", stream_in
.Eof());
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();
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
);
167 // Just try to perform a LastRead() on the input stream.
168 void Input_LastRead()
170 CleanupHelper
cleanup(this);
171 TStreamIn
&stream_in
= CreateInStream();
172 CPPUNIT_ASSERT(!stream_in
.Eof());
175 (void)stream_in
.Read(buf
, 5);
176 CPPUNIT_ASSERT(stream_in
.LastRead() == 5);
177 (void)stream_in
.GetC();
178 CPPUNIT_ASSERT(stream_in
.LastRead() == 1);
183 CleanupHelper
cleanup(this);
184 TStreamIn
&stream_in
= CreateInStream();
186 CPPUNIT_ASSERT( stream_in
.CanRead() );
188 // read the entire contents
189 (void)stream_in
.Read(CreateOutStream());
191 CPPUNIT_ASSERT( !stream_in
.CanRead() );
194 // Just try to perform a SeekI() on the input stream.
197 CleanupHelper
cleanup(this);
198 TStreamIn
&stream_in
= CreateInStream();
200 CPPUNIT_ASSERT( stream_in
.IsSeekable() );
201 CPPUNIT_ASSERT(!stream_in
.Eof());
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(stream_in
.SeekI(2, wxFromStart
) == 2);
207 CPPUNIT_ASSERT(stream_in
.SeekI(2, wxFromCurrent
) == 4);
208 // Not sure the following line is correct, so test it differently.
209 //CPPUNIT_ASSERT(stream_in.SeekI(-2, wxFromEnd) == (off_t)stream_in.GetSize()-2);
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
);
215 // Just try to perform a TellI() on the input stream.
218 CleanupHelper
cleanup(this);
219 TStreamIn
&stream_in
= CreateInStream();
221 // this test shouldn't be used at all if the stream isn't seekable
222 CPPUNIT_ASSERT( stream_in
.IsSeekable() );
224 CPPUNIT_ASSERT(!stream_in
.Eof());
226 // Try to Get the location in the stream...
227 CPPUNIT_ASSERT(stream_in
.TellI() == 0);
228 (void)stream_in
.GetC();
229 CPPUNIT_ASSERT(stream_in
.TellI() == 1);
230 if (!m_bSimpleTellITest
)
232 wxFileOffset pos
= stream_in
.SeekI(5, wxFromStart
);
233 CPPUNIT_ASSERT(stream_in
.TellI() == pos
);
234 (void)stream_in
.GetC();
235 CPPUNIT_ASSERT(stream_in
.TellI() == 6);
236 pos
= stream_in
.SeekI(2, wxFromCurrent
);
237 CPPUNIT_ASSERT(stream_in
.TellI() == pos
);
238 pos
= stream_in
.SeekI(5, wxFromStart
);
239 CPPUNIT_ASSERT(stream_in
.TellI() == pos
);
243 // Just try to perform a Peek() on the input stream.
246 CleanupHelper
cleanup(this);
247 TStreamIn
&stream_in
= CreateInStream();
249 // Test the full stream
250 while (stream_in
.IsOk())
252 char peekChar
= stream_in
.Peek();
253 char getChar
= stream_in
.GetC();
254 if (stream_in
.LastRead() == 1)
255 CPPUNIT_ASSERT(peekChar
== getChar
);
259 // Just try to perform a Ungetch() on the input stream.
262 CleanupHelper
cleanup(this);
263 TStreamIn
&stream_in
= CreateInStream();
264 CPPUNIT_ASSERT(!stream_in
.Eof());
266 const char *ungetstr
= "test";
267 size_t ungetsize
= stream_in
.Ungetch(ungetstr
, strlen(ungetstr
) + 1);
270 CPPUNIT_ASSERT(ungetsize
== strlen(ungetstr
) + 1);
272 (void)stream_in
.Read(buf
, ungetsize
);
273 CPPUNIT_ASSERT(strcmp(buf
, ungetstr
) == 0);
276 if (stream_in
.Ungetch('a'))
278 CPPUNIT_ASSERT(stream_in
.GetC() == 'a');
283 * Output stream tests.
286 // Just try to perform a PutC() on the output stream.
289 CleanupHelper
cleanup(this);
290 TStreamOut
&stream_out
= CreateOutStream();
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
]);
297 if ( stream_out
.IsSeekable() )
298 CPPUNIT_ASSERT_EQUAL(len
, stream_out
.TellO());
301 // Just try to perform a Write() on the output stream.
304 CleanupHelper
cleanup(this);
305 TStreamOut
&stream_out
= CreateOutStream();
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() );
314 // Do the Stream version.
315 TStreamIn
&stream_in
= CreateInStream();
316 (void)stream_out
.Write(stream_in
);
318 if ( stream_out
.IsSeekable() )
319 CPPUNIT_ASSERT(stream_out
.TellO() > len
);
322 // Just try to perform a LastWrite() on the output stream.
323 void Output_LastWrite()
325 CleanupHelper
cleanup(this);
326 TStreamOut
&stream_out
= CreateOutStream();
328 const char *buf
= "12345";
329 (void)stream_out
.Write(buf
, 5);
330 CPPUNIT_ASSERT(stream_out
.LastWrite() == 5);
331 (void)stream_out
.PutC('1');
332 CPPUNIT_ASSERT(stream_out
.LastWrite() == 1);
335 // Just try to perform a SeekO() on the output stream.
338 CleanupHelper
cleanup(this);
339 TStreamOut
&stream_out
= CreateOutStream();
341 CPPUNIT_ASSERT( stream_out
.IsSeekable() );
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);
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(stream_out
.SeekO(2, wxFromStart
) == 2);
351 CPPUNIT_ASSERT(stream_out
.SeekO(2, wxFromCurrent
) == 4);
352 // Not sure the following line is correct, so test it differently.
353 //CPPUNIT_ASSERT(stream_out.SeekO(-2, wxFromEnd) == (off_t)stream_in.GetSize()-2);
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
);
359 // Just try to perform a TellO() on the output stream.
362 CleanupHelper
cleanup(this);
363 TStreamOut
&stream_out
= CreateOutStream();
365 // If this test is used, the stream must be seekable
366 CPPUNIT_ASSERT( stream_out
.IsSeekable() );
368 // Try to Get the location in the stream...
369 CPPUNIT_ASSERT(stream_out
.TellO() == 0);
370 (void)stream_out
.PutC('1');
371 CPPUNIT_ASSERT(stream_out
.TellO() == 1);
372 if (!m_bSimpleTellOTest
)
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);
378 off_t pos
= stream_out
.SeekO(5, wxFromStart
);
379 CPPUNIT_ASSERT(stream_out
.TellO() == pos
);
380 (void)stream_out
.PutC('1');
381 CPPUNIT_ASSERT(stream_out
.TellO() == 6);
382 pos
= stream_out
.SeekO(2, wxFromCurrent
);
383 CPPUNIT_ASSERT(stream_out
.TellO() == pos
);
384 pos
= stream_out
.SeekO(5, wxFromStart
);
385 CPPUNIT_ASSERT(stream_out
.TellO() == pos
);
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.
393 bool m_bSimpleTellOTest
; // if true, no SeekO will be used by the TellI test.
395 bool m_bSeekInvalidBeyondEnd
; // if true a SeekI|O beyond the end of the stream should return wxInvalidOffset
397 bool m_bEofAtLastRead
; // Does EOF occure at the moment the last byte is read or when read past the last byte.
400 TStreamIn
&CreateInStream()
404 wxFAIL_MSG(_T("Error in test case, the previouse input stream needs to be delete first!"));
407 m_pCurrentIn
= DoCreateInStream();
408 wxASSERT(m_pCurrentIn
!= NULL
);
409 return *m_pCurrentIn
;
411 TStreamOut
&CreateOutStream()
415 wxFAIL_MSG(_T("Error in test case, the previouse output stream needs to be delete first!"));
418 m_pCurrentOut
= DoCreateOutStream();
419 wxASSERT(m_pCurrentOut
!= NULL
);
420 return *m_pCurrentOut
;
423 void DeleteInStream()
425 if (m_pCurrentIn
== NULL
)
429 // Incase something extra needs to be done.
432 void DeleteOutStream()
434 if (m_pCurrentOut
== NULL
)
437 CPPUNIT_ASSERT(m_pCurrentOut
->Close());
439 delete m_pCurrentOut
;
440 m_pCurrentOut
= NULL
;
441 // Incase something extra needs to be done.
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 */ }
453 TStreamIn
*m_pCurrentIn
;
454 TStreamOut
*m_pCurrentOut
;