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