OSDN Git Service

8325c97fa3b229a5ff065d4572981472ceb46f37
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 24_iterators / istreambuf_iterator.cc
1 // 1999-06-28 bkoz
2
3 // Copyright (C) 1999, 2001 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // 24.5.3 template class istreambuf_iterator
22
23 #include <sstream>
24 #include <iterator>
25 #include <debug_assert.h>
26
27 bool test01(void)
28 {
29
30   typedef std::istreambuf_iterator<char> cistreambuf_iter;
31   typedef cistreambuf_iter::streambuf_type cstreambuf_type;
32   bool test = true;
33   const char slit01[] = "playa hermosa, liberia, guanacaste";
34   std::string str01(slit01);
35   std::istringstream istrs00(str01);
36   std::istringstream istrs01(str01);
37
38   // ctor sanity checks
39   cistreambuf_iter istrb_it01(istrs00);
40   cistreambuf_iter istrb_it02;
41   std::string tmp(istrb_it01, istrb_it02); 
42   VERIFY( tmp == str01 );
43
44   cistreambuf_iter istrb_it03(0);
45   cistreambuf_iter istrb_it04;
46   VERIFY( istrb_it03 == istrb_it04 );
47
48   cistreambuf_iter istrb_it05(istrs01);
49   cistreambuf_iter istrb_it06(istrs01.rdbuf());
50   VERIFY( istrb_it05 == istrb_it06 );
51   
52   // bool equal(istreambuf_iter& b)
53   cistreambuf_iter istrb_it07(0);
54   cistreambuf_iter istrb_it08;
55   VERIFY( istrb_it07.equal(istrb_it08) );
56   cistreambuf_iter istrb_it09(0);
57   cistreambuf_iter istrb_it10;
58   VERIFY( istrb_it10.equal(istrb_it09) );
59
60   cistreambuf_iter istrb_it11(istrs01);
61   cistreambuf_iter istrb_it12(istrs01.rdbuf());
62   VERIFY( istrb_it11.equal(istrb_it12) );
63   cistreambuf_iter istrb_it13(istrs01);
64   cistreambuf_iter istrb_it14(istrs01.rdbuf());
65   VERIFY( istrb_it14.equal(istrb_it13) );
66
67   cistreambuf_iter istrb_it15(istrs01);
68   cistreambuf_iter istrb_it16;
69   VERIFY( !(istrb_it15.equal(istrb_it16)) );
70   cistreambuf_iter istrb_it17(istrs01);
71   cistreambuf_iter istrb_it18;
72   VERIFY( !(istrb_it18.equal(istrb_it17)) );
73
74   // bool operator==(const istreambuf_iterator&a, const istreambuf_iterator& b)
75   // bool operator!=(const istreambuf_iterator&a, const istreambuf_iterator& b)
76   cistreambuf_iter istrb_it19(0);
77   cistreambuf_iter istrb_it20;
78   VERIFY( istrb_it19 == istrb_it20 );
79
80   cistreambuf_iter istrb_it21(istrs01);
81   cistreambuf_iter istrb_it22(istrs01.rdbuf());
82   VERIFY( istrb_it22 == istrb_it21 );
83
84   cistreambuf_iter istrb_it23(istrs01);
85   cistreambuf_iter istrb_it24;
86   VERIFY( istrb_it23 != istrb_it24 );
87
88   cistreambuf_iter istrb_it25(0);
89   cistreambuf_iter istrb_it26(istrs01.rdbuf());
90   VERIFY( istrb_it25 != istrb_it26 );
91
92   // charT operator*() const
93   // istreambuf_iterator& operator++();
94   // istreambuf_iterator& operator++(int);
95   cistreambuf_iter istrb_it27(istrs01.rdbuf());
96   char c;
97   for (int i = 0; i < sizeof(slit01) - 2; ++i)
98     {
99       c = *istrb_it27++;
100       VERIFY( c == slit01[i] );
101     }
102
103   std::istringstream istrs02(str01);
104   cistreambuf_iter istrb_it28(istrs02);
105   for (int i = 0; i < sizeof(slit01) - 2;)
106     {
107       c = *++istrb_it28;
108       VERIFY( c == slit01[++i] );
109     }
110
111 #ifdef DEBUG_ASSERT
112   assert(test);
113 #endif
114
115   return test;
116 }
117
118 // libstdc++/2627
119 void test02()
120 {
121   bool test = true;
122   const std::string s("free the vieques");
123
124   // 1
125   std::string res_postfix;
126   std::istringstream iss01(s);
127   std::istreambuf_iterator<char> isbufit01(iss01);
128   for (int j = 0; j < s.size(); ++j, isbufit01++)
129     res_postfix += *isbufit01;
130
131   // 2
132   std::string res_prefix;
133   std::istringstream iss02(s);
134   std::istreambuf_iterator<char> isbufit02(iss02);
135   for (int j = 0; j < s.size(); ++j, ++isbufit02)
136     res_prefix += *isbufit02;
137
138   // 3 mixed
139   std::string res_mixed;
140   std::istringstream iss03(s);
141   std::istreambuf_iterator<char> isbufit03(iss03);
142   for (int j = 0; j < int(s.size() / 2); ++j)
143     {
144       res_mixed += *isbufit03;
145       ++isbufit03;
146       res_mixed += *isbufit03;
147       isbufit03++;
148     }
149
150   VERIFY ( res_postfix == res_prefix );
151   VERIFY ( res_mixed == res_prefix );
152 }
153
154 int main()
155 {
156   test01();
157   test02();
158
159   return 0;
160 }
161
162