1 #include <algorithm>
2 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
3 # include <sstream>
4 # include <functional>
5 # include <iterator>
6 # include <vector>
7 # include <string>
8 #endif
9
10 #include "cppunit/cppunit_proxy.h"
11
12 #if !defined (STLPORT) || defined(_STLP_USE_NAMESPACES)
13 using namespace std;
14 #endif
15
16 //
17 // TestCase class
18 //
19 class IStreamIteratorTest : public CPPUNIT_NS::TestCase
20 {
21 CPPUNIT_TEST_SUITE(IStreamIteratorTest);
22 #if defined (STLPORT) && defined (_STLP_USE_NO_IOSTREAMS)
23 CPPUNIT_IGNORE;
24 #endif
25 CPPUNIT_TEST(istmit1);
26 #if !defined (STLPORT) || defined (_STLP_NO_EXTENSIONS)
27 CPPUNIT_IGNORE;
28 #endif
29 CPPUNIT_TEST(copy_n_test);
30 CPPUNIT_TEST_SUITE_END();
31
32 protected:
33 void istmit1();
34 void copy_n_test();
35 };
36
37 CPPUNIT_TEST_SUITE_REGISTRATION(IStreamIteratorTest);
38
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;
44 # else
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;
48 # endif
49 #endif
50
51 //
52 // tests implementation
53 //
istmit1()54 void IStreamIteratorTest::istmit1()
55 {
56 #if !defined (STLPORT) || !defined (_STLP_USE_NO_IOSTREAMS)
57 const char* buff = "MyString";
58 istringstream istr(buff);
59
60 char buffer[100];
61 size_t i = 0;
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
67 (*s != '\n') &&
68 (i < sizeof(buffer) / sizeof(buffer[0]))) { //*TY 07/28/98 - added index check
69 buffer[i++] = *s++;
70 }
71 buffer[i] = '\0'; // Null terminate buffer.
72
73 CPPUNIT_ASSERT(!strcmp(buffer, buff));
74
75 {
76 istringstream empty_istr;
77 CPPUNIT_ASSERT( istream_char_ite(empty_istr) == istream_char_ite() );
78 }
79 #endif
80 }
81
copy_n_test()82 void IStreamIteratorTest::copy_n_test()
83 {
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.
87 {
88 istringstream istr("aabbcd");
89 string chars;
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" );
96 }
97
98 {
99 istringstream istr("11 22 AA BB 33 44 CC DD");
100 vector<int> ints;
101 vector<string> strings;
102
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 );
107 ints.clear();
108 istr.clear();
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" );
113 strings.clear();
114 istr.clear();
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.
124 *
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 );
129 istr.clear();
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" );
134 */
135 }
136
137 {
138 istringstream is("1 2 3 4 5 6 7 8 9 10");
139 vector<int> ints;
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 );
157 }
158 #endif
159 }
160