⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 num_put_get_test.cpp

📁 stl的源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    str >> val;    CPPUNIT_ASSERT(!str.fail());    CPPUNIT_ASSERT(str.eof());    CPPUNIT_ASSERT( numeric_limits<double>::min_exponent10 >= numeric_limits<float>::min_exponent10 ||                    val == 0.0f );  }#if !defined (STLPORT) || !defined (_STLP_NO_LONG_DOUBLE)  {    stringstream str;    str << "1e" << numeric_limits<long double>::max_exponent10;    CPPUNIT_ASSERT(!str.fail());    double val;    str >> val;    CPPUNIT_ASSERT(!str.fail());    CPPUNIT_ASSERT(str.eof());    CPPUNIT_ASSERT( numeric_limits<long double>::max_exponent10 <= numeric_limits<double>::max_exponent10 ||                    val == numeric_limits<double>::infinity() );  }  {    stringstream str;    str << "1e" << numeric_limits<long double>::min_exponent10;    CPPUNIT_ASSERT(!str.fail());    double val;    str >> val;    CPPUNIT_ASSERT(!str.fail());    CPPUNIT_ASSERT(str.eof());    CPPUNIT_ASSERT( numeric_limits<long double>::min_exponent10 >= numeric_limits<double>::min_exponent10 ||                    val == 0.0 );  }  {    const char* p = "2.718281828459045235360287471352662497757247093e0";    std::stringstream s;    s << p;    long double x;    s >> x;    CPPUNIT_ASSERT( x > 2.70l && x < 2.72l );  }#endif}void NumPutGetTest::num_get_integer(){  //octal input  {    istringstream istr;    istr.str("30071");    short val;    istr >> oct >> val;    CPPUNIT_ASSERT( !istr.fail() );    CPPUNIT_ASSERT( istr.eof() );    CPPUNIT_ASSERT( val == 12345 );    istr.clear();    if (sizeof(short) == 2) {      istr.str("177777");      istr >> oct >> val;      CPPUNIT_ASSERT( !istr.fail() );      CPPUNIT_ASSERT( istr.eof() );      CPPUNIT_ASSERT( val == -1 );      istr.clear();    }  }  //decimal input  {    istringstream istr;    istr.str("10000");    short val = -1;    istr >> val;    CPPUNIT_ASSERT( !istr.fail() );    CPPUNIT_ASSERT( istr.eof() );    CPPUNIT_ASSERT( val == 10000 );    istr.clear();    istr.str("+10000");    val = -1;    istr >> val;    CPPUNIT_ASSERT( !istr.fail() );    CPPUNIT_ASSERT( istr.eof() );    CPPUNIT_ASSERT( val == 10000 );    istr.clear();    if (sizeof(short) == 2) {      val = -1;      istr.str("10000000");      istr >> val;      CPPUNIT_ASSERT( istr.fail() );      CPPUNIT_ASSERT( istr.eof() );      CPPUNIT_ASSERT( val == -1 );      istr.clear();    }    val = -1;    istr.str("0x0");    istr >> val;    CPPUNIT_ASSERT( !istr.fail() );    CPPUNIT_ASSERT( !istr.eof() );    CPPUNIT_ASSERT( val == 0 );    istr.clear();    val = -1;    istr.str("000001");    istr >> val;    CPPUNIT_ASSERT( !istr.fail() );    CPPUNIT_ASSERT( istr.eof() );    CPPUNIT_ASSERT( val == 1 );    istr.clear();  }  //hexadecimal input  {    istringstream istr;    istr.str("3039");    short val = -1;    istr >> hex >> val;    CPPUNIT_ASSERT( !istr.fail() );    CPPUNIT_ASSERT( istr.eof() );    CPPUNIT_ASSERT( val == 12345 );    istr.clear();    istr.str("x3039");    val = -1;    istr >> hex >> val;    CPPUNIT_ASSERT( istr.fail() );    CPPUNIT_ASSERT( !istr.eof() );    CPPUNIT_ASSERT( val == -1 );    istr.clear();    istr.str("03039");    val = -1;    istr >> hex >> val;    CPPUNIT_ASSERT( !istr.fail() );    CPPUNIT_ASSERT( istr.eof() );    CPPUNIT_ASSERT( val == 12345 );    istr.clear();    istr.str("0x3039");    istr >> hex >> val;    CPPUNIT_ASSERT( !istr.fail() );    CPPUNIT_ASSERT( istr.eof() );    CPPUNIT_ASSERT( val == 12345 );    istr.clear();    if (sizeof(short) == 2) {      val = -1;      istr.str("cfc7");      istr >> hex >> val;      CPPUNIT_ASSERT( !istr.fail() );      CPPUNIT_ASSERT( istr.eof() );      CPPUNIT_ASSERT( val == -12345 );      istr.clear();    }  }}void NumPutGetTest::inhex(){  {    ostringstream s;    s << hex << 0;    CPPUNIT_CHECK( s.str() == "0" );  }  {    ostringstream s;    s << hex << 0xff;    CPPUNIT_CHECK( s.str() == "ff" );  }  {    ostringstream s;    s << hex << setw( 4 ) << 0xff;    CPPUNIT_CHECK( s.str() == "  ff" );  }  {    ostringstream s;    s << hex << setw( 4 ) << 0;    CPPUNIT_CHECK( s.str() == "   0" );  }  {    ostringstream s;    s << hex << showbase << 0;    CPPUNIT_CHECK( s.str() == "0" );  }  {    ostringstream s;    s << hex << showbase << 0xff;    CPPUNIT_CHECK( s.str() == "0xff" );  }  {    ostringstream s;    s << hex << showbase << setw( 4 ) << 0xff;    CPPUNIT_CHECK( s.str() == "0xff" );  }  { // special case for regression (partially duplicate CHECK_COMPLETE above):    ostringstream s;    s.setf( ios_base::internal, ios_base::adjustfield );    s << hex << showbase << setw(8+2) << 0;    CPPUNIT_CHECK( s.str() == "         0" );  }}void NumPutGetTest::pointer(){  // Problem with printing pointer to null  /*   * Really C's formatting not help here, due to:   *   * p  The argument shall be a pointer to void. The value of   *    the pointer is converted to a sequence of printable characters,   *    in an implementation-defined manner.   */  {    /*    char buf[128];    void *p = (void *)0xff00;    sprintf( buf, "%p", p );    // cerr << buf << endl;    // Hmmm, I see 0xff00 on box with 32-bits address; pointer like 'unsigned hex'?     if ( sizeof( p ) == 2 ) {      CPPUNIT_ASSERT( strcmp( buf, "0xff00" ) == 0 );    } else if ( sizeof( p ) == 4 ) {      CPPUNIT_ASSERT( strcmp( buf, "0x0000ff00" ) == 0 );    } else if ( sizeof( p ) == 8 ) {      CPPUNIT_ASSERT( strcmp( buf, "0x000000000000ff00" ) == 0 );    } else {      CPPUNIT_CHECK( sizeof( p ) == 2 || sizeof( p ) == 4 || sizeof( p ) == 8 );    }    */  }  {    /*    char buf[128];    void *p = 0;    */    // sprintf( buf, "%p", p );    /* Cool. "%p" print '(nil)'; "%#x" print '0' */    // sprintf( buf, "%#x", (unsigned)p );    // cerr << buf << endl;  }  {    ostringstream s;    void *p = (void *)0xff00;    s << p;    CPPUNIT_ASSERT( s.good() );    if ( sizeof( p ) == 2 ) {      CPPUNIT_ASSERT( s.str() == "0xff00" );    } else if ( sizeof( p ) == 4 ) {      CPPUNIT_ASSERT( s.str() == "0x0000ff00" ); // this pass    } else if ( sizeof( p ) == 8 ) {      CPPUNIT_ASSERT( s.str() == "0x000000000000ff00" );    } else {      CPPUNIT_CHECK( sizeof( p ) == 2 || sizeof( p ) == 4 || sizeof( p ) == 8 );    }  }  {    ostringstream s;    void *p = 0;    s << p;    CPPUNIT_ASSERT( s.good() );    if ( sizeof( p ) == 2 ) {      CPPUNIT_ASSERT( s.str() == "0x0000" );    } else if ( sizeof( p ) == 4 ) {      CPPUNIT_ASSERT( s.str() == "0x00000000" ); // but this will fail, if follow %p    } else if ( sizeof( p ) == 8 ) {      CPPUNIT_ASSERT( s.str() == "0x0000000000000000" );    } else {      CPPUNIT_CHECK( sizeof( p ) == 2 || sizeof( p ) == 4 || sizeof( p ) == 8 );    }  }}void NumPutGetTest::fix_float_long(){  ostringstream str;  str.setf(ios::fixed, ios::floatfield);  str << 1.0e+5;  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;  {    istringstream istr( str.str() );    double f;    istr >> f;    CPPUNIT_CHECK( !istr.fail() );    if ( int(numeric_limits<double>::digits10) < 83 ) {      double delta = 1.0;      for ( int ee = 83 - int(numeric_limits<double>::digits10); ee > 0; --ee ) {        delta *= 10.0;      }      // we may loss some digits here, but not more than mantissa:      CPPUNIT_CHECK( (f > (1.0e+83 - delta)) && (f < (1.0e+83 + delta)) );    } else {      CPPUNIT_CHECK( check_double(f, 1.0e+83) );    }  }#if 0 // #ifndef _STLP_NO_LONG_DOUBLE  reset_stream(str);  str.precision(0);  str << 1.0e+83l;  {    istringstream istr( str.str() );    long double f;    istr >> f;    CPPUNIT_CHECK( !istr.fail() );    if ( int(numeric_limits<long double>::digits10) < 83 ) {      long double delta = 1.0l;      for ( int ee = 83 - int(numeric_limits<long double>::digits10); ee > 0; --ee ) {        delta *= 10.0l;      }      // we may loss some digits here, but not more than mantissa:      cerr << "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789" << endl;      cerr << str.str() << endl;      cerr << delta << endl;      cerr << f << endl;      CPPUNIT_CHECK( (f > (1.0e+83l - delta)) && (f < (1.0e+83l + delta)) );    } else {      CPPUNIT_CHECK( check_double(f, 1.0e+83l) );    }  }#endif  reset_stream(str);  str.precision(0);  str << numeric_limits<double>::max();  {    istringstream istr( str.str() );    double f;    istr >> f;    CPPUNIT_CHECK( !istr.fail() );    if ( int(numeric_limits<double>::digits10) < int(numeric_limits<double>::max_exponent10) ) {      double delta = 9.0;      for ( int ee = int(numeric_limits<double>::max_exponent10) - int(numeric_limits<double>::digits10); ee > 0; --ee ) {        delta *= 10.0;      }      // we may loss some digits here, but not more than mantissa:      CPPUNIT_CHECK( (f > (numeric_limits<double>::max() - delta)) );    }  }#if 0 // #ifndef _STLP_NO_LONG_DOUBLE  reset_stream(str);  str.precision(0);  str << numeric_limits<long double>::max();  {    istringstream istr( str.str() );    long double f;    istr >> f;    CPPUNIT_CHECK( !istr.fail() );    if ( int(numeric_limits<long double>::digits10) < int(numeric_limits<long double>::max_exponent10) ) {      long double delta = 1.0l;      for ( int ee = int(numeric_limits<long double>::max_exponent10) - int(numeric_limits<long double>::digits10); ee > 0; --ee ) {        delta *= 10.0l;      }      // we may loss some digits here, but not more than mantissa:      CPPUNIT_CHECK( (f > (numeric_limits<long double>::max() - delta)) );    }  }#endif}class CommaSepNumPunct : public numpunct<char> {  char do_thousands_sep() const { return ','; }  string do_grouping() const { return string("\1\2\3") + (char)CHAR_MAX; }};#define CHECK2(val, expected) \  os.str(""); os << fixed << setprecision(3) << showpos << val; \  CPPUNIT_ASSERT( os.str() == expected )void NumPutGetTest::custom_numpunct(){    ostringstream os;    locale loc(os.getloc(), new CommaSepNumPunct());    os.imbue(loc);    CHECK2(1, "+1");    CHECK2(10, "+1,0");    CHECK2(100, "+10,0");    CHECK2(1000, "+1,00,0");    CHECK2(1.234, "+1.234");    CHECK2(123.456, "+12,3.456");    CHECK2(1234.567, "+1,23,4.567");    CHECK2(12345.678, "+12,34,5.678");    CHECK2(123456.789, "+123,45,6.789");    CHECK2(1234567.891, "+1,234,56,7.891");    CHECK2(123456789.123, "+123,456,78,9.123");    //CHECK2(100000000000000000000000000000.0, "+100000000000000000000000,000,00,0.000");    CHECK2(numeric_limits<double>::infinity(), "+inf");    CHECK2(-1.234, "-1.234");    CHECK2(-123.456, "-12,3.456");    CHECK2(-1234.567, "-1,23,4.567");    CHECK2(-12345.678, "-12,34,5.678");    CHECK2(-123456.789, "-123,45,6.789");    CHECK2(-1234567.891, "-1,234,56,7.891");    CHECK2(-123456789.123, "-123,456,78,9.123");    //CHECK2(-100000000000000000000000000000.0, "-100000000000000000000000,000,00,0.000");    CHECK2(-numeric_limits<double>::infinity(), "-inf");}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -