]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 1999-07-26 bkoz |
2 | ||
3 | // Copyright (C) 1999 Free Software Foundation | |
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 | // 27.6.1.2.3 character extractors | |
22 | ||
23 | #include <istream> | |
24 | #include <sstream> | |
25 | #ifdef DEBUG_ASSERT | |
26 | #include <assert.h> | |
27 | #endif | |
28 | ||
29 | bool test01() { | |
30 | ||
31 | bool test = true; | |
32 | std::string str_01; | |
33 | const std::string str_02("coltrane playing 'softly as a morning sunrise'"); | |
34 | const std::string str_03("coltrane"); | |
35 | ||
36 | std::stringbuf isbuf_01(std::ios_base::in); | |
37 | std::stringbuf isbuf_02(str_02, std::ios_base::in); | |
38 | std::istream is_01(NULL); | |
39 | std::istream is_02(&isbuf_02); | |
40 | ||
41 | std::ios_base::iostate state1, state2, statefail; | |
42 | statefail = std::ios_base::failbit; | |
43 | ||
44 | // template<_CharT, _Traits> | |
45 | // basic_istream& operator>>(istream&, _CharT*) | |
46 | int n = 20; | |
47 | char array1[n]; | |
48 | typedef std::ios::traits_type ctraits_type; | |
49 | ctraits_type::int_type i1, i2; | |
50 | ||
51 | state1 = is_01.rdstate(); | |
52 | i1 = ctraits_type::length(array1); | |
53 | is_01 >> array1; // should snake 0 characters, not alter stream state | |
54 | i2 = ctraits_type::length(array1); | |
55 | state2 = is_01.rdstate(); | |
56 | test &= i1 == i2; | |
57 | test &= state1 != state2; | |
58 | test &= static_cast<bool>(state2 & statefail); | |
59 | ||
60 | state1 = is_02.rdstate(); | |
61 | is_02 >> array1; // should snake "coltrane" | |
62 | state2 = is_02.rdstate(); | |
63 | test &= state1 == state2; | |
64 | test &= !static_cast<bool>(state2 & statefail); | |
65 | test &= array1[str_03.size() - 1] == 'e'; | |
66 | array1[str_03.size()] = '\0'; | |
67 | test &= !str_03.compare(0, str_03.size(), array1); | |
68 | std::istream::int_type int1 = is_02.peek(); // should be ' ' | |
69 | test &= int1 == ' '; | |
70 | ||
71 | state1 = is_02.rdstate(); | |
72 | is_02 >> array1; // should snake "playing" as sentry "eats" ws | |
73 | state2 = is_02.rdstate(); | |
74 | int1 = is_02.peek(); // should be ' ' | |
75 | test &= int1 == ' '; | |
76 | test &= state1 == state2; | |
77 | test &= !static_cast<bool>(state2 & statefail); | |
78 | ||
79 | ||
80 | // template<_CharT, _Traits> | |
81 | // basic_istream& operator>>(istream&, unsigned char*) | |
82 | unsigned char array2[n]; | |
83 | state1 = is_02.rdstate(); | |
84 | is_02 >> array2; // should snake 'softly | |
85 | state2 = is_02.rdstate(); | |
86 | test &= state1 == state2; | |
87 | test &= !static_cast<bool>(state2 & statefail); | |
88 | test &= array2[0] == '\''; | |
89 | test &= array2[1] == 's'; | |
90 | test &= array2[6] == 'y'; | |
91 | int1 = is_02.peek(); // should be ' ' | |
92 | test &= int1 == ' '; | |
93 | ||
94 | ||
95 | // template<_CharT, _Traits> | |
96 | // basic_istream& operator>>(istream&, signed char*) | |
97 | signed char array3[n]; | |
98 | state1 = is_02.rdstate(); | |
99 | is_02 >> array3; // should snake "as" | |
100 | state2 = is_02.rdstate(); | |
101 | test &= state1 == state2; | |
102 | test &= !static_cast<bool>(state2 & statefail); | |
103 | test &= array3[0] == 'a'; | |
104 | test &= array3[1] == 's'; | |
105 | int1 = is_02.peek(); // should be ' ' | |
106 | test &= int1 == ' '; | |
107 | ||
108 | ||
109 | // testing with width() control enabled. | |
110 | is_02.width(8); | |
111 | state1 = is_02.rdstate(); | |
112 | is_02 >> array1; // should snake a | |
113 | state2 = is_02.rdstate(); | |
114 | test &= state1 == state2; | |
115 | test &= !ctraits_type::compare(array1, "a", 2); | |
116 | ||
117 | is_02.width(1); | |
118 | state1 = is_02.rdstate(); | |
119 | is_02 >> array1; // should snake nothing, set failbit | |
120 | state2 = is_02.rdstate(); | |
121 | test &= state1 != state2; | |
122 | test &= state2 == statefail; | |
123 | test &= array1[0] == '\0'; | |
124 | ||
125 | is_02.width(8); | |
126 | is_02.clear(); | |
127 | state1 = is_02.rdstate(); | |
128 | test &= !state1; | |
129 | is_02 >> array1; // should snake "morning" | |
130 | state2 = is_02.rdstate(); | |
131 | test &= state1 == state2; | |
132 | test &= !ctraits_type::compare(array1, "morning", 8); | |
133 | ||
134 | // testing for correct exception setting | |
135 | const std::string str_04(" impulse!!"); | |
136 | std::stringbuf isbuf_03(str_04, std::ios_base::in); | |
137 | std::stringbuf isbuf_04(str_04, std::ios_base::in); | |
138 | std::istream is_03(&isbuf_03); | |
139 | std::istream is_04(&isbuf_04); | |
140 | ||
141 | is_03 >> array1; | |
142 | test &= !ctraits_type::compare(array1,"impulse!!", 10); | |
143 | test &= is_03.rdstate() == std::ios_base::eofbit; | |
144 | ||
145 | is_04.width(9); | |
146 | is_04 >> array1; | |
147 | test &= ! std::ios::traits_type::compare(array1,"impulse!", 9); | |
148 | test &= !is_04.rdstate(); | |
149 | ||
150 | #ifdef DEBUG_ASSERT | |
151 | assert(test); | |
152 | #endif | |
153 | ||
154 | return test; | |
155 | } | |
156 | ||
157 | bool test02() { | |
158 | ||
159 | typedef std::ios::traits_type ctraits_type; | |
160 | ||
161 | bool test = true; | |
162 | std::string str_01; | |
163 | const std::string str_02("or coltrane playing tunji with jimmy garrison"); | |
164 | const std::string str_03("coltrane"); | |
165 | ||
166 | std::stringbuf isbuf_01(std::ios_base::in); | |
167 | std::stringbuf isbuf_02(str_02, std::ios_base::in); | |
168 | std::istream is_01(NULL); | |
169 | std::istream is_02(&isbuf_02); | |
170 | std::ios_base::iostate state1, state2, statefail; | |
171 | statefail = std::ios_base::failbit; | |
172 | ||
173 | // template<_CharT, _Traits> | |
174 | // basic_istream& operator>>(istream&, _CharT&) | |
175 | char c1 = 'c', c2 = 'c'; | |
176 | state1 = is_01.rdstate(); | |
177 | is_01 >> c1; | |
178 | state2 = is_01.rdstate(); | |
179 | test &= state1 != state2; | |
180 | test &= c1 == c2; | |
181 | test &= static_cast<bool>(state2 & statefail); | |
182 | ||
183 | state1 = is_02.rdstate(); | |
184 | is_02 >> c1; | |
185 | state2 = is_02.rdstate(); | |
186 | test &= state1 == state2; | |
187 | test &= c1 == 'o'; | |
188 | is_02 >> c1; | |
189 | is_02 >> c1; | |
190 | test &= c1 == 'c'; | |
191 | test &= !static_cast<bool>(state2 & statefail); | |
192 | ||
193 | // template<_CharT, _Traits> | |
194 | // basic_istream& operator>>(istream&, unsigned char&) | |
195 | unsigned char uc1 = 'c'; | |
196 | state1 = is_02.rdstate(); | |
197 | is_02 >> uc1; | |
198 | state2 = is_02.rdstate(); | |
199 | test &= state1 == state2; | |
200 | test &= uc1 == 'o'; | |
201 | is_02 >> uc1; | |
202 | is_02 >> uc1; | |
203 | test &= uc1 == 't'; | |
204 | ||
205 | // template<_CharT, _Traits> | |
206 | // basic_istream& operator>>(istream&, signed char&) | |
207 | signed char sc1 = 'c'; | |
208 | state1 = is_02.rdstate(); | |
209 | is_02 >> sc1; | |
210 | state2 = is_02.rdstate(); | |
211 | test &= state1 == state2; | |
212 | test &= sc1 == 'r'; | |
213 | is_02 >> sc1; | |
214 | is_02 >> sc1; | |
215 | test &= sc1 == 'n'; | |
216 | ||
217 | #ifdef DEBUG_ASSERT | |
218 | assert(test); | |
219 | #endif | |
220 | ||
221 | return test; | |
222 | } | |
223 | ||
224 | ||
225 | int main() | |
226 | { | |
227 | test01(); | |
228 | test02(); | |
229 | ||
230 | return 0; | |
231 | } | |
232 |