660abd1752a20964d58496ea08810df78efacec7
1 // 2001-05-21 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
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)
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.
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,
21 // 27.8.1.4 Overridden virtual functions
25 #include <testsuite_hooks.h>
27 // @require@ %-*.tst %-*.txt
28 // @diff@ %-*.tst %*.txt
36 const char* strlit
= "how to tell a story and other essays: mark twain";
37 const size_t strlitsize
= std::strlen(strlit
);
40 fbuf01
.pubsetbuf(buf
, 512);
41 fbuf01
.sputn(strlit
, strlitsize
);
42 VERIFY( std::strncmp(strlit
, buf
, strlitsize
) != 0 );
51 const char* strlit
= "how to tell a story and other essays: mark twain";
52 const size_t strlitsize
= std::strlen(strlit
);
54 fbuf01
.open("tmp", ios_base::out
);
56 fbuf01
.pubsetbuf(buf
, strlitsize
);
57 fbuf01
.sputn(strlit
, strlitsize
);
58 VERIFY( std::strncmp(strlit
, buf
, strlitsize
) == 0 );
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;
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
78 class derived_filebuf
: public std::filebuf
82 set_size(int_type __size
) { _M_buf_size_opt
= __size
; }
85 derived_filebuf fb_01
; // in
86 derived_filebuf fb_02
; // out
87 derived_filebuf fb_03
; // in | out
90 // Initialize filebufs to be the same size regardless of platform.
93 fb_01
.set_size(buffer_size
);
94 fb_02
.set_size(buffer_size
);
95 fb_03
.set_size(buffer_size
);
99 // Test the filebuf/stringbuf locale settings.
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
108 // Test overloaded virtual functions.
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
;
119 std::streamsize strmsz_1
, strmsz_2
;
120 std::streamoff strmof_1
, strmof_2
;
121 int i
= 0, j
= 0, k
= 0;
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.
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
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();
159 int_type c3
= fb_01
.sbumpc();
160 int_type c4
= fb_02
.sbumpc();
162 VERIFY( c1
== c3
); // fluke, both happen to be '/'
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.
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();
174 VERIFY( c7
== c4
); // both -1
175 int_type c8
= fb_01
.sgetc();
176 int_type c9
= fb_02
.sgetc();
180 VERIFY( c5
== traits_type::eof() );
183 // calls sbumpc and if sbumpc != eof, return sgetc
187 VERIFY( c7
== c9
); // -1
192 VERIFY( c7
== c9
); // -1
195 VERIFY( c5
== traits_type::eof() );
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 );
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.
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() );
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');
242 c1
= fb_02
.sputc('c');
243 c2
= fb_03
.sputc('d');
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() );
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 );
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
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
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();
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
);
307 strmsz_1
= fb_03
.in_avail();
310 c2
= fb_03
.sputbackc('z');
311 strmsz_2
= fb_03
.in_avail();
317 // VERIFY( strmsz_1 == strmsz_2 );
318 // test for replacing char with identical one
323 strmsz_1
= fb_03
.in_avail();
324 c2
= fb_03
.sputbackc('a');
325 strmsz_2
= fb_03
.in_avail();
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');
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
);
350 strmsz_1
= fb_02
.in_avail();
351 c2
= fb_02
.sungetc();
353 strmsz_2
= fb_02
.in_avail();
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
362 c2
= fb_03
.sungetc();
363 strmsz_2
= fb_03
.in_avail(); // 1
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
371 // BUFFER MANAGEMENT & POSITIONING
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();
377 strmsz_2
= fb_01
.in_avail();
378 VERIFY( strmsz_2
== strmsz_1
);
379 strmsz_1
= fb_02
.in_avail();
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();
388 // bd23456789mzuva?@ABCDEFGHIJKLMNOPQRSTUVWXYZracadabras, i wannaz
390 strmsz_2
= fb_03
.in_avail();
391 VERIFY( strmsz_1
== 1 );
392 // VERIFY( strmsz_2 == 1 );
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.
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));
411 // 27filebuf-3.txt = bd23456789:;<=>?...
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();
418 c1
= fb_03
.snextc(); //current in pointer +1
420 c2
= fb_03
.sputc('\n'); //current in pointer +1
428 // 27filebuf-3.txt = bd2\n456789:;<=>?...
429 pt_2
= fb_03
.pubseekoff(2, std::ios_base::cur
);
431 VERIFY( (off_2
== (off_1
+ 2 + 1 + 1)) );
432 c1
= fb_03
.snextc(); //current in pointer +1
434 c2
= fb_03
.sputc('x'); //test current out pointer
435 c3
= fb_03
.sputc('\n');
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
);
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.
461 // pubseekpos(pos_type sp, ios_base::openmode)
462 // alters the stream position to sp
465 pt_1
= fb_03
.pubseekoff(78, std::ios_base::beg
);
468 c1
= fb_03
.snextc(); //current in pointer +1
470 c2
= fb_03
.sputc('\n'); //test current out pointer
472 fb_03
.pubsync(); //resets pointers
473 pt_2
= fb_03
.pubseekpos(pt_1
);
475 VERIFY( off_1
== off_2
);
476 c3
= fb_03
.snextc(); //current in pointer +1
478 pt_1
= fb_03
.pubseekoff(0, std::ios_base::end
);
480 VERIFY( off_1
> off_2
);
481 fb_03
.sputn("\nof the wonderful things he does!!\nok", 37);
487 // VIRTUALS (indirectly tested)
489 // if read position avail, returns *gptr()
491 // pbackfail(int_type c)
492 // put c back into input sequence
495 // appends c to output seq
497 // NB Have to close these suckers. . .
498 // filebuf_type* close()
502 VERIFY( !fb_01
.is_open() );
503 VERIFY( !fb_02
.is_open() );
504 VERIFY( !fb_03
.is_open() );
510 typedef istream::int_type int_type
;
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' );
528 VERIFY( ob
.getloc() == loc
);
530 locale::global(locale("en_US"));
531 VERIFY( ob
.getloc() == loc
);
533 locale
loc_de ("de_DE");
534 locale ret
= ob
.pubimbue(loc_de
);
535 VERIFY( ob
.getloc() == loc_de
);
536 VERIFY( ret
== loc
);
539 VERIFY( ob
.getloc() == loc_de
);
542 class MyTraits
: public std::char_traits
<char>
545 static bool eq(char c1
, char c2
)
549 return std::char_traits
<char>::eq(c1
, c2
);
553 class MyBuf
: public std::basic_streambuf
<char, MyTraits
>
560 std::memset(buffer
, 'X', sizeof(buffer
));
561 std::memset(buffer
+ 2, 'f', 4);
562 setg(buffer
+ 2, buffer
+ 2, buffer
+ 6);
575 // libstdc++/9439, libstdc++/9425
582 fbuf
.open(name_01
, ios_base::in
);
583 filebuf::int_type r
= fbuf
.sputbackc('a');
586 VERIFY( r
== filebuf::traits_type::eof() );
589 class Cvt_to_upper
: public std::codecvt
<char, char, mbstate_t>
591 bool do_always_noconv() const throw()
604 locale
loc(c_loc
, new Cvt_to_upper
);
606 string
str("abcdefghijklmnopqrstuvwxyz");
613 copy(str
.begin(), str
.end(),
614 ostreambuf_iterator
<char>(out
));
620 copy(istreambuf_iterator
<char>(in
),
621 istreambuf_iterator
<char>(),
625 VERIFY( tmp
.size() == str
.size() );
626 VERIFY( tmp
== str
);
631 class Derived_filebuf
: public std::filebuf
634 int_type
overflow(int_type c
)
637 return std::filebuf::overflow(c
);
640 const char_type
* pub_epptr() const
645 const char_type
* pub_pptr() const
651 // libstdc++/9701 (partial)
659 Derived_filebuf dfbuf_01
;
660 dfbuf_01
.open(name_05
, std::ios_base::out
);
663 VERIFY( !over_called
);
664 over_expected
= dfbuf_01
.pub_epptr() == dfbuf_01
.pub_pptr();
667 VERIFY( (!over_expected
&& !over_called
)
668 || (over_expected
&& over_called
) );
672 Derived_filebuf dfbuf_02
;
673 dfbuf_02
.open(name_05
, std::ios_base::out
);
675 dfbuf_02
.sputn("sonne's", 7);
676 VERIFY( !over_called
);
677 over_expected
= dfbuf_02
.pub_epptr() == dfbuf_02
.pub_pptr();
679 dfbuf_02
.sputn(" peak", 5);
680 VERIFY( (!over_expected
&& !over_called
)
681 || (over_expected
&& over_called
) );
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
);
698 filebuf::int_type c
= fbuf
.sbumpc();