First public contribution.
4 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
8 # include "full_streambuf.h"
10 # include "cppunit/cppunit_proxy.h"
12 # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
19 class SstreamTest : public CPPUNIT_NS::TestCase
21 CPPUNIT_TEST_SUITE(SstreamTest);
24 CPPUNIT_TEST(input_char);
27 CPPUNIT_TEST(err_long);
29 CPPUNIT_TEST(init_in);
30 CPPUNIT_TEST(init_out);
33 CPPUNIT_TEST(streambuf_output);
35 CPPUNIT_TEST(negative);
36 CPPUNIT_TEST_SUITE_END();
50 void streambuf_output();
55 CPPUNIT_TEST_SUITE_REGISTRATION(SstreamTest);
58 // tests implementation
60 void SstreamTest::output()
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" );
70 //Following tests are mostly used to reveal problem with the MSVC /Wp64 option
71 //used to track 64 bits portability issue:
76 CPPUNIT_ASSERT( s.good() );
77 CPPUNIT_ASSERT( s.str() == "0" );
83 CPPUNIT_ASSERT( s.good() );
84 CPPUNIT_ASSERT( s.str() == "0" );
88 void SstreamTest::input()
91 istringstream s( "1\n2\nabcd\nghk lm\nabcd ef" );
94 CPPUNIT_ASSERT( s.good() );
95 CPPUNIT_ASSERT( i == 1 );
98 CPPUNIT_ASSERT( s.good() );
99 CPPUNIT_ASSERT( d == 2.0 );
102 CPPUNIT_ASSERT( s.good() );
103 CPPUNIT_ASSERT( str == "abcd" );
105 s.get(c); // extract newline, that not extracted by operator >>
106 CPPUNIT_ASSERT( s.good() );
107 CPPUNIT_ASSERT( c == '\n' );
109 CPPUNIT_ASSERT( s.good() );
110 CPPUNIT_ASSERT( str == "ghk lm" );
112 CPPUNIT_ASSERT( s.eof() );
113 CPPUNIT_ASSERT( str == "abcd ef" );
116 istringstream s("0");
119 CPPUNIT_ASSERT( !s.fail() );
120 CPPUNIT_ASSERT( s.eof() );
121 CPPUNIT_ASSERT( i == 0 );
125 void SstreamTest::input_char()
127 char buf[16] = { 0, '1', '2', '3' };
128 istringstream s( "0" );
131 CPPUNIT_ASSERT( buf[0] == '0' );
132 CPPUNIT_ASSERT( buf[1] == 0 );
133 CPPUNIT_ASSERT( buf[2] == '2' );
137 void SstreamTest::io()
140 s << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef";
141 CPPUNIT_ASSERT( s.good() );
145 CPPUNIT_ASSERT( i == 1 );
146 CPPUNIT_ASSERT( s.good() );
149 CPPUNIT_ASSERT( d == 2.0 );
150 CPPUNIT_ASSERT( s.good() );
153 CPPUNIT_ASSERT( str == "abcd" );
154 CPPUNIT_ASSERT( s.good() );
156 s.get(c); // extract newline, that not extracted by operator >>
157 CPPUNIT_ASSERT( s.good() );
158 CPPUNIT_ASSERT( c == '\n' );
160 CPPUNIT_ASSERT( s.good() );
161 CPPUNIT_ASSERT( str == "ghk lm" );
163 CPPUNIT_ASSERT( str == "abcd ef" );
164 CPPUNIT_ASSERT( s.eof() );
167 void SstreamTest::err()
169 stringstream s( "9" );
173 CPPUNIT_ASSERT( !s.fail() );
174 CPPUNIT_ASSERT( i == 9 );
176 CPPUNIT_ASSERT( s.fail() );
177 CPPUNIT_ASSERT( s.eof() );
178 CPPUNIT_ASSERT( i == 9 );
181 void SstreamTest::err_long()
183 stringstream s( "9" );
187 CPPUNIT_ASSERT( !s.fail() );
188 CPPUNIT_ASSERT( i == 9 );
190 CPPUNIT_ASSERT( s.fail() );
191 CPPUNIT_ASSERT( s.eof() );
192 CPPUNIT_ASSERT( i == 9 );
195 void SstreamTest::maxint()
199 s << INT_MAX << " " << UINT_MAX << " " << LONG_MAX << " " << ULONG_MAX << " "
200 << INT_MIN << " " << LONG_MIN;
201 CPPUNIT_ASSERT( s.good() );
206 unsigned long ul = 0;
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 );
216 CPPUNIT_ASSERT( !s.fail() );
217 CPPUNIT_ASSERT( i == INT_MIN );
218 CPPUNIT_ASSERT( l == LONG_MIN );
221 void SstreamTest::init_in()
223 istringstream is( "12345" );
227 CPPUNIT_ASSERT( !is.fail() );
228 CPPUNIT_ASSERT( n == 12345 );
230 istringstream dis( "1.2345" );
234 CPPUNIT_ASSERT( !dis.fail() );
235 CPPUNIT_ASSERT( are_equals(d, 1.2345) );
237 istringstream fis( "1.2345" );
241 CPPUNIT_ASSERT( !fis.fail() );
242 CPPUNIT_ASSERT( are_equals(f, 1.2345f) );
245 void SstreamTest::init_out()
247 ostringstream os( "12345" );
248 CPPUNIT_ASSERT( os.str() == "12345" );
251 CPPUNIT_ASSERT( os.good() );
253 // This satisfy to the Standard:
254 CPPUNIT_ASSERT( os.str() == "67345" );
255 // But we don't know the reason, why standard state that.
258 CPPUNIT_ASSERT( os.str() == "89ab" );
261 CPPUNIT_ASSERT( os.good() );
262 CPPUNIT_ASSERT( os.str() == "10ab" );
265 void SstreamTest::buf()
269 ss << "1234567\n89\n";
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
284 CPPUNIT_ASSERT( !ss.fail() );
285 CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 10, paragraph 7
287 CPPUNIT_ASSERT( !ss.fail() );
288 CPPUNIT_ASSERT( c == '8' );
291 void SstreamTest::rdbuf()
295 ss << "1234567\n89\n";
298 ss.get( *os.rdbuf(), '\n' );
299 CPPUNIT_ASSERT( !ss.fail() );
302 CPPUNIT_ASSERT( !ss.fail() );
303 CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 12
304 CPPUNIT_ASSERT( os.str() == "1234567" );
307 void SstreamTest::streambuf_output()
310 istringstream in("01234567890123456789");
311 CPPUNIT_ASSERT( in );
313 auto_ptr<full_streambuf> pfull_buf(new full_streambuf(10));
314 ostream out(pfull_buf.get());
315 CPPUNIT_ASSERT( out );
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" );
324 CPPUNIT_ASSERT( out.fail() );
325 CPPUNIT_ASSERT( in );
329 CPPUNIT_ASSERT( ostr );
330 CPPUNIT_ASSERT( in );
331 CPPUNIT_ASSERT( ostr.str() == "0123456789" );
334 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS)
336 //If the output stream buffer throws:
337 istringstream in("01234567890123456789");
338 CPPUNIT_ASSERT( in );
340 auto_ptr<full_streambuf> pfull_buf(new full_streambuf(10, true));
341 ostream out(pfull_buf.get());
342 CPPUNIT_ASSERT( out );
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" );
352 CPPUNIT_ASSERT( out.fail() && out.bad() );
353 CPPUNIT_ASSERT( in );
357 CPPUNIT_ASSERT( ostr );
358 CPPUNIT_ASSERT( in );
359 CPPUNIT_ASSERT( ostr.str() == "01234567890123456789" );
364 void SstreamTest::seek()
366 stringstream s( "0123456789" );
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) );
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) );
384 string to_string( const T& v )
391 void SstreamTest::negative()
393 CPPUNIT_CHECK( to_string<int>(-1) == "-1" );
394 CPPUNIT_CHECK( to_string<long>(-1) == "-1" );