]> git.saurik.com Git - wxWidgets.git/blame - tests/streams/stdstream.cpp
Make @genericAppearance Doxygen macro consistent with @appearance.
[wxWidgets.git] / tests / streams / stdstream.cpp
CommitLineData
72a7c559
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: tests/streams/stdstream.cpp
3// Purpose: Test wxStdInputStreamBuffer/wxStdOutputStreamBuffer
4// Author: Jonathan Liu <net147@gmail.com>
72a7c559 5// Copyright: (c) 2009 Jonathan Liu
526954c5 6// Licence: wxWindows licence
72a7c559
VZ
7///////////////////////////////////////////////////////////////////////////////
8
9// For compilers that support precompilation, includes "wx/wx.h".
10// and "wx/cppunit.h"
11#include "testprec.h"
12
13#ifdef __BORLANDC__
14 #pragma hdrstop
15#endif
16
17// for all others, include the necessary headers
18#ifndef WX_PRECOMP
19 #include "wx/wx.h"
20#endif
21
22#if wxUSE_STD_IOSTREAM
23
24#include "wx/stdstream.h"
25
26#include <string.h>
27#include "wx/mstream.h"
28
72a7c559
VZ
29// ==========================================================================
30// Definitions
31// ==========================================================================
32
33const int TEST_SIZE = 384;
34
35// ==========================================================================
36// Test class
37// ==========================================================================
38
39class StdStreamTestCase : public CppUnit::TestCase
40{
41public:
42 StdStreamTestCase();
43
44private:
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 );
51
52 // Input functions
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 );
60
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 );
66
67 // Output functions
68 CPPUNIT_TEST( OutputBuffer_sputc );
69 CPPUNIT_TEST( OutputBuffer_sputn );
70 CPPUNIT_TEST_SUITE_END();
71
72 // Input buffer management and positioning
73 void InputBuffer_pubsetbuf();
74 void InputBuffer_pubseekoff();
75 void InputBuffer_pubseekpos();
76 void InputBuffer_pubsync();
77
78 // Input functions
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();
86
87 // Output buffer management and positioning
88 void OutputBuffer_pubsetbuf();
89 void OutputBuffer_pubseekoff();
90 void OutputBuffer_pubseekpos();
91 void OutputBuffer_pubsync();
92
93 // Output functions
94 void OutputBuffer_sputc();
95 void OutputBuffer_sputn();
96
97 char m_testData[TEST_SIZE];
98
99 DECLARE_NO_COPY_CLASS(StdStreamTestCase)
100};
101
102// register in the unnamed registry so that these tests are run by default
103CPPUNIT_TEST_SUITE_REGISTRATION( StdStreamTestCase );
104
e3778b4d 105// also include in its own registry so that these tests can be run alone
72a7c559
VZ
106CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StdStreamTestCase,
107 "StdStreamTestCase" );
108
109// ==========================================================================
110// Implementation
111// ==========================================================================
112
113StdStreamTestCase::StdStreamTestCase()
114{
115 for (int i = 0; i < TEST_SIZE; ++i)
116 m_testData[i] = (i & 0xFF);
117}
118
119// --------------------------------------------------------------------------
120// Input buffer management and positioning
121// --------------------------------------------------------------------------
122
123void StdStreamTestCase::InputBuffer_pubsetbuf()
124{
125 wxMemoryInputStream stream(m_testData, TEST_SIZE);
126 wxStdInputStreamBuffer buffer(stream);
127 char testBuffer[TEST_SIZE];
128
129 CPPUNIT_ASSERT(buffer.pubsetbuf(testBuffer, TEST_SIZE) == NULL);
130}
131
132void StdStreamTestCase::InputBuffer_pubseekoff()
133{
134 const char *testData = "0123456789";
135 wxMemoryInputStream stream(testData, 10);
136 wxStdInputStreamBuffer buffer(stream);
137
138 CPPUNIT_ASSERT_EQUAL(2,
139 buffer.pubseekoff(2, std::ios_base::beg,
140 std::ios_base::in));
141 CPPUNIT_ASSERT_EQUAL(-1,
142 buffer.pubseekoff(2, std::ios_base::beg,
143 std::ios_base::out));
144
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));
150
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));
156
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));
162}
163
164void StdStreamTestCase::InputBuffer_pubseekpos()
165{
166 const char *testData = "0123456789";
167 wxMemoryInputStream stream(testData, 10);
168 wxStdInputStreamBuffer buffer(stream);
169
170 for (int i = 9; i >= 0; --i)
171 {
172 if (i % 2 == 0)
173 CPPUNIT_ASSERT_EQUAL(i, buffer.pubseekpos(i));
174 else
175 CPPUNIT_ASSERT_EQUAL(i, buffer.pubseekpos(i, std::ios_base::in));
176
177 CPPUNIT_ASSERT_EQUAL('0' + i, buffer.sgetc());
178 }
179}
180
181void StdStreamTestCase::InputBuffer_pubsync()
182{
183 wxMemoryInputStream stream(m_testData, TEST_SIZE);
184 wxStdInputStreamBuffer buffer(stream);
185
186 CPPUNIT_ASSERT(buffer.pubsync() == 0);
187}
188
189// --------------------------------------------------------------------------
190// Input functions
191// --------------------------------------------------------------------------
192
193void StdStreamTestCase::InputBuffer_in_avail()
194{
195 wxMemoryInputStream stream(m_testData, TEST_SIZE);
196 wxStdInputStreamBuffer buffer(stream);
197
198 CPPUNIT_ASSERT(buffer.sgetc() != EOF);
199 CPPUNIT_ASSERT_EQUAL(TEST_SIZE, buffer.in_avail());
200
201 char data[TEST_SIZE / 2];
202
203 buffer.sgetn(data, TEST_SIZE / 2);
204 CPPUNIT_ASSERT_EQUAL(TEST_SIZE - TEST_SIZE / 2, buffer.in_avail());
205}
206
207void StdStreamTestCase::InputBuffer_snextc()
208{
209 wxMemoryInputStream stream(m_testData, TEST_SIZE);
210 wxStdInputStreamBuffer buffer(stream);
211
212 CPPUNIT_ASSERT(buffer.sgetc() != EOF);
213 CPPUNIT_ASSERT_EQUAL(TEST_SIZE, buffer.in_avail());
214
215 char data[TEST_SIZE];
216
217 data[0] = buffer.sgetc();
218
219 for (int i = 1; i < TEST_SIZE; ++i)
220 data[i] = buffer.snextc();
221
222 CPPUNIT_ASSERT(memcmp(data, m_testData, TEST_SIZE) == 0);
223 CPPUNIT_ASSERT_EQUAL((int)(unsigned char) (m_testData[TEST_SIZE - 1]),
224 buffer.sbumpc());
225 CPPUNIT_ASSERT(buffer.sgetc() == EOF);
226}
227
228void StdStreamTestCase::InputBuffer_sbumpc()
229{
230 wxMemoryInputStream stream(m_testData, TEST_SIZE);
231 wxStdInputStreamBuffer buffer(stream);
232
233 CPPUNIT_ASSERT(buffer.sgetc() != EOF);
234 CPPUNIT_ASSERT_EQUAL(TEST_SIZE, buffer.in_avail());
235
236 char data[TEST_SIZE];
237
238 for (int i = 0; i < TEST_SIZE; ++i)
239 data[i] = buffer.sbumpc();
240
241 CPPUNIT_ASSERT(memcmp(data, m_testData, TEST_SIZE) == 0);
242 CPPUNIT_ASSERT(buffer.sgetc() == EOF);
243}
244
245void StdStreamTestCase::InputBuffer_sgetc()
246{
247 wxMemoryInputStream stream(m_testData, TEST_SIZE);
248 wxStdInputStreamBuffer buffer(stream);
249
250 CPPUNIT_ASSERT(buffer.sgetc() != EOF);
251 CPPUNIT_ASSERT_EQUAL(TEST_SIZE, buffer.in_avail());
252
253 char data[TEST_SIZE];
254
255 for (int i = 0; i < TEST_SIZE; ++i) {
256 data[i] = buffer.sgetc();
257 buffer.sbumpc();
258 }
259
260 CPPUNIT_ASSERT(memcmp(data, m_testData, TEST_SIZE) == 0);
261 CPPUNIT_ASSERT(buffer.sgetc() == EOF);
262}
263
264void StdStreamTestCase::InputBuffer_sgetn()
265{
266 wxMemoryInputStream stream(m_testData, TEST_SIZE);
267 wxStdInputStreamBuffer buffer(stream);
268
269 CPPUNIT_ASSERT(buffer.sgetc() != EOF);
270 CPPUNIT_ASSERT_EQUAL(TEST_SIZE, buffer.in_avail());
271
272 char data[TEST_SIZE * 2];
273 std::streamsize read = buffer.sgetn(data, TEST_SIZE * 2);
274
275 CPPUNIT_ASSERT_EQUAL(TEST_SIZE, read);
276 CPPUNIT_ASSERT(memcmp(data, m_testData, TEST_SIZE) == 0);
277 CPPUNIT_ASSERT(buffer.sgetc() == EOF);
278}
279
280void StdStreamTestCase::InputBuffer_sputbackc()
281{
282 wxMemoryInputStream stream(m_testData, TEST_SIZE);
283 wxStdInputStreamBuffer buffer(stream);
284
285 CPPUNIT_ASSERT(buffer.sgetc() != EOF);
286 CPPUNIT_ASSERT_EQUAL(TEST_SIZE, buffer.in_avail());
287
288 char data[TEST_SIZE];
289 std::streamsize read = buffer.sgetn(data, TEST_SIZE);
290
291 CPPUNIT_ASSERT_EQUAL(TEST_SIZE, read);
292 CPPUNIT_ASSERT(memcmp(data, m_testData, TEST_SIZE) == 0);
293 CPPUNIT_ASSERT(buffer.sgetc() == EOF);
294
295 char putBackChar = m_testData[TEST_SIZE - 1] + 147;
296
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);
301}
302
303void StdStreamTestCase::InputBuffer_sungetc()
304{
305 wxMemoryInputStream stream(m_testData, TEST_SIZE);
306 wxStdInputStreamBuffer buffer(stream);
307
308 CPPUNIT_ASSERT(buffer.sgetc() != EOF);
309 CPPUNIT_ASSERT_EQUAL(TEST_SIZE, buffer.in_avail());
310
311 char data[TEST_SIZE];
312 std::streamsize read = buffer.sgetn(data, TEST_SIZE);
313
314 CPPUNIT_ASSERT_EQUAL(TEST_SIZE, read);
315 CPPUNIT_ASSERT(memcmp(data, m_testData, TEST_SIZE) == 0);
316 CPPUNIT_ASSERT(buffer.sgetc() == EOF);
317
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);
322}
323
324// --------------------------------------------------------------------------
325// Output buffer management and positioning
326// --------------------------------------------------------------------------
327
328void StdStreamTestCase::OutputBuffer_pubsetbuf()
329{
330 wxMemoryOutputStream stream;
331 wxStdOutputStreamBuffer buffer(stream);
332 char testBuffer[TEST_SIZE];
333
334 CPPUNIT_ASSERT(buffer.pubsetbuf(testBuffer, TEST_SIZE) == NULL);
335}
336
337void StdStreamTestCase::OutputBuffer_pubseekoff()
338{
339 char testData[] = "0123456789";
340 wxMemoryOutputStream stream(testData, 10);
341 wxStdOutputStreamBuffer buffer(stream);
342
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,
348 std::ios_base::in));
349
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,
354 std::ios_base::in));
355
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,
360 std::ios_base::in));
361
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,
366 std::ios_base::in));
367}
368
369void StdStreamTestCase::OutputBuffer_pubseekpos()
370{
371 char testData[] = "0123456789";
372 wxMemoryOutputStream stream(testData, 10);
373 wxStdOutputStreamBuffer buffer(stream);
374
375 for (int i = 9; i >= 0; --i)
376 {
377 if (i % 2 == 0)
378 {
379 CPPUNIT_ASSERT_EQUAL(i, buffer.pubseekpos(i));
380 }
381 else
382 {
383 CPPUNIT_ASSERT_EQUAL(i,
384 buffer.pubseekpos(i, std::ios_base::out));
385 }
386
387 CPPUNIT_ASSERT_EQUAL('0' + (9 - i), buffer.sputc('0' + (9 - i)));
388 }
389
390 CPPUNIT_ASSERT(memcmp(testData, "9876543210", 10) == 0);
391
392 CPPUNIT_ASSERT_EQUAL(-1, buffer.pubseekpos(5, std::ios_base::in));
393}
394
395void StdStreamTestCase::OutputBuffer_pubsync()
396{
397 wxMemoryOutputStream stream;
398 wxStdOutputStreamBuffer buffer(stream);
399
400 CPPUNIT_ASSERT(buffer.pubsync() == 0);
401}
402
403// --------------------------------------------------------------------------
404// Output functions
405// --------------------------------------------------------------------------
406
407void StdStreamTestCase::OutputBuffer_sputc()
408{
409 wxMemoryOutputStream stream;
410 wxStdOutputStreamBuffer buffer(stream);
411
412 for (int i = 0; i < TEST_SIZE; ++i)
413 buffer.sputc(m_testData[i]);
414
415 CPPUNIT_ASSERT_EQUAL(TEST_SIZE, stream.GetSize());
416
417 char result[TEST_SIZE];
418
419 stream.CopyTo(result, TEST_SIZE);
420 CPPUNIT_ASSERT(memcmp(result, m_testData, TEST_SIZE) == 0);
421}
422
423void StdStreamTestCase::OutputBuffer_sputn()
424{
425 wxMemoryOutputStream stream;
426 wxStdOutputStreamBuffer buffer(stream);
427
428 buffer.sputn(m_testData, TEST_SIZE);
429 CPPUNIT_ASSERT_EQUAL(TEST_SIZE, stream.GetSize());
430
431 char result[TEST_SIZE];
432
433 stream.CopyTo(result, TEST_SIZE);
434 CPPUNIT_ASSERT(memcmp(result, m_testData, TEST_SIZE) == 0);
435}
436
437#endif // wxUSE_STD_IOSTREAM