📄 num_put_get_test.cpp
字号:
output = reset_stream(ostr); CPPUNIT_CHECK( output.size() < 200 ); } { ostringstream ostr; ostr << fixed << 1.23457e+17f; CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); CPPUNIT_CHECK(output.size() == 25); CPPUNIT_CHECK(output.substr(0, 5) == "12345"); CPPUNIT_CHECK(output.substr(18) == ".000000"); } { ostringstream ostr; ostr << fixed << showpos << 1.23457e+17f; CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); CPPUNIT_CHECK(output.size() == 26); CPPUNIT_CHECK(output.substr(0, 6) == "+12345"); CPPUNIT_CHECK(output.substr(19) == ".000000"); } { ostringstream ostr; ostr << fixed << showpos << setprecision(100) << 1.23457e+17f; CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); CPPUNIT_CHECK(output.size() == 120); CPPUNIT_CHECK(output.substr(0, 6) == "+12345"); CPPUNIT_CHECK(output.substr(19) == ".0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ); } { ostringstream ostr; ostr << scientific << setprecision(8) << 0.12345678f; CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); digits = "1"; complete_digits(digits); CPPUNIT_CHECK(output == string("1.23456780e-") + digits ); } { ostringstream ostr; ostr << fixed << setprecision(8) << setw(30) << setfill('0') << 0.12345678f; CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); CPPUNIT_CHECK(output == "000000000000000000000.12345678"); } { ostringstream ostr; ostr << fixed << showpos << setprecision(8) << setw(30) << setfill('0') << 0.12345678f; CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); CPPUNIT_CHECK(output == "0000000000000000000+0.12345678"); } { ostringstream ostr; ostr << fixed << showpos << setprecision(8) << setw(30) << left << setfill('0') << 0.12345678f; CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); CPPUNIT_CHECK(output == "+0.123456780000000000000000000"); } { ostringstream ostr; ostr << fixed << showpos << setprecision(8) << setw(30) << internal << setfill('0') << 0.12345678f; CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); CPPUNIT_CHECK(output == "+00000000000000000000.12345678"); } { ostringstream ostr; ostr << fixed << showpos << setprecision(100) << 1.234567e+17; CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); CPPUNIT_CHECK(output.size() == 120); CPPUNIT_CHECK(output.substr(0, 6) == "+12345"); CPPUNIT_CHECK(output.substr(19) == ".0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ); }#if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE) { ostringstream ostr; ostr << fixed << showpos << setprecision(100) << 1.234567e+17l; CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); CPPUNIT_CHECK(output.size() == 120); CPPUNIT_CHECK(output.substr(0, 6) == "+12345"); CPPUNIT_CHECK(output.substr(19) == ".0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ); }#endif { ostringstream ostr; ostr << scientific << setprecision(50) << 0.0; CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); CPPUNIT_CHECK( output == "0.00000000000000000000000000000000000000000000000000e+00" ); } { ostringstream ostr; ostr << fixed << setprecision(100) << numeric_limits<float>::max(); CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); //CPPUNIT_MESSAGE( output.c_str() ); } { ostringstream ostr; ostr << setprecision(100) << numeric_limits<double>::max(); CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); //CPPUNIT_MESSAGE( output.c_str() ); }#if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE) { ostringstream ostr; ostr << setprecision(100) << numeric_limits<long double>::max(); CPPUNIT_ASSERT(ostr.good()); output = reset_stream(ostr); //CPPUNIT_MESSAGE( output.c_str() ); }#endif //{ // ostringstream ostr; // ostr << setprecision(-numeric_limits<float>::min_exponent10 + numeric_limits<float>::digits10 + 9) << numeric_limits<float>::min(); // CPPUNIT_ASSERT(ostr.good()); // output = reset_stream(ostr); // //CPPUNIT_MESSAGE( output.c_str() ); //} //{ // ostringstream ostr; // ostr << setprecision(-numeric_limits<double>::min_exponent10 + numeric_limits<double>::digits10) << numeric_limits<double>::min(); // CPPUNIT_ASSERT(ostr.good()); // output = reset_stream(ostr); // //CPPUNIT_MESSAGE( output.c_str() ); //}//#if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE)// {// ostringstream ostr;// ostr << setprecision(-numeric_limits<long double>::min_exponent10 + numeric_limits<long double>::digits10) << numeric_limits<long double>::min();// CPPUNIT_ASSERT(ostr.good());// output = reset_stream(ostr);// CPPUNIT_MESSAGE( output.c_str() );// }//#endif } { ostringstream str; str.setf(ios::fixed, ios::floatfield); str << 1.0e+5; // cerr << str.str() << endl; CPPUNIT_CHECK( str.str() == "100000.000000" ); reset_stream(str); str.precision(0); str << 1.0e+5; CPPUNIT_CHECK( str.str() == "100000" ); reset_stream(str); str.precision(4); str << 1.0e+5; CPPUNIT_CHECK( str.str() == "100000.0000" ); reset_stream(str); str.precision(0); str << 1.0e+83; CPPUNIT_CHECK( str.str().size() == 84 ); //printf("\nC result: %.0f\n", 1.0e+83); //CPPUNIT_MESSAGE( str.str().c_str() ); //CPPUNIT_CHECK( str.str() == "100000000000000000000000000000000000000000000000000000000000000000000000000000000000" ); // cerr.setf(ios::fixed, ios::floatfield); // cerr << DBL_MAX << endl; // cerr << 1.0e+37 << endl; }}#define CHECK_COMPLETE(type, val, base, showbase, showpos, casing, width, adjust, expected) \{ \ type tmp = val; \ ostringstream ostr; \ ostr << base << showbase << showpos << casing << setw(width) << adjust << tmp; \ CPPUNIT_CHECK( ostr.str() == expected ); \}#define CHECK(type, val, base, expected) \ CHECK_COMPLETE(type, val, base, noshowbase, noshowpos, nouppercase, 0, right, expected)void NumPutGetTest::num_put_integer(){ //octal outputs { CHECK(short, 0, oct, "0") CHECK(short, 1, oct, "1") CHECK(short, 12345, oct, "30071") if (sizeof(short) == 2) { CHECK(short, -1, oct, "177777") CHECK(short, -12345, oct, "147707") } CHECK(unsigned short, 0, oct, "0") CHECK(unsigned short, 12345, oct, "30071") CHECK(int, 0, oct, "0") CHECK(int, 12345678, oct, "57060516") if (sizeof(int) == 4) { CHECK(int, -1, oct, "37777777777") CHECK(int, -12345678, oct, "37720717262") } CHECK(unsigned int, 0, oct, "0") CHECK(unsigned int, 12345678, oct, "57060516") CHECK(long, 0, oct, "0") CHECK(long, 12345678, oct, "57060516") if (sizeof(long) == 4) { CHECK(long, -1, oct, "37777777777") CHECK(long, -12345678, oct, "37720717262") } CHECK(unsigned long, 0, oct, "0") CHECK(unsigned long, 12345678, oct, "57060516")#if defined (STLPORT) && defined (_STLP_LONG_LONG) CHECK(_STLP_LONG_LONG, 0, oct, "0") CHECK(_STLP_LONG_LONG, 12345678, oct, "57060516") if (sizeof(_STLP_LONG_LONG) == 8) { CHECK(_STLP_LONG_LONG, -1, oct, "1777777777777777777777") CHECK(_STLP_LONG_LONG, -12345678, oct, "1777777777777720717262") } CHECK(unsigned _STLP_LONG_LONG, 0, oct, "0") CHECK(unsigned _STLP_LONG_LONG, 12345678, oct, "57060516")#endif //Even with showbase, 0 value gives "0" (see printf documentation) CHECK_COMPLETE(short, 0, oct, showbase, noshowpos, nouppercase, 0, right, "0") CHECK_COMPLETE(short, 0, oct, showbase, showpos, nouppercase, 6, right, " 0") CHECK_COMPLETE(short, 1, oct, showbase, noshowpos, nouppercase, 6, right, " 01") CHECK_COMPLETE(short, 1, oct, showbase, noshowpos, nouppercase, 6, left, "01 ") CHECK_COMPLETE(short, 1, oct, showbase, showpos, nouppercase, 6, internal, " 01") } //decimal outputs { CHECK(short, 0, dec, "0") CHECK(short, -1, dec, "-1") CHECK(short, 12345, dec, "12345") CHECK(short, -12345, dec, "-12345") CHECK(unsigned short, 0, dec, "0") CHECK(unsigned short, 12345, dec, "12345") CHECK(int, 0, dec, "0") CHECK(int, -1, dec, "-1") CHECK(int, 12345678, dec, "12345678") CHECK(int, -12345678, dec, "-12345678") CHECK(unsigned int, 0, dec, "0") CHECK(unsigned int, 12345678, dec, "12345678") CHECK(long, 0, dec, "0") CHECK(long, -1, dec, "-1") CHECK(long, 12345678, dec, "12345678") CHECK(long, -12345678, dec, "-12345678") CHECK(unsigned long, 0, dec, "0") CHECK(unsigned long, 12345678, dec, "12345678")#if defined (STLPORT) && defined (_STLP_LONG_LONG) CHECK(_STLP_LONG_LONG, 0, dec, "0") CHECK(_STLP_LONG_LONG, -1, dec, "-1") CHECK(_STLP_LONG_LONG, 12345678, dec, "12345678") CHECK(_STLP_LONG_LONG, -12345678, dec, "-12345678") CHECK(unsigned _STLP_LONG_LONG, 0, dec, "0") CHECK(unsigned _STLP_LONG_LONG, 12345678, dec, "12345678")#endif CHECK_COMPLETE(short, 0, dec, showbase, showpos, nouppercase, 0, right, "+0") CHECK_COMPLETE(short, 0, dec, showbase, showpos, nouppercase, 6, right, " +0") CHECK_COMPLETE(short, 1, dec, showbase, showpos, nouppercase, 6, right, " +1") CHECK_COMPLETE(short, 1, dec, showbase, showpos, nouppercase, 6, left, "+1 ") CHECK_COMPLETE(short, 1, dec, showbase, showpos, nouppercase, 6, internal, "+ 1") } //hexadecimal outputs { CHECK(short, 0, hex, "0") CHECK(short, 12345, hex, "3039") if (sizeof(short) == 2) { CHECK(short, -1, hex, "ffff") CHECK(short, -12345, hex, "cfc7") } CHECK(unsigned short, 0, hex, "0") CHECK(unsigned short, 12345, hex, "3039") CHECK(int, 0, hex, "0") CHECK(int, 12345678, hex, "bc614e") if (sizeof(int) == 4) { CHECK(int, -1, hex, "ffffffff") CHECK(int, -12345678, hex, "ff439eb2") } CHECK(unsigned int, 0, hex, "0") CHECK(unsigned int, 12345678, hex, "bc614e") CHECK(long, 0, hex, "0") CHECK(long, 12345678, hex, "bc614e") if (sizeof(long) == 4) { CHECK(long, -1, hex, "ffffffff") CHECK(long, -12345678, hex, "ff439eb2") } CHECK(unsigned long, 0, hex, "0") CHECK(unsigned long, 12345678, hex, "bc614e")#if defined (STLPORT) && defined (_STLP_LONG_LONG) CHECK(_STLP_LONG_LONG, 0, hex, "0") CHECK(_STLP_LONG_LONG, 12345678, hex, "bc614e") if (sizeof(_STLP_LONG_LONG) == 8) { CHECK(_STLP_LONG_LONG, -1, hex, "ffffffffffffffff") CHECK(_STLP_LONG_LONG, -12345678, hex, "ffffffffff439eb2") } CHECK(unsigned _STLP_LONG_LONG, 0, hex, "0") CHECK(unsigned _STLP_LONG_LONG, 12345678, hex, "bc614e")#endif //Even with showbase, 0 value gives "0" output (see printf documentation) CHECK_COMPLETE(short, 0, hex, showbase, showpos, nouppercase, 0, right, "0") CHECK_COMPLETE(short, 0, hex, showbase, noshowpos, nouppercase, 6, right, " 0") CHECK_COMPLETE(short, 0, hex, showbase, noshowpos, nouppercase, 6, internal, " 0") CHECK_COMPLETE(short, 1, hex, showbase, noshowpos, nouppercase, 6, right, " 0x1") CHECK_COMPLETE(short, 1, hex, showbase, noshowpos, nouppercase, 6, left, "0x1 ") CHECK_COMPLETE(short, 1, hex, showbase, noshowpos, nouppercase, 6, internal, "0x 1") CHECK_COMPLETE(short, 1, hex, showbase, noshowpos, uppercase, 6, left, "0X1 ") CHECK_COMPLETE(short, 1, hex, showbase, showpos, uppercase, 6, internal, "0X 1") }}void NumPutGetTest::num_get_float(){ float in_val; istringstream istr; istr.str("1.2345"); istr >> in_val; CPPUNIT_ASSERT(!istr.fail()); CPPUNIT_ASSERT(istr.eof()); CPPUNIT_ASSERT(check_float(in_val, 1.2345f)); istr.clear(); istr.str("-1.2345"); istr >> in_val; CPPUNIT_ASSERT(!istr.fail()); CPPUNIT_ASSERT(istr.eof()); CPPUNIT_ASSERT(check_float(in_val, -1.2345f)); istr.clear(); istr.str("+1.2345"); istr >> in_val; CPPUNIT_ASSERT(!istr.fail()); CPPUNIT_ASSERT(check_float(in_val, 1.2345f)); istr.clear(); istr.str("000000000000001.234500000000"); istr >> in_val; CPPUNIT_ASSERT(!istr.fail()); CPPUNIT_ASSERT(istr.eof()); CPPUNIT_ASSERT(check_float(in_val, 1.2345f)); istr.clear(); istr.str("1.2345e+04"); istr >> in_val; CPPUNIT_ASSERT(!istr.fail()); CPPUNIT_ASSERT(istr.eof()); CPPUNIT_ASSERT(check_float(in_val, 12345.0f)); istr.clear(); CPPUNIT_MESSAGE( "float" ); check_get_float( 0.0F ); CPPUNIT_MESSAGE( "double" ); check_get_float( 0.0 );#if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE) CPPUNIT_MESSAGE( "long double" ); check_get_float( 0.0L );#endif { stringstream str; str << "1e" << numeric_limits<double>::max_exponent10; CPPUNIT_ASSERT(!str.fail()); float val; str >> val; CPPUNIT_ASSERT(!str.fail()); CPPUNIT_ASSERT(str.eof()); CPPUNIT_ASSERT( numeric_limits<double>::max_exponent10 <= numeric_limits<float>::max_exponent10 || val == numeric_limits<float>::infinity() ); } { stringstream str; str << "1e" << numeric_limits<double>::min_exponent10; CPPUNIT_ASSERT(!str.fail()); float val;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -