Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygennum_put_get_test.cpp
Go to the documentation of this file.
00001 #include <limits> 00002 00003 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS) 00004 # include <iomanip> 00005 # include <string> 00006 # include <sstream> 00007 # include <cstdio> 00008 /* 00009 # include <iostream> 00010 # include <ieee754.h> 00011 */ 00012 00013 # include "complete_digits.h" 00014 # include "cppunit/cppunit_proxy.h" 00015 00016 # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 00017 using namespace std; 00018 # endif 00019 00020 // 00021 // TestCase class 00022 // 00023 class NumPutGetTest : public CPPUNIT_NS::TestCase 00024 { 00025 CPPUNIT_TEST_SUITE(NumPutGetTest); 00026 # if defined (__BORLANDC__) 00027 /* Ignore FPU exceptions, set FPU precision to 64 bits */ 00028 unsigned int _float_control_word = _control87(0, 0); 00029 _control87(PC_64|MCW_EM|IC_AFFINE, MCW_PC|MCW_EM|MCW_IC); 00030 # endif 00031 CPPUNIT_TEST(num_put_float); 00032 CPPUNIT_TEST(num_put_integer); 00033 CPPUNIT_TEST(num_get_float); 00034 CPPUNIT_TEST(num_get_integer); 00035 CPPUNIT_TEST(inhex); 00036 CPPUNIT_TEST(pointer); 00037 CPPUNIT_TEST(fix_float_long); 00038 CPPUNIT_TEST(custom_numpunct); 00039 # if defined (__BORLANDC__) 00040 /* Reset floating point control word */ 00041 _clear87(); 00042 _control87(_float_control_word, MCW_PC|MCW_EM|MCW_IC); 00043 # endif 00044 CPPUNIT_TEST_SUITE_END(); 00045 00046 private: 00047 void num_put_float(); 00048 void num_put_integer(); 00049 void num_get_float(); 00050 void num_get_integer(); 00051 void inhex(); 00052 void pointer(); 00053 void fix_float_long(); 00054 void custom_numpunct(); 00055 00056 static bool check_float(float val, float ref) 00057 { 00058 float epsilon = numeric_limits<float>::epsilon(); 00059 return val <= ref + epsilon && val >= ref - epsilon; 00060 } 00061 00062 static bool check_double(double val, double ref) 00063 { 00064 double epsilon = numeric_limits<double>::epsilon(); 00065 return val <= ref + epsilon && val >= ref - epsilon; 00066 } 00067 00068 static string reset_stream(ostringstream &ostr) 00069 { 00070 string tmp = ostr.str(); 00071 ostr.str(""); 00072 return tmp; 00073 } 00074 00075 #if !defined (STLPORT) || !defined (_STLP_NO_MEMBER_TEMPLATES) 00076 template <class F> 00077 void check_get_float( F v ) 00078 { 00079 F in_val_d = v; 00080 typedef numeric_limits<F> limits; 00081 { 00082 stringstream str; 00083 00084 str << "1E+" << limits::max_exponent10; 00085 00086 str >> in_val_d; 00087 CPPUNIT_ASSERT(!str.fail()); 00088 CPPUNIT_ASSERT(str.eof()); 00089 CPPUNIT_CHECK( in_val_d == in_val_d ); 00090 CPPUNIT_CHECK( in_val_d != limits::infinity() ); 00091 } 00092 { 00093 stringstream str; 00094 00095 str << "-1E+" << limits::max_exponent10; 00096 00097 str >> in_val_d; 00098 CPPUNIT_ASSERT(!str.fail()); 00099 CPPUNIT_ASSERT(str.eof()); 00100 CPPUNIT_CHECK( in_val_d == in_val_d ); 00101 CPPUNIT_CHECK( in_val_d != -limits::infinity() ); 00102 } 00103 { 00104 stringstream str; 00105 00106 str << "1E" << limits::min_exponent10; 00107 00108 str >> in_val_d; 00109 CPPUNIT_ASSERT(!str.fail()); 00110 CPPUNIT_ASSERT(str.eof()); 00111 CPPUNIT_CHECK( in_val_d == in_val_d ); 00112 CPPUNIT_CHECK( in_val_d != F(0.0) ); 00113 } 00114 { 00115 stringstream str; 00116 00117 str << "1E+" << (limits::max_exponent10 + 1); 00118 00119 str >> in_val_d; 00120 CPPUNIT_ASSERT(!str.fail()); 00121 CPPUNIT_ASSERT(str.eof()); 00122 CPPUNIT_CHECK( in_val_d == in_val_d ); 00123 CPPUNIT_CHECK( in_val_d == limits::infinity() ); 00124 } 00125 { 00126 stringstream str; 00127 00128 str << "-1E+" << (limits::max_exponent10 + 1); 00129 00130 str >> in_val_d; 00131 CPPUNIT_ASSERT(!str.fail()); 00132 CPPUNIT_ASSERT(str.eof()); 00133 CPPUNIT_CHECK( in_val_d == in_val_d ); 00134 CPPUNIT_CHECK( in_val_d == -limits::infinity() ); 00135 } 00136 { 00137 stringstream str; 00138 00139 str << "1E" << (limits::min_exponent10 - 1); 00140 00141 str >> in_val_d; 00142 CPPUNIT_ASSERT(!str.fail()); 00143 CPPUNIT_ASSERT(str.eof()); 00144 CPPUNIT_CHECK( in_val_d == in_val_d ); 00145 CPPUNIT_CHECK( in_val_d >= F(0.0) && in_val_d <= limits::min() ); 00146 } 00147 #if !defined (__MINGW32__) 00148 { 00149 stringstream str; 00150 00151 str << limits::max(); 00152 00153 CPPUNIT_ASSERT(!str.fail()); 00154 CPPUNIT_CHECK( str.str() != "inf" ); 00155 CPPUNIT_CHECK( str.str() != "-inf" ); 00156 CPPUNIT_CHECK( str.str() != "nan" ); 00157 CPPUNIT_CHECK( str.str() != "-nan" ); 00158 //CPPUNIT_MESSAGE( str.str().c_str() ); 00159 00160 //str.str(""); 00161 //str << limits::max_exponent10; 00162 //CPPUNIT_MESSAGE( str.str().c_str() ); 00163 00164 str >> in_val_d; 00165 00166 CPPUNIT_ASSERT(!str.fail()); 00167 CPPUNIT_ASSERT(str.eof()); 00168 CPPUNIT_CHECK( in_val_d == in_val_d ); 00169 CPPUNIT_CHECK( in_val_d != limits::infinity() ); 00170 } 00171 { 00172 stringstream str; 00173 00174 str << fixed << limits::max(); 00175 00176 CPPUNIT_ASSERT(!str.fail()); 00177 CPPUNIT_CHECK( str.str() != "inf" ); 00178 CPPUNIT_CHECK( str.str() != "-inf" ); 00179 CPPUNIT_CHECK( str.str() != "nan" ); 00180 CPPUNIT_CHECK( str.str() != "-nan" ); 00181 //CPPUNIT_MESSAGE( str.str().c_str() ); 00182 00183 //str.str(""); 00184 //str << limits::max_exponent10; 00185 //CPPUNIT_MESSAGE( str.str().c_str() ); 00186 00187 str >> in_val_d; 00188 00189 CPPUNIT_ASSERT(!str.fail()); 00190 CPPUNIT_ASSERT(str.eof()); 00191 CPPUNIT_CHECK( in_val_d == in_val_d ); 00192 CPPUNIT_CHECK( in_val_d != limits::infinity() ); 00193 } 00194 { 00195 stringstream str; 00196 00197 str << scientific << setprecision(50) << limits::max(); 00198 00199 CPPUNIT_ASSERT(!str.fail()); 00200 CPPUNIT_CHECK( str.str() != "inf" ); 00201 CPPUNIT_CHECK( str.str() != "-inf" ); 00202 CPPUNIT_CHECK( str.str() != "nan" ); 00203 CPPUNIT_CHECK( str.str() != "-nan" ); 00204 //CPPUNIT_MESSAGE( str.str().c_str() ); 00205 00206 //str.str(""); 00207 //str << limits::max_exponent10; 00208 //CPPUNIT_MESSAGE( str.str().c_str() ); 00209 00210 str >> in_val_d; 00211 00212 CPPUNIT_ASSERT(!str.fail()); 00213 CPPUNIT_ASSERT(str.eof()); 00214 CPPUNIT_CHECK( in_val_d == in_val_d ); 00215 CPPUNIT_CHECK( in_val_d != limits::infinity() ); 00216 } 00217 #endif 00218 { 00219 stringstream str; 00220 00221 str << limits::infinity(); 00222 00223 CPPUNIT_ASSERT( !str.fail() ); 00224 CPPUNIT_ASSERT( !limits::has_infinity || str.str() == "inf" ); 00225 } 00226 { 00227 stringstream str; 00228 00229 str << -limits::infinity(); 00230 00231 CPPUNIT_ASSERT( !str.fail() ); 00232 CPPUNIT_ASSERT( !limits::has_infinity || str.str() == "-inf" ); 00233 } 00234 { 00235 stringstream str; 00236 00237 str << limits::quiet_NaN(); 00238 00239 CPPUNIT_ASSERT( !str.fail() ); 00240 CPPUNIT_ASSERT( !limits::has_quiet_NaN || str.str() == "nan" ); 00241 } 00242 { 00243 stringstream str; 00244 00245 str << -limits::quiet_NaN(); 00246 00247 CPPUNIT_ASSERT( !str.fail() ); 00248 CPPUNIT_ASSERT( !limits::has_quiet_NaN || str.str() == "-nan" ); 00249 } 00250 { 00251 stringstream str; 00252 00253 str << "0." << string(limits::max_exponent10, '0') << "1e" << (limits::max_exponent10 + 1); 00254 CPPUNIT_ASSERT( !str.fail() ); 00255 00256 str >> in_val_d; 00257 CPPUNIT_ASSERT( !str.fail() ); 00258 CPPUNIT_ASSERT( str.eof() ); 00259 CPPUNIT_CHECK( in_val_d == 1 ); 00260 } 00261 { 00262 stringstream str; 00263 00264 str << "1" << string(-(limits::min_exponent10 - 1), '0') << "e" << (limits::min_exponent10 - 1); 00265 CPPUNIT_ASSERT( !str.fail() ); 00266 00267 str >> in_val_d; 00268 CPPUNIT_ASSERT( !str.fail() ); 00269 CPPUNIT_ASSERT( str.eof() ); 00270 CPPUNIT_CHECK( in_val_d == 1 ); 00271 } 00272 # if defined (_STLPORT_VERSION) && (_STLPORT_VERSION >= 0x530) 00273 // The following tests are showing that simply changing stream 00274 // precision lead to different result. Do not seems to be a real 00275 // problem, simply rounding approximation but additional study should 00276 // be done after 5.2 release. 00277 { 00278 stringstream str; 00279 str << setprecision(limits::digits10 + 2) << limits::max(); 00280 00281 CPPUNIT_MESSAGE(str.str().c_str()); 00282 CPPUNIT_ASSERT( !str.fail() ); 00283 00284 F val; 00285 str >> val; 00286 00287 CPPUNIT_ASSERT( !str.fail() ); 00288 CPPUNIT_ASSERT( limits::infinity() > val ); 00289 } 00290 { 00291 stringstream str; 00292 str << setprecision(limits::digits10 + 1) << limits::max(); 00293 00294 CPPUNIT_MESSAGE(str.str().c_str()); 00295 CPPUNIT_ASSERT( !str.fail() ); 00296 00297 F val; 00298 str >> val; 00299 00300 CPPUNIT_ASSERT( !str.fail() ); 00301 CPPUNIT_ASSERT( limits::infinity() > val ); 00302 } 00303 # endif 00304 } 00305 #else 00306 # define __check_get_float( F ) \ 00307 void check_get_float( F v ) \ 00308 { \ 00309 F in_val_d = v; \ 00310 { \ 00311 stringstream str; \ 00312 \ 00313 str << "1E+" << numeric_limits<F>::max_exponent10; \ 00314 \ 00315 str >> in_val_d; \ 00316 CPPUNIT_ASSERT(!str.fail()); \ 00317 CPPUNIT_ASSERT(str.eof()); \ 00318 CPPUNIT_CHECK( in_val_d == in_val_d ); \ 00319 CPPUNIT_CHECK( in_val_d != numeric_limits<F>::infinity() ); \ 00320 } \ 00321 { \ 00322 stringstream str; \ 00323 \ 00324 str << "-1E+" << numeric_limits<F>::max_exponent10; \ 00325 \ 00326 str >> in_val_d; \ 00327 CPPUNIT_ASSERT(!str.fail()); \ 00328 CPPUNIT_ASSERT(str.eof()); \ 00329 CPPUNIT_CHECK( in_val_d == in_val_d ); \ 00330 CPPUNIT_CHECK( in_val_d != -numeric_limits<F>::infinity() ); \ 00331 } \ 00332 { \ 00333 stringstream str; \ 00334 \ 00335 str << "1E" << numeric_limits<F>::min_exponent10; \ 00336 \ 00337 str >> in_val_d; \ 00338 CPPUNIT_ASSERT(!str.fail()); \ 00339 CPPUNIT_ASSERT(str.eof()); \ 00340 CPPUNIT_CHECK( in_val_d == in_val_d ); \ 00341 CPPUNIT_CHECK( in_val_d != F(0.0) ); \ 00342 } \ 00343 { \ 00344 stringstream str; \ 00345 \ 00346 str << "1E+" << (numeric_limits<F>::max_exponent10 + 1); \ 00347 \ 00348 str >> in_val_d; \ 00349 CPPUNIT_ASSERT(!str.fail()); \ 00350 CPPUNIT_ASSERT(str.eof()); \ 00351 CPPUNIT_CHECK( in_val_d == in_val_d ); \ 00352 CPPUNIT_CHECK( in_val_d == numeric_limits<F>::infinity() ); \ 00353 } \ 00354 { \ 00355 stringstream str; \ 00356 \ 00357 str << "-1E+" << (numeric_limits<F>::max_exponent10 + 1); \ 00358 \ 00359 str >> in_val_d; \ 00360 CPPUNIT_ASSERT(!str.fail()); \ 00361 CPPUNIT_ASSERT(str.eof()); \ 00362 CPPUNIT_CHECK( in_val_d == in_val_d ); \ 00363 CPPUNIT_CHECK( in_val_d == -numeric_limits<F>::infinity() ); \ 00364 } \ 00365 { \ 00366 stringstream str; \ 00367 \ 00368 str << "1E" << (numeric_limits<F>::min_exponent10 - 1); \ 00369 \ 00370 str >> in_val_d; \ 00371 CPPUNIT_ASSERT(!str.fail()); \ 00372 CPPUNIT_ASSERT(str.eof()); \ 00373 CPPUNIT_CHECK( in_val_d == in_val_d ); \ 00374 CPPUNIT_CHECK( in_val_d >= F(0.0) && in_val_d <= numeric_limits<F>::min() ); \ 00375 } \ 00376 } 00377 00378 __check_get_float( float ) 00379 __check_get_float( double ) 00380 # if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE) 00381 __check_get_float( long double ) 00382 # endif 00383 # undef __check_get_float 00384 #endif // _STLP_NO_MEMBER_TEMPLATES 00385 }; 00386 00387 CPPUNIT_TEST_SUITE_REGISTRATION(NumPutGetTest); 00388 00389 #if defined (_MSC_VER) 00390 # pragma warning (disable : 4056) 00391 # pragma warning (disable : 4756) 00392 #endif 00393 00394 // 00395 // tests implementation 00396 // 00397 void NumPutGetTest::num_put_float() 00398 { 00399 { 00400 string output, digits; 00401 00402 { 00403 ostringstream ostr; 00404 ostr << 1.23457e+17f; 00405 CPPUNIT_ASSERT(ostr.good()); 00406 output = reset_stream(ostr); 00407 digits = "17"; 00408 complete_digits(digits); 00409 CPPUNIT_CHECK(output == string("1.23457e+") + digits ); 00410 } 00411 00412 { 00413 ostringstream ostr; 00414 ostr << setprecision(200) << 1.23457e+17f; 00415 CPPUNIT_ASSERT(ostr.good()); 00416 output = reset_stream(ostr); 00417 CPPUNIT_CHECK( output.size() < 200 ); 00418 } 00419 00420 { 00421 ostringstream ostr; 00422 ostr << setprecision(200) << numeric_limits<float>::min(); 00423 CPPUNIT_ASSERT(ostr.good()); 00424 output = reset_stream(ostr); 00425 CPPUNIT_CHECK( output.size() < 200 ); 00426 } 00427 00428 { 00429 ostringstream ostr; 00430 ostr << fixed << 1.23457e+17f; 00431 CPPUNIT_ASSERT(ostr.good()); 00432 output = reset_stream(ostr); 00433 CPPUNIT_CHECK(output.size() == 25); 00434 CPPUNIT_CHECK(output.substr(0, 5) == "12345"); 00435 CPPUNIT_CHECK(output.substr(18) == ".000000"); 00436 } 00437 00438 { 00439 ostringstream ostr; 00440 ostr << fixed << showpos << 1.23457e+17f; 00441 CPPUNIT_ASSERT(ostr.good()); 00442 output = reset_stream(ostr); 00443 CPPUNIT_CHECK(output.size() == 26); 00444 CPPUNIT_CHECK(output.substr(0, 6) == "+12345"); 00445 CPPUNIT_CHECK(output.substr(19) == ".000000"); 00446 } 00447 00448 { 00449 ostringstream ostr; 00450 ostr << fixed << showpos << setprecision(100) << 1.23457e+17f; 00451 CPPUNIT_ASSERT(ostr.good()); 00452 output = reset_stream(ostr); 00453 CPPUNIT_CHECK(output.size() == 120); 00454 CPPUNIT_CHECK(output.substr(0, 6) == "+12345"); 00455 CPPUNIT_CHECK(output.substr(19) == ".0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ); 00456 } 00457 00458 { 00459 ostringstream ostr; 00460 ostr << scientific << setprecision(8) << 0.12345678f; 00461 CPPUNIT_ASSERT(ostr.good()); 00462 output = reset_stream(ostr); 00463 digits = "1"; 00464 complete_digits(digits); 00465 CPPUNIT_CHECK(output == string("1.23456780e-") + digits ); 00466 } 00467 00468 { 00469 ostringstream ostr; 00470 ostr << fixed << setprecision(8) << setw(30) << setfill('0') << 0.12345678f; 00471 CPPUNIT_ASSERT(ostr.good()); 00472 output = reset_stream(ostr); 00473 CPPUNIT_CHECK(output == "000000000000000000000.12345678"); 00474 } 00475 00476 { 00477 ostringstream ostr; 00478 ostr << fixed << showpos << setprecision(8) << setw(30) << setfill('0') << 0.12345678f; 00479 CPPUNIT_ASSERT(ostr.good()); 00480 output = reset_stream(ostr); 00481 CPPUNIT_CHECK(output == "0000000000000000000+0.12345678"); 00482 } 00483 00484 { 00485 ostringstream ostr; 00486 ostr << fixed << showpos << setprecision(8) << setw(30) << left << setfill('0') << 0.12345678f; 00487 CPPUNIT_ASSERT(ostr.good()); 00488 output = reset_stream(ostr); 00489 CPPUNIT_CHECK(output == "+0.123456780000000000000000000"); 00490 } 00491 00492 { 00493 ostringstream ostr; 00494 ostr << fixed << showpos << setprecision(8) << setw(30) << internal << setfill('0') << 0.12345678f; 00495 CPPUNIT_ASSERT(ostr.good()); 00496 output = reset_stream(ostr); 00497 CPPUNIT_CHECK(output == "+00000000000000000000.12345678"); 00498 } 00499 00500 { 00501 ostringstream ostr; 00502 ostr << fixed << showpos << setprecision(100) << 1.234567e+17; 00503 CPPUNIT_ASSERT(ostr.good()); 00504 output = reset_stream(ostr); 00505 CPPUNIT_CHECK(output.size() == 120); 00506 CPPUNIT_CHECK(output.substr(0, 6) == "+12345"); 00507 CPPUNIT_CHECK(output.substr(19) == ".0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ); 00508 } 00509 00510 #if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE) 00511 { 00512 ostringstream ostr; 00513 ostr << fixed << showpos << setprecision(100) << 1.234567e+17l; 00514 CPPUNIT_ASSERT(ostr.good()); 00515 output = reset_stream(ostr); 00516 CPPUNIT_CHECK(output.size() == 120); 00517 CPPUNIT_CHECK(output.substr(0, 6) == "+12345"); 00518 CPPUNIT_CHECK(output.substr(19) == ".0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ); 00519 } 00520 #endif 00521 00522 { 00523 ostringstream ostr; 00524 ostr << scientific << setprecision(50) << 0.0; 00525 CPPUNIT_ASSERT(ostr.good()); 00526 output = reset_stream(ostr); 00527 CPPUNIT_CHECK( output == "0.00000000000000000000000000000000000000000000000000e+00" ); 00528 } 00529 { 00530 ostringstream ostr; 00531 ostr << fixed << setprecision(100) << numeric_limits<float>::max(); 00532 CPPUNIT_ASSERT(ostr.good()); 00533 output = reset_stream(ostr); 00534 //CPPUNIT_MESSAGE( output.c_str() ); 00535 } 00536 00537 { 00538 ostringstream ostr; 00539 ostr << setprecision(100) << numeric_limits<double>::max(); 00540 CPPUNIT_ASSERT(ostr.good()); 00541 output = reset_stream(ostr); 00542 //CPPUNIT_MESSAGE( output.c_str() ); 00543 } 00544 00545 #if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE) 00546 { 00547 ostringstream ostr; 00548 ostr << setprecision(100) << numeric_limits<long double>::max(); 00549 CPPUNIT_ASSERT(ostr.good()); 00550 output = reset_stream(ostr); 00551 //CPPUNIT_MESSAGE( output.c_str() ); 00552 } 00553 #endif 00554 00555 //{ 00556 // ostringstream ostr; 00557 // ostr << setprecision(-numeric_limits<float>::min_exponent10 + numeric_limits<float>::digits10 + 9) << numeric_limits<float>::min(); 00558 // CPPUNIT_ASSERT(ostr.good()); 00559 // output = reset_stream(ostr); 00560 // //CPPUNIT_MESSAGE( output.c_str() ); 00561 //} 00562 00563 //{ 00564 // ostringstream ostr; 00565 // ostr << setprecision(-numeric_limits<double>::min_exponent10 + numeric_limits<double>::digits10) << numeric_limits<double>::min(); 00566 // CPPUNIT_ASSERT(ostr.good()); 00567 // output = reset_stream(ostr); 00568 // //CPPUNIT_MESSAGE( output.c_str() ); 00569 //} 00570 00571 //#if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE) 00572 // { 00573 // ostringstream ostr; 00574 // ostr << setprecision(-numeric_limits<long double>::min_exponent10 + numeric_limits<long double>::digits10) << numeric_limits<long double>::min(); 00575 // CPPUNIT_ASSERT(ostr.good()); 00576 // output = reset_stream(ostr); 00577 // CPPUNIT_MESSAGE( output.c_str() ); 00578 // } 00579 //#endif 00580 } 00581 00582 { 00583 ostringstream str; 00584 00585 str.setf(ios::fixed, ios::floatfield); 00586 str << 1.0e+5; 00587 // cerr << str.str() << endl; 00588 CPPUNIT_CHECK( str.str() == "100000.000000" ); 00589 00590 reset_stream(str); 00591 str.precision(0); 00592 str << 1.0e+5; 00593 CPPUNIT_CHECK( str.str() == "100000" ); 00594 00595 reset_stream(str); 00596 str.precision(4); 00597 str << 1.0e+5; 00598 CPPUNIT_CHECK( str.str() == "100000.0000" ); 00599 00600 reset_stream(str); 00601 str.precision(0); 00602 str << 1.0e+83; 00603 CPPUNIT_CHECK( str.str().size() == 84 ); 00604 //printf("\nC result: %.0f\n", 1.0e+83); 00605 //CPPUNIT_MESSAGE( str.str().c_str() ); 00606 //CPPUNIT_CHECK( str.str() == "100000000000000000000000000000000000000000000000000000000000000000000000000000000000" ); 00607 00608 // cerr.setf(ios::fixed, ios::floatfield); 00609 // cerr << DBL_MAX << endl; 00610 // cerr << 1.0e+37 << endl; 00611 } 00612 } 00613 00614 #define CHECK_COMPLETE(type, val, base, showbase, showpos, casing, width, adjust, expected) \ 00615 { \ 00616 type tmp = val; \ 00617 ostringstream ostr; \ 00618 ostr << base << showbase << showpos << casing << setw(width) << adjust << tmp; \ 00619 CPPUNIT_CHECK( ostr.str() == expected ); \ 00620 } 00621 00622 #define CHECK(type, val, base, expected) \ 00623 CHECK_COMPLETE(type, val, base, noshowbase, noshowpos, nouppercase, 0, right, expected) 00624 00625 void NumPutGetTest::num_put_integer() 00626 { 00627 //octal outputs 00628 { 00629 CHECK(short, 0, oct, "0") 00630 CHECK(short, 1, oct, "1") 00631 CHECK(short, 12345, oct, "30071") 00632 if (sizeof(short) == 2) { 00633 CHECK(short, -1, oct, "177777") 00634 CHECK(short, -12345, oct, "147707") 00635 } 00636 00637 CHECK(unsigned short, 0, oct, "0") 00638 CHECK(unsigned short, 12345, oct, "30071") 00639 00640 CHECK(int, 0, oct, "0") 00641 CHECK(int, 12345678, oct, "57060516") 00642 if (sizeof(int) == 4) { 00643 CHECK(int, -1, oct, "37777777777") 00644 CHECK(int, -12345678, oct, "37720717262") 00645 } 00646 00647 CHECK(unsigned int, 0, oct, "0") 00648 CHECK(unsigned int, 12345678, oct, "57060516") 00649 00650 CHECK(long, 0, oct, "0") 00651 CHECK(long, 12345678, oct, "57060516") 00652 if (sizeof(long) == 4) { 00653 CHECK(long, -1, oct, "37777777777") 00654 CHECK(long, -12345678, oct, "37720717262") 00655 } 00656 00657 CHECK(unsigned long, 0, oct, "0") 00658 CHECK(unsigned long, 12345678, oct, "57060516") 00659 00660 #if defined (STLPORT) && defined (_STLP_LONG_LONG) 00661 CHECK(_STLP_LONG_LONG, 0, oct, "0") 00662 CHECK(_STLP_LONG_LONG, 12345678, oct, "57060516") 00663 if (sizeof(_STLP_LONG_LONG) == 8) { 00664 CHECK(_STLP_LONG_LONG, -1, oct, "1777777777777777777777") 00665 CHECK(_STLP_LONG_LONG, -12345678, oct, "1777777777777720717262") 00666 } 00667 00668 CHECK(unsigned _STLP_LONG_LONG, 0, oct, "0") 00669 CHECK(unsigned _STLP_LONG_LONG, 12345678, oct, "57060516") 00670 #endif 00671 00672 //Even with showbase, 0 value gives "0" (see printf documentation) 00673 CHECK_COMPLETE(short, 0, oct, showbase, noshowpos, nouppercase, 0, right, "0") 00674 CHECK_COMPLETE(short, 0, oct, showbase, showpos, nouppercase, 6, right, " 0") 00675 00676 CHECK_COMPLETE(short, 1, oct, showbase, noshowpos, nouppercase, 6, right, " 01") 00677 CHECK_COMPLETE(short, 1, oct, showbase, noshowpos, nouppercase, 6, left, "01 ") 00678 CHECK_COMPLETE(short, 1, oct, showbase, showpos, nouppercase, 6, internal, " 01") 00679 } 00680 00681 //decimal outputs 00682 { 00683 CHECK(short, 0, dec, "0") 00684 CHECK(short, -1, dec, "-1") 00685 CHECK(short, 12345, dec, "12345") 00686 CHECK(short, -12345, dec, "-12345") 00687 00688 CHECK(unsigned short, 0, dec, "0") 00689 CHECK(unsigned short, 12345, dec, "12345") 00690 00691 CHECK(int, 0, dec, "0") 00692 CHECK(int, -1, dec, "-1") 00693 CHECK(int, 12345678, dec, "12345678") 00694 CHECK(int, -12345678, dec, "-12345678") 00695 00696 CHECK(unsigned int, 0, dec, "0") 00697 CHECK(unsigned int, 12345678, dec, "12345678") 00698 00699 CHECK(long, 0, dec, "0") 00700 CHECK(long, -1, dec, "-1") 00701 CHECK(long, 12345678, dec, "12345678") 00702 CHECK(long, -12345678, dec, "-12345678") 00703 00704 CHECK(unsigned long, 0, dec, "0") 00705 CHECK(unsigned long, 12345678, dec, "12345678") 00706 #if defined (STLPORT) && defined (_STLP_LONG_LONG) 00707 CHECK(_STLP_LONG_LONG, 0, dec, "0") 00708 CHECK(_STLP_LONG_LONG, -1, dec, "-1") 00709 CHECK(_STLP_LONG_LONG, 12345678, dec, "12345678") 00710 CHECK(_STLP_LONG_LONG, -12345678, dec, "-12345678") 00711 00712 CHECK(unsigned _STLP_LONG_LONG, 0, dec, "0") 00713 CHECK(unsigned _STLP_LONG_LONG, 12345678, dec, "12345678") 00714 #endif 00715 00716 CHECK_COMPLETE(short, 0, dec, showbase, showpos, nouppercase, 0, right, "+0") 00717 CHECK_COMPLETE(short, 0, dec, showbase, showpos, nouppercase, 6, right, " +0") 00718 CHECK_COMPLETE(short, 1, dec, showbase, showpos, nouppercase, 6, right, " +1") 00719 CHECK_COMPLETE(short, 1, dec, showbase, showpos, nouppercase, 6, left, "+1 ") 00720 CHECK_COMPLETE(short, 1, dec, showbase, showpos, nouppercase, 6, internal, "+ 1") 00721 } 00722 00723 //hexadecimal outputs 00724 { 00725 CHECK(short, 0, hex, "0") 00726 CHECK(short, 12345, hex, "3039") 00727 if (sizeof(short) == 2) { 00728 CHECK(short, -1, hex, "ffff") 00729 CHECK(short, -12345, hex, "cfc7") 00730 } 00731 00732 CHECK(unsigned short, 0, hex, "0") 00733 CHECK(unsigned short, 12345, hex, "3039") 00734 00735 CHECK(int, 0, hex, "0") 00736 CHECK(int, 12345678, hex, "bc614e") 00737 if (sizeof(int) == 4) { 00738 CHECK(int, -1, hex, "ffffffff") 00739 CHECK(int, -12345678, hex, "ff439eb2") 00740 } 00741 00742 CHECK(unsigned int, 0, hex, "0") 00743 CHECK(unsigned int, 12345678, hex, "bc614e") 00744 00745 CHECK(long, 0, hex, "0") 00746 CHECK(long, 12345678, hex, "bc614e") 00747 if (sizeof(long) == 4) { 00748 CHECK(long, -1, hex, "ffffffff") 00749 CHECK(long, -12345678, hex, "ff439eb2") 00750 } 00751 00752 CHECK(unsigned long, 0, hex, "0") 00753 CHECK(unsigned long, 12345678, hex, "bc614e") 00754 #if defined (STLPORT) && defined (_STLP_LONG_LONG) 00755 CHECK(_STLP_LONG_LONG, 0, hex, "0") 00756 CHECK(_STLP_LONG_LONG, 12345678, hex, "bc614e") 00757 if (sizeof(_STLP_LONG_LONG) == 8) { 00758 CHECK(_STLP_LONG_LONG, -1, hex, "ffffffffffffffff") 00759 CHECK(_STLP_LONG_LONG, -12345678, hex, "ffffffffff439eb2") 00760 } 00761 00762 CHECK(unsigned _STLP_LONG_LONG, 0, hex, "0") 00763 CHECK(unsigned _STLP_LONG_LONG, 12345678, hex, "bc614e") 00764 #endif 00765 00766 //Even with showbase, 0 value gives "0" output (see printf documentation) 00767 CHECK_COMPLETE(short, 0, hex, showbase, showpos, nouppercase, 0, right, "0") 00768 CHECK_COMPLETE(short, 0, hex, showbase, noshowpos, nouppercase, 6, right, " 0") 00769 CHECK_COMPLETE(short, 0, hex, showbase, noshowpos, nouppercase, 6, internal, " 0") 00770 00771 CHECK_COMPLETE(short, 1, hex, showbase, noshowpos, nouppercase, 6, right, " 0x1") 00772 CHECK_COMPLETE(short, 1, hex, showbase, noshowpos, nouppercase, 6, left, "0x1 ") 00773 CHECK_COMPLETE(short, 1, hex, showbase, noshowpos, nouppercase, 6, internal, "0x 1") 00774 CHECK_COMPLETE(short, 1, hex, showbase, noshowpos, uppercase, 6, left, "0X1 ") 00775 CHECK_COMPLETE(short, 1, hex, showbase, showpos, uppercase, 6, internal, "0X 1") 00776 } 00777 } 00778 00779 void NumPutGetTest::num_get_float() 00780 { 00781 float in_val; 00782 00783 istringstream istr; 00784 00785 istr.str("1.2345"); 00786 istr >> in_val; 00787 CPPUNIT_ASSERT(!istr.fail()); 00788 CPPUNIT_ASSERT(istr.eof()); 00789 CPPUNIT_ASSERT(check_float(in_val, 1.2345f)); 00790 istr.clear(); 00791 00792 istr.str("-1.2345"); 00793 istr >> in_val; 00794 CPPUNIT_ASSERT(!istr.fail()); 00795 CPPUNIT_ASSERT(istr.eof()); 00796 CPPUNIT_ASSERT(check_float(in_val, -1.2345f)); 00797 istr.clear(); 00798 00799 istr.str("+1.2345"); 00800 istr >> in_val; 00801 CPPUNIT_ASSERT(!istr.fail()); 00802 CPPUNIT_ASSERT(check_float(in_val, 1.2345f)); 00803 istr.clear(); 00804 00805 istr.str("000000000000001.234500000000"); 00806 istr >> in_val; 00807 CPPUNIT_ASSERT(!istr.fail()); 00808 CPPUNIT_ASSERT(istr.eof()); 00809 CPPUNIT_ASSERT(check_float(in_val, 1.2345f)); 00810 istr.clear(); 00811 00812 istr.str("1.2345e+04"); 00813 istr >> in_val; 00814 CPPUNIT_ASSERT(!istr.fail()); 00815 CPPUNIT_ASSERT(istr.eof()); 00816 CPPUNIT_ASSERT(check_float(in_val, 12345.0f)); 00817 istr.clear(); 00818 00819 CPPUNIT_MESSAGE( "float" ); 00820 check_get_float( 0.0F ); 00821 CPPUNIT_MESSAGE( "double" ); 00822 check_get_float( 0.0 ); 00823 #if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE) 00824 CPPUNIT_MESSAGE( "long double" ); 00825 check_get_float( 0.0L ); 00826 #endif 00827 { 00828 stringstream str; 00829 00830 str << "1e" << numeric_limits<double>::max_exponent10; 00831 CPPUNIT_ASSERT(!str.fail()); 00832 00833 float val; 00834 str >> val; 00835 CPPUNIT_ASSERT(!str.fail()); 00836 CPPUNIT_ASSERT(str.eof()); 00837 CPPUNIT_ASSERT( numeric_limits<double>::max_exponent10 <= numeric_limits<float>::max_exponent10 || 00838 val == numeric_limits<float>::infinity() ); 00839 } 00840 { 00841 stringstream str; 00842 00843 str << "1e" << numeric_limits<double>::min_exponent10; 00844 CPPUNIT_ASSERT(!str.fail()); 00845 00846 float val; 00847 str >> val; 00848 CPPUNIT_ASSERT(!str.fail()); 00849 CPPUNIT_ASSERT(str.eof()); 00850 CPPUNIT_ASSERT( numeric_limits<double>::min_exponent10 >= numeric_limits<float>::min_exponent10 || 00851 val == 0.0f ); 00852 } 00853 #if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE) 00854 { 00855 stringstream str; 00856 00857 str << "1e" << numeric_limits<long double>::max_exponent10; 00858 CPPUNIT_ASSERT(!str.fail()); 00859 00860 double val; 00861 str >> val; 00862 CPPUNIT_ASSERT(!str.fail()); 00863 CPPUNIT_ASSERT(str.eof()); 00864 CPPUNIT_ASSERT( numeric_limits<long double>::max_exponent10 <= numeric_limits<double>::max_exponent10 || 00865 val == numeric_limits<double>::infinity() ); 00866 } 00867 { 00868 stringstream str; 00869 00870 str << "1e" << numeric_limits<long double>::min_exponent10; 00871 CPPUNIT_ASSERT(!str.fail()); 00872 00873 double val; 00874 str >> val; 00875 CPPUNIT_ASSERT(!str.fail()); 00876 CPPUNIT_ASSERT(str.eof()); 00877 CPPUNIT_ASSERT( numeric_limits<long double>::min_exponent10 >= numeric_limits<double>::min_exponent10 || 00878 val == 0.0 ); 00879 } 00880 { 00881 const char* p = "2.718281828459045235360287471352662497757247093e0"; 00882 std::stringstream s; 00883 s << p; 00884 long double x; 00885 s >> x; 00886 CPPUNIT_ASSERT( x > 2.70l && x < 2.72l ); 00887 } 00888 #endif 00889 } 00890 00891 void NumPutGetTest::num_get_integer() 00892 { 00893 //octal input 00894 { 00895 istringstream istr; 00896 istr.str("30071"); 00897 short val; 00898 istr >> oct >> val; 00899 CPPUNIT_ASSERT( !istr.fail() ); 00900 CPPUNIT_ASSERT( istr.eof() ); 00901 CPPUNIT_ASSERT( val == 12345 ); 00902 istr.clear(); 00903 00904 if (sizeof(short) == 2) { 00905 istr.str("177777"); 00906 istr >> oct >> val; 00907 CPPUNIT_ASSERT( !istr.fail() ); 00908 CPPUNIT_ASSERT( istr.eof() ); 00909 CPPUNIT_ASSERT( val == -1 ); 00910 istr.clear(); 00911 } 00912 } 00913 00914 //decimal input 00915 { 00916 istringstream istr; 00917 istr.str("10000"); 00918 short val = -1; 00919 istr >> val; 00920 CPPUNIT_ASSERT( !istr.fail() ); 00921 CPPUNIT_ASSERT( istr.eof() ); 00922 CPPUNIT_ASSERT( val == 10000 ); 00923 istr.clear(); 00924 00925 istr.str("+10000"); 00926 val = -1; 00927 istr >> val; 00928 CPPUNIT_ASSERT( !istr.fail() ); 00929 CPPUNIT_ASSERT( istr.eof() ); 00930 CPPUNIT_ASSERT( val == 10000 ); 00931 istr.clear(); 00932 00933 if (sizeof(short) == 2) { 00934 val = -1; 00935 istr.str("10000000"); 00936 istr >> val; 00937 CPPUNIT_ASSERT( istr.fail() ); 00938 CPPUNIT_ASSERT( istr.eof() ); 00939 CPPUNIT_ASSERT( val == -1 ); 00940 istr.clear(); 00941 } 00942 00943 val = -1; 00944 istr.str("0x0"); 00945 istr >> val; 00946 CPPUNIT_ASSERT( !istr.fail() ); 00947 CPPUNIT_ASSERT( !istr.eof() ); 00948 CPPUNIT_ASSERT( val == 0 ); 00949 istr.clear(); 00950 00951 val = -1; 00952 istr.str("000001"); 00953 istr >> val; 00954 CPPUNIT_ASSERT( !istr.fail() ); 00955 CPPUNIT_ASSERT( istr.eof() ); 00956 CPPUNIT_ASSERT( val == 1 ); 00957 istr.clear(); 00958 } 00959 00960 //hexadecimal input 00961 { 00962 istringstream istr; 00963 istr.str("3039"); 00964 short val = -1; 00965 istr >> hex >> val; 00966 CPPUNIT_ASSERT( !istr.fail() ); 00967 CPPUNIT_ASSERT( istr.eof() ); 00968 CPPUNIT_ASSERT( val == 12345 ); 00969 istr.clear(); 00970 00971 istr.str("x3039"); 00972 val = -1; 00973 istr >> hex >> val; 00974 CPPUNIT_ASSERT( istr.fail() ); 00975 CPPUNIT_ASSERT( !istr.eof() ); 00976 CPPUNIT_ASSERT( val == -1 ); 00977 istr.clear(); 00978 00979 istr.str("03039"); 00980 val = -1; 00981 istr >> hex >> val; 00982 CPPUNIT_ASSERT( !istr.fail() ); 00983 CPPUNIT_ASSERT( istr.eof() ); 00984 CPPUNIT_ASSERT( val == 12345 ); 00985 istr.clear(); 00986 00987 istr.str("0x3039"); 00988 istr >> hex >> val; 00989 CPPUNIT_ASSERT( !istr.fail() ); 00990 CPPUNIT_ASSERT( istr.eof() ); 00991 CPPUNIT_ASSERT( val == 12345 ); 00992 istr.clear(); 00993 00994 if (sizeof(short) == 2) { 00995 val = -1; 00996 istr.str("cfc7"); 00997 istr >> hex >> val; 00998 CPPUNIT_ASSERT( !istr.fail() ); 00999 CPPUNIT_ASSERT( istr.eof() ); 01000 CPPUNIT_ASSERT( val == -12345 ); 01001 istr.clear(); 01002 } 01003 } 01004 } 01005 01006 void NumPutGetTest::inhex() 01007 { 01008 { 01009 ostringstream s; 01010 s << hex << 0; 01011 CPPUNIT_CHECK( s.str() == "0" ); 01012 } 01013 { 01014 ostringstream s; 01015 s << hex << 0xff; 01016 CPPUNIT_CHECK( s.str() == "ff" ); 01017 } 01018 { 01019 ostringstream s; 01020 s << hex << setw( 4 ) << 0xff; 01021 CPPUNIT_CHECK( s.str() == " ff" ); 01022 } 01023 { 01024 ostringstream s; 01025 s << hex << setw( 4 ) << 0; 01026 CPPUNIT_CHECK( s.str() == " 0" ); 01027 } 01028 { 01029 ostringstream s; 01030 s << hex << showbase << 0; 01031 CPPUNIT_CHECK( s.str() == "0" ); 01032 } 01033 { 01034 ostringstream s; 01035 s << hex << showbase << 0xff; 01036 CPPUNIT_CHECK( s.str() == "0xff" ); 01037 } 01038 { 01039 ostringstream s; 01040 s << hex << showbase << setw( 4 ) << 0xff; 01041 CPPUNIT_CHECK( s.str() == "0xff" ); 01042 } 01043 { // special case for regression (partially duplicate CHECK_COMPLETE above): 01044 ostringstream s; 01045 s.setf( ios_base::internal, ios_base::adjustfield ); 01046 s << hex << showbase << setw(8+2) << 0; 01047 CPPUNIT_CHECK( s.str() == " 0" ); 01048 } 01049 } 01050 01051 void NumPutGetTest::pointer() 01052 { 01053 // Problem with printing pointer to null 01054 01055 /* 01056 * Really C's formatting not help here, due to: 01057 * 01058 * p The argument shall be a pointer to void. The value of 01059 * the pointer is converted to a sequence of printable characters, 01060 * in an implementation-defined manner. 01061 */ 01062 { 01063 /* 01064 char buf[128]; 01065 void *p = (void *)0xff00; 01066 sprintf( buf, "%p", p ); 01067 // cerr << buf << endl; 01068 // Hmmm, I see 0xff00 on box with 32-bits address; pointer like 'unsigned hex'? 01069 if ( sizeof( p ) == 2 ) { 01070 CPPUNIT_ASSERT( strcmp( buf, "0xff00" ) == 0 ); 01071 } else if ( sizeof( p ) == 4 ) { 01072 CPPUNIT_ASSERT( strcmp( buf, "0x0000ff00" ) == 0 ); 01073 } else if ( sizeof( p ) == 8 ) { 01074 CPPUNIT_ASSERT( strcmp( buf, "0x000000000000ff00" ) == 0 ); 01075 } else { 01076 CPPUNIT_CHECK( sizeof( p ) == 2 || sizeof( p ) == 4 || sizeof( p ) == 8 ); 01077 } 01078 */ 01079 } 01080 { 01081 /* 01082 char buf[128]; 01083 void *p = 0; 01084 */ 01085 // sprintf( buf, "%p", p ); 01086 /* Cool. "%p" print '(nil)'; "%#x" print '0' */ 01087 // sprintf( buf, "%#x", (unsigned)p ); 01088 // cerr << buf << endl; 01089 } 01090 { 01091 ostringstream s; 01092 void *p = (void *)0xff00; 01093 s << p; 01094 CPPUNIT_ASSERT( s.good() ); 01095 if ( sizeof( p ) == 2 ) { 01096 CPPUNIT_ASSERT( s.str() == "0xff00" ); 01097 } else if ( sizeof( p ) == 4 ) { 01098 CPPUNIT_ASSERT( s.str() == "0x0000ff00" ); // this pass 01099 } else if ( sizeof( p ) == 8 ) { 01100 CPPUNIT_ASSERT( s.str() == "0x000000000000ff00" ); 01101 } else { 01102 CPPUNIT_CHECK( sizeof( p ) == 2 || sizeof( p ) == 4 || sizeof( p ) == 8 ); 01103 } 01104 } 01105 { 01106 ostringstream s; 01107 void *p = 0; 01108 s << p; 01109 CPPUNIT_ASSERT( s.good() ); 01110 if ( sizeof( p ) == 2 ) { 01111 CPPUNIT_ASSERT( s.str() == "0x0000" ); 01112 } else if ( sizeof( p ) == 4 ) { 01113 CPPUNIT_ASSERT( s.str() == "0x00000000" ); // but this will fail, if follow %p 01114 } else if ( sizeof( p ) == 8 ) { 01115 CPPUNIT_ASSERT( s.str() == "0x0000000000000000" ); 01116 } else { 01117 CPPUNIT_CHECK( sizeof( p ) == 2 || sizeof( p ) == 4 || sizeof( p ) == 8 ); 01118 } 01119 } 01120 } 01121 01122 void NumPutGetTest::fix_float_long() 01123 { 01124 ostringstream str; 01125 01126 str.setf(ios::fixed, ios::floatfield); 01127 str << 1.0e+5; 01128 CPPUNIT_CHECK( str.str() == "100000.000000" ); 01129 01130 reset_stream(str); 01131 str.precision(0); 01132 str << 1.0e+5; 01133 CPPUNIT_CHECK( str.str() == "100000" ); 01134 01135 reset_stream(str); 01136 str.precision(4); 01137 str << 1.0e+5; 01138 CPPUNIT_CHECK( str.str() == "100000.0000" ); 01139 01140 reset_stream(str); 01141 str.precision(0); 01142 str << 1.0e+83; 01143 { 01144 istringstream istr( str.str() ); 01145 double f; 01146 istr >> f; 01147 CPPUNIT_CHECK( !istr.fail() ); 01148 if ( int(numeric_limits<double>::digits10) < 83 ) { 01149 double delta = 1.0; 01150 for ( int ee = 83 - int(numeric_limits<double>::digits10); ee > 0; --ee ) { 01151 delta *= 10.0; 01152 } 01153 // we may loss some digits here, but not more than mantissa: 01154 CPPUNIT_CHECK( (f > (1.0e+83 - delta)) && (f < (1.0e+83 + delta)) ); 01155 } else { 01156 CPPUNIT_CHECK( check_double(f, 1.0e+83) ); 01157 } 01158 } 01159 01160 #if 0 // #ifndef _STLP_NO_LONG_DOUBLE 01161 reset_stream(str); 01162 str.precision(0); 01163 str << 1.0e+83l; 01164 { 01165 istringstream istr( str.str() ); 01166 long double f; 01167 istr >> f; 01168 CPPUNIT_CHECK( !istr.fail() ); 01169 if ( int(numeric_limits<long double>::digits10) < 83 ) { 01170 long double delta = 1.0l; 01171 for ( int ee = 83 - int(numeric_limits<long double>::digits10); ee > 0; --ee ) { 01172 delta *= 10.0l; 01173 } 01174 // we may loss some digits here, but not more than mantissa: 01175 cerr << "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" << endl; 01176 cerr << str.str() << endl; 01177 cerr << delta << endl; 01178 cerr << f << endl; 01179 CPPUNIT_CHECK( (f > (1.0e+83l - delta)) && (f < (1.0e+83l + delta)) ); 01180 } else { 01181 CPPUNIT_CHECK( check_double(f, 1.0e+83l) ); 01182 } 01183 } 01184 #endif 01185 01186 reset_stream(str); 01187 str.precision(0); 01188 str << numeric_limits<double>::max(); 01189 { 01190 istringstream istr( str.str() ); 01191 double f; 01192 istr >> f; 01193 CPPUNIT_CHECK( !istr.fail() ); 01194 if ( int(numeric_limits<double>::digits10) < int(numeric_limits<double>::max_exponent10) ) { 01195 double delta = 9.0; 01196 for ( int ee = int(numeric_limits<double>::max_exponent10) - int(numeric_limits<double>::digits10); ee > 0; --ee ) { 01197 delta *= 10.0; 01198 } 01199 // we may loss some digits here, but not more than mantissa: 01200 CPPUNIT_CHECK( (f > (numeric_limits<double>::max() - delta)) ); 01201 } 01202 } 01203 01204 #if 0 // #ifndef _STLP_NO_LONG_DOUBLE 01205 reset_stream(str); 01206 str.precision(0); 01207 str << numeric_limits<long double>::max(); 01208 { 01209 istringstream istr( str.str() ); 01210 long double f; 01211 istr >> f; 01212 CPPUNIT_CHECK( !istr.fail() ); 01213 if ( int(numeric_limits<long double>::digits10) < int(numeric_limits<long double>::max_exponent10) ) { 01214 long double delta = 1.0l; 01215 for ( int ee = int(numeric_limits<long double>::max_exponent10) - int(numeric_limits<long double>::digits10); ee > 0; --ee ) { 01216 delta *= 10.0l; 01217 } 01218 // we may loss some digits here, but not more than mantissa: 01219 CPPUNIT_CHECK( (f > (numeric_limits<long double>::max() - delta)) ); 01220 } 01221 } 01222 #endif 01223 } 01224 01225 class CommaSepNumPunct : public numpunct<char> { 01226 char do_thousands_sep() const { return ','; } 01227 string do_grouping() const { return string("\1\2\3") + (char)CHAR_MAX; } 01228 }; 01229 01230 #define CHECK2(val, expected) \ 01231 os.str(""); os << fixed << setprecision(3) << showpos << val; \ 01232 CPPUNIT_ASSERT( os.str() == expected ) 01233 01234 void NumPutGetTest::custom_numpunct() 01235 { 01236 ostringstream os; 01237 locale loc(os.getloc(), new CommaSepNumPunct()); 01238 os.imbue(loc); 01239 01240 CHECK2(1, "+1"); 01241 CHECK2(10, "+1,0"); 01242 CHECK2(100, "+10,0"); 01243 CHECK2(1000, "+1,00,0"); 01244 01245 CHECK2(1.234, "+1.234"); 01246 CHECK2(123.456, "+12,3.456"); 01247 CHECK2(1234.567, "+1,23,4.567"); 01248 CHECK2(12345.678, "+12,34,5.678"); 01249 CHECK2(123456.789, "+123,45,6.789"); 01250 CHECK2(1234567.891, "+1,234,56,7.891"); 01251 CHECK2(123456789.123, "+123,456,78,9.123"); 01252 //CHECK2(100000000000000000000000000000.0, "+100000000000000000000000,000,00,0.000"); 01253 CHECK2(numeric_limits<double>::infinity(), "+inf"); 01254 01255 CHECK2(-1.234, "-1.234"); 01256 CHECK2(-123.456, "-12,3.456"); 01257 CHECK2(-1234.567, "-1,23,4.567"); 01258 CHECK2(-12345.678, "-12,34,5.678"); 01259 CHECK2(-123456.789, "-123,45,6.789"); 01260 CHECK2(-1234567.891, "-1,234,56,7.891"); 01261 CHECK2(-123456789.123, "-123,456,78,9.123"); 01262 //CHECK2(-100000000000000000000000000000.0, "-100000000000000000000000,000,00,0.000"); 01263 CHECK2(-numeric_limits<double>::infinity(), "-inf"); 01264 } 01265 01266 #endif Generated on Sun May 27 2012 04:35:39 for ReactOS by
1.7.6.1
|