2002-11-21 Phil Edwards <pme@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / stringbuf_members.cc
blobbc0bbb4dd8e1365e5ffe2135e991ba0ce7da342d
1 // 981208 bkoz test functionality of basic_stringbuf for char_type == char
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
22 #include <sstream>
23 #include <testsuite_hooks.h>
25 std::string str_01("mykonos. . . or what?");
26 std::string str_02("paris, or sainte-maxime?");
27 std::string str_03;
28 std::stringbuf strb_01(str_01);
29 std::stringbuf strb_02(str_02, std::ios_base::in);
30 std::stringbuf strb_03(str_03, std::ios_base::out);
33 // test the underlying allocator
34 bool test01() {
35 bool test = false;
36 std::allocator<char> alloc_01;
37 std::allocator<char>::size_type size_01 = alloc_01.max_size();
38 std::allocator<char>::pointer p_01 = alloc_01.allocate(32);
40 return true;
44 // test the streambuf/stringbuf locale settings
45 bool test02() {
46 std::locale loc_tmp;
47 loc_tmp = strb_01.getloc();
48 strb_01.pubimbue(loc_tmp); //This should initialize _M_init to true
49 strb_01.getloc(); //This should just return _M_locale
51 return true;
55 // test member functions
56 bool test03() {
57 bool test = true;
59 //stringbuf::str()
60 VERIFY( strb_01.str() == str_01 );
61 VERIFY( strb_02.str() == str_02 );
62 VERIFY( strb_03.str() == str_03 );
64 //stringbuf::str(string&)
65 strb_03.str("none of the above, go to the oberoi in cairo, egypt.");
66 strb_03.str(str_01);
67 std::streamsize d1 = strb_01.in_avail();
68 std::streamsize d2 = strb_03.in_avail();
69 VERIFY( d1 ); // non-zero
70 VERIFY( !d2 ); // zero, cuz ios_base::out
71 VERIFY( d1 != d2 ); //these should be the same
72 VERIFY( str_01.length() == d1 );
73 VERIFY( strb_01.str() == strb_03.str() ); //ditto
75 // stringbuf::str(string&) and stringbuf::stringbuf(string&), where the
76 // string in question contains embedded NUL characters. Note that in this
77 // embedded-NUL situation, the size must be passed to the string ctor.
78 std::string str_nulls ("eschew \0 obfuscation", 20); // tested in 21_strings
79 std::stringbuf strb_normal (str_01);
80 std::stringbuf strb_nulls (str_nulls);
81 strb_normal.str(str_nulls); // tried using 'strb_01' rather than declaring
82 // another variable, but then test04 broke!
83 VERIFY( strb_nulls.in_avail() == str_nulls.size() );
84 VERIFY( strb_nulls.str().size() == 20 );
85 VERIFY( strb_normal.in_avail() == str_nulls.size() );
87 #ifdef DEBUG_ASSERT
88 assert(test);
89 #endif
91 return test;
95 // test overloaded virtual functions
96 bool test04() {
97 bool test = true;
98 std::string str_tmp;
99 std::stringbuf strb_tmp;
100 std::streamsize strmsz_1, strmsz_2;
101 std::streamoff strmof_1(-1), strmof_2;
102 typedef std::stringbuf::int_type int_type;
103 typedef std::stringbuf::traits_type traits_type;
104 typedef std::stringbuf::pos_type pos_type;
105 typedef std::stringbuf::off_type off_type;
107 // GET
108 // int in_avail()
109 strmof_1 = strb_01.in_avail();
110 strmof_2 = strb_02.in_avail();
111 VERIFY( strmof_1 != strmof_2 );
112 VERIFY( strmof_1 == str_01.length() );
113 VERIFY( strmof_2 == str_02.length() );
114 strmof_1 = strb_03.in_avail();
115 VERIFY( strmof_1 == 0 ); // zero cuz write-only, or eof()? zero, from showmany
117 // int_type sbumpc()
118 // if read_cur not avail, return uflow(), else return *read_cur & increment
119 int_type c1 = strb_01.sbumpc();
120 int_type c2 = strb_02.sbumpc();
121 VERIFY( c1 != c2 );
122 VERIFY( c1 == str_01[0] );
123 VERIFY( c2 == str_02[0] ); //should equal first letter at this point
124 int_type c3 = strb_01.sbumpc();
125 int_type c4 = strb_02.sbumpc();
126 VERIFY( c1 != c2 );
127 VERIFY( c1 != c3 );
128 VERIFY( c2 != c4 );
129 int_type c5 = strb_03.sbumpc();
130 VERIFY( c5 == traits_type::eof() );
132 // int_type sgetc()
133 // if read_cur not avail, return uflow(), else return *read_cur
134 int_type c6 = strb_01.sgetc();
135 int_type c7 = strb_02.sgetc();
136 VERIFY( c6 != c3 );
137 VERIFY( c7 != c4 );
138 int_type c8 = strb_01.sgetc();
139 int_type c9 = strb_02.sgetc();
140 VERIFY( c6 == c8 );
141 VERIFY( c7 == c9 );
142 c5 = strb_03.sgetc();
143 VERIFY( c5 == traits_type::eof() );
145 // int_type snextc()
146 // calls sbumpc and if sbumpc != eof, return sgetc
147 c6 = strb_01.snextc();
148 c7 = strb_02.snextc();
149 VERIFY( c6 != c8 );
150 VERIFY( c7 != c9 );
151 VERIFY( c6 == str_01[3] );
152 VERIFY( c7 == str_02[3] ); //should equal fourth letter at this point
153 c5 = strb_03.snextc();
154 VERIFY( c5 == traits_type::eof() );
156 // int showmanyc
157 // streamsize sgetn(char_type *s, streamsize n)
158 // streamsize xsgetn(char_type *s, streamsize n)
159 // assign up to n chars to s from input sequence, indexing in_cur as
160 // approp and returning the number of chars assigned
161 strmsz_1 = strb_01.in_avail();
162 strmsz_2 = strb_02.in_avail();
163 test = strmsz_1 != strmsz_2;
164 VERIFY( strmsz_1 != str_01.length() );
165 VERIFY( strmsz_2 != str_02.length() ); //because now we've moved into string
166 char carray1[11] = "";
167 strmsz_1 = strb_01.sgetn(carray1, 10);
168 char carray2[20] = "";
169 strmsz_2 = strb_02.sgetn(carray2, 10);
170 VERIFY( strmsz_1 == strmsz_2 );
171 VERIFY( strmsz_1 == 10 );
172 c1 = strb_01.sgetc();
173 c2 = strb_02.sgetc();
174 VERIFY( c6 == c1 ); //just by co-incidence both o's
175 VERIFY( c7 != c2 ); // n != i
176 VERIFY( c1 == str_01[13] );
177 VERIFY( c2 == str_02[13] ); //should equal fourteenth letter at this point
178 strmsz_1 = strb_03.sgetn(carray1, 10);
179 VERIFY( !strmsz_1 ); //zero
180 strmsz_1 = strb_02.in_avail();
181 strmsz_2 = strb_02.sgetn(carray2, strmsz_1 + 5);
182 VERIFY( strmsz_1 == strmsz_2 ); //write off the end
183 c4 = strb_02.sgetc(); // should be EOF
184 VERIFY( c4 == traits_type::eof() );
186 // PUT
187 // int_type sputc(char_type c)
188 // if out_cur not avail, return overflow. Else, stores c at out_cur,
189 // increments out_cur, and returns c as int_type
190 strb_03.str(str_01); //reset
191 std::string::size_type sz1 = strb_03.str().length();
192 c1 = strb_03.sputc('a');
193 std::string::size_type sz2 = strb_03.str().length();
194 VERIFY( sz1 == sz2 ); //cuz inserting at out_cur, which is at beg to start
195 c2 = strb_03.sputc('b');
196 VERIFY( c1 != c2 );
197 VERIFY( strb_03.str() != str_01 );
198 c3 = strb_02.sputc('a'); // should be EOF because this is read-only
199 VERIFY( c3 == traits_type::eof() );
201 // streamsize sputn(const char_typs* s, streamsize n)
202 // write up to n chars to out_cur from s, returning number assigned
203 // NB *sputn will happily put '\0' into your stream if you give it a chance*
204 str_tmp = strb_03.str();
205 sz1 = str_tmp.length();
206 strmsz_1 = strb_03.sputn("racadabras", 10);//"abracadabras or what?"
207 sz2 = strb_03.str().length();
208 VERIFY( sz1 == sz2 ); //shouldn't have changed length
209 VERIFY( strmsz_1 == 10 );
210 VERIFY( str_tmp != strb_03.str() );
211 strmsz_2 = strb_03.sputn(", i wanna reach out and", 10);
212 VERIFY( strmsz_1 == strmsz_2 ); // should re-allocate, copy 10 chars.
213 VERIFY( strmsz_1 == 10 );
214 VERIFY( strmsz_2 == 10 );
215 sz2 = strb_03.str().length();
216 VERIFY( sz1 != sz2 ); // need to change length
217 VERIFY( str_tmp != strb_03.str() );
218 str_tmp = strb_02.str();
219 strmsz_1 = strb_02.sputn("racadabra", 10);
220 VERIFY( strmsz_1 == 0 );
221 VERIFY( str_tmp == strb_02.str() );
223 // PUTBACK
224 // int_type pbfail(int_type c)
225 // called when gptr() null, gptr() == eback(), or traits::eq(*gptr, c) false
226 // "pending sequence" is:
227 // 1) everything as defined in underflow
228 // 2) + if (traits::eq_int_type(c, traits::eof()), then input
229 // sequence is backed up one char before the pending sequence is
230 // determined.
231 // 3) + if (not 2) then c is prepended. Left unspecified is
232 // whether the input sequence is backedup or modified in any way
233 // returns traits::eof() for failure, unspecified other value for success
235 // int_type sputbackc(char_type c)
236 // if in_cur not avail || ! traits::eq(c, gptr() [-1]), return pbfail
237 // otherwise decrements in_cur and returns *gptr()
238 strmsz_1 = strb_01.in_avail();
239 str_tmp = strb_01.str();
240 c1 = strb_01.sgetc(); //"mykonos. . . 'o'r what?"
241 c2 = strb_01.sputbackc('z');//"mykonos. . .zor what?"
242 c3 = strb_01.sgetc();
243 VERIFY( c1 != c2 );
244 VERIFY( c3 == c2 );
245 VERIFY( strb_01.str() == std::string("mykonos. . .zor what?") );
246 VERIFY( str_tmp.size() == strb_01.str().size() );
247 //test for _in_cur == _in_beg
248 strb_01.str(str_tmp);
249 strmsz_1 = strb_01.in_avail();
250 c1 = strb_01.sgetc(); //"'m'ykonos. . . or what?"
251 c2 = strb_01.sputbackc('z');//"mykonos. . . or what?"
252 c3 = strb_01.sgetc();
253 VERIFY( c1 != c2 );
254 VERIFY( c3 != c2 );
255 VERIFY( c1 == c3 );
256 VERIFY( c2 == traits_type::eof() );
257 VERIFY( strb_01.str() == str_tmp );
258 VERIFY( str_tmp.size() == strb_01.str().size() );
259 // test for replacing char with identical one
260 strb_01.str(str_01); //reset
261 strmsz_1 = strb_01.in_avail();
262 strb_01.sbumpc();
263 strb_01.sbumpc();
264 c1 = strb_01.sgetc(); //"my'k'onos. . . or what?"
265 c2 = strb_01.sputbackc('y');//"mykonos. . . or what?"
266 c3 = strb_01.sgetc();
267 VERIFY( c1 != c2 );
268 VERIFY( c3 == c2 );
269 VERIFY( c1 != c3 );
270 VERIFY( strb_01.str() == str_01 );
271 VERIFY( str_01.size() == strb_01.str().size() );
272 //test for ios_base::out
273 strmsz_2 = strb_03.in_avail();
274 c4 = strb_03.sputbackc('x');
275 VERIFY( c4 == traits_type::eof() );
277 // int_type sungetc()
278 // if in_cur not avail, return pbackfail(), else decrement and
279 // return to_int_type(*gptr())
280 for (int i = 0; i<12; ++i)
281 strb_01.sbumpc();
282 strmsz_1 = strb_01.in_avail();
283 str_tmp = strb_01.str();
284 c1 = strb_01.sgetc(); //"mykonos. . . 'o'r what?"
285 c2 = strb_01.sungetc();//"mykonos. . . or what?"
286 c3 = strb_01.sgetc();
287 VERIFY( c1 != c2 );
288 VERIFY( c3 == c2 );
289 VERIFY( c1 != c3 );
290 VERIFY( c2 == ' ' );
291 VERIFY( strb_01.str() == str_01 );
292 VERIFY( str_01.size() == strb_01.str().size() );
293 //test for _in_cur == _in_beg
294 strb_01.str(str_tmp);
295 strmsz_1 = strb_01.in_avail();
296 c1 = strb_01.sgetc(); //"'m'ykonos. . . or what?"
297 c2 = strb_01.sungetc();//"mykonos. . . or what?"
298 c3 = strb_01.sgetc();
299 VERIFY( c1 != c2 );
300 VERIFY( c3 != c2 );
301 VERIFY( c1 == c3 );
302 VERIFY( c2 == traits_type::eof() );
303 VERIFY( strb_01.str() == str_01 );
304 VERIFY( str_01.size() == strb_01.str().size() );
305 // test for replacing char with identical one
306 strb_01.str(str_01); //reset
307 strmsz_1 = strb_01.in_avail();
308 strb_01.sbumpc();
309 strb_01.sbumpc();
310 c1 = strb_01.sgetc(); //"my'k'onos. . . or what?"
311 c2 = strb_01.sungetc();//"mykonos. . . or what?"
312 c3 = strb_01.sgetc();
313 VERIFY( c1 != c2 );
314 VERIFY( c3 == c2 );
315 VERIFY( c1 != c3 );
316 VERIFY( strb_01.str() == str_01 );
317 VERIFY( str_01.size() == strb_01.str().size() );
318 //test for ios_base::out
319 strmsz_2 = strb_03.in_avail();
320 c4 = strb_03.sungetc();
321 VERIFY( c4 == traits_type::eof() );
323 // BUFFER MANAGEMENT & POSITIONING
324 // sync
325 // pubsync
326 strb_01.pubsync();
327 strb_02.pubsync();
328 strb_03.pubsync();
330 // setbuf
331 // pubsetbuf(char_type* s, streamsize n)
332 str_tmp = std::string("naaaah, go to cebu");
333 strb_01.pubsetbuf(const_cast<char*> (str_tmp.c_str()), str_tmp.size());
334 VERIFY( strb_01.str() == str_tmp );
335 strb_01.pubsetbuf(0,0);
336 VERIFY( strb_01.str() == str_tmp );
338 // seekoff
339 // pubseekoff(off_type off, ios_base::seekdir way, ios_base::openmode which)
340 // alters the stream position to off
341 pos_type pt_1(off_type(-1));
342 pos_type pt_2(off_type(0));
343 off_type off_1 = 0;
344 off_type off_2 = 0;
345 strb_01.str(str_01); //in|out ("mykonos. . . or what?");
346 strb_02.str(str_02); //in ("paris, or sainte-maxime?");
347 strb_03.str(str_03); //out ("")
348 //IN|OUT
349 //beg
350 pt_1 = strb_01.pubseekoff(2, std::ios_base::beg);
351 off_1 = pt_1;
352 VERIFY( off_1 >= 0 );
353 c1 = strb_01.snextc(); //current in pointer +1
354 VERIFY( c1 == 'o' );
355 c2 = strb_01.sputc('x'); //test current out pointer
356 str_tmp = std::string("myxonos. . . or what?");
357 VERIFY( strb_01.str() == str_tmp );
358 //cur
359 pt_1 = strb_01.pubseekoff(2, std::ios_base::cur);
360 off_1 = pt_1;
361 VERIFY( off_1 == -1 ); // can't seekoff for in and out + cur in sstreams
362 pt_1 = strb_01.pubseekoff(2, std::ios_base::cur, std::ios_base::in);
363 off_1 = pt_1;
364 pt_2 = strb_01.pubseekoff(2, std::ios_base::cur, std::ios_base::in);
365 off_2 = pt_2;
366 VERIFY( off_2 == off_1 + 2 );
367 c1 = strb_01.snextc(); //current in pointer + 1
368 VERIFY( c1 == ' ' );
369 c2 = strb_01.sputc('x'); //test current out pointer
370 str_tmp = std::string("myxxnos. . . or what?");
371 VERIFY( strb_01.str() == str_tmp );
372 //end
373 pt_2 = strb_01.pubseekoff(2, std::ios_base::end);
374 off_1 = pt_2;
375 VERIFY( off_1 == -1 ); // not a valid position
376 VERIFY( strb_01.str() == str_tmp );
377 // end part two (from the filebuf tests)
378 strb_01.pubseekoff(0, std::ios_base::end);
379 strmsz_1 = strb_01.in_avail(); // 0 cuz at the end
380 c1 = strb_01.sgetc();
381 c2 = strb_01.sungetc();
382 strmsz_2 = strb_01.in_avail(); // 1
383 c3 = strb_01.sgetc();
384 VERIFY( c1 != c2 );
385 VERIFY( strmsz_2 != strmsz_1 );
386 VERIFY( strmsz_2 == 1 );
387 // end part three
388 strmsz_1 = strb_01.str().size();
389 strmsz_2 = strb_01.sputn(" ravi shankar meets carlos santana in LoHa", 90);
390 strb_01.pubseekoff(0, std::ios_base::end);
391 strb_01.sputc('<');
392 str_tmp = strb_01.str();
393 VERIFY( str_tmp.size() == strmsz_1 + strmsz_2 + 1 );
394 // IN
395 // OUT
397 // seekpos
398 // pubseekpos(pos_type sp, ios_base::openmode)
399 // alters the stream position to sp
400 strb_01.str(str_01); //in|out ("mykonos. . . or what?");
401 strb_02.str(str_02); //in ("paris, or sainte-maxime?");
402 strb_03.str(str_03); //out ("")
403 //IN|OUT
404 //beg
405 pt_1 = strb_01.pubseekoff(2, std::ios_base::beg);
406 off_1 = pt_1;
407 VERIFY( off_1 >= 0 );
408 pt_1 = strb_01.pubseekoff(0, std::ios_base::cur, std::ios_base::out);
409 off_1 = pt_1;
410 c1 = strb_01.snextc(); //current in pointer +1
411 VERIFY( c1 == 'o' );
412 c2 = strb_01.sputc('x'); //test current out pointer
413 str_tmp = std::string("myxonos. . . or what?");
414 VERIFY( strb_01.str() == str_tmp );
415 strb_01.pubsync(); //resets pointers
416 pt_2 = strb_01.pubseekpos(pt_1, std::ios_base::in|std::ios_base::out);
417 off_2 = pt_2;
418 VERIFY( off_1 == off_2 );
419 c3 = strb_01.snextc(); //current in pointer +1
420 VERIFY( c1 == c3 );
421 c2 = strb_01.sputc('x'); //test current out pointer
422 str_tmp = std::string("myxonos. . . or what?");
423 VERIFY( strb_01.str() == str_tmp );
425 // VIRTUALS (indirectly tested)
426 // underflow
427 // if read position avail, returns *gptr()
429 // pbackfail(int_type c)
430 // put c back into input sequence
432 // overflow
433 // appends c to output seq
435 #ifdef DEBUG_ASSERT
436 assert(test);
437 #endif
439 return test;
443 // libstdc++/3955 -- ios_base::app overwrites from the beginning
444 bool test05()
446 bool test = true;
448 std::ostringstream os ("foo");
449 os << "bar";
451 test = os.str() == "bar";
453 #ifdef DEBUG_ASSERT
454 assert(test);
455 #endif
457 return test;
460 bool test06()
462 bool test = true;
464 std::ostringstream os ("foo", std::ios_base::app);
465 os << "bar";
467 test = os.str() == "foobar";
469 #ifdef DEBUG_ASSERT
470 assert(test);
471 #endif
473 return test;
476 int main()
478 test01();
479 test02();
480 test03();
481 test04();
482 test05();
483 test06();
485 return 0;
490 // more candy!!!