660abd1752a20964d58496ea08810df78efacec7
[gcc.git] / libstdc++-v3 / testsuite / 27_io / filebuf_virtuals.cc
1 // 2001-05-21 Benjamin Kosnik <bkoz@redhat.com>
2
3 // Copyright (C) 2001, 2002, 2003 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 // 27.8.1.4 Overridden virtual functions
22
23 #include <fstream>
24 #include <locale>
25 #include <testsuite_hooks.h>
26
27 // @require@ %-*.tst %-*.txt
28 // @diff@ %-*.tst %*.txt
29
30 void test01()
31 {
32 using namespace std;
33
34 bool test = true;
35 char buf[512];
36 const char* strlit = "how to tell a story and other essays: mark twain";
37 const size_t strlitsize = std::strlen(strlit);
38 filebuf fbuf01;
39
40 fbuf01.pubsetbuf(buf, 512);
41 fbuf01.sputn(strlit, strlitsize);
42 VERIFY( std::strncmp(strlit, buf, strlitsize) != 0 );
43 }
44
45 void test02()
46 {
47 using namespace std;
48
49 bool test = true;
50 char buf[512];
51 const char* strlit = "how to tell a story and other essays: mark twain";
52 const size_t strlitsize = std::strlen(strlit);
53 filebuf fbuf01;
54 fbuf01.open("tmp", ios_base::out);
55
56 fbuf01.pubsetbuf(buf, strlitsize);
57 fbuf01.sputn(strlit, strlitsize);
58 VERIFY( std::strncmp(strlit, buf, strlitsize) == 0 );
59 }
60
61
62 // NB: This test assumes that _M_buf_size == 40, and not the usual
63 // buffer_size length of BUFSIZ (8192), so that overflow/underflow can be
64 // simulated a bit more readily.
65 // NRB (Nota Really Bene): setting it to 40 breaks the test, as intended.
66 const int buffer_size = 8192;
67 //const int buffer_size = 40;
68
69 const char carray_01[] = "santa cruz or sandiego?";
70 const char carray_02[] = "memphis, new orleans, and savanah";
71 const char name_01[] = "filebuf_virtuals-1.txt"; // file with data in it
72 const char name_02[] = "filebuf_virtuals-2.txt"; // empty file, need to create
73 const char name_03[] = "filebuf_virtuals-3.txt"; // empty file, need to create
74 const char name_04[] = "filebuf_virtuals-4.txt"; // empty file, need to create
75 const char name_05[] = "filebuf_virtuals-5.txt"; // empty file, need to create
76 const char name_06[] = "filebuf_virtuals-6.txt"; // empty file, need to create
77
78 class derived_filebuf: public std::filebuf
79 {
80 public:
81 void
82 set_size(int_type __size) { _M_buf_size_opt = __size; }
83 };
84
85 derived_filebuf fb_01; // in
86 derived_filebuf fb_02; // out
87 derived_filebuf fb_03; // in | out
88
89
90 // Initialize filebufs to be the same size regardless of platform.
91 void test03()
92 {
93 fb_01.set_size(buffer_size);
94 fb_02.set_size(buffer_size);
95 fb_03.set_size(buffer_size);
96 }
97
98
99 // Test the filebuf/stringbuf locale settings.
100 void test04()
101 {
102 std::locale loc_tmp;
103 loc_tmp = fb_01.getloc();
104 fb_01.pubimbue(loc_tmp); //This should initialize _M_init to true
105 fb_01.getloc(); //This should just return _M_locale
106 }
107
108 // Test overloaded virtual functions.
109 void test05()
110 {
111 typedef std::filebuf::int_type int_type;
112 typedef std::filebuf::traits_type traits_type;
113 typedef std::filebuf::pos_type pos_type;
114 typedef std::filebuf::off_type off_type;
115 typedef size_t size_type;
116
117 bool test = true;
118 std::filebuf f_tmp;
119 std::streamsize strmsz_1, strmsz_2;
120 std::streamoff strmof_1, strmof_2;
121 int i = 0, j = 0, k = 0;
122
123 // GET
124 // int showmanyc()
125 // returns an estimate of the numbers of chars in the seq, or -1.
126 // if __retval > 0, then calls to underflow won't return
127 // traits_type::eof() till at least __retval chars.
128 // if __retval == -1, then calls to underflow or uflow will fail.
129 // NB overriding def if it can determine more chars can be read from
130 // the input sequence.
131
132 // int in_avail()
133 // if a read position is available, return _M_in_end - _M_in_cur.
134 // else return showmanyc.
135 strmof_1 = fb_01.in_avail();
136 strmof_2 = fb_02.in_avail();
137 VERIFY( strmof_1 == -1 );
138 VERIFY( strmof_1 == strmof_2 ); //fail because not open
139 strmof_1 = fb_03.in_avail();
140 VERIFY( strmof_1 == strmof_2 );
141 fb_01.open(name_01, std::ios_base::in);
142 fb_02.open(name_02, std::ios_base::out | std::ios_base::trunc);
143 fb_03.open(name_03, std::ios_base::out | std::ios_base::in | std::ios_base::trunc);
144 strmof_1 = fb_01.in_avail();
145 strmof_2 = fb_02.in_avail();
146 VERIFY( strmof_1 != strmof_2 );
147 VERIFY( strmof_1 >= 0 );
148 VERIFY( strmof_2 == -1 ); // empty file
149 strmof_1 = fb_03.in_avail();
150 VERIFY( strmof_1 == 0 ); // empty file
151
152 // int_type sbumpc()
153 // if read_cur not avail returns uflow(), else return *read_cur & increment
154 int_type c1 = fb_01.sbumpc();
155 int_type c2 = fb_02.sbumpc();
156 VERIFY( c1 != c2 );
157 VERIFY( c1 == '/' );
158 VERIFY( c2 == -1 );
159 int_type c3 = fb_01.sbumpc();
160 int_type c4 = fb_02.sbumpc();
161 VERIFY( c3 != c4 );
162 VERIFY( c1 == c3 ); // fluke, both happen to be '/'
163 VERIFY( c2 == c4 );
164 int_type c5 = fb_03.sbumpc();
165 VERIFY( c5 == traits_type::eof() );
166 // XXX should do some kind of test to make sure that internal
167 // buffers point ot the same thing, to check consistancy.
168
169 // int_type sgetc()
170 // if read_cur not avail, return uflow(), else return *read_cur
171 int_type c6 = fb_01.sgetc();
172 int_type c7 = fb_02.sgetc();
173 VERIFY( c6 != c3 );
174 VERIFY( c7 == c4 ); // both -1
175 int_type c8 = fb_01.sgetc();
176 int_type c9 = fb_02.sgetc();
177 VERIFY( c6 == c8 );
178 VERIFY( c7 == c9 );
179 c5 = fb_03.sgetc();
180 VERIFY( c5 == traits_type::eof() );
181
182 // int_type snextc()
183 // calls sbumpc and if sbumpc != eof, return sgetc
184 c6 = fb_01.snextc();
185 c7 = fb_02.snextc();
186 VERIFY( c6 != c8 );
187 VERIFY( c7 == c9 ); // -1
188 VERIFY( c6 == '9' );
189 c6 = fb_01.snextc();
190 c7 = fb_02.snextc();
191 VERIFY( c6 != c8 );
192 VERIFY( c7 == c9 ); // -1
193 VERIFY( c6 == '9' );
194 c5 = fb_03.snextc();
195 VERIFY( c5 == traits_type::eof() );
196
197 // streamsize sgetn(char_type *s, streamsize n)
198 // streamsize xsgetn(char_type *s, streamsize n)
199 // assign up to n chars to s from input sequence, indexing in_cur as
200 // approp and returning the number of chars assigned
201 strmsz_1 = fb_01.in_avail();
202 strmsz_2 = fb_02.in_avail();
203 test = strmsz_1 != strmsz_2;
204 char carray1[13] = "";
205 strmsz_1 = fb_01.sgetn(carray1, 10);
206 char carray2[buffer_size] = "";
207 strmsz_2 = fb_02.sgetn(carray2, 10);
208 VERIFY( strmsz_1 != strmsz_2 );
209 VERIFY( strmsz_1 == 10 );
210 VERIFY( strmsz_2 == 0 );
211 c1 = fb_01.sgetc();
212 c2 = fb_02.sgetc();
213 VERIFY( c1 == '\n' );
214 VERIFY( c7 == c2 ); // n != i
215 strmsz_1 = fb_03.sgetn(carray1, 10);
216 VERIFY( !strmsz_1 ); //zero
217 strmsz_1 = fb_01.in_avail();
218 strmsz_2 = fb_01.sgetn(carray2, strmsz_1 + 5);
219 VERIFY( strmsz_1 == strmsz_2 - 5 );
220 c4 = fb_01.sgetc(); // buffer should have underflowed from above.
221 VERIFY( c4 == 'i' );
222 strmsz_1 = fb_01.in_avail();
223 VERIFY( strmsz_1 > 0 );
224 strmsz_2 = fb_01.sgetn(carray2, strmsz_1 + 5);
225 VERIFY( strmsz_1 == strmsz_2 ); //at the end of the actual file
226 strmsz_1 = fb_02.in_avail();
227 strmsz_2 = fb_02.sgetn(carray2, strmsz_1 + 5);
228 VERIFY( strmsz_1 == -1 );
229 VERIFY( strmsz_2 == 0 );
230 c4 = fb_02.sgetc(); // should be EOF
231 VERIFY( c4 == traits_type::eof() );
232
233 // PUT
234 // int_type sputc(char_type c)
235 // if out_cur not avail, return overflow(traits_type::to_int_type(c))
236 // else, stores c at out_cur,
237 // increments out_cur, and returns c as int_type
238 // strmsz_1 = fb_03.in_avail(); // XXX valid for in|out??
239 c1 = fb_02.sputc('a');
240 c2 = fb_03.sputc('b');
241 VERIFY( c1 != c2 );
242 c1 = fb_02.sputc('c');
243 c2 = fb_03.sputc('d');
244 VERIFY( c1 != c2 );
245 // strmsz_2 = fb_03.in_avail();
246 // VERIFY( strmsz_1 != strmsz_2 );
247 for (int i = 50; i <= 90; ++i)
248 c2 = fb_02.sputc(char(i));
249 // 27filebuf-2.txt == ac23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
250 // fb_02._M_out_cur = '2'
251 strmsz_1 = fb_03.in_avail();
252 for (int i = 50; i <= 90; ++i)
253 c2 = fb_03.sputc(char(i));
254 strmsz_2 = fb_03.in_avail();
255 // VERIFY( strmsz_1 != strmsz_2 );
256 // VERIFY( strmsz_1 > 0 );
257 // VERIFY( strmsz_2 > 0 );
258 // 27filebuf-2.txt == bd23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
259 // fb_02._M_out_cur = '2'
260 c3 = fb_01.sputc('a'); // should be EOF because this is read-only
261 VERIFY( c3 == traits_type::eof() );
262
263 // streamsize sputn(const char_typs* s, streamsize n)
264 // write up to n chars to out_cur from s, returning number assigned
265 // NB *sputn will happily put '\0' into your stream if you give it a chance*
266 strmsz_1 = fb_03.sputn("racadabras", 10);//"abracadabras or what?"
267 VERIFY( strmsz_1 == 10 );
268 strmsz_2 = fb_03.sputn(", i wanna reach out and", 10);
269 VERIFY( strmsz_2 == 10 );
270 VERIFY( strmsz_1 == strmsz_2 );
271 // fb_03._M_out_beg = "YZracadabras, i wanna FGHIJKLMNOPQRSTUVW"
272 // fb_03._M_out_cur = "FGHIJKLMNOPQRSTUVW"
273 strmsz_1 = fb_02.sputn("racadabras", 10);
274 VERIFY( strmsz_1 == 10 );
275 // fb_02._M_out_beg = "YZracadabras<=>?@ABCDEFGHIJKLMNOPQRSTUVW"
276 // fb_02._M_out_cur = "<=>?@ABCDEFGHIJKLMNOPQRSTUVW"
277 strmsz_1 = fb_01.sputn("racadabra", 10);
278 VERIFY( strmsz_1 == 0 );
279
280 // PUTBACK
281 // int_type pbfail(int_type c)
282 // called when gptr() null, gptr() == eback(), or traits::eq(*gptr, c) false
283 // "pending sequence" is:
284 // 1) everything as defined in underflow
285 // 2) + if (traits::eq_int_type(c, traits::eof()), then input
286 // sequence is backed up one char before the pending sequence is
287 // determined.
288 // 3) + if (not 2) then c is prepended. Left unspecified is
289 // whether the input sequence is backedup or modified in any way
290 // returns traits::eof() for failure, unspecified other value for success
291
292 // int_type sputbackc(char_type c)
293 // if in_cur not avail || ! traits::eq(c, gptr() [-1]), return pbfail
294 // otherwise decrements in_cur and returns *gptr()
295 c1 = fb_03.sgetc(); // -1
296 c2 = fb_03.sputbackc('z');
297 strmsz_2 = fb_03.in_avail();
298 c3 = fb_03.sgetc();
299 VERIFY( c3 == c2 );
300 VERIFY( c1 != c3 );
301 VERIFY( 1 == strmsz_2 );
302 //test for _in_cur == _in_beg
303 // fb_03._M_out_beg = "bd23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZracada" etc
304 fb_03.pubseekoff(10, std::ios_base::beg,
305 std::ios_base::in | std::ios_base::out);
306 fb_03.sputc('m');
307 strmsz_1 = fb_03.in_avail();
308 c1 = fb_03.sgetc();
309 fb_03.snextc();
310 c2 = fb_03.sputbackc('z');
311 strmsz_2 = fb_03.in_avail();
312 c3 = fb_03.sgetc();
313 VERIFY( c1 != c2 );
314 VERIFY( c3 == c2 );
315 VERIFY( c1 != c3 );
316 VERIFY( c2 == 'z' );
317 // VERIFY( strmsz_1 == strmsz_2 );
318 // test for replacing char with identical one
319 fb_03.snextc();
320 fb_03.sputc('u');
321 fb_03.sputc('v');
322 fb_03.sputc('a');
323 strmsz_1 = fb_03.in_avail();
324 c2 = fb_03.sputbackc('a');
325 strmsz_2 = fb_03.in_avail();
326 c3 = fb_03.sgetc();
327 VERIFY( c3 == c2 );
328 VERIFY( strmsz_1 + 1 == strmsz_2 );
329 //test for ios_base::out
330 c1 = fb_02.sgetc(); // undefined
331 c2 = fb_02.sputbackc('a');
332 VERIFY( c1 == c2 );
333 VERIFY( c1 == -1 );
334
335 // int_type sungetc()
336 // if in_cur not avail, return pbackfail(), else decrement and
337 // return to_int_type(*gptr())
338 // fb_03._M_out_beg = "uvaacadabras, i wannaZ[\\]^_`abcdefghijkl"
339 // fb_03._M_out_cur = "aacadabras, i wannaZ[\\]^_`abcdefghijkl"
340 strmsz_1 = fb_03.in_avail();
341 c2 = fb_03.sungetc(); // delete the 'a'
342 strmsz_2 = fb_03.in_avail();
343 VERIFY( c2 == 'v' ); // VERIFY( c2 != traits_type::eof() );
344 VERIFY( strmsz_1 + 1 == strmsz_2 );
345 //test for _in_cur == _in_beg
346 for (int i = 50; i < 32 + 29; ++i)
347 fb_02.sputc(char(i));
348 fb_02.pubseekoff(0, std::ios_base::beg, std::ios_base::out);
349 c1 = fb_02.sgetc();
350 strmsz_1 = fb_02.in_avail();
351 c2 = fb_02.sungetc();
352 c3 = fb_02.sgetc();
353 strmsz_2 = fb_02.in_avail();
354 VERIFY( c1 == c2 );
355 VERIFY( c2 == c3 );
356 VERIFY( c1 == traits_type::eof() );
357 VERIFY( strmsz_1 == strmsz_2 );
358 //test for _in_cur == _in_end
359 fb_03.pubseekoff(0, std::ios_base::end);
360 strmsz_1 = fb_03.in_avail(); // -1 cuz at the end
361 c1 = fb_03.sgetc();
362 c2 = fb_03.sungetc();
363 strmsz_2 = fb_03.in_avail(); // 1
364 c3 = fb_03.sgetc();
365 VERIFY( c1 != c2 );
366 // VERIFY( c2 == c3 || c2 == traits_type::not_eof(int(c3)) );
367 VERIFY( strmsz_2 != strmsz_1 );
368 VERIFY( strmsz_2 == 1 );
369 //test for ios_base::out
370
371 // BUFFER MANAGEMENT & POSITIONING
372 // int sync()
373 // if a put area exists, overflow.
374 // if a get area exists, do something undefined. (like, nothing)
375 strmsz_1 = fb_01.in_avail();
376 fb_01.pubsync();
377 strmsz_2 = fb_01.in_avail();
378 VERIFY( strmsz_2 == strmsz_1 );
379 strmsz_1 = fb_02.in_avail();
380 fb_02.pubsync();
381 // 27filebuf-2.txt == 53 bytes after this.
382 strmsz_2 = fb_02.in_avail();
383 VERIFY( strmsz_2 == -1 );
384 VERIFY( strmsz_2 == strmsz_1 );
385 strmsz_1 = fb_03.in_avail();
386 fb_03.pubsync();
387 // 27filebuf-3.txt
388 // bd23456789mzuva?@ABCDEFGHIJKLMNOPQRSTUVWXYZracadabras, i wannaz
389 // 63 bytes.
390 strmsz_2 = fb_03.in_avail();
391 VERIFY( strmsz_1 == 1 );
392 // VERIFY( strmsz_2 == 1 );
393
394 // setbuf
395 // pubsetbuf(char_type* s, streamsize n)
396 fb_01.pubsetbuf(0,0);
397 fb_02.pubsetbuf(0,0);
398 fb_03.pubsetbuf(0,0);
399 // Need to test unbuffered output, which means calling this on some
400 // things that have just been opened.
401
402
403 // seekoff
404 // pubseekoff(off_type off, ios_base::seekdir way, ios_base::openmode which)
405 // alters the stream position to off
406 pos_type pt_1(off_type(-1));
407 pos_type pt_2(off_type(0));
408 off_type off_1 = 0;
409 off_type off_2 = 0;
410 //IN|OUT
411 // 27filebuf-3.txt = bd23456789:;<=>?...
412 //beg
413 strmsz_1 = fb_03.in_avail();
414 pt_1 = fb_03.pubseekoff(2, std::ios_base::beg);
415 strmsz_2 = fb_03.in_avail();
416 off_1 = pt_1;
417 VERIFY( off_1 > 0 );
418 c1 = fb_03.snextc(); //current in pointer +1
419 VERIFY( c1 == '3' );
420 c2 = fb_03.sputc('\n'); //current in pointer +1
421 c3 = fb_03.sgetc();
422 VERIFY( c2 != c3 );
423 VERIFY( c3 == '4' );
424 fb_03.pubsync();
425 c1 = fb_03.sgetc();
426 VERIFY( c1 == c3 );
427 //cur
428 // 27filebuf-3.txt = bd2\n456789:;<=>?...
429 pt_2 = fb_03.pubseekoff(2, std::ios_base::cur);
430 off_2 = pt_2;
431 VERIFY( (off_2 == (off_1 + 2 + 1 + 1)) );
432 c1 = fb_03.snextc(); //current in pointer +1
433 VERIFY( c1 == '7' );
434 c2 = fb_03.sputc('x'); //test current out pointer
435 c3 = fb_03.sputc('\n');
436 c1 = fb_03.sgetc();
437 fb_03.pubsync();
438 c3 = fb_03.sgetc();
439 VERIFY( c1 == c3 );
440 //end
441 // 27filebuf-3.txt = "bd2\n456x\n9"
442 pt_2 = fb_03.pubseekoff(0, std::ios_base::end,
443 std::ios_base::in|std::ios_base::out);
444 off_1 = pt_2;
445 VERIFY( off_1 > off_2 ); //weak, but don't know exactly where it ends
446 c3 = fb_03.sputc('\n');
447 strmsz_1 = fb_03.sputn("because because because. . .", 28);
448 VERIFY( strmsz_1 == 28 );
449 c1 = fb_03.sungetc();
450 // Defect? retval of sungetc is not necessarily the character ungotten.
451 // So re-get it.
452 c1 = fb_03.sgetc();
453 fb_03.pubsync();
454 c3 = fb_03.sgetc();
455 VERIFY( c1 == c3 );
456 // IN
457 // OUT
458
459
460 // seekpos
461 // pubseekpos(pos_type sp, ios_base::openmode)
462 // alters the stream position to sp
463 //IN|OUT
464 //beg
465 pt_1 = fb_03.pubseekoff(78, std::ios_base::beg);
466 off_1 = pt_1;
467 VERIFY( off_1 > 0 );
468 c1 = fb_03.snextc(); //current in pointer +1
469 VERIFY( c1 == ' ' );
470 c2 = fb_03.sputc('\n'); //test current out pointer
471 c3 = fb_03.sgetc();
472 fb_03.pubsync(); //resets pointers
473 pt_2 = fb_03.pubseekpos(pt_1);
474 off_2 = pt_2;
475 VERIFY( off_1 == off_2 );
476 c3 = fb_03.snextc(); //current in pointer +1
477 VERIFY( c2 == c3 );
478 pt_1 = fb_03.pubseekoff(0, std::ios_base::end);
479 off_1 = pt_1;
480 VERIFY( off_1 > off_2 );
481 fb_03.sputn("\nof the wonderful things he does!!\nok", 37);
482 fb_03.pubsync();
483
484 // IN
485 // OUT
486
487 // VIRTUALS (indirectly tested)
488 // underflow
489 // if read position avail, returns *gptr()
490
491 // pbackfail(int_type c)
492 // put c back into input sequence
493
494 // overflow
495 // appends c to output seq
496
497 // NB Have to close these suckers. . .
498 // filebuf_type* close()
499 fb_01.close();
500 fb_02.close();
501 fb_03.close();
502 VERIFY( !fb_01.is_open() );
503 VERIFY( !fb_02.is_open() );
504 VERIFY( !fb_03.is_open() );
505 }
506
507 void test06()
508 {
509 using namespace std;
510 typedef istream::int_type int_type;
511
512 bool test = true;
513 ifstream ifs(name_02);
514 char buffer[] = "xxxxxxxxxx";
515 int_type len1 = ifs.rdbuf()->sgetn(buffer, sizeof(buffer));
516 VERIFY( len1 == sizeof(buffer) );
517 VERIFY( buffer[0] == 'a' );
518 }
519
520 // libstdc++/9322
521 void test07()
522 {
523 using std::locale;
524 bool test = true;
525
526 locale loc;
527 std::filebuf ob;
528 VERIFY( ob.getloc() == loc );
529
530 locale::global(locale("en_US"));
531 VERIFY( ob.getloc() == loc );
532
533 locale loc_de ("de_DE");
534 locale ret = ob.pubimbue(loc_de);
535 VERIFY( ob.getloc() == loc_de );
536 VERIFY( ret == loc );
537
538 locale::global(loc);
539 VERIFY( ob.getloc() == loc_de );
540 }
541
542 class MyTraits : public std::char_traits<char>
543 {
544 public:
545 static bool eq(char c1, char c2)
546 {
547 VERIFY( c1 != 'X' );
548 VERIFY( c2 != 'X' );
549 return std::char_traits<char>::eq(c1, c2);
550 }
551 };
552
553 class MyBuf : public std::basic_streambuf<char, MyTraits>
554 {
555 char buffer[8];
556
557 public:
558 MyBuf()
559 {
560 std::memset(buffer, 'X', sizeof(buffer));
561 std::memset(buffer + 2, 'f', 4);
562 setg(buffer + 2, buffer + 2, buffer + 6);
563 }
564 };
565
566 // libstdc++/9538
567 void test08()
568 {
569 bool test = true;
570
571 MyBuf mb;
572 mb.sputbackc('a');
573 }
574
575 // libstdc++/9439, libstdc++/9425
576 void test09()
577 {
578 using namespace std;
579 bool test = true;
580
581 filebuf fbuf;
582 fbuf.open(name_01, ios_base::in);
583 filebuf::int_type r = fbuf.sputbackc('a');
584 fbuf.close();
585
586 VERIFY( r == filebuf::traits_type::eof() );
587 }
588
589 class Cvt_to_upper : public std::codecvt<char, char, mbstate_t>
590 {
591 bool do_always_noconv() const throw()
592 {
593 return false;
594 }
595 };
596
597 // libstdc++/9169
598 void test10()
599 {
600 using namespace std;
601 bool test = true;
602
603 locale c_loc;
604 locale loc(c_loc, new Cvt_to_upper);
605
606 string str("abcdefghijklmnopqrstuvwxyz");
607 string tmp;
608
609 {
610 ofstream out;
611 out.imbue(loc);
612 out.open(name_04);
613 copy(str.begin(), str.end(),
614 ostreambuf_iterator<char>(out));
615 }
616
617 {
618 ifstream in;
619 in.open(name_04);
620 copy(istreambuf_iterator<char>(in),
621 istreambuf_iterator<char>(),
622 back_inserter(tmp));
623 }
624
625 VERIFY( tmp.size() == str.size() );
626 VERIFY( tmp == str );
627 }
628
629 bool over_called;
630
631 class Derived_filebuf : public std::filebuf
632 {
633 public:
634 int_type overflow(int_type c)
635 {
636 over_called = true;
637 return std::filebuf::overflow(c);
638 }
639
640 const char_type* pub_epptr() const
641 {
642 return epptr();
643 }
644
645 const char_type* pub_pptr() const
646 {
647 return pptr();
648 }
649 };
650
651 // libstdc++/9701 (partial)
652 void test11()
653 {
654 bool test = true;
655
656 bool over_expected;
657
658 // sputc
659 Derived_filebuf dfbuf_01;
660 dfbuf_01.open(name_05, std::ios_base::out);
661 over_called = false;
662 dfbuf_01.sputc('i');
663 VERIFY( !over_called );
664 over_expected = dfbuf_01.pub_epptr() == dfbuf_01.pub_pptr();
665 over_called = false;
666 dfbuf_01.sputc('v');
667 VERIFY( (!over_expected && !over_called)
668 || (over_expected && over_called) );
669 dfbuf_01.close();
670
671 // sputn
672 Derived_filebuf dfbuf_02;
673 dfbuf_02.open(name_05, std::ios_base::out);
674 over_called = false;
675 dfbuf_02.sputn("sonne's", 7);
676 VERIFY( !over_called );
677 over_expected = dfbuf_02.pub_epptr() == dfbuf_02.pub_pptr();
678 over_called = false;
679 dfbuf_02.sputn(" peak", 5);
680 VERIFY( (!over_expected && !over_called)
681 || (over_expected && over_called) );
682 dfbuf_02.close();
683 }
684
685 // libstdc++/9825
686 void test12()
687 {
688 using namespace std;
689 bool test = true;
690
691 filebuf fbuf;
692
693 fbuf.open(name_06, ios_base::in|ios_base::out|ios_base::trunc);
694 fbuf.sputn("crazy bees!", 11);
695 fbuf.pubseekoff(0, ios_base::beg);
696 fbuf.sbumpc();
697 fbuf.sputbackc('x');
698 filebuf::int_type c = fbuf.sbumpc();
699 VERIFY( c == 'x' );
700 c = fbuf.sbumpc();
701 VERIFY( c == 'r' );
702 c = fbuf.sbumpc();
703 VERIFY( c == 'a' );
704 fbuf.close();
705 }
706
707 main()
708 {
709 test01();
710 test02();
711
712 test03();
713 test04();
714 test05();
715 test06();
716
717 test07();
718 test08();
719 test09();
720 test10();
721 test11();
722 test12();
723 return 0;
724 }