Update contrib.
2 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
10 #include "cppunit/cppunit_proxy.h"
12 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
19 class IStreamIteratorTest : public CPPUNIT_NS::TestCase
21 CPPUNIT_TEST_SUITE(IStreamIteratorTest);
22 #if defined (STLPORT) && defined (_STLP_USE_NO_IOSTREAMS)
25 CPPUNIT_TEST(istmit1);
26 #if !defined (STLPORT) || defined (_STLP_NO_EXTENSIONS)
29 CPPUNIT_TEST(copy_n_test);
30 CPPUNIT_TEST_SUITE_END();
37 CPPUNIT_TEST_SUITE_REGISTRATION(IStreamIteratorTest);
39 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
40 # if !defined (STLPORT) || !defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
41 typedef istream_iterator<char> istream_char_ite;
42 typedef istream_iterator<int> istream_int_ite;
43 typedef istream_iterator<string> istream_string_ite;
45 typedef istream_iterator<char, ptrdiff_t> istream_char_ite;
46 typedef istream_iterator<int, ptrdiff_t> istream_int_ite;
47 typedef istream_iterator<string, ptrdiff_t> istream_string_ite;
52 // tests implementation
54 void IStreamIteratorTest::istmit1()
56 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
57 const char* buff = "MyString";
58 istringstream istr(buff);
62 istr.unsetf(ios::skipws); // Disable white-space skipping.
63 istream_char_ite s(istr), meos;
64 while (!(s == meos) &&
65 //*TY 01/10/1999 - added end of stream check
66 // NOTE operator!= should not be used here ifndef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
68 (i < sizeof(buffer) / sizeof(buffer[0]))) { //*TY 07/28/98 - added index check
71 buffer[i] = '\0'; // Null terminate buffer.
73 CPPUNIT_ASSERT(!strcmp(buffer, buff));
76 istringstream empty_istr;
77 CPPUNIT_ASSERT( istream_char_ite(empty_istr) == istream_char_ite() );
82 void IStreamIteratorTest::copy_n_test()
84 #if defined (STLPORT) && !defined (_STLP_NO_EXTENSIONS) && !defined (_STLP_USE_NO_IOSTREAMS)
85 //This test check that no character is lost while reading the istream
86 //through a istream_iterator.
88 istringstream istr("aabbcd");
90 istream_char_ite ite = copy_n(copy_n(istream_char_ite(istr),
91 2, back_inserter(chars)).first,
92 2, back_inserter(chars)).first;
93 CPPUNIT_ASSERT( chars == "aabb" );
94 copy_n(ite, 2, back_inserter(chars));
95 CPPUNIT_ASSERT( chars == "aabbcd" );
99 istringstream istr("11 22 AA BB 33 44 CC DD");
101 vector<string> strings;
103 copy_n(istream_int_ite(istr), 2, back_inserter(ints));
104 CPPUNIT_ASSERT( ints.size() == 2 );
105 CPPUNIT_ASSERT( ints[0] == 11 );
106 CPPUNIT_ASSERT( ints[1] == 22 );
109 copy_n(istream_string_ite(istr), 2, back_inserter(strings));
110 CPPUNIT_ASSERT( strings.size() == 2 );
111 CPPUNIT_ASSERT( strings[0] == "AA" );
112 CPPUNIT_ASSERT( strings[1] == "BB" );
115 /* The following code cannot work, '33' is extracted as a string
116 * in the previous copy_n call, this value is returned in the pair
117 * returned by copy_n but is lost as this istream_iterator is not used.
118 * copy_n and istream_iterator can only be combined safely if:
119 * - you always extract the same type of istream_iterator and you always reuse
120 * the istream_iterator returned by copy_n (see previous test with "aabbcd")
121 * - you extract different type of object and no object is convertible to an other
122 * as in this current test when you extract int and string (when you extract ints
123 * again it fails as int can be converted to strings.
125 copy_n(istream_int_ite(istr), 2, back_inserter(ints));
126 CPPUNIT_ASSERT( ints.size() == 2 );
127 CPPUNIT_ASSERT( ints[0] == 33 );
128 CPPUNIT_ASSERT( ints[1] == 44 );
130 copy_n(istream_string_ite(istr), 2, back_inserter(strings));
131 CPPUNIT_ASSERT( strings.size() == 2 );
132 CPPUNIT_ASSERT( strings[0] == "CC" );
133 CPPUNIT_ASSERT( strings[1] == "DD" );
138 istringstream is("1 2 3 4 5 6 7 8 9 10");
140 istream_iterator<int> itr(is);
141 itr = copy_n(itr, 0, back_inserter(ints)).first;
142 CPPUNIT_ASSERT( ints.empty() );
143 itr = copy_n(itr, -1, back_inserter(ints)).first;
144 CPPUNIT_ASSERT( ints.empty() );
145 itr = copy_n(itr, 2, back_inserter(ints)).first;
146 CPPUNIT_ASSERT( ints.size() == 2 );
147 CPPUNIT_ASSERT( ints[0] == 1 );
148 CPPUNIT_ASSERT( ints[1] == 2 );
149 itr = copy_n(itr, 2, back_inserter(ints)).first;
150 CPPUNIT_ASSERT( ints.size() == 4 );
151 CPPUNIT_ASSERT( ints[2] == 3 );
152 CPPUNIT_ASSERT( ints[3] == 4 );
153 itr = copy_n(itr, 2, back_inserter(ints)).first;
154 CPPUNIT_ASSERT( ints.size() == 6 );
155 CPPUNIT_ASSERT( ints[4] == 5 );
156 CPPUNIT_ASSERT( ints[5] == 6 );