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