]> git.saurik.com Git - wxWidgets.git/blob - tests/streams/bstream.h
cleanup
[wxWidgets.git] / 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 // RCS-ID: $Id$
6 // Copyright: (c) 2004 Hans Van Leemputten
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_TESTBSTREAM_H__
11 #define _WX_TESTBSTREAM_H__
12
13 #include "wx/cppunit.h"
14
15 ///////////////////////////////////////////////////////////////////////////////
16 // Some macros preventing us from typing too much ;-)
17 //
18
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 )
30
31
32 ///////////////////////////////////////////////////////////////////////////////
33 // Template class that implements a test for all base stream functions.
34 //
35
36 template <class TStreamIn, class TStreamOut> class BaseStreamTestCase : public CppUnit::TestCase
37 {
38 protected:
39 typedef BaseStreamTestCase<TStreamIn, TStreamOut> StreamTestCase;
40
41 class CleanupHelper
42 {
43 public:
44 CleanupHelper(StreamTestCase *value)
45 :m_pCleanup(value)
46 {}
47 ~CleanupHelper()
48 {
49 m_pCleanup->DeleteInStream();
50 m_pCleanup->DeleteOutStream();
51 }
52 private:
53 StreamTestCase *m_pCleanup;
54 };
55 friend class CleanupHelper;
56
57 public:
58 BaseStreamTestCase()
59 :m_bSimpleTellITest(false),
60 m_bSimpleTellOTest(false),
61 m_bSeekInvalidBeyondEnd(true),
62 m_bEofAtLastRead(true),
63 m_pCurrentIn(NULL),
64 m_pCurrentOut(NULL)
65 { /* Nothing extra */ }
66 virtual ~BaseStreamTestCase()
67 {
68 // Prevent mem leaks!
69 delete m_pCurrentIn;
70 delete m_pCurrentOut;
71 }
72
73 protected:
74 /*
75 * Input stream tests.
76 */
77
78 // Just try to perform a GetSize() on the input stream.
79 void Input_GetSize()
80 {
81 CleanupHelper cleanup(this);
82 const TStreamIn &stream_in = CreateInStream();
83 CPPUNIT_ASSERT(!stream_in.Eof());
84
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);
89 }
90
91 // Just try to perform a GetC() on the input stream.
92 void Input_GetC()
93 {
94 CleanupHelper cleanup(this);
95 TStreamIn &stream_in = CreateInStream();
96 CPPUNIT_ASSERT(!stream_in.Eof());
97
98 // If no exception occurs the test is successful.
99 (void)stream_in.GetC();
100 }
101
102 // Just try to perform a Read() on the input stream.
103 void Input_Read()
104 {
105 CleanupHelper cleanup(this);
106 TStreamIn &stream_in = CreateInStream();
107 CPPUNIT_ASSERT(!stream_in.Eof());
108
109 // Note: the input stream should at least be of min size +10!
110
111 char buf[10];
112 (void)stream_in.Read(buf, 10);
113
114 CPPUNIT_ASSERT(!stream_in.Eof());
115 CPPUNIT_ASSERT(stream_in.IsOk());
116
117 // Test the stream version aswell.
118 TStreamOut &stream_out = CreateOutStream();
119 (void)stream_in.Read(stream_out);
120
121 // The output stream should have read the input stream till the end.
122 CPPUNIT_ASSERT(stream_in.Eof());
123 }
124
125 // Test and see what happens to the EOF when we
126 // read after EOF was encountered.
127 void Input_Eof()
128 {
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());
134
135 // Travel to the end of the stream.
136 while(!stream_in.Eof())
137 {
138 // Read, we move one byte along.
139 (void)stream_in.GetC();
140 #if 0
141 // EOF behaviour is different in streams, disabled (for now?)
142
143 if (m_bEofAtLastRead)
144 // EOF should only occure after the last successful get.
145 CPPUNIT_ASSERT_MESSAGE("Eof is detected too late.", !(stream_in.LastRead() != 1 && stream_in.Eof()));
146 else
147 // EOF should only occure after a failed get.
148 CPPUNIT_ASSERT_MESSAGE("Eof is detected too soon.", !(stream_in.LastRead() == 1 && stream_in.Eof()));
149 #endif
150 }
151
152 // Check EOF stream state.
153 CPPUNIT_ASSERT_MESSAGE("EOF is not EOF?", stream_in.Eof());
154
155 // Ok we found the end, lets see if we can go past it.
156 for (size_t i = 0; i < 100; i++)
157 (void)stream_in.GetC();
158
159 // Check for EOF correctness.
160 CPPUNIT_ASSERT_MESSAGE("EOF is wrong when we read past EOF!", stream_in.Eof());
161 CPPUNIT_ASSERT_MESSAGE("Last error is not EOF while stream_in.Eof() is true", stream_in.GetLastError() == wxSTREAM_EOF);
162 }
163
164 // Just try to perform a LastRead() on the input stream.
165 void Input_LastRead()
166 {
167 CleanupHelper cleanup(this);
168 TStreamIn &stream_in = CreateInStream();
169 CPPUNIT_ASSERT(!stream_in.Eof());
170
171 char buf[5];
172 (void)stream_in.Read(buf, 5);
173 CPPUNIT_ASSERT(stream_in.LastRead() == 5);
174 (void)stream_in.GetC();
175 CPPUNIT_ASSERT(stream_in.LastRead() == 1);
176 }
177
178 void Input_CanRead()
179 {
180 CleanupHelper cleanup(this);
181 TStreamIn &stream_in = CreateInStream();
182
183 CPPUNIT_ASSERT( stream_in.CanRead() );
184
185 // read the entire contents
186 (void)stream_in.Read(CreateOutStream());
187
188 CPPUNIT_ASSERT( !stream_in.CanRead() );
189 }
190
191 // Just try to perform a SeekI() on the input stream.
192 void Input_SeekI()
193 {
194 CleanupHelper cleanup(this);
195 TStreamIn &stream_in = CreateInStream();
196 CPPUNIT_ASSERT(!stream_in.Eof());
197
198 // Try to Seek in the stream...
199 // Note: streams not supporting this should register this test
200 // with CPPUNIT_TEST_FAIL instead of CPPUNIT_TEST.
201 CPPUNIT_ASSERT(stream_in.SeekI(2, wxFromStart) == 2);
202 CPPUNIT_ASSERT(stream_in.SeekI(2, wxFromCurrent) == 4);
203 // Not sure the following line is correct, so test it differently.
204 //CPPUNIT_ASSERT(stream_in.SeekI(-2, wxFromEnd) == (off_t)stream_in.GetSize()-2);
205 CPPUNIT_ASSERT(stream_in.SeekI(-2, wxFromEnd) != wxInvalidOffset);
206 // Go beyond the stream size.
207 CPPUNIT_ASSERT((stream_in.SeekI(10, wxFromCurrent) == wxInvalidOffset) == m_bSeekInvalidBeyondEnd);
208 }
209
210 // Just try to perform a TellI() on the input stream.
211 void Input_TellI()
212 {
213 CleanupHelper cleanup(this);
214 TStreamIn &stream_in = CreateInStream();
215 CPPUNIT_ASSERT(!stream_in.Eof());
216
217 // Try to Get the location in the stream...
218 CPPUNIT_ASSERT(stream_in.TellI() == 0);
219 (void)stream_in.GetC();
220 CPPUNIT_ASSERT(stream_in.TellI() == 1);
221 if (!m_bSimpleTellITest)
222 {
223 wxFileOffset pos = stream_in.SeekI(5, wxFromStart);
224 CPPUNIT_ASSERT(stream_in.TellI() == pos);
225 (void)stream_in.GetC();
226 CPPUNIT_ASSERT(stream_in.TellI() == 6);
227 pos = stream_in.SeekI(2, wxFromCurrent);
228 CPPUNIT_ASSERT(stream_in.TellI() == pos);
229 pos = stream_in.SeekI(5, wxFromStart);
230 CPPUNIT_ASSERT(stream_in.TellI() == pos);
231 }
232 }
233
234 // Just try to perform a Peek() on the input stream.
235 void Input_Peek()
236 {
237 CleanupHelper cleanup(this);
238 TStreamIn &stream_in = CreateInStream();
239
240 // Test the full stream
241 while (stream_in.IsOk())
242 {
243 char peekChar = stream_in.Peek();
244 char getChar = stream_in.GetC();
245 if (stream_in.LastRead() == 1)
246 CPPUNIT_ASSERT(peekChar == getChar);
247 }
248 }
249
250 // Just try to perform a Ungetch() on the input stream.
251 void Input_Ungetch()
252 {
253 CleanupHelper cleanup(this);
254 TStreamIn &stream_in = CreateInStream();
255 CPPUNIT_ASSERT(!stream_in.Eof());
256
257 const char *ungetstr = "test";
258 size_t ungetsize = stream_in.Ungetch(ungetstr, strlen(ungetstr) + 1);
259 if (ungetsize != 0)
260 {
261 CPPUNIT_ASSERT(ungetsize == strlen(ungetstr) + 1);
262 char buf[10];
263 (void)stream_in.Read(buf, ungetsize);
264 CPPUNIT_ASSERT(strcmp(buf, ungetstr) == 0);
265 }
266
267 if (stream_in.Ungetch('a'))
268 {
269 CPPUNIT_ASSERT(stream_in.GetC() == 'a');
270 }
271 }
272
273 /*
274 * Output stream tests.
275 */
276
277 // Just try to perform a PutC() on the output stream.
278 void Output_PutC()
279 {
280 CleanupHelper cleanup(this);
281 TStreamOut &stream_out = CreateOutStream();
282
283 char *buf = "Some text";
284 int i;
285 int len = strlen(buf);
286 for (i = 0; i < len; i++)
287 stream_out.PutC(buf[i]);
288
289 CPPUNIT_ASSERT(i == stream_out.TellO());
290 }
291
292 // Just try to perform a Write() on the output stream.
293 void Output_Write()
294 {
295 CleanupHelper cleanup(this);
296 TStreamOut &stream_out = CreateOutStream();
297
298 // Do the buffer version.
299 char *buf = "Some text";
300 int len = strlen(buf);
301 (void)stream_out.Write(buf, len);
302 CPPUNIT_ASSERT(stream_out.TellO() == len);
303
304 // Do the Stream version.
305 TStreamIn &stream_in = CreateInStream();
306 (void)stream_out.Write(stream_in);
307 CPPUNIT_ASSERT(stream_out.TellO() > len);
308 }
309
310 // Just try to perform a LastWrite() on the output stream.
311 void Output_LastWrite()
312 {
313 CleanupHelper cleanup(this);
314 TStreamOut &stream_out = CreateOutStream();
315
316 char *buf = "12345";
317 (void)stream_out.Write(buf, 5);
318 CPPUNIT_ASSERT(stream_out.LastWrite() == 5);
319 (void)stream_out.PutC('1');
320 CPPUNIT_ASSERT(stream_out.LastWrite() == 1);
321 }
322
323 // Just try to perform a SeekO() on the output stream.
324 void Output_SeekO()
325 {
326 CleanupHelper cleanup(this);
327 TStreamOut &stream_out = CreateOutStream();
328
329 // First put some data in the stream, so it is not empty.
330 char *buf = "1234567890";
331 (void)stream_out.Write(buf, 10);
332
333 // Try to Seek in the stream...
334 // Note: streams not supporting this should register this test
335 // with CPPUNIT_TEST_FAIL instead of CPPUNIT_TEST.
336 CPPUNIT_ASSERT(stream_out.SeekO(2, wxFromStart) == 2);
337 CPPUNIT_ASSERT(stream_out.SeekO(2, wxFromCurrent) == 4);
338 // Not sure the following line is correct, so test it differently.
339 //CPPUNIT_ASSERT(stream_out.SeekO(-2, wxFromEnd) == (off_t)stream_in.GetSize()-2);
340 CPPUNIT_ASSERT(stream_out.SeekO(-2, wxFromEnd) != wxInvalidOffset);
341 // Go beyond the stream size.
342 CPPUNIT_ASSERT((stream_out.SeekO(10, wxFromCurrent) == wxInvalidOffset) == m_bSeekInvalidBeyondEnd);
343 }
344
345 // Just try to perform a TellO() on the output stream.
346 void Output_TellO()
347 {
348 CleanupHelper cleanup(this);
349 TStreamOut &stream_out = CreateOutStream();
350
351 // Try to Get the location in the stream...
352 CPPUNIT_ASSERT(stream_out.TellO() == 0);
353 (void)stream_out.PutC('1');
354 CPPUNIT_ASSERT(stream_out.TellO() == 1);
355 if (!m_bSimpleTellOTest)
356 {
357 // First put some extra data in the stream, so it's not empty.
358 char *buf = "1234567890";
359 (void)stream_out.Write(buf, 10);
360
361 off_t pos = stream_out.SeekO(5, wxFromStart);
362 CPPUNIT_ASSERT(stream_out.TellO() == pos);
363 (void)stream_out.PutC('1');
364 CPPUNIT_ASSERT(stream_out.TellO() == 6);
365 pos = stream_out.SeekO(2, wxFromCurrent);
366 CPPUNIT_ASSERT(stream_out.TellO() == pos);
367 pos = stream_out.SeekO(5, wxFromStart);
368 CPPUNIT_ASSERT(stream_out.TellO() == pos);
369 }
370 }
371
372 protected:
373 // Some tests can be configured... here you can find the config settings
374 bool m_bSimpleTellITest; // if true, no SeekI will be used by the TellI test.
375 // Default false.
376 bool m_bSimpleTellOTest; // if true, no SeekO will be used by the TellI test.
377 // Default false.
378 bool m_bSeekInvalidBeyondEnd; // if true a SeekI|O beyond the end of the stream should return wxInvalidOffset
379 // Default true.
380 bool m_bEofAtLastRead; // Does EOF occure at the moment the last byte is read or when read past the last byte.
381 // Default true.
382 protected:
383 TStreamIn &CreateInStream()
384 {
385 if (m_pCurrentIn)
386 {
387 wxFAIL_MSG(_T("Error in test case, the previouse input stream needs to be delete first!"));
388 }
389
390 m_pCurrentIn = DoCreateInStream();
391 wxASSERT(m_pCurrentIn != NULL);
392 return *m_pCurrentIn;
393 }
394 TStreamOut &CreateOutStream()
395 {
396 if (m_pCurrentOut)
397 {
398 wxFAIL_MSG(_T("Error in test case, the previouse output stream needs to be delete first!"));
399 }
400
401 m_pCurrentOut = DoCreateOutStream();
402 wxASSERT(m_pCurrentOut != NULL);
403 return *m_pCurrentOut;
404 }
405
406 void DeleteInStream()
407 {
408 if (m_pCurrentIn == NULL)
409 return;
410 delete m_pCurrentIn;
411 m_pCurrentIn = NULL;
412 // Incase something extra needs to be done.
413 DoDeleteInStream();
414 }
415 void DeleteOutStream()
416 {
417 if (m_pCurrentOut == NULL)
418 return;
419
420 CPPUNIT_ASSERT(m_pCurrentOut->Close());
421
422 delete m_pCurrentOut;
423 m_pCurrentOut = NULL;
424 // Incase something extra needs to be done.
425 DoDeleteOutStream();
426 }
427
428 protected:
429 // Items that need to be implemented by a derived class!
430 virtual TStreamIn *DoCreateInStream() = 0;
431 virtual TStreamOut *DoCreateOutStream() = 0;
432 virtual void DoDeleteInStream() { /* Depends on the base class */ }
433 virtual void DoDeleteOutStream() { /* Depends on the base class */ }
434
435 private:
436 TStreamIn *m_pCurrentIn;
437 TStreamOut *m_pCurrentOut;
438 };
439
440 #endif
441
442