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
77 const char name_07[] = "filebuf_virtuals-7.txt"; // empty file, need to create
79 class derived_filebuf: public std::filebuf
83 set_size(int_type __size) { _M_buf_size_opt = __size; }
86 derived_filebuf fb_01; // in
87 derived_filebuf fb_02; // out
88 derived_filebuf fb_03; // in | out
91 // Initialize filebufs to be the same size regardless of platform.
94 fb_01.set_size(buffer_size);
95 fb_02.set_size(buffer_size);
96 fb_03.set_size(buffer_size);
100 // Test the filebuf/stringbuf locale settings.
104 loc_tmp = fb_01.getloc();
105 fb_01.pubimbue(loc_tmp); //This should initialize _M_init to true
106 fb_01.getloc(); //This should just return _M_locale
109 // Test overloaded virtual functions.
112 typedef std::filebuf::int_type int_type;
113 typedef std::filebuf::traits_type traits_type;
114 typedef std::filebuf::pos_type pos_type;
115 typedef std::filebuf::off_type off_type;
116 typedef size_t size_type;
120 std::streamsize strmsz_1, strmsz_2;
121 std::streamoff strmof_1, strmof_2;
122 int i = 0, j = 0, k = 0;
126 // returns an estimate of the numbers of chars in the seq, or -1.
127 // if __retval > 0, then calls to underflow won't return
128 // traits_type::eof() till at least __retval chars.
129 // if __retval == -1, then calls to underflow or uflow will fail.
130 // NB overriding def if it can determine more chars can be read from
131 // the input sequence.
134 // if a read position is available, return _M_in_end - _M_in_cur.
135 // else return showmanyc.
136 strmof_1 = fb_01.in_avail();
137 strmof_2 = fb_02.in_avail();
138 VERIFY( strmof_1 == -1 );
139 VERIFY( strmof_1 == strmof_2 ); //fail because not open
140 strmof_1 = fb_03.in_avail();
141 VERIFY( strmof_1 == strmof_2 );
142 fb_01.open(name_01, std::ios_base::in);
143 fb_02.open(name_02, std::ios_base::out | std::ios_base::trunc);
144 fb_03.open(name_03, std::ios_base::out | std::ios_base::in | std::ios_base::trunc);
145 strmof_1 = fb_01.in_avail();
146 strmof_2 = fb_02.in_avail();
147 VERIFY( strmof_1 != strmof_2 );
148 VERIFY( strmof_1 >= 0 );
149 VERIFY( strmof_2 == -1 ); // empty file
150 strmof_1 = fb_03.in_avail();
151 VERIFY( strmof_1 == 0 ); // empty file
154 // if read_cur not avail returns uflow(), else return *read_cur & increment
155 int_type c1 = fb_01.sbumpc();
156 int_type c2 = fb_02.sbumpc();
160 int_type c3 = fb_01.sbumpc();
161 int_type c4 = fb_02.sbumpc();
163 VERIFY( c1 == c3 ); // fluke, both happen to be '/'
165 int_type c5 = fb_03.sbumpc();
166 VERIFY( c5 == traits_type::eof() );
167 // XXX should do some kind of test to make sure that internal
168 // buffers point ot the same thing, to check consistancy.
171 // if read_cur not avail, return uflow(), else return *read_cur
172 int_type c6 = fb_01.sgetc();
173 int_type c7 = fb_02.sgetc();
175 VERIFY( c7 == c4 ); // both -1
176 int_type c8 = fb_01.sgetc();
177 int_type c9 = fb_02.sgetc();
181 VERIFY( c5 == traits_type::eof() );
184 // calls sbumpc and if sbumpc != eof, return sgetc
188 VERIFY( c7 == c9 ); // -1
193 VERIFY( c7 == c9 ); // -1
196 VERIFY( c5 == traits_type::eof() );
198 // streamsize sgetn(char_type *s, streamsize n)
199 // streamsize xsgetn(char_type *s, streamsize n)
200 // assign up to n chars to s from input sequence, indexing in_cur as
201 // approp and returning the number of chars assigned
202 strmsz_1 = fb_01.in_avail();
203 strmsz_2 = fb_02.in_avail();
204 test = strmsz_1 != strmsz_2;
205 char carray1[13] = "";
206 strmsz_1 = fb_01.sgetn(carray1, 10);
207 char carray2[buffer_size] = "";
208 strmsz_2 = fb_02.sgetn(carray2, 10);
209 VERIFY( strmsz_1 != strmsz_2 );
210 VERIFY( strmsz_1 == 10 );
211 VERIFY( strmsz_2 == 0 );
214 VERIFY( c1 == '\n' );
215 VERIFY( c7 == c2 ); // n != i
216 strmsz_1 = fb_03.sgetn(carray1, 10);
217 VERIFY( !strmsz_1 ); //zero
218 strmsz_1 = fb_01.in_avail();
219 strmsz_2 = fb_01.sgetn(carray2, strmsz_1 + 5);
220 VERIFY( strmsz_1 == strmsz_2 - 5 );
221 c4 = fb_01.sgetc(); // buffer should have underflowed from above.
223 strmsz_1 = fb_01.in_avail();
224 VERIFY( strmsz_1 > 0 );
225 strmsz_2 = fb_01.sgetn(carray2, strmsz_1 + 5);
226 VERIFY( strmsz_1 == strmsz_2 ); //at the end of the actual file
227 strmsz_1 = fb_02.in_avail();
228 strmsz_2 = fb_02.sgetn(carray2, strmsz_1 + 5);
229 VERIFY( strmsz_1 == -1 );
230 VERIFY( strmsz_2 == 0 );
231 c4 = fb_02.sgetc(); // should be EOF
232 VERIFY( c4 == traits_type::eof() );
235 // int_type sputc(char_type c)
236 // if out_cur not avail, return overflow(traits_type::to_int_type(c))
237 // else, stores c at out_cur,
238 // increments out_cur, and returns c as int_type
239 // strmsz_1 = fb_03.in_avail(); // XXX valid for in|out??
240 c1 = fb_02.sputc('a');
241 c2 = fb_03.sputc('b');
243 c1 = fb_02.sputc('c');
244 c2 = fb_03.sputc('d');
246 // strmsz_2 = fb_03.in_avail();
247 // VERIFY( strmsz_1 != strmsz_2 );
248 for (int i = 50; i <= 90; ++i)
249 c2 = fb_02.sputc(char(i));
250 // 27filebuf-2.txt == ac23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
251 // fb_02._M_out_cur = '2'
252 strmsz_1 = fb_03.in_avail();
253 for (int i = 50; i <= 90; ++i)
254 c2 = fb_03.sputc(char(i));
255 strmsz_2 = fb_03.in_avail();
256 // VERIFY( strmsz_1 != strmsz_2 );
257 // VERIFY( strmsz_1 > 0 );
258 // VERIFY( strmsz_2 > 0 );
259 // 27filebuf-2.txt == bd23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
260 // fb_02._M_out_cur = '2'
261 c3 = fb_01.sputc('a'); // should be EOF because this is read-only
262 VERIFY( c3 == traits_type::eof() );
264 // streamsize sputn(const char_typs* s, streamsize n)
265 // write up to n chars to out_cur from s, returning number assigned
266 // NB *sputn will happily put '\0' into your stream if you give it a chance*
267 strmsz_1 = fb_03.sputn("racadabras", 10);//"abracadabras or what?"
268 VERIFY( strmsz_1 == 10 );
269 strmsz_2 = fb_03.sputn(", i wanna reach out and", 10);
270 VERIFY( strmsz_2 == 10 );
271 VERIFY( strmsz_1 == strmsz_2 );
272 // fb_03._M_out_beg = "YZracadabras, i wanna FGHIJKLMNOPQRSTUVW"
273 // fb_03._M_out_cur = "FGHIJKLMNOPQRSTUVW"
274 strmsz_1 = fb_02.sputn("racadabras", 10);
275 VERIFY( strmsz_1 == 10 );
276 // fb_02._M_out_beg = "YZracadabras<=>?@ABCDEFGHIJKLMNOPQRSTUVW"
277 // fb_02._M_out_cur = "<=>?@ABCDEFGHIJKLMNOPQRSTUVW"
278 strmsz_1 = fb_01.sputn("racadabra", 10);
279 VERIFY( strmsz_1 == 0 );
282 // int_type pbfail(int_type c)
283 // called when gptr() null, gptr() == eback(), or traits::eq(*gptr, c) false
284 // "pending sequence" is:
285 // 1) everything as defined in underflow
286 // 2) + if (traits::eq_int_type(c, traits::eof()), then input
287 // sequence is backed up one char before the pending sequence is
289 // 3) + if (not 2) then c is prepended. Left unspecified is
290 // whether the input sequence is backedup or modified in any way
291 // returns traits::eof() for failure, unspecified other value for success
293 // int_type sputbackc(char_type c)
294 // if in_cur not avail || ! traits::eq(c, gptr() [-1]), return pbfail
295 // otherwise decrements in_cur and returns *gptr()
296 c1 = fb_03.sgetc(); // -1
297 c2 = fb_03.sputbackc('z');
298 strmsz_2 = fb_03.in_avail();
302 VERIFY( 1 == strmsz_2 );
303 //test for _in_cur == _in_beg
304 // fb_03._M_out_beg = "bd23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZracada" etc
305 fb_03.pubseekoff(10, std::ios_base::beg,
306 std::ios_base::in | std::ios_base::out);
308 strmsz_1 = fb_03.in_avail();
311 c2 = fb_03.sputbackc('z');
312 strmsz_2 = fb_03.in_avail();
318 // VERIFY( strmsz_1 == strmsz_2 );
319 // test for replacing char with identical one
324 strmsz_1 = fb_03.in_avail();
325 c2 = fb_03.sputbackc('a');
326 strmsz_2 = fb_03.in_avail();
329 VERIFY( strmsz_1 + 1 == strmsz_2 );
330 //test for ios_base::out
331 c1 = fb_02.sgetc(); // undefined
332 c2 = fb_02.sputbackc('a');
336 // int_type sungetc()
337 // if in_cur not avail, return pbackfail(), else decrement and
338 // return to_int_type(*gptr())
339 // fb_03._M_out_beg = "uvaacadabras, i wannaZ[\\]^_`abcdefghijkl"
340 // fb_03._M_out_cur = "aacadabras, i wannaZ[\\]^_`abcdefghijkl"
341 strmsz_1 = fb_03.in_avail();
342 c2 = fb_03.sungetc(); // delete the 'a'
343 strmsz_2 = fb_03.in_avail();
344 VERIFY( c2 == 'v' ); // VERIFY( c2 != traits_type::eof() );
345 VERIFY( strmsz_1 + 1 == strmsz_2 );
346 //test for _in_cur == _in_beg
347 for (int i = 50; i < 32 + 29; ++i)
348 fb_02.sputc(char(i));
349 fb_02.pubseekoff(0, std::ios_base::beg, std::ios_base::out);
351 strmsz_1 = fb_02.in_avail();
352 c2 = fb_02.sungetc();
354 strmsz_2 = fb_02.in_avail();
357 VERIFY( c1 == traits_type::eof() );
358 VERIFY( strmsz_1 == strmsz_2 );
359 //test for _in_cur == _in_end
360 fb_03.pubseekoff(0, std::ios_base::end);
361 strmsz_1 = fb_03.in_avail(); // -1 cuz at the end
363 c2 = fb_03.sungetc();
364 strmsz_2 = fb_03.in_avail(); // 1
367 // VERIFY( c2 == c3 || c2 == traits_type::not_eof(int(c3)) );
368 VERIFY( strmsz_2 != strmsz_1 );
369 VERIFY( strmsz_2 == 1 );
370 //test for ios_base::out
372 // BUFFER MANAGEMENT & POSITIONING
374 // if a put area exists, overflow.
375 // if a get area exists, do something undefined. (like, nothing)
376 strmsz_1 = fb_01.in_avail();
378 strmsz_2 = fb_01.in_avail();
379 VERIFY( strmsz_2 == strmsz_1 );
380 strmsz_1 = fb_02.in_avail();
382 // 27filebuf-2.txt == 53 bytes after this.
383 strmsz_2 = fb_02.in_avail();
384 VERIFY( strmsz_2 == -1 );
385 VERIFY( strmsz_2 == strmsz_1 );
386 strmsz_1 = fb_03.in_avail();
389 // bd23456789mzuva?@ABCDEFGHIJKLMNOPQRSTUVWXYZracadabras, i wannaz
391 strmsz_2 = fb_03.in_avail();
392 VERIFY( strmsz_1 == 1 );
393 // VERIFY( strmsz_2 == 1 );
396 // pubsetbuf(char_type* s, streamsize n)
397 fb_01.pubsetbuf(0,0);
398 fb_02.pubsetbuf(0,0);
399 fb_03.pubsetbuf(0,0);
400 // Need to test unbuffered output, which means calling this on some
401 // things that have just been opened.
405 // pubseekoff(off_type off, ios_base::seekdir way, ios_base::openmode which)
406 // alters the stream position to off
407 pos_type pt_1(off_type(-1));
408 pos_type pt_2(off_type(0));
412 // 27filebuf-3.txt = bd23456789:;<=>?...
414 strmsz_1 = fb_03.in_avail();
415 pt_1 = fb_03.pubseekoff(2, std::ios_base::beg);
416 strmsz_2 = fb_03.in_avail();
419 c1 = fb_03.snextc(); //current in pointer +1
421 c2 = fb_03.sputc('\n'); //current in pointer +1
429 // 27filebuf-3.txt = bd2\n456789:;<=>?...
430 pt_2 = fb_03.pubseekoff(2, std::ios_base::cur);
432 VERIFY( (off_2 == (off_1 + 2 + 1 + 1)) );
433 c1 = fb_03.snextc(); //current in pointer +1
435 c2 = fb_03.sputc('x'); //test current out pointer
436 c3 = fb_03.sputc('\n');
442 // 27filebuf-3.txt = "bd2\n456x\n9"
443 pt_2 = fb_03.pubseekoff(0, std::ios_base::end,
444 std::ios_base::in|std::ios_base::out);
446 VERIFY( off_1 > off_2 ); //weak, but don't know exactly where it ends
447 c3 = fb_03.sputc('\n');
448 strmsz_1 = fb_03.sputn("because because because. . .", 28);
449 VERIFY( strmsz_1 == 28 );
450 c1 = fb_03.sungetc();
451 // Defect? retval of sungetc is not necessarily the character ungotten.
462 // pubseekpos(pos_type sp, ios_base::openmode)
463 // alters the stream position to sp
466 pt_1 = fb_03.pubseekoff(78, std::ios_base::beg);
469 c1 = fb_03.snextc(); //current in pointer +1
471 c2 = fb_03.sputc('\n'); //test current out pointer
473 fb_03.pubsync(); //resets pointers
474 pt_2 = fb_03.pubseekpos(pt_1);
476 VERIFY( off_1 == off_2 );
477 c3 = fb_03.snextc(); //current in pointer +1
479 pt_1 = fb_03.pubseekoff(0, std::ios_base::end);
481 VERIFY( off_1 > off_2 );
482 fb_03.sputn("\nof the wonderful things he does!!\nok", 37);
488 // VIRTUALS (indirectly tested)
490 // if read position avail, returns *gptr()
492 // pbackfail(int_type c)
493 // put c back into input sequence
496 // appends c to output seq
498 // NB Have to close these suckers. . .
499 // filebuf_type* close()
503 VERIFY( !fb_01.is_open() );
504 VERIFY( !fb_02.is_open() );
505 VERIFY( !fb_03.is_open() );
511 typedef istream::int_type int_type;
514 ifstream ifs(name_02);
515 char buffer[] = "xxxxxxxxxx";
516 int_type len1 = ifs.rdbuf()->sgetn(buffer, sizeof(buffer));
517 VERIFY( len1 == sizeof(buffer) );
518 VERIFY( buffer[0] == 'a' );
529 VERIFY( ob.getloc() == loc );
531 locale::global(locale("en_US"));
532 VERIFY( ob.getloc() == loc );
534 locale loc_de ("de_DE");
535 locale ret = ob.pubimbue(loc_de);
536 VERIFY( ob.getloc() == loc_de );
537 VERIFY( ret == loc );
540 VERIFY( ob.getloc() == loc_de );
543 class MyTraits : public std::char_traits<char>
546 static bool eq(char c1, char c2)
550 return std::char_traits<char>::eq(c1, c2);
554 class MyBuf : public std::basic_streambuf<char, MyTraits>
561 std::memset(buffer, 'X', sizeof(buffer));
562 std::memset(buffer + 2, 'f', 4);
563 setg(buffer + 2, buffer + 2, buffer + 6);
576 // libstdc++/9439, libstdc++/9425
583 fbuf.open(name_01, ios_base::in);
584 filebuf::int_type r = fbuf.sputbackc('a');
587 VERIFY( r == filebuf::traits_type::eof() );
590 class Cvt_to_upper : public std::codecvt<char, char, mbstate_t>
592 bool do_always_noconv() const throw()
605 locale loc(c_loc, new Cvt_to_upper);
607 string str("abcdefghijklmnopqrstuvwxyz");
614 copy(str.begin(), str.end(),
615 ostreambuf_iterator<char>(out));
621 copy(istreambuf_iterator<char>(in),
622 istreambuf_iterator<char>(),
626 VERIFY( tmp.size() == str.size() );
627 VERIFY( tmp == str );
632 class Derived_filebuf : public std::filebuf
635 int_type overflow(int_type c)
638 return std::filebuf::overflow(c);
641 const char_type* pub_epptr() const
646 const char_type* pub_pptr() const
652 // libstdc++/9701 (partial)
660 Derived_filebuf dfbuf_01;
661 dfbuf_01.open(name_05, std::ios_base::out);
664 VERIFY( !over_called );
665 over_expected = dfbuf_01.pub_epptr() == dfbuf_01.pub_pptr();
668 VERIFY( (!over_expected && !over_called)
669 || (over_expected && over_called) );
673 Derived_filebuf dfbuf_02;
674 dfbuf_02.open(name_05, std::ios_base::out);
676 dfbuf_02.sputn("sonne's", 7);
677 VERIFY( !over_called );
678 over_expected = dfbuf_02.pub_epptr() == dfbuf_02.pub_pptr();
680 dfbuf_02.sputn(" peak", 5);
681 VERIFY( (!over_expected && !over_called)
682 || (over_expected && over_called) );
694 fbuf.open(name_06, ios_base::in|ios_base::out|ios_base::trunc);
695 fbuf.sputn("crazy bees!", 11);
696 fbuf.pubseekoff(0, ios_base::beg);
699 filebuf::int_type c = fbuf.sbumpc();
708 class errorcvt : public std::codecvt<char, char, mbstate_t>
711 std::codecvt_base::result
712 do_out(mbstate_t&, const char* from, const char*,
713 const char*& from_next, char* to, char*,
714 char*& to_next) const
718 return std::codecvt<char, char, mbstate_t>::error;
721 virtual bool do_always_noconv() const throw()
734 loc = locale(loc, new errorcvt);
738 fbuf1.open(name_07, ios_base::out | ios_base::trunc);
739 fbuf1.sputn("ison", 4);
740 int r = fbuf1.pubsync();
751 loc = locale(loc, new errorcvt);
755 fbuf1.pubsetbuf(0, 0);
756 fbuf1.open(name_07, ios_base::out | ios_base::trunc);
757 streamsize n = fbuf1.sputn("onne", 4);
772 __gnu_cxx_test::run_test_wrapped_generic_locale_exception_catcher(test07);