1 // 1999-08-16 bkoz
2
3 // Copyright (C) 1999, 2000, 2002 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.2.5.4 basic_ostream character inserters
22
23 #include <string>
24 #include <ostream>
25 #include <sstream>
26 #include <fstream>
27 #include <testsuite_hooks.h>
28
29 // ofstream
test01()30 bool test01()
31 {
32 bool test = true;
33 std::string str01;
34 const int size = 1000;
35 const char name_02[] = "ostream_inserter_char-1.txt";
36
37 // initialize string
38 for(int i=0 ; i < size; i++) {
39 str01 += '1';
40 str01 += '2';
41 str01 += '3';
42 str01 += '4';
43 str01 += '5';
44 str01 += '6';
45 str01 += '7';
46 str01 += '8';
47 str01 += '9';
48 str01 += '\n';
49 }
50 std::ofstream f(name_02);
51
52 f << str01;
53 f.close();
54
55 VERIFY( test );
56 return test;
57 }
58
59 // ostringstream width() != zero
60 // left
test02(void)61 bool test02(void)
62 {
63 bool test = true;
64 std::string tmp;
65
66 std::string str01 = "";
67 std::ostringstream oss01;
68 oss01.width(5);
69 oss01.fill('0');
70 oss01.flags(std::ios_base::left);
71 oss01 << str01;
72 tmp = oss01.str();
73 VERIFY( tmp == "00000" );
74
75 std::string str02 = "1";
76 std::ostringstream oss02;
77 oss02.width(5);
78 oss02.fill('0');
79 oss02.flags(std::ios_base::left);
80 oss02 << str02;
81 tmp = oss02.str();
82 VERIFY( tmp == "10000" );
83
84 std::string str03 = "909909";
85 std::ostringstream oss03;
86 oss03.width(5);
87 oss03.fill('0');
88 oss03.flags(std::ios_base::left);
89 oss03 << str03;
90 tmp = oss03.str();
91 VERIFY( tmp == "909909" );
92 return test;
93 }
94
95 // width() != zero
96 // right
test03(void)97 bool test03(void)
98 {
99 bool test = true;
100 std::string tmp;
101
102 std::string str01 = "";
103 std::ostringstream oss01;
104 oss01.width(5);
105 oss01.fill('0');
106 oss01.flags(std::ios_base::right);
107 oss01 << str01;
108 tmp = oss01.str();
109 VERIFY( tmp == "00000" );
110
111 std::string str02 = "1";
112 std::ostringstream oss02;
113 oss02.width(5);
114 oss02.fill('0');
115 oss02.flags(std::ios_base::right);
116 oss02 << str02;
117 tmp = oss02.str();
118 VERIFY( tmp == "00001" );
119
120 std::string str03 = "909909";
121 std::ostringstream oss03;
122 oss03.width(5);
123 oss03.fill('0');
124 oss03.flags(std::ios_base::right);
125 oss03 << str03;
126 tmp = oss03.str();
127 VERIFY( tmp == "909909" );
128 return test;
129 }
130
131 // stringstream and large strings
test04()132 bool test04()
133 {
134 bool test = true;
135 std::string str_01;
136 const std::string str_02("coltrane playing 'softly as a morning sunrise'");
137 const std::string str_03("coltrane");
138 std::string str_tmp;
139 const int i_max=250;
140
141 std::ostringstream oss_01(std::ios_base::out);
142 std::ostringstream oss_02(str_01, std::ios_base::out);
143
144 std::ios_base::iostate state1, state2, statefail;
145 statefail = std::ios_base::failbit;
146
147 // template<_CharT, _Traits>
148 // basic_ostream& operator<<(ostream&, const char*)
149 for (int i = 0; i < i_max; ++i)
150 oss_02 << "Test: " << i << std::endl;
151 str_tmp = oss_02.str();
152 VERIFY( !oss_02.bad() );
153 VERIFY( oss_02.good() );
154 VERIFY( str_tmp != str_01 );
155 VERIFY( str_tmp.size() == 2390 );
156 return test;
157 }
158
159 // ostringstream and large strings number 2
test05()160 bool test05()
161 {
162 bool test = true;
163 std::string str05, str10;
164
165 typedef std::ostream::pos_type pos_type;
166 typedef std::ostream::off_type off_type;
167 std::string str01;
168 const int size = 1000;
169
170 // initialize string
171 for(int i=0 ; i < size; i++) {
172 str01 += '1';
173 str01 += '2';
174 str01 += '3';
175 str01 += '4';
176 str01 += '5';
177 str01 += '6';
178 str01 += '7';
179 str01 += '8';
180 str01 += '9';
181 str01 += '\n';
182 }
183
184 // test 1: out
185 std::ostringstream sstr01(str01, std::ios_base::out);
186 std::ostringstream sstr02;
187 sstr02 << str01;
188 str05 = sstr01.str();
189 str10 = sstr02.str();
190 VERIFY( str05 == str01 );
191 VERIFY( str10 == str01 );
192
193 // test 2: in | out
194 std::ostringstream sstr04(str01, std::ios_base::out | std::ios_base::in);
195 std::ostringstream sstr05(std::ios_base::in | std::ios_base::out);
196 sstr05 << str01;
197 str05 = sstr04.str();
198 str10 = sstr05.str();
199 VERIFY( str05 == str01 );
200 VERIFY( str10 == str01 );
201 return test;
202 }
203
204
205 // ostringstream and positioning, multiple writes
206 // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00326.html
test06()207 void test06()
208 {
209 bool test = true;
210 const char carray01[] = "mos def & talib kweli are black star";
211
212 // normal
213 std::ostringstream ostr1("mos def");
214 VERIFY( ostr1.str() == "mos def" );
215 ostr1 << " & talib kweli"; // should overwrite first part of buffer
216 VERIFY( ostr1.str() == " & talib kweli" );
217 ostr1 << " are black star"; // should append to string from above
218 VERIFY( ostr1.str() != carray01 );
219 VERIFY( ostr1.str() == " & talib kweli are black star" );
220
221 // appending
222 std::ostringstream ostr2("blackalicious",
223 std::ios_base::out | std::ios_base::ate);
224 VERIFY( ostr2.str() == "blackalicious" );
225 ostr2 << " NIA "; // should not overwrite first part of buffer
226 VERIFY( ostr2.str() == "blackalicious NIA " );
227 ostr2 << "4: deception (5:19)"; // should append to full string from above
228 VERIFY( ostr2.str() == "blackalicious NIA 4: deception (5:19)" );
229 }
230
231 // Global counter, needs to be reset after use.
232 bool used;
233
234 class gnu_ctype : public std::ctype<wchar_t>
235 {
236 protected:
237 char_type
do_widen(char c) const238 do_widen(char c) const
239 {
240 used = true;
241 return std::ctype<wchar_t>::do_widen(c);
242 }
243
244 const char*
do_widen(const char * low,const char * high,char_type * dest) const245 do_widen(const char* low, const char* high, char_type* dest) const
246 {
247 used = true;
248 return std::ctype<wchar_t>::do_widen(low, high, dest);
249 }
250 };
251
252 // 27.6.2.5.4 - Character inserter template functions
253 // [lib.ostream.inserters.character]
test07()254 void test07()
255 {
256 #if defined(_GLIBCPP_USE_WCHAR_T)
257 using namespace std;
258 bool test = true;
259
260 const char* buffer = "SFPL 5th floor, outside carrol, the Asian side";
261
262 wostringstream oss;
263 oss.imbue(locale(locale::classic(), new gnu_ctype));
264
265 // 1
266 // template<class charT, class traits>
267 // basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>& out,
268 // const char* s);
269 used = false;
270 oss << buffer;
271 VERIFY( used ); // Only required for char_type != char
272 wstring str = oss.str();
273 wchar_t c1 = oss.widen(buffer[0]);
274 VERIFY( str[0] == c1 );
275 wchar_t c2 = oss.widen(buffer[1]);
276 VERIFY( str[1] == c2 );
277
278 // 2
279 // template<class charT, class traits>
280 // basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>& out,
281 // char c);
282 used = false;
283 oss.str(wstring());
284 oss << 'b';
285 VERIFY( used ); // Only required for char_type != char
286 str = oss.str();
287 wchar_t c3 = oss.widen('b');
288 VERIFY( str[0] == c3 );
289 #endif
290 }
291
test08()292 void test08()
293 {
294 bool test = true;
295 char* pt = NULL;
296
297 // 1
298 std::ostringstream oss;
299 oss << pt;
300 VERIFY( oss.bad() );
301 VERIFY( oss.str().size() == 0 );
302
303 oss.clear();
304 oss << "";
305 VERIFY( oss.good() );
306
307 #if defined(_GLIBCPP_USE_WCHAR_T)
308 // 2
309 std::wostringstream woss;
310 woss << pt;
311 VERIFY( woss.bad() );
312 VERIFY( woss.str().size() == 0 );
313
314 woss.clear();
315 woss << "";
316 VERIFY( woss.good() );
317
318 // 3
319 wchar_t* wt = NULL;
320 woss.clear();
321 woss << wt;
322 VERIFY( woss.bad() );
323 VERIFY( woss.str().size() == 0 );
324
325 woss.clear();
326 woss << L"";
327 VERIFY( woss.good() );
328 #endif
329 }
330
main()331 int main()
332 {
333 test01();
334 test02();
335 test03();
336 test04();
337 test05();
338 test06();
339 test07();
340 test08();
341 return 0;
342 }
343