]>
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 )
31 ///////////////////////////////////////////////////////////////////////////////
32 // Template class that implements a test for all base stream functions.
35 template <class TStreamIn
, class TStreamOut
> class BaseStreamTestCase
: public CppUnit::TestCase
38 typedef BaseStreamTestCase
<TStreamIn
, TStreamOut
> StreamTestCase
;
43 CleanupHelper(StreamTestCase
*value
)
48 m_pCleanup
->DeleteInStream();
49 m_pCleanup
->DeleteOutStream();
52 StreamTestCase
*m_pCleanup
;
54 friend class CleanupHelper
;
58 :m_bSimpleTellITest(false),
59 m_bSimpleTellOTest(false),
60 m_bSeekInvalidBeyondEnd(true),
61 m_bEofAtLastRead(true),
64 { /* Nothing extra */ }
65 virtual ~BaseStreamTestCase()
77 // Just try to perform a GetSize() on the input stream.
80 CleanupHelper
cleanup(this);
81 const TStreamIn
&stream_in
= CreateInStream();
82 CPPUNIT_ASSERT(!stream_in
.Eof());
84 // Size should be greater then zero.
85 // Note: streams not supporting this should register this test
86 // with CPPUNIT_TEST_FAIL instead of CPPUNIT_TEST.
87 CPPUNIT_ASSERT(stream_in
.GetSize() != 0);
90 // Just try to perform a GetC() on the input stream.
93 CleanupHelper
cleanup(this);
94 TStreamIn
&stream_in
= CreateInStream();
95 CPPUNIT_ASSERT(!stream_in
.Eof());
97 // If no exception occurs the test is successful.
98 (void)stream_in
.GetC();
101 // Just try to perform a Read() on the input stream.
104 CleanupHelper
cleanup(this);
105 TStreamIn
&stream_in
= CreateInStream();
106 CPPUNIT_ASSERT(!stream_in
.Eof());
108 // Note: the input stream should at least be of min size +10!
111 (void)stream_in
.Read(buf
, 10);
113 CPPUNIT_ASSERT(!stream_in
.Eof());
114 CPPUNIT_ASSERT(stream_in
.IsOk());
116 // Test the stream version aswell.
117 TStreamOut
&stream_out
= CreateOutStream();
118 (void)stream_in
.Read(stream_out
);
120 // The output stream should have read the input stream till the end.
121 CPPUNIT_ASSERT(stream_in
.Eof());
124 // Test and see what happens to the EOF when we
125 // read after EOF was encountered.
128 CleanupHelper
cleanup(this);
129 TStreamIn
&stream_in
= CreateInStream();
130 CPPUNIT_ASSERT(!stream_in
.Eof());
131 // Double check to see if Eof it self doesn't changes the Eof status.
132 CPPUNIT_ASSERT(!stream_in
.Eof());
134 // Travel to the end of the stream.
135 while(!stream_in
.Eof())
137 CPPUNIT_ASSERT_MESSAGE( "unexpected non-EOF stream error",
140 // Read, we move one byte along.
141 (void)stream_in
.GetC();
143 // EOF behaviour is different in streams, disabled (for now?)
145 if (m_bEofAtLastRead
)
146 // EOF should only occure after the last successful get.
147 CPPUNIT_ASSERT_MESSAGE("Eof is detected too late.", !(stream_in
.LastRead() != 1 && stream_in
.Eof()));
149 // EOF should only occure after a failed get.
150 CPPUNIT_ASSERT_MESSAGE("Eof is detected too soon.", !(stream_in
.LastRead() == 1 && stream_in
.Eof()));
154 // Check EOF stream state.
155 CPPUNIT_ASSERT_MESSAGE("EOF is not EOF?", stream_in
.Eof());
157 // Ok we found the end, lets see if we can go past it.
158 for (size_t i
= 0; i
< 100; i
++)
159 (void)stream_in
.GetC();
161 // Check for EOF correctness.
162 CPPUNIT_ASSERT_MESSAGE("EOF is wrong when we read past EOF!", stream_in
.Eof());
163 CPPUNIT_ASSERT_MESSAGE("Last error is not EOF while stream_in.Eof() is true", stream_in
.GetLastError() == wxSTREAM_EOF
);
166 // Just try to perform a LastRead() on the input stream.
167 void Input_LastRead()
169 CleanupHelper
cleanup(this);
170 TStreamIn
&stream_in
= CreateInStream();
171 CPPUNIT_ASSERT(!stream_in
.Eof());
174 (void)stream_in
.Read(buf
, 5);
175 CPPUNIT_ASSERT_EQUAL(5, stream_in
.LastRead());
176 (void)stream_in
.GetC();
177 CPPUNIT_ASSERT_EQUAL(1, stream_in
.LastRead());
182 CleanupHelper
cleanup(this);
183 TStreamIn
&stream_in
= CreateInStream();
185 CPPUNIT_ASSERT( stream_in
.CanRead() );
187 // read the entire contents
188 (void)stream_in
.Read(CreateOutStream());
190 CPPUNIT_ASSERT( !stream_in
.CanRead() );
193 // Just try to perform a SeekI() on the input stream.
196 CleanupHelper
cleanup(this);
197 TStreamIn
&stream_in
= CreateInStream();
199 CPPUNIT_ASSERT( stream_in
.IsSeekable() );
200 CPPUNIT_ASSERT(!stream_in
.Eof());
202 // Try to Seek in the stream...
203 // Note: streams not supporting this should register this test
204 // with CPPUNIT_TEST_FAIL instead of CPPUNIT_TEST.
205 CPPUNIT_ASSERT_EQUAL(2, stream_in
.SeekI(2, wxFromStart
));
206 CPPUNIT_ASSERT_EQUAL(4, stream_in
.SeekI(2, wxFromCurrent
));
207 // Not sure the following line is correct, so test it differently.
208 //CPPUNIT_ASSERT_EQUAL(stream_in.GetSize()-2, stream_in.SeekI(-2, wxFromEnd));
209 CPPUNIT_ASSERT(stream_in
.SeekI(-2, wxFromEnd
) != wxInvalidOffset
);
210 // Go beyond the stream size.
211 CPPUNIT_ASSERT((stream_in
.SeekI(10, wxFromCurrent
) == wxInvalidOffset
) == m_bSeekInvalidBeyondEnd
);
214 // Just try to perform a TellI() on the input stream.
217 CleanupHelper
cleanup(this);
218 TStreamIn
&stream_in
= CreateInStream();
220 // this test shouldn't be used at all if the stream isn't seekable
221 CPPUNIT_ASSERT( stream_in
.IsSeekable() );
223 CPPUNIT_ASSERT(!stream_in
.Eof());
225 // Try to Get the location in the stream...
226 CPPUNIT_ASSERT_EQUAL(0, stream_in
.TellI());
227 (void)stream_in
.GetC();
228 CPPUNIT_ASSERT_EQUAL(1, stream_in
.TellI());
229 if (!m_bSimpleTellITest
)
231 wxFileOffset pos
= stream_in
.SeekI(5, wxFromStart
);
232 CPPUNIT_ASSERT_EQUAL(pos
, stream_in
.TellI());
233 (void)stream_in
.GetC();
234 CPPUNIT_ASSERT_EQUAL(6, stream_in
.TellI());
235 pos
= stream_in
.SeekI(2, wxFromCurrent
);
236 CPPUNIT_ASSERT_EQUAL(pos
, stream_in
.TellI());
237 pos
= stream_in
.SeekI(5, wxFromStart
);
238 CPPUNIT_ASSERT_EQUAL(pos
, stream_in
.TellI());
242 // Just try to perform a Peek() on the input stream.
245 CleanupHelper
cleanup(this);
246 TStreamIn
&stream_in
= CreateInStream();
248 // Test the full stream
249 while (stream_in
.IsOk())
251 char peekChar
= stream_in
.Peek();
252 char getChar
= stream_in
.GetC();
253 if (stream_in
.LastRead() == 1)
254 CPPUNIT_ASSERT_EQUAL(getChar
, peekChar
);
258 // Just try to perform a Ungetch() on the input stream.
261 CleanupHelper
cleanup(this);
262 TStreamIn
&stream_in
= CreateInStream();
263 CPPUNIT_ASSERT(!stream_in
.Eof());
265 const char *ungetstr
= "test";
266 size_t ungetsize
= stream_in
.Ungetch(ungetstr
, strlen(ungetstr
) + 1);
269 CPPUNIT_ASSERT_EQUAL(strlen(ungetstr
) + 1, ungetsize
);
271 (void)stream_in
.Read(buf
, ungetsize
);
272 CPPUNIT_ASSERT(strcmp(buf
, ungetstr
) == 0);
275 if (stream_in
.Ungetch('a'))
277 CPPUNIT_ASSERT_EQUAL(int('a'), stream_in
.GetC());
282 * Output stream tests.
285 // Just try to perform a PutC() on the output stream.
288 CleanupHelper
cleanup(this);
289 TStreamOut
&stream_out
= CreateOutStream();
291 const char *buf
= "Some text";
292 const wxFileOffset len
= strlen(buf
);
293 for ( int i
= 0; i
< len
; i
++ )
294 stream_out
.PutC(buf
[i
]);
296 if ( stream_out
.IsSeekable() )
297 CPPUNIT_ASSERT_EQUAL(len
, stream_out
.TellO());
300 // Just try to perform a Write() on the output stream.
303 CleanupHelper
cleanup(this);
304 TStreamOut
&stream_out
= CreateOutStream();
306 // Do the buffer version.
307 const char *buf
= "Some text";
308 const wxFileOffset len
= strlen(buf
);
309 (void)stream_out
.Write(buf
, len
);
310 if ( stream_out
.IsSeekable() )
311 CPPUNIT_ASSERT_EQUAL( len
, stream_out
.TellO() );
313 // Do the Stream version.
314 TStreamIn
&stream_in
= CreateInStream();
315 (void)stream_out
.Write(stream_in
);
317 if ( stream_out
.IsSeekable() )
318 CPPUNIT_ASSERT(stream_out
.TellO() > len
);
321 // Just try to perform a LastWrite() on the output stream.
322 void Output_LastWrite()
324 CleanupHelper
cleanup(this);
325 TStreamOut
&stream_out
= CreateOutStream();
327 const char *buf
= "12345";
328 (void)stream_out
.Write(buf
, 5);
329 CPPUNIT_ASSERT_EQUAL(5, stream_out
.LastWrite());
330 (void)stream_out
.PutC('1');
331 CPPUNIT_ASSERT_EQUAL(1, stream_out
.LastWrite());
334 // Just try to perform a SeekO() on the output stream.
337 CleanupHelper
cleanup(this);
338 TStreamOut
&stream_out
= CreateOutStream();
340 CPPUNIT_ASSERT( stream_out
.IsSeekable() );
342 // First put some data in the stream, so it is not empty.
343 const char *buf
= "1234567890";
344 (void)stream_out
.Write(buf
, 10);
346 // Try to Seek in the stream...
347 // Note: streams not supporting this should register this test
348 // with CPPUNIT_TEST_FAIL instead of CPPUNIT_TEST.
349 CPPUNIT_ASSERT_EQUAL(2, stream_out
.SeekO(2, wxFromStart
));
350 CPPUNIT_ASSERT_EQUAL(4, stream_out
.SeekO(2, wxFromCurrent
));
351 // Not sure the following line is correct, so test it differently.
352 //CPPUNIT_ASSERT_EQUAL(stream_in.GetSize()-2, stream_out.SeekO(-2, wxFromEnd));
353 CPPUNIT_ASSERT(stream_out
.SeekO(-2, wxFromEnd
) != wxInvalidOffset
);
354 // Go beyond the stream size.
355 CPPUNIT_ASSERT((stream_out
.SeekO(10, wxFromCurrent
) == wxInvalidOffset
) == m_bSeekInvalidBeyondEnd
);
358 // Just try to perform a TellO() on the output stream.
361 CleanupHelper
cleanup(this);
362 TStreamOut
&stream_out
= CreateOutStream();
364 // If this test is used, the stream must be seekable
365 CPPUNIT_ASSERT( stream_out
.IsSeekable() );
367 // Try to Get the location in the stream...
368 CPPUNIT_ASSERT_EQUAL(0, stream_out
.TellO());
369 (void)stream_out
.PutC('1');
370 CPPUNIT_ASSERT_EQUAL(1, stream_out
.TellO());
371 if (!m_bSimpleTellOTest
)
373 // First put some extra data in the stream, so it's not empty.
374 const char *buf
= "1234567890";
375 (void)stream_out
.Write(buf
, 10);
377 wxFileOffset pos
= stream_out
.SeekO(5, wxFromStart
);
378 CPPUNIT_ASSERT_EQUAL(pos
, stream_out
.TellO());
379 (void)stream_out
.PutC('1');
380 CPPUNIT_ASSERT_EQUAL(6, stream_out
.TellO());
381 pos
= stream_out
.SeekO(2, wxFromCurrent
);
382 CPPUNIT_ASSERT_EQUAL(pos
, stream_out
.TellO());
383 pos
= stream_out
.SeekO(5, wxFromStart
);
384 CPPUNIT_ASSERT_EQUAL(pos
, stream_out
.TellO());
389 // Some tests can be configured... here you can find the config settings
390 bool m_bSimpleTellITest
; // if true, no SeekI will be used by the TellI test.
392 bool m_bSimpleTellOTest
; // if true, no SeekO will be used by the TellI test.
394 bool m_bSeekInvalidBeyondEnd
; // if true a SeekI|O beyond the end of the stream should return wxInvalidOffset
396 bool m_bEofAtLastRead
; // Does EOF occure at the moment the last byte is read or when read past the last byte.
399 TStreamIn
&CreateInStream()
403 wxFAIL_MSG(wxT("Error in test case, the previouse input stream needs to be delete first!"));
406 m_pCurrentIn
= DoCreateInStream();
407 wxASSERT(m_pCurrentIn
!= NULL
);
408 return *m_pCurrentIn
;
410 TStreamOut
&CreateOutStream()
414 wxFAIL_MSG(wxT("Error in test case, the previouse output stream needs to be delete first!"));
417 m_pCurrentOut
= DoCreateOutStream();
418 wxASSERT(m_pCurrentOut
!= NULL
);
419 return *m_pCurrentOut
;
422 void DeleteInStream()
424 if (m_pCurrentIn
== NULL
)
428 // Incase something extra needs to be done.
431 void DeleteOutStream()
433 if (m_pCurrentOut
== NULL
)
436 CPPUNIT_ASSERT(m_pCurrentOut
->Close());
438 delete m_pCurrentOut
;
439 m_pCurrentOut
= NULL
;
440 // Incase something extra needs to be done.
445 // Items that need to be implemented by a derived class!
446 virtual TStreamIn
*DoCreateInStream() = 0;
447 virtual TStreamOut
*DoCreateOutStream() = 0;
448 virtual void DoDeleteInStream() { /* Depends on the base class */ }
449 virtual void DoDeleteOutStream() { /* Depends on the base class */ }
452 TStreamIn
*m_pCurrentIn
;
453 TStreamOut
*m_pCurrentOut
;