]>
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"
14 using namespace CppUnit
;
16 ///////////////////////////////////////////////////////////////////////////////
17 // Some macros preventing us from typing too much ;-)
20 #define STREAM_TEST_NAME "Streams"
21 #define COMPOSE_TEST_NAME(Name) \
22 STREAM_TEST_NAME "." #Name
23 #define STREAM_REGISTER_SUB_SUITE(Name) \
24 extern Test* Get##Name##Suite(); \
25 suite->addTest(Get##Name##Suite())
26 #define STREAM_IMPLEMENT_SUB_REGISTRATION_ROUTINE(Name) \
27 Test* Get##Name##Suite() { return Name::suite(); }
28 #define STREAM_TEST_SUBSUITE_NAMED_REGISTRATION(Name) \
29 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Name, COMPOSE_TEST_NAME(Name) ); \
30 STREAM_IMPLEMENT_SUB_REGISTRATION_ROUTINE( Name )
33 ///////////////////////////////////////////////////////////////////////////////
34 // Template class that implements a test for all base stream functions.
37 template <class TStreamIn
, class TStreamOut
> class BaseStreamTestCase
: public TestCase
40 typedef BaseStreamTestCase
<TStreamIn
, TStreamOut
> StreamTestCase
;
45 CleanupHelper(StreamTestCase
*value
)
50 m_pCleanup
->DeleteInStream();
51 m_pCleanup
->DeleteOutStream();
54 StreamTestCase
*m_pCleanup
;
56 friend class CleanupHelper
;
60 :m_bSimpleTellITest(false),
61 m_bSimpleTellOTest(false),
62 m_bSeekInvalidBeyondEnd(true),
63 m_bEofAtLastRead(true),
66 { /* Nothing extra */ }
67 virtual ~BaseStreamTestCase()
79 // Just try to perform a GetSize() on the input stream.
82 CleanupHelper
cleanup(this);
83 const TStreamIn
&stream_in
= CreateInStream();
84 CPPUNIT_ASSERT(!stream_in
.Eof());
86 // Size should be greater then zero.
87 // Note: streams not supporting this should register this test
88 // with CPPUNIT_TEST_FAIL instead of CPPUNIT_TEST.
89 CPPUNIT_ASSERT(stream_in
.GetSize() != 0);
92 // Just try to perform a GetC() on the input stream.
95 CleanupHelper
cleanup(this);
96 TStreamIn
&stream_in
= CreateInStream();
97 CPPUNIT_ASSERT(!stream_in
.Eof());
99 // If no exception occurs the test is successful.
100 (void)stream_in
.GetC();
103 // Just try to perform a Read() on the input stream.
106 CleanupHelper
cleanup(this);
107 TStreamIn
&stream_in
= CreateInStream();
108 CPPUNIT_ASSERT(!stream_in
.Eof());
110 // Note: the input stream should at least be of min size +10!
113 (void)stream_in
.Read(buf
, 10);
115 CPPUNIT_ASSERT(!stream_in
.Eof());
116 CPPUNIT_ASSERT(stream_in
.IsOk());
118 // Test the stream version aswell.
119 TStreamOut
&stream_out
= CreateOutStream();
120 (void)stream_in
.Read(stream_out
);
122 // The output stream should have read the input stream till the end.
123 CPPUNIT_ASSERT(stream_in
.Eof());
126 // Test and see what happens to the EOF when we
127 // read after EOF was encountered.
130 CleanupHelper
cleanup(this);
131 TStreamIn
&stream_in
= CreateInStream();
132 CPPUNIT_ASSERT(!stream_in
.Eof());
133 // Double check to see if Eof it self doesn't changes the Eof status.
134 CPPUNIT_ASSERT(!stream_in
.Eof());
136 // Travel to the end of the stream.
137 while(!stream_in
.Eof())
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, lets 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(stream_in
.LastRead() == 5);
175 (void)stream_in
.GetC();
176 CPPUNIT_ASSERT(stream_in
.LastRead() == 1);
179 // Just try to perform a SeekI() on the input stream.
182 CleanupHelper
cleanup(this);
183 TStreamIn
&stream_in
= CreateInStream();
184 CPPUNIT_ASSERT(!stream_in
.Eof());
186 // Try to Seek in the stream...
187 // Note: streams not supporting this should register this test
188 // with CPPUNIT_TEST_FAIL instead of CPPUNIT_TEST.
189 CPPUNIT_ASSERT(stream_in
.SeekI(2, wxFromStart
) == 2);
190 CPPUNIT_ASSERT(stream_in
.SeekI(2, wxFromCurrent
) == 4);
191 // Not sure the following line is correct, so test it differently.
192 //CPPUNIT_ASSERT(stream_in.SeekI(-2, wxFromEnd) == (off_t)stream_in.GetSize()-2);
193 CPPUNIT_ASSERT(stream_in
.SeekI(-2, wxFromEnd
) != wxInvalidOffset
);
194 // Go beyond the stream size.
195 CPPUNIT_ASSERT((stream_in
.SeekI(10, wxFromCurrent
) == wxInvalidOffset
) == m_bSeekInvalidBeyondEnd
);
198 // Just try to perform a TellI() on the input stream.
201 CleanupHelper
cleanup(this);
202 TStreamIn
&stream_in
= CreateInStream();
203 CPPUNIT_ASSERT(!stream_in
.Eof());
205 // Try to Get the location in the stream...
206 CPPUNIT_ASSERT(stream_in
.TellI() == 0);
207 (void)stream_in
.GetC();
208 CPPUNIT_ASSERT(stream_in
.TellI() == 1);
209 if (!m_bSimpleTellITest
)
211 off_t pos
= stream_in
.SeekI(5, wxFromStart
);
212 CPPUNIT_ASSERT(stream_in
.TellI() == pos
);
213 (void)stream_in
.GetC();
214 CPPUNIT_ASSERT(stream_in
.TellI() == 6);
215 pos
= stream_in
.SeekI(2, wxFromCurrent
);
216 CPPUNIT_ASSERT(stream_in
.TellI() == pos
);
217 pos
= stream_in
.SeekI(5, wxFromStart
);
218 CPPUNIT_ASSERT(stream_in
.TellI() == pos
);
222 // Just try to perform a Peek() on the input stream.
225 CleanupHelper
cleanup(this);
226 TStreamIn
&stream_in
= CreateInStream();
228 // Test the full stream
229 while (stream_in
.IsOk())
231 char peekChar
= stream_in
.Peek();
232 char getChar
= stream_in
.GetC();
233 if (stream_in
.LastRead() == 1)
234 CPPUNIT_ASSERT(peekChar
== getChar
);
238 // Just try to perform a Ungetch() on the input stream.
241 CleanupHelper
cleanup(this);
242 TStreamIn
&stream_in
= CreateInStream();
243 CPPUNIT_ASSERT(!stream_in
.Eof());
245 const char *ungetstr
= "test";
246 size_t ungetsize
= stream_in
.Ungetch(ungetstr
, strlen(ungetstr
) + 1);
249 CPPUNIT_ASSERT(ungetsize
== strlen(ungetstr
) + 1);
251 (void)stream_in
.Read(buf
, ungetsize
);
252 CPPUNIT_ASSERT(strcmp(buf
, ungetstr
) == 0);
255 if (stream_in
.Ungetch('a'))
257 CPPUNIT_ASSERT(stream_in
.GetC() == 'a');
262 * Output stream tests.
265 // Just try to perform a PutC() on the output stream.
268 CleanupHelper
cleanup(this);
269 TStreamOut
&stream_out
= CreateOutStream();
271 char *buf
= "Some text";
273 off_t len
= (off_t
) strlen(buf
);
274 for (i
= 0; i
< len
; i
++)
275 stream_out
.PutC(buf
[i
]);
277 CPPUNIT_ASSERT(i
== stream_out
.TellO());
280 // Just try to perform a Write() on the output stream.
283 CleanupHelper
cleanup(this);
284 TStreamOut
&stream_out
= CreateOutStream();
286 // Do the buffer version.
287 char *buf
= "Some text";
288 off_t len
= (off_t
) strlen(buf
);
289 (void)stream_out
.Write(buf
, len
);
290 CPPUNIT_ASSERT(stream_out
.TellO() == len
);
292 // Do the Stream version.
293 TStreamIn
&stream_in
= CreateInStream();
294 (void)stream_out
.Write(stream_in
);
295 CPPUNIT_ASSERT(stream_out
.TellO() > len
);
298 // Just try to perform a LastWrite() on the output stream.
299 void Output_LastWrite()
301 CleanupHelper
cleanup(this);
302 TStreamOut
&stream_out
= CreateOutStream();
305 (void)stream_out
.Write(buf
, 5);
306 CPPUNIT_ASSERT(stream_out
.LastWrite() == 5);
307 (void)stream_out
.PutC('1');
308 CPPUNIT_ASSERT(stream_out
.LastWrite() == 1);
311 // Just try to perform a SeekO() on the output stream.
314 CleanupHelper
cleanup(this);
315 TStreamOut
&stream_out
= CreateOutStream();
317 // First put some data in the stream, so it is not empty.
318 char *buf
= "1234567890";
319 (void)stream_out
.Write(buf
, 10);
321 // Try to Seek in the stream...
322 // Note: streams not supporting this should register this test
323 // with CPPUNIT_TEST_FAIL instead of CPPUNIT_TEST.
324 CPPUNIT_ASSERT(stream_out
.SeekO(2, wxFromStart
) == 2);
325 CPPUNIT_ASSERT(stream_out
.SeekO(2, wxFromCurrent
) == 4);
326 // Not sure the following line is correct, so test it differently.
327 //CPPUNIT_ASSERT(stream_out.SeekO(-2, wxFromEnd) == (off_t)stream_in.GetSize()-2);
328 CPPUNIT_ASSERT(stream_out
.SeekO(-2, wxFromEnd
) != wxInvalidOffset
);
329 // Go beyond the stream size.
330 CPPUNIT_ASSERT((stream_out
.SeekO(10, wxFromCurrent
) == wxInvalidOffset
) == m_bSeekInvalidBeyondEnd
);
333 // Just try to perform a TellO() on the output stream.
336 CleanupHelper
cleanup(this);
337 TStreamOut
&stream_out
= CreateOutStream();
339 // Try to Get the location in the stream...
340 CPPUNIT_ASSERT(stream_out
.TellO() == 0);
341 (void)stream_out
.PutC('1');
342 CPPUNIT_ASSERT(stream_out
.TellO() == 1);
343 if (!m_bSimpleTellOTest
)
345 // First put some extra data in the stream, so it's not empty.
346 char *buf
= "1234567890";
347 (void)stream_out
.Write(buf
, 10);
349 off_t pos
= stream_out
.SeekO(5, wxFromStart
);
350 CPPUNIT_ASSERT(stream_out
.TellO() == pos
);
351 (void)stream_out
.PutC('1');
352 CPPUNIT_ASSERT(stream_out
.TellO() == 6);
353 pos
= stream_out
.SeekO(2, wxFromCurrent
);
354 CPPUNIT_ASSERT(stream_out
.TellO() == pos
);
355 pos
= stream_out
.SeekO(5, wxFromStart
);
356 CPPUNIT_ASSERT(stream_out
.TellO() == pos
);
361 // Some tests can be configured... here you can find the config settings
362 bool m_bSimpleTellITest
; // if true, no SeekI will be used by the TellI test.
364 bool m_bSimpleTellOTest
; // if true, no SeekO will be used by the TellI test.
366 bool m_bSeekInvalidBeyondEnd
; // if true a SeekI|O beyond the end of the stream should return wxInvalidOffset
368 bool m_bEofAtLastRead
; // Does EOF occure at the moment the last byte is read or when read past the last byte.
371 TStreamIn
&CreateInStream()
375 wxFAIL_MSG(_T("Error in test case, the previouse input stream needs to be delete first!"));
378 m_pCurrentIn
= DoCreateInStream();
379 wxASSERT(m_pCurrentIn
!= NULL
);
380 return *m_pCurrentIn
;
382 TStreamOut
&CreateOutStream()
386 wxFAIL_MSG(_T("Error in test case, the previouse output stream needs to be delete first!"));
389 m_pCurrentOut
= DoCreateOutStream();
390 wxASSERT(m_pCurrentOut
!= NULL
);
391 return *m_pCurrentOut
;
394 void DeleteInStream()
396 if (m_pCurrentIn
== NULL
)
400 // Incase something extra needs to be done.
403 void DeleteOutStream()
405 if (m_pCurrentOut
== NULL
)
407 delete m_pCurrentOut
;
408 m_pCurrentOut
= NULL
;
409 // Incase something extra needs to be done.
414 // Items that need to be implemented by a derived class!
415 virtual TStreamIn
*DoCreateInStream() = 0;
416 virtual TStreamOut
*DoCreateOutStream() = 0;
417 virtual void DoDeleteInStream() { /* Depends on the base class */ }
418 virtual void DoDeleteOutStream() { /* Depends on the base class */ }
421 TStreamIn
*m_pCurrentIn
;
422 TStreamOut
*m_pCurrentOut
;