1 #include <string> 2 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS) 3 #include <sstream> 4 #include <vector> 5 #include <iterator> 6 7 #include "cppunit/cppunit_proxy.h" 8 9 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES) 10 using namespace std; 11 #endif 12 13 class IoiterTest : public CPPUNIT_NS::TestCase 14 { 15 CPPUNIT_TEST_SUITE(IoiterTest); 16 CPPUNIT_TEST(ioiter_test); 17 CPPUNIT_TEST(assign_test); 18 CPPUNIT_TEST(assign2_test); 19 CPPUNIT_TEST_SUITE_END(); 20 21 protected: 22 void ioiter_test(); 23 void assign_test(); 24 void assign2_test(); 25 }; 26 27 CPPUNIT_TEST_SUITE_REGISTRATION(IoiterTest); 28 29 void IoiterTest::ioiter_test() 30 { 31 32 char c; 33 const char *pc; 34 const char *strorg = "abcd"; 35 string tmp; 36 37 string objStr(strorg); 38 39 istringstream objIStrStrm1(objStr); 40 istringstream objIStrStrm2(objStr); 41 istringstream objIStrStrm3(objStr); 42 43 pc = strorg; 44 string::size_type sz = strlen(strorg); 45 string::size_type i; 46 for ( i = 0; i < sz; ++i ) { 47 c = *pc++; 48 tmp += c; 49 } 50 CPPUNIT_ASSERT( tmp == "abcd" ); 51 52 istreambuf_iterator<char, char_traits<char> > objIStrmbIt1( objIStrStrm1.rdbuf() ); 53 istreambuf_iterator<char, char_traits<char> > end; 54 55 tmp.clear(); 56 57 for ( i = 0; i < sz /* objIStrmbIt1 != end */; ++i ) { 58 c = *objIStrmbIt1++; 59 tmp += c; 60 } 61 CPPUNIT_ASSERT( tmp == "abcd" ); 62 63 tmp.clear(); 64 65 istreambuf_iterator<char, char_traits<char> > objIStrmbIt2( objIStrStrm2.rdbuf() ); 66 for ( i = 0; i < sz; ++i ) { 67 c = *objIStrmbIt2; 68 tmp += c; 69 objIStrmbIt2++; 70 } 71 CPPUNIT_ASSERT( tmp == "abcd" ); 72 73 tmp.clear(); 74 75 istreambuf_iterator<char, char_traits<char> > objIStrmbIt3( objIStrStrm3.rdbuf() ); 76 77 while ( objIStrmbIt3 != end ) { 78 c = *objIStrmbIt3++; 79 tmp += c; 80 } 81 CPPUNIT_ASSERT( tmp == "abcd" ); 82 } 83 84 void IoiterTest::assign_test() 85 { 86 stringstream s( "1234567890" ); 87 vector<char> v; 88 89 v.assign( istreambuf_iterator<char>(s), istreambuf_iterator<char>() ); 90 CPPUNIT_CHECK( v.size() == 10 ); 91 if ( v.size() == 10 ) { 92 CPPUNIT_CHECK( v[0] == '1' ); 93 CPPUNIT_CHECK( v[9] == '0' ); 94 } 95 } 96 97 void IoiterTest::assign2_test() 98 { 99 stringstream s( "1234567890" ); 100 vector<char> v; 101 102 v.assign( istreambuf_iterator<char>(s.rdbuf()), istreambuf_iterator<char>() ); 103 CPPUNIT_CHECK( v.size() == 10 ); 104 if ( v.size() == 10 ) { 105 CPPUNIT_CHECK( v[0] == '1' ); 106 CPPUNIT_CHECK( v[9] == '0' ); 107 } 108 } 109 110 #endif 111