sl@0: #include sl@0: #include "math_aux.h" sl@0: sl@0: #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS) sl@0: # include sl@0: # include sl@0: sl@0: # include "full_streambuf.h" sl@0: sl@0: # include "cppunit/cppunit_proxy.h" sl@0: sl@0: # if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) sl@0: using namespace std; sl@0: # endif sl@0: sl@0: // sl@0: // TestCase class sl@0: // sl@0: class SstreamTest : public CPPUNIT_NS::TestCase sl@0: { sl@0: CPPUNIT_TEST_SUITE(SstreamTest); sl@0: CPPUNIT_TEST(output); sl@0: CPPUNIT_TEST(input); sl@0: CPPUNIT_TEST(input_char); sl@0: CPPUNIT_TEST(io); sl@0: CPPUNIT_TEST(err); sl@0: CPPUNIT_TEST(err_long); sl@0: CPPUNIT_TEST(maxint); sl@0: CPPUNIT_TEST(init_in); sl@0: CPPUNIT_TEST(init_out); sl@0: CPPUNIT_TEST(buf); sl@0: CPPUNIT_TEST(rdbuf); sl@0: CPPUNIT_TEST(streambuf_output); sl@0: CPPUNIT_TEST(seek); sl@0: CPPUNIT_TEST(negative); sl@0: CPPUNIT_TEST_SUITE_END(); sl@0: sl@0: protected: sl@0: void output(); sl@0: void input(); sl@0: void input_char(); sl@0: void io(); sl@0: void err(); sl@0: void err_long(); sl@0: void maxint(); sl@0: void init_in(); sl@0: void init_out(); sl@0: void buf(); sl@0: void rdbuf(); sl@0: void streambuf_output(); sl@0: void seek(); sl@0: void negative(); sl@0: }; sl@0: sl@0: CPPUNIT_TEST_SUITE_REGISTRATION(SstreamTest); sl@0: sl@0: // sl@0: // tests implementation sl@0: // sl@0: void SstreamTest::output() sl@0: { sl@0: { sl@0: ostringstream s; sl@0: sl@0: s << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef"; sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: CPPUNIT_ASSERT( s.str() == "1\n2\nabcd\nghk lm\nabcd ef" ); sl@0: } sl@0: sl@0: //Following tests are mostly used to reveal problem with the MSVC /Wp64 option sl@0: //used to track 64 bits portability issue: sl@0: { sl@0: ostringstream s; sl@0: size_t i = 0; sl@0: s << i; sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: CPPUNIT_ASSERT( s.str() == "0" ); sl@0: } sl@0: { sl@0: ostringstream s; sl@0: ptrdiff_t i = 0; sl@0: s << i; sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: CPPUNIT_ASSERT( s.str() == "0" ); sl@0: } sl@0: } sl@0: sl@0: void SstreamTest::input() sl@0: { sl@0: { sl@0: istringstream s( "1\n2\nabcd\nghk lm\nabcd ef" ); sl@0: int i = 0; sl@0: s >> i; sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: CPPUNIT_ASSERT( i == 1 ); sl@0: double d = 0.0; sl@0: s >> d; sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: CPPUNIT_ASSERT( d == 2.0 ); sl@0: string str; sl@0: s >> str; sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: CPPUNIT_ASSERT( str == "abcd" ); sl@0: char c; sl@0: s.get(c); // extract newline, that not extracted by operator >> sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: CPPUNIT_ASSERT( c == '\n' ); sl@0: getline( s, str ); sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: CPPUNIT_ASSERT( str == "ghk lm" ); sl@0: getline( s, str ); sl@0: CPPUNIT_ASSERT( s.eof() ); sl@0: CPPUNIT_ASSERT( str == "abcd ef" ); sl@0: } sl@0: { sl@0: istringstream s("0"); sl@0: size_t i = 1; sl@0: s >> i; sl@0: CPPUNIT_ASSERT( !s.fail() ); sl@0: CPPUNIT_ASSERT( s.eof() ); sl@0: CPPUNIT_ASSERT( i == 0 ); sl@0: } sl@0: } sl@0: sl@0: void SstreamTest::input_char() sl@0: { sl@0: char buf[16] = { 0, '1', '2', '3' }; sl@0: istringstream s( "0" ); sl@0: s >> buf; sl@0: sl@0: CPPUNIT_ASSERT( buf[0] == '0' ); sl@0: CPPUNIT_ASSERT( buf[1] == 0 ); sl@0: CPPUNIT_ASSERT( buf[2] == '2' ); sl@0: } sl@0: sl@0: sl@0: void SstreamTest::io() sl@0: { sl@0: stringstream s; sl@0: s << 1 << '\n' << 2.0 << '\n' << "abcd\n" << "ghk lm\n" << "abcd ef"; sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: sl@0: int i = 0; sl@0: s >> i; sl@0: CPPUNIT_ASSERT( i == 1 ); sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: double d = 0.0; sl@0: s >> d; sl@0: CPPUNIT_ASSERT( d == 2.0 ); sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: string str; sl@0: s >> str; sl@0: CPPUNIT_ASSERT( str == "abcd" ); sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: char c; sl@0: s.get(c); // extract newline, that not extracted by operator >> sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: CPPUNIT_ASSERT( c == '\n' ); sl@0: getline( s, str ); sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: CPPUNIT_ASSERT( str == "ghk lm" ); sl@0: getline( s, str ); sl@0: CPPUNIT_ASSERT( str == "abcd ef" ); sl@0: CPPUNIT_ASSERT( s.eof() ); sl@0: } sl@0: sl@0: void SstreamTest::err() sl@0: { sl@0: stringstream s( "9" ); sl@0: sl@0: int i = 0; sl@0: s >> i; sl@0: CPPUNIT_ASSERT( !s.fail() ); sl@0: CPPUNIT_ASSERT( i == 9 ); sl@0: s >> i; sl@0: CPPUNIT_ASSERT( s.fail() ); sl@0: CPPUNIT_ASSERT( s.eof() ); sl@0: CPPUNIT_ASSERT( i == 9 ); sl@0: } sl@0: sl@0: void SstreamTest::err_long() sl@0: { sl@0: stringstream s( "9" ); sl@0: sl@0: long i = 0; sl@0: s >> i; sl@0: CPPUNIT_ASSERT( !s.fail() ); sl@0: CPPUNIT_ASSERT( i == 9 ); sl@0: s >> i; sl@0: CPPUNIT_ASSERT( s.fail() ); sl@0: CPPUNIT_ASSERT( s.eof() ); sl@0: CPPUNIT_ASSERT( i == 9 ); sl@0: } sl@0: sl@0: void SstreamTest::maxint() sl@0: { sl@0: stringstream s; sl@0: sl@0: s << INT_MAX << " " << UINT_MAX << " " << LONG_MAX << " " << ULONG_MAX << " " sl@0: << INT_MIN << " " << LONG_MIN; sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: sl@0: int i = 0; sl@0: unsigned int u = 0; sl@0: long l = 0; sl@0: unsigned long ul = 0; sl@0: sl@0: s >> i >> u >> l >> ul; sl@0: CPPUNIT_ASSERT( s.good() ); sl@0: CPPUNIT_ASSERT( i == INT_MAX ); sl@0: CPPUNIT_ASSERT( u == UINT_MAX ); sl@0: CPPUNIT_ASSERT( l == LONG_MAX ); sl@0: CPPUNIT_ASSERT( ul == ULONG_MAX ); sl@0: sl@0: s >> i >> l; sl@0: CPPUNIT_ASSERT( !s.fail() ); sl@0: CPPUNIT_ASSERT( i == INT_MIN ); sl@0: CPPUNIT_ASSERT( l == LONG_MIN ); sl@0: } sl@0: sl@0: void SstreamTest::init_in() sl@0: { sl@0: istringstream is( "12345" ); sl@0: int n; sl@0: sl@0: is >> n; sl@0: CPPUNIT_ASSERT( !is.fail() ); sl@0: CPPUNIT_ASSERT( n == 12345 ); sl@0: sl@0: istringstream dis( "1.2345" ); sl@0: double d; sl@0: sl@0: dis >> d; sl@0: CPPUNIT_ASSERT( !dis.fail() ); sl@0: CPPUNIT_ASSERT( are_equals(d, 1.2345) ); sl@0: sl@0: istringstream fis( "1.2345" ); sl@0: float f; sl@0: sl@0: fis >> f; sl@0: CPPUNIT_ASSERT( !fis.fail() ); sl@0: CPPUNIT_ASSERT( are_equals(f, 1.2345f) ); sl@0: } sl@0: sl@0: void SstreamTest::init_out() sl@0: { sl@0: ostringstream os( "12345" ); sl@0: CPPUNIT_ASSERT( os.str() == "12345" ); sl@0: sl@0: os << 67; sl@0: CPPUNIT_ASSERT( os.good() ); sl@0: sl@0: // This satisfy to the Standard: sl@0: CPPUNIT_ASSERT( os.str() == "67345" ); sl@0: // But we don't know the reason, why standard state that. sl@0: sl@0: os.str( "89ab" ); sl@0: CPPUNIT_ASSERT( os.str() == "89ab" ); sl@0: sl@0: os << 10; sl@0: CPPUNIT_ASSERT( os.good() ); sl@0: CPPUNIT_ASSERT( os.str() == "10ab" ); sl@0: } sl@0: sl@0: void SstreamTest::buf() sl@0: { sl@0: stringstream ss; sl@0: sl@0: ss << "1234567\n89\n"; sl@0: char buf[10]; sl@0: buf[7] = 'x'; sl@0: ss.get( buf, 10 ); sl@0: CPPUNIT_ASSERT( !ss.fail() ); sl@0: CPPUNIT_ASSERT( buf[0] == '1' ); sl@0: CPPUNIT_ASSERT( buf[1] == '2' ); sl@0: CPPUNIT_ASSERT( buf[2] == '3' ); sl@0: CPPUNIT_ASSERT( buf[3] == '4' ); sl@0: CPPUNIT_ASSERT( buf[4] == '5' ); sl@0: CPPUNIT_ASSERT( buf[5] == '6' ); sl@0: CPPUNIT_ASSERT( buf[6] == '7' ); // 27.6.1.3 paragraph 10, paragraph 7 sl@0: CPPUNIT_ASSERT( buf[7] == 0 ); // 27.6.1.3 paragraph 8 sl@0: char c; sl@0: ss.get(c); sl@0: CPPUNIT_ASSERT( !ss.fail() ); sl@0: CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 10, paragraph 7 sl@0: ss.get(c); sl@0: CPPUNIT_ASSERT( !ss.fail() ); sl@0: CPPUNIT_ASSERT( c == '8' ); sl@0: } sl@0: sl@0: void SstreamTest::rdbuf() sl@0: { sl@0: stringstream ss; sl@0: sl@0: ss << "1234567\n89\n"; sl@0: sl@0: ostringstream os; sl@0: ss.get( *os.rdbuf(), '\n' ); sl@0: CPPUNIT_ASSERT( !ss.fail() ); sl@0: char c; sl@0: ss.get(c); sl@0: CPPUNIT_ASSERT( !ss.fail() ); sl@0: CPPUNIT_ASSERT( c == '\n' ); // 27.6.1.3 paragraph 12 sl@0: CPPUNIT_ASSERT( os.str() == "1234567" ); sl@0: } sl@0: sl@0: void SstreamTest::streambuf_output() sl@0: { sl@0: { sl@0: istringstream in("01234567890123456789"); sl@0: CPPUNIT_ASSERT( in ); sl@0: sl@0: auto_ptr pfull_buf(new full_streambuf(10)); sl@0: ostream out(pfull_buf.get()); sl@0: CPPUNIT_ASSERT( out ); sl@0: sl@0: out << in.rdbuf(); sl@0: CPPUNIT_ASSERT( out ); sl@0: CPPUNIT_ASSERT( in ); sl@0: //out is good we can check what has been extracted: sl@0: CPPUNIT_ASSERT( pfull_buf->str() == "0123456789" ); sl@0: sl@0: out << in.rdbuf(); sl@0: CPPUNIT_ASSERT( out.fail() ); sl@0: CPPUNIT_ASSERT( in ); sl@0: sl@0: ostringstream ostr; sl@0: ostr << in.rdbuf(); sl@0: CPPUNIT_ASSERT( ostr ); sl@0: CPPUNIT_ASSERT( in ); sl@0: CPPUNIT_ASSERT( ostr.str() == "0123456789" ); sl@0: } sl@0: sl@0: # if !defined (STLPORT) || defined (_STLP_USE_EXCEPTIONS) sl@0: { sl@0: //If the output stream buffer throws: sl@0: istringstream in("01234567890123456789"); sl@0: CPPUNIT_ASSERT( in ); sl@0: sl@0: auto_ptr pfull_buf(new full_streambuf(10, true)); sl@0: ostream out(pfull_buf.get()); sl@0: CPPUNIT_ASSERT( out ); sl@0: sl@0: out << in.rdbuf(); sl@0: CPPUNIT_ASSERT( out.bad() ); sl@0: CPPUNIT_ASSERT( in ); sl@0: //out is bad we have no guaranty on what has been extracted: sl@0: //CPPUNIT_ASSERT( pfull_buf->str() == "0123456789" ); sl@0: sl@0: out.clear(); sl@0: out << in.rdbuf(); sl@0: CPPUNIT_ASSERT( out.fail() && out.bad() ); sl@0: CPPUNIT_ASSERT( in ); sl@0: sl@0: ostringstream ostr; sl@0: ostr << in.rdbuf(); sl@0: CPPUNIT_ASSERT( ostr ); sl@0: CPPUNIT_ASSERT( in ); sl@0: CPPUNIT_ASSERT( ostr.str() == "01234567890123456789" ); sl@0: } sl@0: # endif sl@0: } sl@0: sl@0: void SstreamTest::seek() sl@0: { sl@0: stringstream s( "0123456789" ); sl@0: sl@0: CPPUNIT_ASSERT( s.tellg() == stringstream::pos_type(0) ); sl@0: s.seekg( 6, ios::beg ); sl@0: CPPUNIT_ASSERT( s.tellg() == stringstream::pos_type(6) ); sl@0: s.seekg( -3, ios::cur ); sl@0: CPPUNIT_ASSERT( s.tellg() == stringstream::pos_type(3) ); sl@0: sl@0: istringstream is( "0123456789" ); sl@0: CPPUNIT_ASSERT( is.tellg() == stringstream::pos_type(0) ); sl@0: is.seekg( 6, ios::beg ); sl@0: CPPUNIT_ASSERT( is.tellg() == stringstream::pos_type(6) ); sl@0: is.seekg( -3, ios::cur ); sl@0: CPPUNIT_ASSERT( is.tellg() == stringstream::pos_type(3) ); sl@0: } sl@0: sl@0: sl@0: template < class T > sl@0: string to_string( const T& v ) sl@0: { sl@0: ostringstream oss; sl@0: oss << v; sl@0: return oss.str(); sl@0: } sl@0: sl@0: void SstreamTest::negative() sl@0: { sl@0: CPPUNIT_CHECK( to_string(-1) == "-1" ); sl@0: CPPUNIT_CHECK( to_string(-1) == "-1" ); sl@0: } sl@0: sl@0: #endif