1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/stdstream.cpp
3 // Purpose: Test wxStdInputStreamBuffer/wxStdOutputStreamBuffer
4 // Author: Jonathan Liu <net147@gmail.com>
6 // Copyright: (c) 2009 Jonathan Liu
7 // Licence: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx/wx.h".
18 // for all others, include the necessary headers
23 #if wxUSE_STD_IOSTREAM
25 #include "wx/stdstream.h"
28 #include "wx/mstream.h"
30 // ==========================================================================
32 // ==========================================================================
34 const int TEST_SIZE
= 384;
36 // ==========================================================================
38 // ==========================================================================
40 class StdStreamTestCase
: public CppUnit::TestCase
46 CPPUNIT_TEST_SUITE( StdStreamTestCase
);
47 // Input buffer management and positioning
48 CPPUNIT_TEST( InputBuffer_pubsetbuf
);
49 CPPUNIT_TEST( InputBuffer_pubseekoff
);
50 CPPUNIT_TEST( InputBuffer_pubseekpos
);
51 CPPUNIT_TEST( InputBuffer_pubsync
);
54 CPPUNIT_TEST( InputBuffer_in_avail
);
55 CPPUNIT_TEST( InputBuffer_snextc
);
56 CPPUNIT_TEST( InputBuffer_sbumpc
);
57 CPPUNIT_TEST( InputBuffer_sgetc
);
58 CPPUNIT_TEST( InputBuffer_sgetn
);
59 CPPUNIT_TEST( InputBuffer_sputbackc
);
60 CPPUNIT_TEST( InputBuffer_sungetc
);
62 // Output buffer management and positioning
63 CPPUNIT_TEST( OutputBuffer_pubsetbuf
);
64 CPPUNIT_TEST( OutputBuffer_pubseekoff
);
65 CPPUNIT_TEST( OutputBuffer_pubseekpos
);
66 CPPUNIT_TEST( OutputBuffer_pubsync
);
69 CPPUNIT_TEST( OutputBuffer_sputc
);
70 CPPUNIT_TEST( OutputBuffer_sputn
);
71 CPPUNIT_TEST_SUITE_END();
73 // Input buffer management and positioning
74 void InputBuffer_pubsetbuf();
75 void InputBuffer_pubseekoff();
76 void InputBuffer_pubseekpos();
77 void InputBuffer_pubsync();
80 void InputBuffer_in_avail();
81 void InputBuffer_snextc();
82 void InputBuffer_sbumpc();
83 void InputBuffer_sgetc();
84 void InputBuffer_sgetn();
85 void InputBuffer_sputbackc();
86 void InputBuffer_sungetc();
88 // Output buffer management and positioning
89 void OutputBuffer_pubsetbuf();
90 void OutputBuffer_pubseekoff();
91 void OutputBuffer_pubseekpos();
92 void OutputBuffer_pubsync();
95 void OutputBuffer_sputc();
96 void OutputBuffer_sputn();
98 char m_testData
[TEST_SIZE
];
100 DECLARE_NO_COPY_CLASS(StdStreamTestCase
)
103 // register in the unnamed registry so that these tests are run by default
104 CPPUNIT_TEST_SUITE_REGISTRATION( StdStreamTestCase
);
106 // also include in it's own registry so that these tests can be run alone
107 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StdStreamTestCase
,
108 "StdStreamTestCase" );
110 // ==========================================================================
112 // ==========================================================================
114 StdStreamTestCase::StdStreamTestCase()
116 for (int i
= 0; i
< TEST_SIZE
; ++i
)
117 m_testData
[i
] = (i
& 0xFF);
120 // --------------------------------------------------------------------------
121 // Input buffer management and positioning
122 // --------------------------------------------------------------------------
124 void StdStreamTestCase::InputBuffer_pubsetbuf()
126 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
127 wxStdInputStreamBuffer
buffer(stream
);
128 char testBuffer
[TEST_SIZE
];
130 CPPUNIT_ASSERT(buffer
.pubsetbuf(testBuffer
, TEST_SIZE
) == NULL
);
133 void StdStreamTestCase::InputBuffer_pubseekoff()
135 const char *testData
= "0123456789";
136 wxMemoryInputStream
stream(testData
, 10);
137 wxStdInputStreamBuffer
buffer(stream
);
139 CPPUNIT_ASSERT_EQUAL(2,
140 buffer
.pubseekoff(2, std::ios_base::beg
,
142 CPPUNIT_ASSERT_EQUAL(-1,
143 buffer
.pubseekoff(2, std::ios_base::beg
,
144 std::ios_base::out
));
146 CPPUNIT_ASSERT_EQUAL(4,
147 buffer
.pubseekoff(2, std::ios_base::cur
));
148 CPPUNIT_ASSERT_EQUAL(-1,
149 buffer
.pubseekoff(2, std::ios_base::cur
,
150 std::ios_base::out
));
152 CPPUNIT_ASSERT_EQUAL(8,
153 buffer
.pubseekoff(-2, std::ios_base::end
));
154 CPPUNIT_ASSERT_EQUAL(-1,
155 buffer
.pubseekoff(-2, std::ios_base::end
,
156 std::ios_base::out
));
158 CPPUNIT_ASSERT_EQUAL(-1,
159 buffer
.pubseekoff(3, std::ios_base::cur
));
160 CPPUNIT_ASSERT_EQUAL(-1,
161 buffer
.pubseekoff(3, std::ios_base::cur
,
162 std::ios_base::out
));
165 void StdStreamTestCase::InputBuffer_pubseekpos()
167 const char *testData
= "0123456789";
168 wxMemoryInputStream
stream(testData
, 10);
169 wxStdInputStreamBuffer
buffer(stream
);
171 for (int i
= 9; i
>= 0; --i
)
174 CPPUNIT_ASSERT_EQUAL(i
, buffer
.pubseekpos(i
));
176 CPPUNIT_ASSERT_EQUAL(i
, buffer
.pubseekpos(i
, std::ios_base::in
));
178 CPPUNIT_ASSERT_EQUAL('0' + i
, buffer
.sgetc());
182 void StdStreamTestCase::InputBuffer_pubsync()
184 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
185 wxStdInputStreamBuffer
buffer(stream
);
187 CPPUNIT_ASSERT(buffer
.pubsync() == 0);
190 // --------------------------------------------------------------------------
192 // --------------------------------------------------------------------------
194 void StdStreamTestCase::InputBuffer_in_avail()
196 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
197 wxStdInputStreamBuffer
buffer(stream
);
199 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
200 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
202 char data
[TEST_SIZE
/ 2];
204 buffer
.sgetn(data
, TEST_SIZE
/ 2);
205 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
- TEST_SIZE
/ 2, buffer
.in_avail());
208 void StdStreamTestCase::InputBuffer_snextc()
210 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
211 wxStdInputStreamBuffer
buffer(stream
);
213 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
214 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
216 char data
[TEST_SIZE
];
218 data
[0] = buffer
.sgetc();
220 for (int i
= 1; i
< TEST_SIZE
; ++i
)
221 data
[i
] = buffer
.snextc();
223 CPPUNIT_ASSERT(memcmp(data
, m_testData
, TEST_SIZE
) == 0);
224 CPPUNIT_ASSERT_EQUAL((int)(unsigned char) (m_testData
[TEST_SIZE
- 1]),
226 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
229 void StdStreamTestCase::InputBuffer_sbumpc()
231 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
232 wxStdInputStreamBuffer
buffer(stream
);
234 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
235 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
237 char data
[TEST_SIZE
];
239 for (int i
= 0; i
< TEST_SIZE
; ++i
)
240 data
[i
] = buffer
.sbumpc();
242 CPPUNIT_ASSERT(memcmp(data
, m_testData
, TEST_SIZE
) == 0);
243 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
246 void StdStreamTestCase::InputBuffer_sgetc()
248 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
249 wxStdInputStreamBuffer
buffer(stream
);
251 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
252 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
254 char data
[TEST_SIZE
];
256 for (int i
= 0; i
< TEST_SIZE
; ++i
) {
257 data
[i
] = buffer
.sgetc();
261 CPPUNIT_ASSERT(memcmp(data
, m_testData
, TEST_SIZE
) == 0);
262 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
265 void StdStreamTestCase::InputBuffer_sgetn()
267 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
268 wxStdInputStreamBuffer
buffer(stream
);
270 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
271 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
273 char data
[TEST_SIZE
* 2];
274 std::streamsize read
= buffer
.sgetn(data
, TEST_SIZE
* 2);
276 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, read
);
277 CPPUNIT_ASSERT(memcmp(data
, m_testData
, TEST_SIZE
) == 0);
278 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
281 void StdStreamTestCase::InputBuffer_sputbackc()
283 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
284 wxStdInputStreamBuffer
buffer(stream
);
286 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
287 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
289 char data
[TEST_SIZE
];
290 std::streamsize read
= buffer
.sgetn(data
, TEST_SIZE
);
292 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, read
);
293 CPPUNIT_ASSERT(memcmp(data
, m_testData
, TEST_SIZE
) == 0);
294 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
296 char putBackChar
= m_testData
[TEST_SIZE
- 1] + 147;
298 CPPUNIT_ASSERT_EQUAL((int) putBackChar
, buffer
.sputbackc(putBackChar
));
299 CPPUNIT_ASSERT_EQUAL((int) putBackChar
, buffer
.sgetc());
300 CPPUNIT_ASSERT_EQUAL((int) putBackChar
, buffer
.sbumpc());
301 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
304 void StdStreamTestCase::InputBuffer_sungetc()
306 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
307 wxStdInputStreamBuffer
buffer(stream
);
309 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
310 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
312 char data
[TEST_SIZE
];
313 std::streamsize read
= buffer
.sgetn(data
, TEST_SIZE
);
315 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, read
);
316 CPPUNIT_ASSERT(memcmp(data
, m_testData
, TEST_SIZE
) == 0);
317 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
319 CPPUNIT_ASSERT_EQUAL((int) m_testData
[TEST_SIZE
- 1], buffer
.sungetc());
320 CPPUNIT_ASSERT_EQUAL((int) m_testData
[TEST_SIZE
- 1], buffer
.sgetc());
321 CPPUNIT_ASSERT_EQUAL((int) m_testData
[TEST_SIZE
- 1], buffer
.sbumpc());
322 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
325 // --------------------------------------------------------------------------
326 // Output buffer management and positioning
327 // --------------------------------------------------------------------------
329 void StdStreamTestCase::OutputBuffer_pubsetbuf()
331 wxMemoryOutputStream stream
;
332 wxStdOutputStreamBuffer
buffer(stream
);
333 char testBuffer
[TEST_SIZE
];
335 CPPUNIT_ASSERT(buffer
.pubsetbuf(testBuffer
, TEST_SIZE
) == NULL
);
338 void StdStreamTestCase::OutputBuffer_pubseekoff()
340 char testData
[] = "0123456789";
341 wxMemoryOutputStream
stream(testData
, 10);
342 wxStdOutputStreamBuffer
buffer(stream
);
344 CPPUNIT_ASSERT_EQUAL(2,
345 buffer
.pubseekoff(2, std::ios_base::beg
,
346 std::ios_base::out
));
347 CPPUNIT_ASSERT_EQUAL(-1,
348 buffer
.pubseekoff(2, std::ios_base::beg
,
351 CPPUNIT_ASSERT_EQUAL(4,
352 buffer
.pubseekoff(2, std::ios_base::cur
));
353 CPPUNIT_ASSERT_EQUAL(-1,
354 buffer
.pubseekoff(2, std::ios_base::cur
,
357 CPPUNIT_ASSERT_EQUAL(8,
358 buffer
.pubseekoff(-2, std::ios_base::end
));
359 CPPUNIT_ASSERT_EQUAL(-1,
360 buffer
.pubseekoff(-2, std::ios_base::end
,
363 CPPUNIT_ASSERT_EQUAL(-1,
364 buffer
.pubseekoff(3, std::ios_base::cur
));
365 CPPUNIT_ASSERT_EQUAL(-1,
366 buffer
.pubseekoff(3, std::ios_base::cur
,
370 void StdStreamTestCase::OutputBuffer_pubseekpos()
372 char testData
[] = "0123456789";
373 wxMemoryOutputStream
stream(testData
, 10);
374 wxStdOutputStreamBuffer
buffer(stream
);
376 for (int i
= 9; i
>= 0; --i
)
380 CPPUNIT_ASSERT_EQUAL(i
, buffer
.pubseekpos(i
));
384 CPPUNIT_ASSERT_EQUAL(i
,
385 buffer
.pubseekpos(i
, std::ios_base::out
));
388 CPPUNIT_ASSERT_EQUAL('0' + (9 - i
), buffer
.sputc('0' + (9 - i
)));
391 CPPUNIT_ASSERT(memcmp(testData
, "9876543210", 10) == 0);
393 CPPUNIT_ASSERT_EQUAL(-1, buffer
.pubseekpos(5, std::ios_base::in
));
396 void StdStreamTestCase::OutputBuffer_pubsync()
398 wxMemoryOutputStream stream
;
399 wxStdOutputStreamBuffer
buffer(stream
);
401 CPPUNIT_ASSERT(buffer
.pubsync() == 0);
404 // --------------------------------------------------------------------------
406 // --------------------------------------------------------------------------
408 void StdStreamTestCase::OutputBuffer_sputc()
410 wxMemoryOutputStream stream
;
411 wxStdOutputStreamBuffer
buffer(stream
);
413 for (int i
= 0; i
< TEST_SIZE
; ++i
)
414 buffer
.sputc(m_testData
[i
]);
416 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, stream
.GetSize());
418 char result
[TEST_SIZE
];
420 stream
.CopyTo(result
, TEST_SIZE
);
421 CPPUNIT_ASSERT(memcmp(result
, m_testData
, TEST_SIZE
) == 0);
424 void StdStreamTestCase::OutputBuffer_sputn()
426 wxMemoryOutputStream stream
;
427 wxStdOutputStreamBuffer
buffer(stream
);
429 buffer
.sputn(m_testData
, TEST_SIZE
);
430 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, stream
.GetSize());
432 char result
[TEST_SIZE
];
434 stream
.CopyTo(result
, TEST_SIZE
);
435 CPPUNIT_ASSERT(memcmp(result
, m_testData
, TEST_SIZE
) == 0);
438 #endif // wxUSE_STD_IOSTREAM