]>
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
5 // Copyright: (c) 2004 Hans Van Leemputten
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
9 #ifndef _WX_TESTBSTREAM_H__
10 #define _WX_TESTBSTREAM_H__
12 #include "wx/cppunit.h"
14 ///////////////////////////////////////////////////////////////////////////////
15 // Some macros preventing us from typing too much ;-)
18 #define STREAM_TEST_NAME "Streams"
19 #define COMPOSE_TEST_NAME(Name) \
20 STREAM_TEST_NAME "." #Name
21 #define STREAM_REGISTER_SUB_SUITE(Name) \
22 extern CppUnit::Test* Get##Name##Suite(); \
23 suite->addTest(Get##Name##Suite())
24 #define STREAM_IMPLEMENT_SUB_REGISTRATION_ROUTINE(Name) \
25 CppUnit::Test* Get##Name##Suite() { return Name::suite(); }
26 #define STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(Name) \
27 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Name, COMPOSE_TEST_NAME(Name) ); \
28 STREAM_IMPLEMENT_SUB_REGISTRATION_ROUTINE( Name )
30 ///////////////////////////////////////////////////////////////////////////////
31 // Template class that implements a test for all base stream functions.
34 template <class TStreamIn
, class TStreamOut
> class BaseStreamTestCase
: public CppUnit::TestCase
37 typedef BaseStreamTestCase
<TStreamIn
, TStreamOut
> StreamTestCase
;
42 CleanupHelper(StreamTestCase
*value
)
47 m_pCleanup
->DeleteInStream();
48 m_pCleanup
->DeleteOutStream();
51 StreamTestCase
*m_pCleanup
;
53 friend class CleanupHelper
;
57 :m_bSimpleTellITest(false),
58 m_bSimpleTellOTest(false),
59 m_bSeekInvalidBeyondEnd(true),
60 m_bEofAtLastRead(true),
63 { /* Nothing extra */ }
64 virtual ~BaseStreamTestCase()
76 // Just try to perform a GetSize() on the input stream.
79 CleanupHelper
cleanup(this);
80 const TStreamIn
&stream_in
= CreateInStream();
81 CPPUNIT_ASSERT(!stream_in
.Eof());
83 // Size should be greater than zero.
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);
89 // Just try to perform a GetC() on the input stream.
92 CleanupHelper
cleanup(this);
93 TStreamIn
&stream_in
= CreateInStream();
94 CPPUNIT_ASSERT(!stream_in
.Eof());
96 // If no exception occurs the test is successful.
97 (void)stream_in
.GetC();
100 // Just try to perform a Read() on the input stream.
103 CleanupHelper
cleanup(this);
104 TStreamIn
&stream_in
= CreateInStream();
105 CPPUNIT_ASSERT(!stream_in
.Eof());
107 // Note: the input stream should at least be of min size +10!
110 (void)stream_in
.Read(buf
, 10);
112 CPPUNIT_ASSERT(!stream_in
.Eof());
113 CPPUNIT_ASSERT(stream_in
.IsOk());
115 // Test the stream version aswell.
116 TStreamOut
&stream_out
= CreateOutStream();
117 (void)stream_in
.Read(stream_out
);
119 // The output stream should have read the input stream till the end.
120 CPPUNIT_ASSERT(stream_in
.Eof());
123 // Test and see what happens to the EOF when we
124 // read after EOF was encountered.
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());
133 // Travel to the end of the stream.
134 while(!stream_in
.Eof())
136 CPPUNIT_ASSERT_MESSAGE( "unexpected non-EOF stream error",
139 // Read, we move one byte along.
140 (void)stream_in
.GetC();
142 // EOF behaviour is different in streams, disabled (for now?)
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()));
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()));
153 // Check EOF stream state.
154 CPPUNIT_ASSERT_MESSAGE("EOF is not EOF?", stream_in
.Eof());
156 // Ok we found the end, let's see if we can go past it.
157 for (size_t i
= 0; i
< 100; i
++)
158 (void)stream_in
.GetC();
160 // Check for EOF correctness.
161 CPPUNIT_ASSERT_MESSAGE("EOF is wrong when we read past EOF!", stream_in
.Eof());
162 CPPUNIT_ASSERT_MESSAGE("Last error is not EOF while stream_in.Eof() is true", stream_in
.GetLastError() == wxSTREAM_EOF
);
165 // Just try to perform a LastRead() on the input stream.
166 void Input_LastRead()
168 CleanupHelper
cleanup(this);
169 TStreamIn
&stream_in
= CreateInStream();
170 CPPUNIT_ASSERT(!stream_in
.Eof());
173 (void)stream_in
.Read(buf
, 5);
174 CPPUNIT_ASSERT_EQUAL(5, stream_in
.LastRead());
175 (void)stream_in
.GetC();
176 CPPUNIT_ASSERT_EQUAL(1, stream_in
.LastRead());
181 CleanupHelper
cleanup(this);
182 TStreamIn
&stream_in
= CreateInStream();
184 CPPUNIT_ASSERT( stream_in
.CanRead() );
186 // read the entire contents
187 (void)stream_in
.Read(CreateOutStream());
189 CPPUNIT_ASSERT( !stream_in
.CanRead() );
192 // Just try to perform a SeekI() on the input stream.
195 CleanupHelper
cleanup(this);
196 TStreamIn
&stream_in
= CreateInStream();
198 CPPUNIT_ASSERT( stream_in
.IsSeekable() );
199 CPPUNIT_ASSERT(!stream_in
.Eof());
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.
204 CPPUNIT_ASSERT_EQUAL(2, stream_in
.SeekI(2, wxFromStart
));
205 CPPUNIT_ASSERT_EQUAL(4, stream_in
.SeekI(2, wxFromCurrent
));
206 // Not sure the following line is correct, so test it differently.
207 //CPPUNIT_ASSERT_EQUAL(stream_in.GetSize()-2, stream_in.SeekI(-2, wxFromEnd));
208 CPPUNIT_ASSERT(stream_in
.SeekI(-2, wxFromEnd
) != wxInvalidOffset
);
209 // Go beyond the stream size.
210 CPPUNIT_ASSERT((stream_in
.SeekI(10, wxFromCurrent
) == wxInvalidOffset
) == m_bSeekInvalidBeyondEnd
);
213 // Just try to perform a TellI() on the input stream.
216 CleanupHelper
cleanup(this);
217 TStreamIn
&stream_in
= CreateInStream();
219 CPPUNIT_ASSERT(!stream_in
.Eof());
221 // Try to Get the location in the stream...
222 CPPUNIT_ASSERT_EQUAL(0, stream_in
.TellI());
223 (void)stream_in
.GetC();
224 CPPUNIT_ASSERT_EQUAL(1, stream_in
.TellI());
225 if (!m_bSimpleTellITest
)
227 wxFileOffset pos
= stream_in
.SeekI(5, wxFromStart
);
228 CPPUNIT_ASSERT_EQUAL(pos
, stream_in
.TellI());
229 (void)stream_in
.GetC();
230 CPPUNIT_ASSERT_EQUAL(6, stream_in
.TellI());
231 pos
= stream_in
.SeekI(2, wxFromCurrent
);
232 CPPUNIT_ASSERT_EQUAL(pos
, stream_in
.TellI());
233 pos
= stream_in
.SeekI(5, wxFromStart
);
234 CPPUNIT_ASSERT_EQUAL(pos
, stream_in
.TellI());
238 // Just try to perform a Peek() on the input stream.
241 CleanupHelper
cleanup(this);
242 TStreamIn
&stream_in
= CreateInStream();
244 // Test the full stream
245 while (stream_in
.IsOk())
247 char peekChar
= stream_in
.Peek();
248 char getChar
= stream_in
.GetC();
249 if (stream_in
.LastRead() == 1)
250 CPPUNIT_ASSERT_EQUAL(getChar
, peekChar
);
254 // Just try to perform a Ungetch() on the input stream.
257 CleanupHelper
cleanup(this);
258 TStreamIn
&stream_in
= CreateInStream();
259 CPPUNIT_ASSERT(!stream_in
.Eof());
261 const char *ungetstr
= "test";
262 size_t ungetsize
= stream_in
.Ungetch(ungetstr
, strlen(ungetstr
) + 1);
265 CPPUNIT_ASSERT_EQUAL(strlen(ungetstr
) + 1, ungetsize
);
267 (void)stream_in
.Read(buf
, ungetsize
);
268 CPPUNIT_ASSERT(strcmp(buf
, ungetstr
) == 0);
271 if (stream_in
.Ungetch('a'))
273 CPPUNIT_ASSERT_EQUAL(int('a'), stream_in
.GetC());
278 * Output stream tests.
281 // Just try to perform a PutC() on the output stream.
284 CleanupHelper
cleanup(this);
285 TStreamOut
&stream_out
= CreateOutStream();
287 const char *buf
= "Some text";
288 const wxFileOffset len
= strlen(buf
);
289 for ( int i
= 0; i
< len
; i
++ )
290 stream_out
.PutC(buf
[i
]);
292 if ( stream_out
.IsSeekable() )
293 CPPUNIT_ASSERT_EQUAL(len
, stream_out
.TellO());
296 // Just try to perform a Write() on the output stream.
299 CleanupHelper
cleanup(this);
300 TStreamOut
&stream_out
= CreateOutStream();
302 // Do the buffer version.
303 const char *buf
= "Some text";
304 const wxFileOffset len
= strlen(buf
);
305 (void)stream_out
.Write(buf
, len
);
306 if ( stream_out
.IsSeekable() )
307 CPPUNIT_ASSERT_EQUAL( len
, stream_out
.TellO() );
309 // Do the Stream version.
310 TStreamIn
&stream_in
= CreateInStream();
311 (void)stream_out
.Write(stream_in
);
313 if ( stream_out
.IsSeekable() )
314 CPPUNIT_ASSERT(stream_out
.TellO() > len
);
317 // Just try to perform a LastWrite() on the output stream.
318 void Output_LastWrite()
320 CleanupHelper
cleanup(this);
321 TStreamOut
&stream_out
= CreateOutStream();
323 const char *buf
= "12345";
324 (void)stream_out
.Write(buf
, 5);
325 CPPUNIT_ASSERT_EQUAL(5, stream_out
.LastWrite());
326 (void)stream_out
.PutC('1');
327 CPPUNIT_ASSERT_EQUAL(1, stream_out
.LastWrite());
330 // Just try to perform a SeekO() on the output stream.
333 CleanupHelper
cleanup(this);
334 TStreamOut
&stream_out
= CreateOutStream();
336 CPPUNIT_ASSERT( stream_out
.IsSeekable() );
338 // First put some data in the stream, so it is not empty.
339 const char *buf
= "1234567890";
340 (void)stream_out
.Write(buf
, 10);
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.
345 CPPUNIT_ASSERT_EQUAL(2, stream_out
.SeekO(2, wxFromStart
));
346 CPPUNIT_ASSERT_EQUAL(4, stream_out
.SeekO(2, wxFromCurrent
));
347 // Not sure the following line is correct, so test it differently.
348 //CPPUNIT_ASSERT_EQUAL(stream_in.GetSize()-2, stream_out.SeekO(-2, wxFromEnd));
349 CPPUNIT_ASSERT(stream_out
.SeekO(-2, wxFromEnd
) != wxInvalidOffset
);
350 // Go beyond the stream size.
351 CPPUNIT_ASSERT((stream_out
.SeekO(10, wxFromCurrent
) == wxInvalidOffset
) == m_bSeekInvalidBeyondEnd
);
354 // Just try to perform a TellO() on the output stream.
357 CleanupHelper
cleanup(this);
358 TStreamOut
&stream_out
= CreateOutStream();
360 // Try to Get the location in the stream...
361 CPPUNIT_ASSERT_EQUAL(0, stream_out
.TellO());
362 (void)stream_out
.PutC('1');
363 CPPUNIT_ASSERT_EQUAL(1, stream_out
.TellO());
364 if (!m_bSimpleTellOTest
)
366 // First put some extra data in the stream, so it's not empty.
367 const char *buf
= "1234567890";
368 (void)stream_out
.Write(buf
, 10);
370 wxFileOffset pos
= stream_out
.SeekO(5, wxFromStart
);
371 CPPUNIT_ASSERT_EQUAL(pos
, stream_out
.TellO());
372 (void)stream_out
.PutC('1');
373 CPPUNIT_ASSERT_EQUAL(6, stream_out
.TellO());
374 pos
= stream_out
.SeekO(2, wxFromCurrent
);
375 CPPUNIT_ASSERT_EQUAL(pos
, stream_out
.TellO());
376 pos
= stream_out
.SeekO(5, wxFromStart
);
377 CPPUNIT_ASSERT_EQUAL(pos
, stream_out
.TellO());
382 // Some tests can be configured... here you can find the config settings
383 bool m_bSimpleTellITest
; // if true, no SeekI will be used by the TellI test.
385 bool m_bSimpleTellOTest
; // if true, no SeekO will be used by the TellI test.
387 bool m_bSeekInvalidBeyondEnd
; // if true a SeekI|O beyond the end of the stream should return wxInvalidOffset
389 bool m_bEofAtLastRead
; // Does EOF occure at the moment the last byte is read or when read past the last byte.
392 TStreamIn
&CreateInStream()
396 wxFAIL_MSG(wxT("Error in test case, the previouse input stream needs to be delete first!"));
399 m_pCurrentIn
= DoCreateInStream();
400 wxASSERT(m_pCurrentIn
!= NULL
);
401 return *m_pCurrentIn
;
403 TStreamOut
&CreateOutStream()
407 wxFAIL_MSG(wxT("Error in test case, the previouse output stream needs to be delete first!"));
410 m_pCurrentOut
= DoCreateOutStream();
411 wxASSERT(m_pCurrentOut
!= NULL
);
412 return *m_pCurrentOut
;
415 void DeleteInStream()
417 if (m_pCurrentIn
== NULL
)
421 // In case something extra needs to be done.
424 void DeleteOutStream()
426 if (m_pCurrentOut
== NULL
)
429 CPPUNIT_ASSERT(m_pCurrentOut
->Close());
431 delete m_pCurrentOut
;
432 m_pCurrentOut
= NULL
;
433 // In case something extra needs to be done.
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 */ }
445 TStreamIn
*m_pCurrentIn
;
446 TStreamOut
*m_pCurrentOut
;