]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/stringbuf.cc
*.cc: Remove spaces, make sure testcases return zero.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / stringbuf.cc
CommitLineData
b2dad0e3
BK
1// 981208 bkoz test functionality of basic_stringbuf for char_type == char
2
97e0a05a 3// Copyright (C) 1997-1999, 2000 Free Software Foundation, Inc.
b2dad0e3
BK
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#include <sstream>
aa1b2f7d 22#include <debug_assert.h>
b2dad0e3
BK
23
24std::string str_01("mykonos. . . or what?");
25std::string str_02("paris, or sainte-maxime?");
26std::string str_03;
27std::stringbuf strb_01(str_01);
28std::stringbuf strb_02(str_02, std::ios_base::in);
29std::stringbuf strb_03(str_03, std::ios_base::out);
30
31
32// test the underlying allocator
33bool test01() {
34 bool test = false;
35 std::allocator<char> alloc_01;
36 std::allocator<char>::size_type size_01 = alloc_01.max_size();
37 std::allocator<char>::pointer p_01 = alloc_01.allocate(32);
38
39 return true;
40}
41
42
43// test the streambuf/stringbuf locale settings
44bool test02() {
45 std::locale loc_tmp;
46 loc_tmp = strb_01.getloc();
47 strb_01.pubimbue(loc_tmp); //This should initialize _M_init to true
48 strb_01.getloc(); //This should just return _M_locale
49
50 return true;
51}
52
53
54// test member functions
55bool test03() {
56 bool test = true;
57 std::string str_tmp;
58
59 //stringbuf::str()
aa1b2f7d
BV
60 VERIFY( strb_01.str() == str_01 );
61 VERIFY( strb_02.str() == str_02 );
62 VERIFY( strb_03.str() == str_03 );
b2dad0e3
BK
63
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();
aa1b2f7d
BV
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
b2dad0e3
BK
74
75#ifdef DEBUG_ASSERT
76 assert(test);
77#endif
78
79 return test;
80}
81
82
83// test overloaded virtual functions
84bool test04() {
85 bool test = true;
86 std::string str_tmp;
87 std::stringbuf strb_tmp;
88 std::streamsize strmsz_1, strmsz_2;
89 std::streamoff strmof_1(-1), strmof_2;
90 typedef std::stringbuf::int_type int_type;
91 typedef std::stringbuf::traits_type traits_type;
92 typedef std::stringbuf::pos_type pos_type;
93 typedef std::stringbuf::off_type off_type;
94
95 // GET
96 // int in_avail()
97 strmof_1 = strb_01.in_avail();
98 strmof_2 = strb_02.in_avail();
aa1b2f7d
BV
99 VERIFY( strmof_1 != strmof_2 );
100 VERIFY( strmof_1 == str_01.length() );
101 VERIFY( strmof_2 == str_02.length() );
b2dad0e3 102 strmof_1 = strb_03.in_avail();
aa1b2f7d 103 VERIFY( strmof_1 == 0 ); // zero cuz write-only, or eof()? zero, from showmany
b2dad0e3
BK
104
105 // int_type sbumpc()
106 // if read_cur not avail, return uflow(), else return *read_cur & increment
107 int_type c1 = strb_01.sbumpc();
108 int_type c2 = strb_02.sbumpc();
aa1b2f7d
BV
109 VERIFY( c1 != c2 );
110 VERIFY( c1 == str_01[0] );
111 VERIFY( c2 == str_02[0] ); //should equal first letter at this point
b2dad0e3
BK
112 int_type c3 = strb_01.sbumpc();
113 int_type c4 = strb_02.sbumpc();
aa1b2f7d
BV
114 VERIFY( c1 != c2 );
115 VERIFY( c1 != c3 );
116 VERIFY( c2 != c4 );
b2dad0e3 117 int_type c5 = strb_03.sbumpc();
aa1b2f7d 118 VERIFY( c5 == traits_type::eof() );
b2dad0e3
BK
119
120 // int_type sgetc()
121 // if read_cur not avail, return uflow(), else return *read_cur
122 int_type c6 = strb_01.sgetc();
123 int_type c7 = strb_02.sgetc();
aa1b2f7d
BV
124 VERIFY( c6 != c3 );
125 VERIFY( c7 != c4 );
b2dad0e3
BK
126 int_type c8 = strb_01.sgetc();
127 int_type c9 = strb_02.sgetc();
aa1b2f7d
BV
128 VERIFY( c6 == c8 );
129 VERIFY( c7 == c9 );
b2dad0e3 130 c5 = strb_03.sgetc();
aa1b2f7d 131 VERIFY( c5 == traits_type::eof() );
b2dad0e3
BK
132
133 // int_type snextc()
134 // calls sbumpc and if sbumpc != eof, return sgetc
135 c6 = strb_01.snextc();
136 c7 = strb_02.snextc();
aa1b2f7d
BV
137 VERIFY( c6 != c8 );
138 VERIFY( c7 != c9 );
139 VERIFY( c6 == str_01[3] );
140 VERIFY( c7 == str_02[3] ); //should equal fourth letter at this point
b2dad0e3 141 c5 = strb_03.snextc();
aa1b2f7d 142 VERIFY( c5 == traits_type::eof() );
b2dad0e3
BK
143
144 // int showmanyc
145 // streamsize sgetn(char_type *s, streamsize n)
146 // streamsize xsgetn(char_type *s, streamsize n)
147 // assign up to n chars to s from input sequence, indexing in_cur as
148 // approp and returning the number of chars assigned
149 strmsz_1 = strb_01.in_avail();
150 strmsz_2 = strb_02.in_avail();
151 test = strmsz_1 != strmsz_2;
aa1b2f7d
BV
152 VERIFY( strmsz_1 != str_01.length() );
153 VERIFY( strmsz_2 != str_02.length() ); //because now we've moved into string
b2dad0e3
BK
154 char carray1[11] = "";
155 strmsz_1 = strb_01.sgetn(carray1, 10);
156 char carray2[20] = "";
157 strmsz_2 = strb_02.sgetn(carray2, 10);
aa1b2f7d
BV
158 VERIFY( strmsz_1 == strmsz_2 );
159 VERIFY( strmsz_1 == 10 );
b2dad0e3
BK
160 c1 = strb_01.sgetc();
161 c2 = strb_02.sgetc();
aa1b2f7d
BV
162 VERIFY( c6 == c1 ); //just by co-incidence both o's
163 VERIFY( c7 != c2 ); // n != i
164 VERIFY( c1 == str_01[13] );
165 VERIFY( c2 == str_02[13] ); //should equal fourteenth letter at this point
b2dad0e3 166 strmsz_1 = strb_03.sgetn(carray1, 10);
aa1b2f7d 167 VERIFY( !strmsz_1 ); //zero
b2dad0e3
BK
168 strmsz_1 = strb_02.in_avail();
169 strmsz_2 = strb_02.sgetn(carray2, strmsz_1 + 5);
aa1b2f7d 170 VERIFY( strmsz_1 == strmsz_2 ); //write off the end
b2dad0e3 171 c4 = strb_02.sgetc(); // should be EOF
aa1b2f7d 172 VERIFY( c4 == traits_type::eof() );
b2dad0e3
BK
173
174 // PUT
175 // int_type sputc(char_type c)
176 // if out_cur not avail, return overflow. Else, stores c at out_cur,
177 // increments out_cur, and returns c as int_type
178 strb_03.str(str_01); //reset
179 std::string::size_type sz1 = strb_03.str().length();
180 c1 = strb_03.sputc('a');
181 std::string::size_type sz2 = strb_03.str().length();
aa1b2f7d 182 VERIFY( sz1 == sz2 ); //cuz inserting at out_cur, which is at beg to start
b2dad0e3 183 c2 = strb_03.sputc('b');
aa1b2f7d
BV
184 VERIFY( c1 != c2 );
185 VERIFY( strb_03.str() != str_01 );
b2dad0e3 186 c3 = strb_02.sputc('a'); // should be EOF because this is read-only
aa1b2f7d 187 VERIFY( c3 == traits_type::eof() );
b2dad0e3
BK
188
189 // streamsize sputn(const char_typs* s, streamsize n)
190 // write up to n chars to out_cur from s, returning number assigned
191 // NB *sputn will happily put '\0' into your stream if you give it a chance*
192 str_tmp = strb_03.str();
193 sz1 = str_tmp.length();
194 strmsz_1 = strb_03.sputn("racadabras", 10);//"abracadabras or what?"
195 sz2 = strb_03.str().length();
aa1b2f7d
BV
196 VERIFY( sz1 == sz2 ); //shouldn't have changed length
197 VERIFY( strmsz_1 == 10 );
198 VERIFY( str_tmp != strb_03.str() );
b2dad0e3 199 strmsz_2 = strb_03.sputn(", i wanna reach out and", 10);
aa1b2f7d
BV
200 VERIFY( strmsz_1 == strmsz_2 ); // should re-allocate, copy 10 chars.
201 VERIFY( strmsz_1 == 10 );
202 VERIFY( strmsz_2 == 10 );
b2dad0e3 203 sz2 = strb_03.str().length();
aa1b2f7d
BV
204 VERIFY( sz1 != sz2 ); // need to change length
205 VERIFY( str_tmp != strb_03.str() );
b2dad0e3
BK
206 str_tmp = strb_02.str();
207 strmsz_1 = strb_02.sputn("racadabra", 10);
aa1b2f7d
BV
208 VERIFY( strmsz_1 == 0 );
209 VERIFY( str_tmp == strb_02.str() );
b2dad0e3
BK
210
211 // PUTBACK
212 // int_type pbfail(int_type c)
213 // called when gptr() null, gptr() == eback(), or traits::eq(*gptr, c) false
214 // "pending sequence" is:
215 // 1) everything as defined in underflow
216 // 2) + if (traits::eq_int_type(c, traits::eof()), then input
217 // sequence is backed up one char before the pending sequence is
218 // determined.
219 // 3) + if (not 2) then c is prepended. Left unspecified is
220 // whether the input sequence is backedup or modified in any way
221 // returns traits::eof() for failure, unspecified other value for success
222
223 // int_type sputbackc(char_type c)
224 // if in_cur not avail || ! traits::eq(c, gptr() [-1]), return pbfail
225 // otherwise decrements in_cur and returns *gptr()
226 strmsz_1 = strb_01.in_avail();
227 str_tmp = strb_01.str();
228 c1 = strb_01.sgetc(); //"mykonos. . . 'o'r what?"
229 c2 = strb_01.sputbackc('z');//"mykonos. . .zor what?"
230 c3 = strb_01.sgetc();
aa1b2f7d
BV
231 VERIFY( c1 != c2 );
232 VERIFY( c3 == c2 );
233 VERIFY( strb_01.str() == std::string("mykonos. . .zor what?") );
234 VERIFY( str_tmp.size() == strb_01.str().size() );
b2dad0e3
BK
235 //test for _in_cur == _in_beg
236 strb_01.str(str_tmp);
237 strmsz_1 = strb_01.in_avail();
238 c1 = strb_01.sgetc(); //"'m'ykonos. . . or what?"
239 c2 = strb_01.sputbackc('z');//"mykonos. . . or what?"
240 c3 = strb_01.sgetc();
aa1b2f7d
BV
241 VERIFY( c1 != c2 );
242 VERIFY( c3 != c2 );
243 VERIFY( c1 == c3 );
244 VERIFY( c2 == traits_type::eof() );
245 VERIFY( strb_01.str() == str_tmp );
246 VERIFY( str_tmp.size() == strb_01.str().size() );
b2dad0e3
BK
247 // test for replacing char with identical one
248 strb_01.str(str_01); //reset
249 strmsz_1 = strb_01.in_avail();
250 strb_01.sbumpc();
251 strb_01.sbumpc();
252 c1 = strb_01.sgetc(); //"my'k'onos. . . or what?"
253 c2 = strb_01.sputbackc('y');//"mykonos. . . or what?"
254 c3 = strb_01.sgetc();
aa1b2f7d
BV
255 VERIFY( c1 != c2 );
256 VERIFY( c3 == c2 );
257 VERIFY( c1 != c3 );
258 VERIFY( strb_01.str() == str_01 );
259 VERIFY( str_01.size() == strb_01.str().size() );
b2dad0e3
BK
260 //test for ios_base::out
261 strmsz_2 = strb_03.in_avail();
262 c4 = strb_03.sputbackc('x');
aa1b2f7d 263 VERIFY( c4 == traits_type::eof() );
b2dad0e3
BK
264
265 // int_type sungetc()
266 // if in_cur not avail, return pbackfail(), else decrement and
267 // return to_int_type(*gptr())
268 for (int i = 0; i<12; ++i)
269 strb_01.sbumpc();
270 strmsz_1 = strb_01.in_avail();
271 str_tmp = strb_01.str();
272 c1 = strb_01.sgetc(); //"mykonos. . . 'o'r what?"
273 c2 = strb_01.sungetc();//"mykonos. . . or what?"
274 c3 = strb_01.sgetc();
aa1b2f7d
BV
275 VERIFY( c1 != c2 );
276 VERIFY( c3 == c2 );
277 VERIFY( c1 != c3 );
278 VERIFY( c2 == ' ' );
279 VERIFY( strb_01.str() == str_01 );
280 VERIFY( str_01.size() == strb_01.str().size() );
b2dad0e3
BK
281 //test for _in_cur == _in_beg
282 strb_01.str(str_tmp);
283 strmsz_1 = strb_01.in_avail();
284 c1 = strb_01.sgetc(); //"'m'ykonos. . . or what?"
285 c2 = strb_01.sungetc();//"mykonos. . . or what?"
286 c3 = strb_01.sgetc();
aa1b2f7d
BV
287 VERIFY( c1 != c2 );
288 VERIFY( c3 != c2 );
289 VERIFY( c1 == c3 );
290 VERIFY( c2 == traits_type::eof() );
291 VERIFY( strb_01.str() == str_01 );
292 VERIFY( str_01.size() == strb_01.str().size() );
b2dad0e3
BK
293 // test for replacing char with identical one
294 strb_01.str(str_01); //reset
295 strmsz_1 = strb_01.in_avail();
296 strb_01.sbumpc();
297 strb_01.sbumpc();
298 c1 = strb_01.sgetc(); //"my'k'onos. . . or what?"
299 c2 = strb_01.sungetc();//"mykonos. . . or what?"
300 c3 = strb_01.sgetc();
aa1b2f7d
BV
301 VERIFY( c1 != c2 );
302 VERIFY( c3 == c2 );
303 VERIFY( c1 != c3 );
304 VERIFY( strb_01.str() == str_01 );
305 VERIFY( str_01.size() == strb_01.str().size() );
b2dad0e3
BK
306 //test for ios_base::out
307 strmsz_2 = strb_03.in_avail();
308 c4 = strb_03.sungetc();
aa1b2f7d 309 VERIFY( c4 == traits_type::eof() );
b2dad0e3
BK
310
311 // BUFFER MANAGEMENT & POSITIONING
312 // sync
313 // pubsync
314 strb_01.pubsync();
315 strb_02.pubsync();
316 strb_03.pubsync();
317
318 // setbuf
319 // pubsetbuf(char_type* s, streamsize n)
320 str_tmp = std::string("naaaah, go to cebu");
321 strb_01.pubsetbuf(const_cast<char*> (str_tmp.c_str()), str_tmp.size());
aa1b2f7d 322 VERIFY( strb_01.str() == str_tmp );
b2dad0e3 323 strb_01.pubsetbuf(0,0);
aa1b2f7d 324 VERIFY( strb_01.str() == str_tmp );
b2dad0e3
BK
325
326 // seekoff
327 // pubseekoff(off_type off, ios_base::seekdir way, ios_base::openmode which)
328 // alters the stream position to off
329 pos_type pt_1(off_type(-1));
330 pos_type pt_2(off_type(0));
331 off_type off_1 = 0;
332 off_type off_2 = 0;
333 strb_01.str(str_01); //in|out ("mykonos. . . or what?");
334 strb_02.str(str_02); //in ("paris, or sainte-maxime?");
335 strb_03.str(str_03); //out ("")
336 //IN|OUT
337 //beg
338 pt_1 = strb_01.pubseekoff(2, std::ios_base::beg);
97e0a05a 339 off_1 = pt_1;
aa1b2f7d 340 VERIFY( off_1 >= 0 );
b2dad0e3 341 c1 = strb_01.snextc(); //current in pointer +1
aa1b2f7d 342 VERIFY( c1 == 'o' );
b2dad0e3
BK
343 c2 = strb_01.sputc('x'); //test current out pointer
344 str_tmp = std::string("myxonos. . . or what?");
aa1b2f7d 345 VERIFY( strb_01.str() == str_tmp );
b2dad0e3
BK
346 //cur
347 pt_1 = strb_01.pubseekoff(2, std::ios_base::cur);
97e0a05a 348 off_1 = pt_1;
aa1b2f7d 349 VERIFY( off_1 == -1 ); // can't seekoff for in and out + cur in sstreams
b2dad0e3 350 pt_1 = strb_01.pubseekoff(2, std::ios_base::cur, std::ios_base::in);
97e0a05a 351 off_1 = pt_1;
b2dad0e3 352 pt_2 = strb_01.pubseekoff(2, std::ios_base::cur, std::ios_base::in);
97e0a05a 353 off_2 = pt_2;
aa1b2f7d 354 VERIFY( off_2 == off_1 + 2 );
b2dad0e3 355 c1 = strb_01.snextc(); //current in pointer + 1
aa1b2f7d 356 VERIFY( c1 == ' ' );
b2dad0e3
BK
357 c2 = strb_01.sputc('x'); //test current out pointer
358 str_tmp = std::string("myxxnos. . . or what?");
aa1b2f7d 359 VERIFY( strb_01.str() == str_tmp );
b2dad0e3
BK
360 //end
361 pt_2 = strb_01.pubseekoff(2, std::ios_base::end);
97e0a05a 362 off_1 = pt_2;
aa1b2f7d
BV
363 VERIFY( off_1 == -1 ); // not a valid position
364 VERIFY( strb_01.str() == str_tmp );
b2dad0e3
BK
365 // end part two (from the filebuf tests)
366 strb_01.pubseekoff(0, std::ios_base::end);
367 strmsz_1 = strb_01.in_avail(); // 0 cuz at the end
368 c1 = strb_01.sgetc();
369 c2 = strb_01.sungetc();
370 strmsz_2 = strb_01.in_avail(); // 1
371 c3 = strb_01.sgetc();
aa1b2f7d
BV
372 VERIFY( c1 != c2 );
373 VERIFY( strmsz_2 != strmsz_1 );
374 VERIFY( strmsz_2 == 1 );
b2dad0e3
BK
375 // end part three
376 strmsz_1 = strb_01.str().size();
377 strmsz_2 = strb_01.sputn(" ravi shankar meets carlos santana in LoHa", 90);
378 strb_01.pubseekoff(0, std::ios_base::end);
379 strb_01.sputc('<');
380 str_tmp = strb_01.str();
aa1b2f7d 381 VERIFY( str_tmp.size() == strmsz_1 + strmsz_2 + 1 );
b2dad0e3
BK
382 // IN
383 // OUT
384
385 // seekpos
386 // pubseekpos(pos_type sp, ios_base::openmode)
387 // alters the stream position to sp
388 strb_01.str(str_01); //in|out ("mykonos. . . or what?");
389 strb_02.str(str_02); //in ("paris, or sainte-maxime?");
390 strb_03.str(str_03); //out ("")
391 //IN|OUT
392 //beg
393 pt_1 = strb_01.pubseekoff(2, std::ios_base::beg);
97e0a05a 394 off_1 = pt_1;
aa1b2f7d 395 VERIFY( off_1 >= 0 );
b2dad0e3 396 pt_1 = strb_01.pubseekoff(0, std::ios_base::cur, std::ios_base::out);
97e0a05a 397 off_1 = pt_1;
b2dad0e3 398 c1 = strb_01.snextc(); //current in pointer +1
aa1b2f7d 399 VERIFY( c1 == 'o' );
b2dad0e3
BK
400 c2 = strb_01.sputc('x'); //test current out pointer
401 str_tmp = std::string("myxonos. . . or what?");
aa1b2f7d 402 VERIFY( strb_01.str() == str_tmp );
b2dad0e3
BK
403 strb_01.pubsync(); //resets pointers
404 pt_2 = strb_01.pubseekpos(pt_1, std::ios_base::in|std::ios_base::out);
97e0a05a 405 off_2 = pt_2;
aa1b2f7d 406 VERIFY( off_1 == off_2 );
b2dad0e3 407 c3 = strb_01.snextc(); //current in pointer +1
aa1b2f7d 408 VERIFY( c1 == c3 );
b2dad0e3
BK
409 c2 = strb_01.sputc('x'); //test current out pointer
410 str_tmp = std::string("myxonos. . . or what?");
aa1b2f7d 411 VERIFY( strb_01.str() == str_tmp );
b2dad0e3
BK
412
413 // VIRTUALS (indirectly tested)
414 // underflow
415 // if read position avail, returns *gptr()
416
417 // pbackfail(int_type c)
418 // put c back into input sequence
419
420 // overflow
421 // appends c to output seq
422
423#ifdef DEBUG_ASSERT
424 assert(test);
425#endif
426
427 return test;
428}
429
430
431int main() {
432
433 test01();
434 test02();
435 test03();
436 test04();
437
438 return 0;
439}
440
441
442
443// more candy!!!