Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygensstream_test.cpp
Go to the documentation of this file.
00001 #include <string> 00002 #include "math_aux.h" 00003 00004 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS) 00005 # include <sstream> 00006 # include <memory> 00007 00008 # include "full_streambuf.h" 00009 00010 # include "cppunit/cppunit_proxy.h" 00011 00012 # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00013 using namespace std; 00014 # endif 00015 00016 // 00017 // TestCase class 00018 // 00019 class SstreamTest : public CPPUNIT_NS::TestCase 00020 { 00021 CPPUNIT_TEST_SUITE(SstreamTest); 00022 CPPUNIT_TEST(output); 00023 CPPUNIT_TEST(input); 00024 CPPUNIT_TEST(input_char); 00025 CPPUNIT_TEST(io); 00026 CPPUNIT_TEST(err); 00027 CPPUNIT_TEST(err_long); 00028 CPPUNIT_TEST(maxint); 00029 CPPUNIT_TEST(init_in); 00030 CPPUNIT_TEST(init_out); 00031 CPPUNIT_TEST(buf); 00032 CPPUNIT_TEST(rdbuf); 00033 CPPUNIT_TEST(streambuf_output); 00034 CPPUNIT_TEST(seek); 00035 CPPUNIT_TEST(seekp); 00036 CPPUNIT_TEST(seek_gp); 00037 CPPUNIT_TEST(tellp); 00038 CPPUNIT_TEST(negative); 00039 CPPUNIT_TEST_SUITE_END(); 00040 00041 protected: 00042 void output(); 00043 void input(); 00044 void input_char(); 00045 void io(); 00046 void err(); 00047 void err_long(); 00048 void maxint(); 00049 void init_in(); 00050 void init_out(); 00051 void buf(); 00052 void rdbuf(); 00053 void streambuf_output(); 00054 void seek(); 00055 void seekp(); 00056 void seek_gp(); 00057 void tellp(); 00058 void negative(); 00059 }; 00060 00061 CPPUNIT_TEST_SUITE_REGISTRATION(SstreamTest); 00062 00063 // 00064 // tests implementation 00065 // 00066 void SstreamTest::output() 00067 { 00068 { 00069 ostringstream s; 00070 00071 s << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef"; 00072 CPPUNIT_ASSERT( s.good() ); 00073 CPPUNIT_ASSERT( s.str() == "1\n2\nabcd\nghk lm\nabcd ef" ); 00074 } 00075 00076 //Following tests are mostly used to reveal problem with the MSVC /Wp64 option 00077 //used to track 64 bits portability issue: 00078 { 00079 ostringstream s; 00080 size_t i = 0; 00081 s << i; 00082 CPPUNIT_ASSERT( s.good() ); 00083 CPPUNIT_ASSERT( s.str() == "0" ); 00084 } 00085 { 00086 ostringstream s; 00087 ptrdiff_t i = 0; 00088 s << i; 00089 CPPUNIT_ASSERT( s.good() ); 00090 CPPUNIT_ASSERT( s.str() == "0" ); 00091 } 00092 } 00093 00094 void SstreamTest::input() 00095 { 00096 { 00097 istringstream s( "1\n2\nabcd\nghk lm\nabcd ef" ); 00098 int i = 0; 00099 s >> i; 00100 CPPUNIT_ASSERT( s.good() ); 00101 CPPUNIT_ASSERT( i == 1 ); 00102 double d = 0.0; 00103 s >> d; 00104 CPPUNIT_ASSERT( s.good() ); 00105 CPPUNIT_ASSERT( d == 2.0 ); 00106 string str; 00107 s >> str; 00108 CPPUNIT_ASSERT( s.good() ); 00109 CPPUNIT_ASSERT( str == "abcd" ); 00110 char c; 00111 s.get(c); // extract newline, that not extracted by operator >> 00112 CPPUNIT_ASSERT( s.good() ); 00113 CPPUNIT_ASSERT( c == '\n' ); 00114 getline( s, str ); 00115 CPPUNIT_ASSERT( s.good() ); 00116 CPPUNIT_ASSERT( str == "ghk lm" ); 00117 getline( s, str ); 00118 CPPUNIT_ASSERT( s.eof() ); 00119 CPPUNIT_ASSERT( str == "abcd ef" ); 00120 } 00121 { 00122 istringstream s("0"); 00123 size_t i = 1; 00124 s >> i; 00125 CPPUNIT_ASSERT( !s.fail() ); 00126 CPPUNIT_ASSERT( s.eof() ); 00127 CPPUNIT_ASSERT( i == 0 ); 00128 } 00129 } 00130 00131 void SstreamTest::input_char() 00132 { 00133 char buf[16] = { 0, '1', '2', '3' }; 00134 istringstream s( "0" ); 00135 s >> buf; 00136 00137 CPPUNIT_ASSERT( buf[0] == '0' ); 00138 CPPUNIT_ASSERT( buf[1] == 0 ); 00139 CPPUNIT_ASSERT( buf[2] == '2' ); 00140 } 00141 00142 00143 void SstreamTest::io() 00144 { 00145 stringstream s; 00146 s << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef"; 00147 CPPUNIT_ASSERT( s.good() ); 00148 00149 int i = 0; 00150 s >> i; 00151 CPPUNIT_ASSERT( i == 1 ); 00152 CPPUNIT_ASSERT( s.good() ); 00153 double d = 0.0; 00154 s >> d; 00155 CPPUNIT_ASSERT( d == 2.0 ); 00156 CPPUNIT_ASSERT( s.good() ); 00157 string str; 00158 s >> str; 00159 CPPUNIT_ASSERT( str == "abcd" ); 00160 CPPUNIT_ASSERT( s.good() ); 00161 char c; 00162 s.get(c); // extract newline, that not extracted by operator >> 00163 CPPUNIT_ASSERT( s.good() ); 00164 CPPUNIT_ASSERT( c == '\n' ); 00165 getline( s, str ); 00166 CPPUNIT_ASSERT( s.good() ); 00167 CPPUNIT_ASSERT( str == "ghk lm" ); 00168 getline( s, str ); 00169 CPPUNIT_ASSERT( str == "abcd ef" ); 00170 CPPUNIT_ASSERT( s.eof() ); 00171 } 00172 00173 void SstreamTest::err() 00174 { 00175 stringstream s( "9" ); 00176 00177 int i = 0; 00178 s >> i; 00179 CPPUNIT_ASSERT( !s.fail() ); 00180 CPPUNIT_ASSERT( i == 9 ); 00181 s >> i; 00182 CPPUNIT_ASSERT( s.fail() ); 00183 CPPUNIT_ASSERT( s.eof() ); 00184 CPPUNIT_ASSERT( i == 9 ); 00185 } 00186 00187 void SstreamTest::err_long() 00188 { 00189 stringstream s( "9" ); 00190 00191 long i = 0; 00192 s >> i; 00193 CPPUNIT_ASSERT( !s.fail() ); 00194 CPPUNIT_ASSERT( i == 9 ); 00195 s >> i; 00196 CPPUNIT_ASSERT( s.fail() ); 00197 CPPUNIT_ASSERT( s.eof() ); 00198 CPPUNIT_ASSERT( i == 9 ); 00199 } 00200 00201 void SstreamTest::maxint() 00202 { 00203 stringstream s; 00204 00205 s << INT_MAX << " " << UINT_MAX << " " << LONG_MAX << " " << ULONG_MAX << " " 00206 << INT_MIN << " " << LONG_MIN; 00207 CPPUNIT_ASSERT( s.good() ); 00208 00209 int i = 0; 00210 unsigned int u = 0; 00211 long l = 0; 00212 unsigned long ul = 0; 00213 00214 s >> i >> u >> l >> ul; 00215 CPPUNIT_ASSERT( s.good() ); 00216 CPPUNIT_ASSERT( i == INT_MAX ); 00217 CPPUNIT_ASSERT( u == UINT_MAX ); 00218 CPPUNIT_ASSERT( l == LONG_MAX ); 00219 CPPUNIT_ASSERT( ul == ULONG_MAX ); 00220 00221 s >> i >> l; 00222 CPPUNIT_ASSERT( !s.fail() ); 00223 CPPUNIT_ASSERT( i == INT_MIN ); 00224 CPPUNIT_ASSERT( l == LONG_MIN ); 00225 } 00226 00227 void SstreamTest::init_in() 00228 { 00229 istringstream is( "12345" ); 00230 int n; 00231 00232 is >> n; 00233 CPPUNIT_ASSERT( !is.fail() ); 00234 CPPUNIT_ASSERT( n == 12345 ); 00235 00236 istringstream dis( "1.2345" ); 00237 double d; 00238 00239 dis >> d; 00240 CPPUNIT_ASSERT( !dis.fail() ); 00241 CPPUNIT_ASSERT( are_equals(d, 1.2345) ); 00242 00243 istringstream fis( "1.2345" ); 00244 float f; 00245 00246 fis >> f; 00247 CPPUNIT_ASSERT( !fis.fail() ); 00248 CPPUNIT_ASSERT( are_equals(f, 1.2345f) ); 00249 } 00250 00251 void SstreamTest::init_out() 00252 { 00253 ostringstream os( "12345" ); 00254 CPPUNIT_ASSERT( os.str() == "12345" ); 00255 00256 os << 67; 00257 CPPUNIT_ASSERT( os.good() ); 00258 00259 // This satisfy to the Standard: 00260 // CPPUNIT_ASSERT( os.str() == "67345" ); 00261 // But we don't know the reason, why standard state that. 00262 00263 /* 00264 * 27.7.1.1: ... then copies the content of str into the basic_sringbuf 00265 * underlying character sequence and initializes the input and output 00266 * sequences according to which. If which & ios_base::out is true, initializes 00267 * the output sequence with underlying sequence. ... 00268 * 00269 * I can treat this as 'like output was performed', and then I should bump 00270 * put pointer... Looks like more useful then my previous treatment. 00271 * 00272 * - ptr 00273 */ 00274 00275 CPPUNIT_ASSERT( os.str() == "1234567" ); 00276 00277 00278 os.str( "89ab" ); 00279 CPPUNIT_ASSERT( os.str() == "89ab" ); 00280 00281 os << 10; 00282 CPPUNIT_ASSERT( os.good() ); 00283 // CPPUNIT_ASSERT( os.str() == "10ab" ); 00284 CPPUNIT_ASSERT( os.str() == "89ab10" ); 00285 } 00286 00287 void SstreamTest::buf() 00288 { 00289 stringstream ss; 00290 00291 ss << "1234567\n89\n"; 00292 char buf[10]; 00293 buf[7] = 'x'; 00294 ss.get( buf, 10 ); 00295 CPPUNIT_ASSERT( !ss.fail() ); 00296 CPPUNIT_ASSERT( buf[0] == '1' ); 00297 CPPUNIT_ASSERT( buf[1] == '2' ); 00298 CPPUNIT_ASSERT( buf[2] == '3' ); 00299 CPPUNIT_ASSERT( buf[3] == '4' ); 00300 CPPUNIT_ASSERT( buf[4] == '5' ); 00301 CPPUNIT_ASSERT( buf[5] == '6' ); 00302 CPPUNIT_ASSERT( buf[6] == '7' ); // 27.6.1.3 paragraph 10, paragraph 7 00303 CPPUNIT_ASSERT( buf[7] == 0 ); // 27.6.1.3 paragraph 8 00304 char c; 00305 ss.get(c); 00306 CPPUNIT_ASSERT( !ss.fail() ); 00307 CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 10, paragraph 7 00308 ss.get(c); 00309 CPPUNIT_ASSERT( !ss.fail() ); 00310 CPPUNIT_ASSERT( c == '8' ); 00311 } 00312 00313 void SstreamTest::rdbuf() 00314 { 00315 stringstream ss; 00316 00317 ss << "1234567\n89\n"; 00318 00319 ostringstream os; 00320 ss.get( *os.rdbuf(), '\n' ); 00321 CPPUNIT_ASSERT( !ss.fail() ); 00322 char c; 00323 ss.get(c); 00324 CPPUNIT_ASSERT( !ss.fail() ); 00325 CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 12 00326 CPPUNIT_ASSERT( os.str() == "1234567" ); 00327 } 00328 00329 void SstreamTest::streambuf_output() 00330 { 00331 { 00332 istringstream in("01234567890123456789"); 00333 CPPUNIT_ASSERT( in ); 00334 00335 full_streambuf full_buf(10); 00336 ostream out(&full_buf); 00337 CPPUNIT_ASSERT( out ); 00338 00339 out << in.rdbuf(); 00340 CPPUNIT_ASSERT( out ); 00341 CPPUNIT_ASSERT( in ); 00342 //out is good we can check what has been extracted: 00343 CPPUNIT_ASSERT( full_buf.str() == "0123456789" ); 00344 00345 out << in.rdbuf(); 00346 CPPUNIT_ASSERT( out.fail() ); 00347 CPPUNIT_ASSERT( in ); 00348 00349 ostringstream ostr; 00350 ostr << in.rdbuf(); 00351 CPPUNIT_ASSERT( ostr ); 00352 CPPUNIT_ASSERT( in ); 00353 CPPUNIT_ASSERT( ostr.str() == "0123456789" ); 00354 } 00355 00356 # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) 00357 { 00358 //If the output stream buffer throws: 00359 istringstream in("01234567890123456789"); 00360 CPPUNIT_ASSERT( in ); 00361 00362 full_streambuf full_buf(10, true); 00363 ostream out(&full_buf); 00364 CPPUNIT_ASSERT( out ); 00365 00366 out << in.rdbuf(); 00367 CPPUNIT_ASSERT( out.bad() ); 00368 CPPUNIT_ASSERT( in ); 00369 //out is bad we have no guaranty on what has been extracted: 00370 //CPPUNIT_ASSERT( full_buf.str() == "0123456789" ); 00371 00372 out.clear(); 00373 out << in.rdbuf(); 00374 CPPUNIT_ASSERT( out.fail() && out.bad() ); 00375 CPPUNIT_ASSERT( in ); 00376 00377 ostringstream ostr; 00378 ostr << in.rdbuf(); 00379 CPPUNIT_ASSERT( ostr ); 00380 CPPUNIT_ASSERT( in ); 00381 CPPUNIT_ASSERT( ostr.str() == "01234567890123456789" ); 00382 } 00383 # endif 00384 } 00385 00386 void SstreamTest::seek() 00387 { 00388 stringstream s( "0123456789" ); 00389 00390 CPPUNIT_ASSERT( s.tellg() == stringstream::pos_type(0) ); 00391 s.seekg( 6, ios::beg ); 00392 CPPUNIT_ASSERT( s.tellg() == stringstream::pos_type(6) ); 00393 s.seekg( -3, ios::cur ); 00394 CPPUNIT_ASSERT( s.tellg() == stringstream::pos_type(3) ); 00395 00396 istringstream is( "0123456789" ); 00397 CPPUNIT_ASSERT( is.tellg() == stringstream::pos_type(0) ); 00398 is.seekg( 6, ios::beg ); 00399 CPPUNIT_ASSERT( is.tellg() == stringstream::pos_type(6) ); 00400 is.seekg( -3, ios::cur ); 00401 CPPUNIT_ASSERT( is.tellg() == stringstream::pos_type(3) ); 00402 } 00403 00404 void SstreamTest::seekp() 00405 { 00406 ostringstream s; 00407 00408 s << "1234567"; 00409 CPPUNIT_CHECK( s.tellp() == stringstream::pos_type(7) ); 00410 CPPUNIT_CHECK( s.str() == "1234567" ); 00411 s.seekp( 0 ); 00412 s << "X"; 00413 CPPUNIT_CHECK( s.str() == "X234567" ); 00414 s.seekp( 0, ios::beg ); 00415 s << "Y"; 00416 CPPUNIT_CHECK( s.str() == "Y234567" ); 00417 } 00418 00419 void SstreamTest::seek_gp() 00420 { 00421 stringstream ss( "1" ); 00422 00423 /* ISO/IEC 14882 2003 (and 1998 too) assume change as get as put positions 00424 with seekg and seekp (27.6.1.3, par 38; 27.6.2.4 par 2), 00425 but this contradict to common practice and proposed draft N2588 00426 (27.6.1.3, par 41; 27.6.2.5, par 4) 00427 00428 Now STLport implement (i.e. change behaviour ) the draft's point of view. 00429 */ 00430 00431 ss.seekg( 0, ios::beg ); 00432 ss.seekp( 0, ios::end ); 00433 00434 ss << "2"; 00435 00436 string str; 00437 00438 ss >> str; 00439 00440 /* CPPUNIT_CHECK( str == "2" ); --- according ISO/IEC 14882 2003 */ 00441 CPPUNIT_CHECK( str == "12" ); 00442 } 00443 00444 void SstreamTest::tellp() 00445 { 00446 { 00447 ostringstream o( "1" ); 00448 00449 o << "23456"; 00450 00451 CPPUNIT_CHECK( o.rdbuf()->pubseekoff( 0, ios_base::cur, ios_base::out ) == stringstream::pos_type(6) ); 00452 CPPUNIT_CHECK( o.tellp() == stringstream::pos_type(6) ); 00453 } 00454 { 00455 ostringstream o; 00456 00457 o << "123456"; 00458 00459 CPPUNIT_CHECK( o.rdbuf()->pubseekoff( 0, ios_base::cur, ios_base::out ) == stringstream::pos_type(6) ); 00460 CPPUNIT_CHECK( o.tellp() == stringstream::pos_type(6) ); 00461 } 00462 { 00463 ostringstream o( "1" ); 00464 00465 o << "23456789"; 00466 00467 CPPUNIT_CHECK( o.rdbuf()->pubseekoff( 0, ios_base::cur, ios_base::out ) == stringstream::pos_type(9) ); 00468 CPPUNIT_CHECK( o.tellp() == stringstream::pos_type(9) ); 00469 } 00470 } 00471 00472 00473 template < class T > 00474 string to_string( const T& v ) 00475 { 00476 ostringstream oss; 00477 oss << v; 00478 return oss.str(); 00479 } 00480 00481 void SstreamTest::negative() 00482 { 00483 CPPUNIT_CHECK( to_string<int>(-1) == "-1" ); 00484 CPPUNIT_CHECK( to_string<long>(-1) == "-1" ); 00485 } 00486 00487 #endif Generated on Sun May 27 2012 04:35:49 for ReactOS by
1.7.6.1
|