1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/streams/stdstream.cpp
3 // Purpose: Test wxStdInputStreamBuffer/wxStdOutputStreamBuffer
4 // Author: Jonathan Liu <net147@gmail.com>
5 // Copyright: (c) 2009 Jonathan Liu
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx/wx.h".
17 // for all others, include the necessary headers
22 #if wxUSE_STD_IOSTREAM
24 #include "wx/stdstream.h"
27 #include "wx/mstream.h"
29 // ==========================================================================
31 // ==========================================================================
33 const int TEST_SIZE
= 384;
35 // ==========================================================================
37 // ==========================================================================
39 class StdStreamTestCase
: public CppUnit::TestCase
45 CPPUNIT_TEST_SUITE( StdStreamTestCase
);
46 // Input buffer management and positioning
47 CPPUNIT_TEST( InputBuffer_pubsetbuf
);
48 CPPUNIT_TEST( InputBuffer_pubseekoff
);
49 CPPUNIT_TEST( InputBuffer_pubseekpos
);
50 CPPUNIT_TEST( InputBuffer_pubsync
);
53 CPPUNIT_TEST( InputBuffer_in_avail
);
54 CPPUNIT_TEST( InputBuffer_snextc
);
55 CPPUNIT_TEST( InputBuffer_sbumpc
);
56 CPPUNIT_TEST( InputBuffer_sgetc
);
57 CPPUNIT_TEST( InputBuffer_sgetn
);
58 CPPUNIT_TEST( InputBuffer_sputbackc
);
59 CPPUNIT_TEST( InputBuffer_sungetc
);
61 // Output buffer management and positioning
62 CPPUNIT_TEST( OutputBuffer_pubsetbuf
);
63 CPPUNIT_TEST( OutputBuffer_pubseekoff
);
64 CPPUNIT_TEST( OutputBuffer_pubseekpos
);
65 CPPUNIT_TEST( OutputBuffer_pubsync
);
68 CPPUNIT_TEST( OutputBuffer_sputc
);
69 CPPUNIT_TEST( OutputBuffer_sputn
);
70 CPPUNIT_TEST_SUITE_END();
72 // Input buffer management and positioning
73 void InputBuffer_pubsetbuf();
74 void InputBuffer_pubseekoff();
75 void InputBuffer_pubseekpos();
76 void InputBuffer_pubsync();
79 void InputBuffer_in_avail();
80 void InputBuffer_snextc();
81 void InputBuffer_sbumpc();
82 void InputBuffer_sgetc();
83 void InputBuffer_sgetn();
84 void InputBuffer_sputbackc();
85 void InputBuffer_sungetc();
87 // Output buffer management and positioning
88 void OutputBuffer_pubsetbuf();
89 void OutputBuffer_pubseekoff();
90 void OutputBuffer_pubseekpos();
91 void OutputBuffer_pubsync();
94 void OutputBuffer_sputc();
95 void OutputBuffer_sputn();
97 char m_testData
[TEST_SIZE
];
99 DECLARE_NO_COPY_CLASS(StdStreamTestCase
)
102 // register in the unnamed registry so that these tests are run by default
103 CPPUNIT_TEST_SUITE_REGISTRATION( StdStreamTestCase
);
105 // also include in its own registry so that these tests can be run alone
106 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StdStreamTestCase
,
107 "StdStreamTestCase" );
109 // ==========================================================================
111 // ==========================================================================
113 StdStreamTestCase::StdStreamTestCase()
115 for (int i
= 0; i
< TEST_SIZE
; ++i
)
116 m_testData
[i
] = (i
& 0xFF);
119 // --------------------------------------------------------------------------
120 // Input buffer management and positioning
121 // --------------------------------------------------------------------------
123 void StdStreamTestCase::InputBuffer_pubsetbuf()
125 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
126 wxStdInputStreamBuffer
buffer(stream
);
127 char testBuffer
[TEST_SIZE
];
129 CPPUNIT_ASSERT(buffer
.pubsetbuf(testBuffer
, TEST_SIZE
) == NULL
);
132 void StdStreamTestCase::InputBuffer_pubseekoff()
134 const char *testData
= "0123456789";
135 wxMemoryInputStream
stream(testData
, 10);
136 wxStdInputStreamBuffer
buffer(stream
);
138 CPPUNIT_ASSERT_EQUAL(2,
139 buffer
.pubseekoff(2, std::ios_base::beg
,
141 CPPUNIT_ASSERT_EQUAL(-1,
142 buffer
.pubseekoff(2, std::ios_base::beg
,
143 std::ios_base::out
));
145 CPPUNIT_ASSERT_EQUAL(4,
146 buffer
.pubseekoff(2, std::ios_base::cur
));
147 CPPUNIT_ASSERT_EQUAL(-1,
148 buffer
.pubseekoff(2, std::ios_base::cur
,
149 std::ios_base::out
));
151 CPPUNIT_ASSERT_EQUAL(8,
152 buffer
.pubseekoff(-2, std::ios_base::end
));
153 CPPUNIT_ASSERT_EQUAL(-1,
154 buffer
.pubseekoff(-2, std::ios_base::end
,
155 std::ios_base::out
));
157 CPPUNIT_ASSERT_EQUAL(-1,
158 buffer
.pubseekoff(3, std::ios_base::cur
));
159 CPPUNIT_ASSERT_EQUAL(-1,
160 buffer
.pubseekoff(3, std::ios_base::cur
,
161 std::ios_base::out
));
164 void StdStreamTestCase::InputBuffer_pubseekpos()
166 const char *testData
= "0123456789";
167 wxMemoryInputStream
stream(testData
, 10);
168 wxStdInputStreamBuffer
buffer(stream
);
170 for (int i
= 9; i
>= 0; --i
)
173 CPPUNIT_ASSERT_EQUAL(i
, buffer
.pubseekpos(i
));
175 CPPUNIT_ASSERT_EQUAL(i
, buffer
.pubseekpos(i
, std::ios_base::in
));
177 CPPUNIT_ASSERT_EQUAL('0' + i
, buffer
.sgetc());
181 void StdStreamTestCase::InputBuffer_pubsync()
183 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
184 wxStdInputStreamBuffer
buffer(stream
);
186 CPPUNIT_ASSERT(buffer
.pubsync() == 0);
189 // --------------------------------------------------------------------------
191 // --------------------------------------------------------------------------
193 void StdStreamTestCase::InputBuffer_in_avail()
195 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
196 wxStdInputStreamBuffer
buffer(stream
);
198 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
199 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
201 char data
[TEST_SIZE
/ 2];
203 buffer
.sgetn(data
, TEST_SIZE
/ 2);
204 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
- TEST_SIZE
/ 2, buffer
.in_avail());
207 void StdStreamTestCase::InputBuffer_snextc()
209 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
210 wxStdInputStreamBuffer
buffer(stream
);
212 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
213 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
215 char data
[TEST_SIZE
];
217 data
[0] = buffer
.sgetc();
219 for (int i
= 1; i
< TEST_SIZE
; ++i
)
220 data
[i
] = buffer
.snextc();
222 CPPUNIT_ASSERT(memcmp(data
, m_testData
, TEST_SIZE
) == 0);
223 CPPUNIT_ASSERT_EQUAL((int)(unsigned char) (m_testData
[TEST_SIZE
- 1]),
225 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
228 void StdStreamTestCase::InputBuffer_sbumpc()
230 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
231 wxStdInputStreamBuffer
buffer(stream
);
233 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
234 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
236 char data
[TEST_SIZE
];
238 for (int i
= 0; i
< TEST_SIZE
; ++i
)
239 data
[i
] = buffer
.sbumpc();
241 CPPUNIT_ASSERT(memcmp(data
, m_testData
, TEST_SIZE
) == 0);
242 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
245 void StdStreamTestCase::InputBuffer_sgetc()
247 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
248 wxStdInputStreamBuffer
buffer(stream
);
250 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
251 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
253 char data
[TEST_SIZE
];
255 for (int i
= 0; i
< TEST_SIZE
; ++i
) {
256 data
[i
] = buffer
.sgetc();
260 CPPUNIT_ASSERT(memcmp(data
, m_testData
, TEST_SIZE
) == 0);
261 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
264 void StdStreamTestCase::InputBuffer_sgetn()
266 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
267 wxStdInputStreamBuffer
buffer(stream
);
269 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
270 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
272 char data
[TEST_SIZE
* 2];
273 std::streamsize read
= buffer
.sgetn(data
, TEST_SIZE
* 2);
275 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, read
);
276 CPPUNIT_ASSERT(memcmp(data
, m_testData
, TEST_SIZE
) == 0);
277 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
280 void StdStreamTestCase::InputBuffer_sputbackc()
282 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
283 wxStdInputStreamBuffer
buffer(stream
);
285 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
286 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
288 char data
[TEST_SIZE
];
289 std::streamsize read
= buffer
.sgetn(data
, TEST_SIZE
);
291 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, read
);
292 CPPUNIT_ASSERT(memcmp(data
, m_testData
, TEST_SIZE
) == 0);
293 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
295 char putBackChar
= m_testData
[TEST_SIZE
- 1] + 147;
297 CPPUNIT_ASSERT_EQUAL((int) putBackChar
, buffer
.sputbackc(putBackChar
));
298 CPPUNIT_ASSERT_EQUAL((int) putBackChar
, buffer
.sgetc());
299 CPPUNIT_ASSERT_EQUAL((int) putBackChar
, buffer
.sbumpc());
300 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
303 void StdStreamTestCase::InputBuffer_sungetc()
305 wxMemoryInputStream
stream(m_testData
, TEST_SIZE
);
306 wxStdInputStreamBuffer
buffer(stream
);
308 CPPUNIT_ASSERT(buffer
.sgetc() != EOF
);
309 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, buffer
.in_avail());
311 char data
[TEST_SIZE
];
312 std::streamsize read
= buffer
.sgetn(data
, TEST_SIZE
);
314 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, read
);
315 CPPUNIT_ASSERT(memcmp(data
, m_testData
, TEST_SIZE
) == 0);
316 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
318 CPPUNIT_ASSERT_EQUAL((int) m_testData
[TEST_SIZE
- 1], buffer
.sungetc());
319 CPPUNIT_ASSERT_EQUAL((int) m_testData
[TEST_SIZE
- 1], buffer
.sgetc());
320 CPPUNIT_ASSERT_EQUAL((int) m_testData
[TEST_SIZE
- 1], buffer
.sbumpc());
321 CPPUNIT_ASSERT(buffer
.sgetc() == EOF
);
324 // --------------------------------------------------------------------------
325 // Output buffer management and positioning
326 // --------------------------------------------------------------------------
328 void StdStreamTestCase::OutputBuffer_pubsetbuf()
330 wxMemoryOutputStream stream
;
331 wxStdOutputStreamBuffer
buffer(stream
);
332 char testBuffer
[TEST_SIZE
];
334 CPPUNIT_ASSERT(buffer
.pubsetbuf(testBuffer
, TEST_SIZE
) == NULL
);
337 void StdStreamTestCase::OutputBuffer_pubseekoff()
339 char testData
[] = "0123456789";
340 wxMemoryOutputStream
stream(testData
, 10);
341 wxStdOutputStreamBuffer
buffer(stream
);
343 CPPUNIT_ASSERT_EQUAL(2,
344 buffer
.pubseekoff(2, std::ios_base::beg
,
345 std::ios_base::out
));
346 CPPUNIT_ASSERT_EQUAL(-1,
347 buffer
.pubseekoff(2, std::ios_base::beg
,
350 CPPUNIT_ASSERT_EQUAL(4,
351 buffer
.pubseekoff(2, std::ios_base::cur
));
352 CPPUNIT_ASSERT_EQUAL(-1,
353 buffer
.pubseekoff(2, std::ios_base::cur
,
356 CPPUNIT_ASSERT_EQUAL(8,
357 buffer
.pubseekoff(-2, std::ios_base::end
));
358 CPPUNIT_ASSERT_EQUAL(-1,
359 buffer
.pubseekoff(-2, std::ios_base::end
,
362 CPPUNIT_ASSERT_EQUAL(-1,
363 buffer
.pubseekoff(3, std::ios_base::cur
));
364 CPPUNIT_ASSERT_EQUAL(-1,
365 buffer
.pubseekoff(3, std::ios_base::cur
,
369 void StdStreamTestCase::OutputBuffer_pubseekpos()
371 char testData
[] = "0123456789";
372 wxMemoryOutputStream
stream(testData
, 10);
373 wxStdOutputStreamBuffer
buffer(stream
);
375 for (int i
= 9; i
>= 0; --i
)
379 CPPUNIT_ASSERT_EQUAL(i
, buffer
.pubseekpos(i
));
383 CPPUNIT_ASSERT_EQUAL(i
,
384 buffer
.pubseekpos(i
, std::ios_base::out
));
387 CPPUNIT_ASSERT_EQUAL('0' + (9 - i
), buffer
.sputc('0' + (9 - i
)));
390 CPPUNIT_ASSERT(memcmp(testData
, "9876543210", 10) == 0);
392 CPPUNIT_ASSERT_EQUAL(-1, buffer
.pubseekpos(5, std::ios_base::in
));
395 void StdStreamTestCase::OutputBuffer_pubsync()
397 wxMemoryOutputStream stream
;
398 wxStdOutputStreamBuffer
buffer(stream
);
400 CPPUNIT_ASSERT(buffer
.pubsync() == 0);
403 // --------------------------------------------------------------------------
405 // --------------------------------------------------------------------------
407 void StdStreamTestCase::OutputBuffer_sputc()
409 wxMemoryOutputStream stream
;
410 wxStdOutputStreamBuffer
buffer(stream
);
412 for (int i
= 0; i
< TEST_SIZE
; ++i
)
413 buffer
.sputc(m_testData
[i
]);
415 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, stream
.GetSize());
417 char result
[TEST_SIZE
];
419 stream
.CopyTo(result
, TEST_SIZE
);
420 CPPUNIT_ASSERT(memcmp(result
, m_testData
, TEST_SIZE
) == 0);
423 void StdStreamTestCase::OutputBuffer_sputn()
425 wxMemoryOutputStream stream
;
426 wxStdOutputStreamBuffer
buffer(stream
);
428 buffer
.sputn(m_testData
, TEST_SIZE
);
429 CPPUNIT_ASSERT_EQUAL(TEST_SIZE
, stream
.GetSize());
431 char result
[TEST_SIZE
];
433 stream
.CopyTo(result
, TEST_SIZE
);
434 CPPUNIT_ASSERT(memcmp(result
, m_testData
, TEST_SIZE
) == 0);
437 #endif // wxUSE_STD_IOSTREAM