31
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
#include <string>
|
|
18 |
#include "math_aux.h"
|
|
19 |
|
|
20 |
#if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
|
|
21 |
# include <sstream>
|
|
22 |
# include <memory>
|
|
23 |
|
|
24 |
# include "full_streambuf.h"
|
|
25 |
|
|
26 |
# include "cppunit/cppunit_proxy.h"
|
|
27 |
|
|
28 |
# if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
|
|
29 |
using namespace std;
|
|
30 |
# endif
|
|
31 |
|
|
32 |
//
|
|
33 |
// TestCase class
|
|
34 |
//
|
|
35 |
class SstreamTest : public CPPUNIT_NS::TestCase
|
|
36 |
{
|
|
37 |
CPPUNIT_TEST_SUITE(SstreamTest);
|
|
38 |
CPPUNIT_TEST(output);
|
|
39 |
CPPUNIT_TEST(input);
|
|
40 |
CPPUNIT_TEST(input_char);
|
|
41 |
CPPUNIT_TEST(io);
|
|
42 |
CPPUNIT_TEST(err);
|
|
43 |
CPPUNIT_TEST(err_long);
|
|
44 |
CPPUNIT_TEST(maxint);
|
|
45 |
CPPUNIT_TEST(init_in);
|
|
46 |
CPPUNIT_TEST(init_out);
|
|
47 |
CPPUNIT_TEST(buf);
|
|
48 |
CPPUNIT_TEST(rdbuf);
|
|
49 |
CPPUNIT_TEST(streambuf_output);
|
|
50 |
CPPUNIT_TEST(seek);
|
|
51 |
CPPUNIT_TEST(negative);
|
|
52 |
CPPUNIT_TEST_SUITE_END();
|
|
53 |
|
|
54 |
protected:
|
|
55 |
void output();
|
|
56 |
void input();
|
|
57 |
void input_char();
|
|
58 |
void io();
|
|
59 |
void err();
|
|
60 |
void err_long();
|
|
61 |
void maxint();
|
|
62 |
void init_in();
|
|
63 |
void init_out();
|
|
64 |
void buf();
|
|
65 |
void rdbuf();
|
|
66 |
void streambuf_output();
|
|
67 |
void seek();
|
|
68 |
void negative();
|
|
69 |
};
|
|
70 |
|
|
71 |
CPPUNIT_TEST_SUITE_REGISTRATION(SstreamTest);
|
|
72 |
|
|
73 |
//
|
|
74 |
// tests implementation
|
|
75 |
//
|
|
76 |
void SstreamTest::output()
|
|
77 |
{
|
|
78 |
{
|
|
79 |
ostringstream s;
|
|
80 |
|
|
81 |
s << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef";
|
|
82 |
CPPUNIT_ASSERT( s.good() );
|
|
83 |
CPPUNIT_ASSERT( s.str() == "1\n2\nabcd\nghk lm\nabcd ef" );
|
|
84 |
}
|
|
85 |
|
|
86 |
//Following tests are mostly used to reveal problem with the MSVC /Wp64 option
|
|
87 |
//used to track 64 bits portability issue:
|
|
88 |
{
|
|
89 |
ostringstream s;
|
|
90 |
size_t i = 0;
|
|
91 |
s << i;
|
|
92 |
CPPUNIT_ASSERT( s.good() );
|
|
93 |
CPPUNIT_ASSERT( s.str() == "0" );
|
|
94 |
}
|
|
95 |
{
|
|
96 |
ostringstream s;
|
|
97 |
ptrdiff_t i = 0;
|
|
98 |
s << i;
|
|
99 |
CPPUNIT_ASSERT( s.good() );
|
|
100 |
CPPUNIT_ASSERT( s.str() == "0" );
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
void SstreamTest::input()
|
|
105 |
{
|
|
106 |
{
|
|
107 |
istringstream s( "1\n2\nabcd\nghk lm\nabcd ef" );
|
|
108 |
int i = 0;
|
|
109 |
s >> i;
|
|
110 |
CPPUNIT_ASSERT( s.good() );
|
|
111 |
CPPUNIT_ASSERT( i == 1 );
|
|
112 |
double d = 0.0;
|
|
113 |
s >> d;
|
|
114 |
CPPUNIT_ASSERT( s.good() );
|
|
115 |
CPPUNIT_ASSERT( d == 2.0 );
|
|
116 |
string str;
|
|
117 |
s >> str;
|
|
118 |
CPPUNIT_ASSERT( s.good() );
|
|
119 |
CPPUNIT_ASSERT( str == "abcd" );
|
|
120 |
char c;
|
|
121 |
s.get(c); // extract newline, that not extracted by operator >>
|
|
122 |
CPPUNIT_ASSERT( s.good() );
|
|
123 |
CPPUNIT_ASSERT( c == '\n' );
|
|
124 |
getline( s, str );
|
|
125 |
CPPUNIT_ASSERT( s.good() );
|
|
126 |
CPPUNIT_ASSERT( str == "ghk lm" );
|
|
127 |
getline( s, str );
|
|
128 |
CPPUNIT_ASSERT( s.eof() );
|
|
129 |
CPPUNIT_ASSERT( str == "abcd ef" );
|
|
130 |
}
|
|
131 |
{
|
|
132 |
istringstream s("0");
|
|
133 |
size_t i = 1;
|
|
134 |
s >> i;
|
|
135 |
CPPUNIT_ASSERT( !s.fail() );
|
|
136 |
CPPUNIT_ASSERT( s.eof() );
|
|
137 |
CPPUNIT_ASSERT( i == 0 );
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
void SstreamTest::input_char()
|
|
142 |
{
|
|
143 |
char buf[16] = { 0, '1', '2', '3' };
|
|
144 |
istringstream s( "0" );
|
|
145 |
s >> buf;
|
|
146 |
|
|
147 |
CPPUNIT_ASSERT( buf[0] == '0' );
|
|
148 |
CPPUNIT_ASSERT( buf[1] == 0 );
|
|
149 |
CPPUNIT_ASSERT( buf[2] == '2' );
|
|
150 |
}
|
|
151 |
|
|
152 |
|
|
153 |
void SstreamTest::io()
|
|
154 |
{
|
|
155 |
stringstream s;
|
|
156 |
s << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef";
|
|
157 |
CPPUNIT_ASSERT( s.good() );
|
|
158 |
|
|
159 |
int i = 0;
|
|
160 |
s >> i;
|
|
161 |
CPPUNIT_ASSERT( i == 1 );
|
|
162 |
CPPUNIT_ASSERT( s.good() );
|
|
163 |
double d = 0.0;
|
|
164 |
s >> d;
|
|
165 |
CPPUNIT_ASSERT( d == 2.0 );
|
|
166 |
CPPUNIT_ASSERT( s.good() );
|
|
167 |
string str;
|
|
168 |
s >> str;
|
|
169 |
CPPUNIT_ASSERT( str == "abcd" );
|
|
170 |
CPPUNIT_ASSERT( s.good() );
|
|
171 |
char c;
|
|
172 |
s.get(c); // extract newline, that not extracted by operator >>
|
|
173 |
CPPUNIT_ASSERT( s.good() );
|
|
174 |
CPPUNIT_ASSERT( c == '\n' );
|
|
175 |
getline( s, str );
|
|
176 |
CPPUNIT_ASSERT( s.good() );
|
|
177 |
CPPUNIT_ASSERT( str == "ghk lm" );
|
|
178 |
getline( s, str );
|
|
179 |
CPPUNIT_ASSERT( str == "abcd ef" );
|
|
180 |
CPPUNIT_ASSERT( s.eof() );
|
|
181 |
}
|
|
182 |
|
|
183 |
void SstreamTest::err()
|
|
184 |
{
|
|
185 |
stringstream s( "9" );
|
|
186 |
|
|
187 |
int i = 0;
|
|
188 |
s >> i;
|
|
189 |
CPPUNIT_ASSERT( !s.fail() );
|
|
190 |
CPPUNIT_ASSERT( i == 9 );
|
|
191 |
s >> i;
|
|
192 |
CPPUNIT_ASSERT( s.fail() );
|
|
193 |
CPPUNIT_ASSERT( s.eof() );
|
|
194 |
CPPUNIT_ASSERT( i == 9 );
|
|
195 |
}
|
|
196 |
|
|
197 |
void SstreamTest::err_long()
|
|
198 |
{
|
|
199 |
stringstream s( "9" );
|
|
200 |
|
|
201 |
long i = 0;
|
|
202 |
s >> i;
|
|
203 |
CPPUNIT_ASSERT( !s.fail() );
|
|
204 |
CPPUNIT_ASSERT( i == 9 );
|
|
205 |
s >> i;
|
|
206 |
CPPUNIT_ASSERT( s.fail() );
|
|
207 |
CPPUNIT_ASSERT( s.eof() );
|
|
208 |
CPPUNIT_ASSERT( i == 9 );
|
|
209 |
}
|
|
210 |
|
|
211 |
void SstreamTest::maxint()
|
|
212 |
{
|
|
213 |
stringstream s;
|
|
214 |
|
|
215 |
s << INT_MAX << " " << UINT_MAX << " " << LONG_MAX << " " << ULONG_MAX << " "
|
|
216 |
<< INT_MIN << " " << LONG_MIN;
|
|
217 |
CPPUNIT_ASSERT( s.good() );
|
|
218 |
|
|
219 |
int i = 0;
|
|
220 |
unsigned int u = 0;
|
|
221 |
long l = 0;
|
|
222 |
unsigned long ul = 0;
|
|
223 |
|
|
224 |
s >> i >> u >> l >> ul;
|
|
225 |
CPPUNIT_ASSERT( s.good() );
|
|
226 |
CPPUNIT_ASSERT( i == INT_MAX );
|
|
227 |
CPPUNIT_ASSERT( u == UINT_MAX );
|
|
228 |
CPPUNIT_ASSERT( l == LONG_MAX );
|
|
229 |
CPPUNIT_ASSERT( ul == ULONG_MAX );
|
|
230 |
|
|
231 |
s >> i >> l;
|
|
232 |
CPPUNIT_ASSERT( !s.fail() );
|
|
233 |
CPPUNIT_ASSERT( i == INT_MIN );
|
|
234 |
CPPUNIT_ASSERT( l == LONG_MIN );
|
|
235 |
}
|
|
236 |
|
|
237 |
void SstreamTest::init_in()
|
|
238 |
{
|
|
239 |
istringstream is( "12345" );
|
|
240 |
int n;
|
|
241 |
|
|
242 |
is >> n;
|
|
243 |
CPPUNIT_ASSERT( !is.fail() );
|
|
244 |
CPPUNIT_ASSERT( n == 12345 );
|
|
245 |
|
|
246 |
istringstream dis( "1.2345" );
|
|
247 |
double d;
|
|
248 |
|
|
249 |
dis >> d;
|
|
250 |
CPPUNIT_ASSERT( !dis.fail() );
|
|
251 |
CPPUNIT_ASSERT( are_equals(d, 1.2345) );
|
|
252 |
|
|
253 |
istringstream fis( "1.2345" );
|
|
254 |
float f;
|
|
255 |
|
|
256 |
fis >> f;
|
|
257 |
CPPUNIT_ASSERT( !fis.fail() );
|
|
258 |
CPPUNIT_ASSERT( are_equals(f, 1.2345f) );
|
|
259 |
}
|
|
260 |
|
|
261 |
void SstreamTest::init_out()
|
|
262 |
{
|
|
263 |
ostringstream os( "12345" );
|
|
264 |
CPPUNIT_ASSERT( os.str() == "12345" );
|
|
265 |
|
|
266 |
os << 67;
|
|
267 |
CPPUNIT_ASSERT( os.good() );
|
|
268 |
|
|
269 |
// This satisfy to the Standard:
|
|
270 |
CPPUNIT_ASSERT( os.str() == "67345" );
|
|
271 |
// But we don't know the reason, why standard state that.
|
|
272 |
|
|
273 |
os.str( "89ab" );
|
|
274 |
CPPUNIT_ASSERT( os.str() == "89ab" );
|
|
275 |
|
|
276 |
os << 10;
|
|
277 |
CPPUNIT_ASSERT( os.good() );
|
|
278 |
CPPUNIT_ASSERT( os.str() == "10ab" );
|
|
279 |
}
|
|
280 |
|
|
281 |
void SstreamTest::buf()
|
|
282 |
{
|
|
283 |
stringstream ss;
|
|
284 |
|
|
285 |
ss << "1234567\n89\n";
|
|
286 |
char buf[10];
|
|
287 |
buf[7] = 'x';
|
|
288 |
ss.get( buf, 10 );
|
|
289 |
CPPUNIT_ASSERT( !ss.fail() );
|
|
290 |
CPPUNIT_ASSERT( buf[0] == '1' );
|
|
291 |
CPPUNIT_ASSERT( buf[1] == '2' );
|
|
292 |
CPPUNIT_ASSERT( buf[2] == '3' );
|
|
293 |
CPPUNIT_ASSERT( buf[3] == '4' );
|
|
294 |
CPPUNIT_ASSERT( buf[4] == '5' );
|
|
295 |
CPPUNIT_ASSERT( buf[5] == '6' );
|
|
296 |
CPPUNIT_ASSERT( buf[6] == '7' ); // 27.6.1.3 paragraph 10, paragraph 7
|
|
297 |
CPPUNIT_ASSERT( buf[7] == 0 ); // 27.6.1.3 paragraph 8
|
|
298 |
char c;
|
|
299 |
ss.get(c);
|
|
300 |
CPPUNIT_ASSERT( !ss.fail() );
|
|
301 |
CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 10, paragraph 7
|
|
302 |
ss.get(c);
|
|
303 |
CPPUNIT_ASSERT( !ss.fail() );
|
|
304 |
CPPUNIT_ASSERT( c == '8' );
|
|
305 |
}
|
|
306 |
|
|
307 |
void SstreamTest::rdbuf()
|
|
308 |
{
|
|
309 |
stringstream ss;
|
|
310 |
|
|
311 |
ss << "1234567\n89\n";
|
|
312 |
|
|
313 |
ostringstream os;
|
|
314 |
ss.get( *os.rdbuf(), '\n' );
|
|
315 |
CPPUNIT_ASSERT( !ss.fail() );
|
|
316 |
char c;
|
|
317 |
ss.get(c);
|
|
318 |
CPPUNIT_ASSERT( !ss.fail() );
|
|
319 |
CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 12
|
|
320 |
CPPUNIT_ASSERT( os.str() == "1234567" );
|
|
321 |
}
|
|
322 |
|
|
323 |
void SstreamTest::streambuf_output()
|
|
324 |
{
|
|
325 |
{
|
|
326 |
istringstream in("01234567890123456789");
|
|
327 |
CPPUNIT_ASSERT( in );
|
|
328 |
|
|
329 |
auto_ptr<full_streambuf> pfull_buf(new full_streambuf(10));
|
|
330 |
ostream out(pfull_buf.get());
|
|
331 |
CPPUNIT_ASSERT( out );
|
|
332 |
|
|
333 |
out << in.rdbuf();
|
|
334 |
CPPUNIT_ASSERT( out );
|
|
335 |
CPPUNIT_ASSERT( in );
|
|
336 |
//out is good we can check what has been extracted:
|
|
337 |
CPPUNIT_ASSERT( pfull_buf->str() == "0123456789" );
|
|
338 |
|
|
339 |
out << in.rdbuf();
|
|
340 |
CPPUNIT_ASSERT( out.fail() );
|
|
341 |
CPPUNIT_ASSERT( in );
|
|
342 |
|
|
343 |
ostringstream ostr;
|
|
344 |
ostr << in.rdbuf();
|
|
345 |
CPPUNIT_ASSERT( ostr );
|
|
346 |
CPPUNIT_ASSERT( in );
|
|
347 |
CPPUNIT_ASSERT( ostr.str() == "0123456789" );
|
|
348 |
}
|
|
349 |
|
|
350 |
# if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
|
|
351 |
{
|
|
352 |
//If the output stream buffer throws:
|
|
353 |
istringstream in("01234567890123456789");
|
|
354 |
CPPUNIT_ASSERT( in );
|
|
355 |
|
|
356 |
auto_ptr<full_streambuf> pfull_buf(new full_streambuf(10, true));
|
|
357 |
ostream out(pfull_buf.get());
|
|
358 |
CPPUNIT_ASSERT( out );
|
|
359 |
|
|
360 |
out << in.rdbuf();
|
|
361 |
CPPUNIT_ASSERT( out.bad() );
|
|
362 |
CPPUNIT_ASSERT( in );
|
|
363 |
//out is bad we have no guaranty on what has been extracted:
|
|
364 |
//CPPUNIT_ASSERT( pfull_buf->str() == "0123456789" );
|
|
365 |
|
|
366 |
out.clear();
|
|
367 |
out << in.rdbuf();
|
|
368 |
CPPUNIT_ASSERT( out.fail() && out.bad() );
|
|
369 |
CPPUNIT_ASSERT( in );
|
|
370 |
|
|
371 |
ostringstream ostr;
|
|
372 |
ostr << in.rdbuf();
|
|
373 |
CPPUNIT_ASSERT( ostr );
|
|
374 |
CPPUNIT_ASSERT( in );
|
|
375 |
CPPUNIT_ASSERT( ostr.str() == "01234567890123456789" );
|
|
376 |
}
|
|
377 |
# endif
|
|
378 |
}
|
|
379 |
|
|
380 |
void SstreamTest::seek()
|
|
381 |
{
|
|
382 |
stringstream s( "0123456789" );
|
|
383 |
|
|
384 |
CPPUNIT_ASSERT( s.tellg() == stringstream::pos_type(0) );
|
|
385 |
s.seekg( 6, ios::beg );
|
|
386 |
CPPUNIT_ASSERT( s.tellg() == stringstream::pos_type(6) );
|
|
387 |
s.seekg( -3, ios::cur );
|
|
388 |
CPPUNIT_ASSERT( s.tellg() == stringstream::pos_type(3) );
|
|
389 |
|
|
390 |
istringstream is( "0123456789" );
|
|
391 |
CPPUNIT_ASSERT( is.tellg() == stringstream::pos_type(0) );
|
|
392 |
is.seekg( 6, ios::beg );
|
|
393 |
CPPUNIT_ASSERT( is.tellg() == stringstream::pos_type(6) );
|
|
394 |
is.seekg( -3, ios::cur );
|
|
395 |
CPPUNIT_ASSERT( is.tellg() == stringstream::pos_type(3) );
|
|
396 |
}
|
|
397 |
|
|
398 |
|
|
399 |
template < class T >
|
|
400 |
string to_string( const T& v )
|
|
401 |
{
|
|
402 |
ostringstream oss;
|
|
403 |
oss << v;
|
|
404 |
return oss.str();
|
|
405 |
}
|
|
406 |
|
|
407 |
void SstreamTest::negative()
|
|
408 |
{
|
|
409 |
CPPUNIT_CHECK( to_string<int>(-1) == "-1" );
|
|
410 |
CPPUNIT_CHECK( to_string<long>(-1) == "-1" );
|
|
411 |
}
|
|
412 |
|
|
413 |
#endif
|