1 // 2001-05-21 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2001, 2002 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
24 #include <testsuite_hooks.h>
26 // @require@ %-*.tst %-*.txt
27 // @diff@ %-*.tst %*.txt
35 const char* strlit
= "how to tell a story and other essays: mark twain";
36 const size_t strlitsize
= std::strlen(strlit
);
39 fbuf01
.pubsetbuf(buf
, 512);
40 fbuf01
.sputn(strlit
, strlitsize
);
41 VERIFY( std::strncmp(strlit
, buf
, strlitsize
) != 0 );
50 const char* strlit
= "how to tell a story and other essays: mark twain";
51 const size_t strlitsize
= std::strlen(strlit
);
53 fbuf01
.open("tmp", ios_base::out
);
55 fbuf01
.pubsetbuf(buf
, strlitsize
);
56 fbuf01
.sputn(strlit
, strlitsize
);
57 VERIFY( std::strncmp(strlit
, buf
, strlitsize
) == 0 );
61 // NB: This test assumes that _M_buf_size == 40, and not the usual
62 // buffer_size length of BUFSIZ (8192), so that overflow/underflow can be
63 // simulated a bit more readily.
64 // NRB (Nota Really Bene): setting it to 40 breaks the test, as intended.
65 const int buffer_size
= 8192;
66 //const int buffer_size = 40;
68 const char carray_01
[] = "santa cruz or sandiego?";
69 const char carray_02
[] = "memphis, new orleans, and savanah";
70 const char name_01
[] = "filebuf_virtuals-1.txt"; // file with data in it
71 const char name_02
[] = "filebuf_virtuals-2.txt"; // empty file, need to create
72 const char name_03
[] = "filebuf_virtuals-3.txt"; // empty file, need to create
75 class derived_filebuf
: public std::filebuf
79 set_size(int_type __size
) { _M_buf_size_opt
= __size
; }
82 derived_filebuf fb_01
; // in
83 derived_filebuf fb_02
; // out
84 derived_filebuf fb_03
; // in | out
87 // Initialize filebufs to be the same size regardless of platform.
90 fb_01
.set_size(buffer_size
);
91 fb_02
.set_size(buffer_size
);
92 fb_03
.set_size(buffer_size
);
96 // Test the filebuf/stringbuf locale settings.
100 loc_tmp
= fb_01
.getloc();
101 fb_01
.pubimbue(loc_tmp
); //This should initialize _M_init to true
102 fb_01
.getloc(); //This should just return _M_locale
105 // Test overloaded virtual functions.
108 typedef std::filebuf::int_type int_type
;
109 typedef std::filebuf::traits_type traits_type
;
110 typedef std::filebuf::pos_type pos_type
;
111 typedef std::filebuf::off_type off_type
;
112 typedef size_t size_type
;
116 std::streamsize strmsz_1
, strmsz_2
;
117 std::streamoff strmof_1
, strmof_2
;
118 int i
= 0, j
= 0, k
= 0;
122 // returns an estimate of the numbers of chars in the seq, or -1.
123 // if __retval > 0, then calls to underflow won't return
124 // traits_type::eof() till at least __retval chars.
125 // if __retval == -1, then calls to underflow or uflow will fail.
126 // NB overriding def if it can determine more chars can be read from
127 // the input sequence.
130 // if a read position is available, return _M_in_end - _M_in_cur.
131 // else return showmanyc.
132 strmof_1
= fb_01
.in_avail();
133 strmof_2
= fb_02
.in_avail();
134 VERIFY( strmof_1
== -1 );
135 VERIFY( strmof_1
== strmof_2
); //fail because not open
136 strmof_1
= fb_03
.in_avail();
137 VERIFY( strmof_1
== strmof_2
);
138 fb_01
.open(name_01
, std::ios_base::in
);
139 fb_02
.open(name_02
, std::ios_base::out
| std::ios_base::trunc
);
140 fb_03
.open(name_03
, std::ios_base::out
| std::ios_base::in
| std::ios_base::trunc
);
141 strmof_1
= fb_01
.in_avail();
142 strmof_2
= fb_02
.in_avail();
143 VERIFY( strmof_1
!= strmof_2
);
144 VERIFY( strmof_1
>= 0 );
145 VERIFY( strmof_2
== -1 ); // empty file
146 strmof_1
= fb_03
.in_avail();
147 VERIFY( strmof_1
== 0 ); // empty file
150 // if read_cur not avail returns uflow(), else return *read_cur & increment
151 int_type c1
= fb_01
.sbumpc();
152 int_type c2
= fb_02
.sbumpc();
156 int_type c3
= fb_01
.sbumpc();
157 int_type c4
= fb_02
.sbumpc();
159 VERIFY( c1
== c3
); // fluke, both happen to be '/'
161 int_type c5
= fb_03
.sbumpc();
162 VERIFY( c5
== traits_type::eof() );
163 // XXX should do some kind of test to make sure that internal
164 // buffers point ot the same thing, to check consistancy.
167 // if read_cur not avail, return uflow(), else return *read_cur
168 int_type c6
= fb_01
.sgetc();
169 int_type c7
= fb_02
.sgetc();
171 VERIFY( c7
== c4
); // both -1
172 int_type c8
= fb_01
.sgetc();
173 int_type c9
= fb_02
.sgetc();
177 VERIFY( c5
== traits_type::eof() );
180 // calls sbumpc and if sbumpc != eof, return sgetc
184 VERIFY( c7
== c9
); // -1
189 VERIFY( c7
== c9
); // -1
192 VERIFY( c5
== traits_type::eof() );
194 // streamsize sgetn(char_type *s, streamsize n)
195 // streamsize xsgetn(char_type *s, streamsize n)
196 // assign up to n chars to s from input sequence, indexing in_cur as
197 // approp and returning the number of chars assigned
198 strmsz_1
= fb_01
.in_avail();
199 strmsz_2
= fb_02
.in_avail();
200 test
= strmsz_1
!= strmsz_2
;
201 char carray1
[13] = "";
202 strmsz_1
= fb_01
.sgetn(carray1
, 10);
203 char carray2
[buffer_size
] = "";
204 strmsz_2
= fb_02
.sgetn(carray2
, 10);
205 VERIFY( strmsz_1
!= strmsz_2
);
206 VERIFY( strmsz_1
== 10 );
207 VERIFY( strmsz_2
== 0 );
210 VERIFY( c1
== '\n' );
211 VERIFY( c7
== c2
); // n != i
212 strmsz_1
= fb_03
.sgetn(carray1
, 10);
213 VERIFY( !strmsz_1
); //zero
214 strmsz_1
= fb_01
.in_avail();
215 strmsz_2
= fb_01
.sgetn(carray2
, strmsz_1
+ 5);
216 VERIFY( strmsz_1
== strmsz_2
- 5 );
217 c4
= fb_01
.sgetc(); // buffer should have underflowed from above.
219 strmsz_1
= fb_01
.in_avail();
220 VERIFY( strmsz_1
> 0 );
221 strmsz_2
= fb_01
.sgetn(carray2
, strmsz_1
+ 5);
222 VERIFY( strmsz_1
== strmsz_2
); //at the end of the actual file
223 strmsz_1
= fb_02
.in_avail();
224 strmsz_2
= fb_02
.sgetn(carray2
, strmsz_1
+ 5);
225 VERIFY( strmsz_1
== -1 );
226 VERIFY( strmsz_2
== 0 );
227 c4
= fb_02
.sgetc(); // should be EOF
228 VERIFY( c4
== traits_type::eof() );
231 // int_type sputc(char_type c)
232 // if out_cur not avail, return overflow(traits_type::to_int_type(c))
233 // else, stores c at out_cur,
234 // increments out_cur, and returns c as int_type
235 // strmsz_1 = fb_03.in_avail(); // XXX valid for in|out??
236 c1
= fb_02
.sputc('a');
237 c2
= fb_03
.sputc('b');
239 c1
= fb_02
.sputc('c');
240 c2
= fb_03
.sputc('d');
242 // strmsz_2 = fb_03.in_avail();
243 // VERIFY( strmsz_1 != strmsz_2 );
244 for (int i
= 50; i
<= 90; ++i
)
245 c2
= fb_02
.sputc(char(i
));
246 // 27filebuf-2.txt == ac23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
247 // fb_02._M_out_cur = '2'
248 strmsz_1
= fb_03
.in_avail();
249 for (int i
= 50; i
<= 90; ++i
)
250 c2
= fb_03
.sputc(char(i
));
251 strmsz_2
= fb_03
.in_avail();
252 // VERIFY( strmsz_1 != strmsz_2 );
253 // VERIFY( strmsz_1 > 0 );
254 // VERIFY( strmsz_2 > 0 );
255 // 27filebuf-2.txt == bd23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
256 // fb_02._M_out_cur = '2'
257 c3
= fb_01
.sputc('a'); // should be EOF because this is read-only
258 VERIFY( c3
== traits_type::eof() );
260 // streamsize sputn(const char_typs* s, streamsize n)
261 // write up to n chars to out_cur from s, returning number assigned
262 // NB *sputn will happily put '\0' into your stream if you give it a chance*
263 strmsz_1
= fb_03
.sputn("racadabras", 10);//"abracadabras or what?"
264 VERIFY( strmsz_1
== 10 );
265 strmsz_2
= fb_03
.sputn(", i wanna reach out and", 10);
266 VERIFY( strmsz_2
== 10 );
267 VERIFY( strmsz_1
== strmsz_2
);
268 // fb_03._M_out_beg = "YZracadabras, i wanna FGHIJKLMNOPQRSTUVW"
269 // fb_03._M_out_cur = "FGHIJKLMNOPQRSTUVW"
270 strmsz_1
= fb_02
.sputn("racadabras", 10);
271 VERIFY( strmsz_1
== 10 );
272 // fb_02._M_out_beg = "YZracadabras<=>?@ABCDEFGHIJKLMNOPQRSTUVW"
273 // fb_02._M_out_cur = "<=>?@ABCDEFGHIJKLMNOPQRSTUVW"
274 strmsz_1
= fb_01
.sputn("racadabra", 10);
275 VERIFY( strmsz_1
== 0 );
278 // int_type pbfail(int_type c)
279 // called when gptr() null, gptr() == eback(), or traits::eq(*gptr, c) false
280 // "pending sequence" is:
281 // 1) everything as defined in underflow
282 // 2) + if (traits::eq_int_type(c, traits::eof()), then input
283 // sequence is backed up one char before the pending sequence is
285 // 3) + if (not 2) then c is prepended. Left unspecified is
286 // whether the input sequence is backedup or modified in any way
287 // returns traits::eof() for failure, unspecified other value for success
289 // int_type sputbackc(char_type c)
290 // if in_cur not avail || ! traits::eq(c, gptr() [-1]), return pbfail
291 // otherwise decrements in_cur and returns *gptr()
292 c1
= fb_03
.sgetc(); // -1
293 c2
= fb_03
.sputbackc('z');
294 strmsz_2
= fb_03
.in_avail();
298 VERIFY( 1 == strmsz_2
);
299 //test for _in_cur == _in_beg
300 // fb_03._M_out_beg = "bd23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZracada" etc
301 fb_03
.pubseekoff(10, std::ios_base::beg
,
302 std::ios_base::in
| std::ios_base::out
);
304 strmsz_1
= fb_03
.in_avail();
307 c2
= fb_03
.sputbackc('z');
308 strmsz_2
= fb_03
.in_avail();
314 // VERIFY( strmsz_1 == strmsz_2 );
315 // test for replacing char with identical one
320 strmsz_1
= fb_03
.in_avail();
321 c2
= fb_03
.sputbackc('a');
322 strmsz_2
= fb_03
.in_avail();
325 VERIFY( strmsz_1
+ 1 == strmsz_2
);
326 //test for ios_base::out
327 c1
= fb_02
.sgetc(); // undefined
328 c2
= fb_02
.sputbackc('a');
332 // int_type sungetc()
333 // if in_cur not avail, return pbackfail(), else decrement and
334 // return to_int_type(*gptr())
335 // fb_03._M_out_beg = "uvaacadabras, i wannaZ[\\]^_`abcdefghijkl"
336 // fb_03._M_out_cur = "aacadabras, i wannaZ[\\]^_`abcdefghijkl"
337 strmsz_1
= fb_03
.in_avail();
338 c2
= fb_03
.sungetc(); // delete the 'a'
339 strmsz_2
= fb_03
.in_avail();
340 VERIFY( c2
== 'v' ); // VERIFY( c2 != traits_type::eof() );
341 VERIFY( strmsz_1
+ 1 == strmsz_2
);
342 //test for _in_cur == _in_beg
343 for (int i
= 50; i
< 32 + 29; ++i
)
344 fb_02
.sputc(char(i
));
345 fb_02
.pubseekoff(0, std::ios_base::beg
, std::ios_base::out
);
347 strmsz_1
= fb_02
.in_avail();
348 c2
= fb_02
.sungetc();
350 strmsz_2
= fb_02
.in_avail();
353 VERIFY( c1
== traits_type::eof() );
354 VERIFY( strmsz_1
== strmsz_2
);
355 //test for _in_cur == _in_end
356 fb_03
.pubseekoff(0, std::ios_base::end
);
357 strmsz_1
= fb_03
.in_avail(); // -1 cuz at the end
359 c2
= fb_03
.sungetc();
360 strmsz_2
= fb_03
.in_avail(); // 1
363 // VERIFY( c2 == c3 || c2 == traits_type::not_eof(int(c3)) );
364 VERIFY( strmsz_2
!= strmsz_1
);
365 VERIFY( strmsz_2
== 1 );
366 //test for ios_base::out
368 // BUFFER MANAGEMENT & POSITIONING
370 // if a put area exists, overflow.
371 // if a get area exists, do something undefined. (like, nothing)
372 strmsz_1
= fb_01
.in_avail();
374 strmsz_2
= fb_01
.in_avail();
375 VERIFY( strmsz_2
== strmsz_1
);
376 strmsz_1
= fb_02
.in_avail();
378 // 27filebuf-2.txt == 53 bytes after this.
379 strmsz_2
= fb_02
.in_avail();
380 VERIFY( strmsz_2
== -1 );
381 VERIFY( strmsz_2
== strmsz_1
);
382 strmsz_1
= fb_03
.in_avail();
385 // bd23456789mzuva?@ABCDEFGHIJKLMNOPQRSTUVWXYZracadabras, i wannaz
387 strmsz_2
= fb_03
.in_avail();
388 VERIFY( strmsz_1
== 1 );
389 // VERIFY( strmsz_2 == 1 );
392 // pubsetbuf(char_type* s, streamsize n)
393 fb_01
.pubsetbuf(0,0);
394 fb_02
.pubsetbuf(0,0);
395 fb_03
.pubsetbuf(0,0);
396 // Need to test unbuffered output, which means calling this on some
397 // things that have just been opened.
401 // pubseekoff(off_type off, ios_base::seekdir way, ios_base::openmode which)
402 // alters the stream position to off
403 pos_type
pt_1(off_type(-1));
404 pos_type
pt_2(off_type(0));
408 // 27filebuf-3.txt = bd23456789:;<=>?...
410 strmsz_1
= fb_03
.in_avail();
411 pt_1
= fb_03
.pubseekoff(2, std::ios_base::beg
);
412 strmsz_2
= fb_03
.in_avail();
415 c1
= fb_03
.snextc(); //current in pointer +1
417 c2
= fb_03
.sputc('\n'); //current in pointer +1
425 // 27filebuf-3.txt = bd2\n456789:;<=>?...
426 pt_2
= fb_03
.pubseekoff(2, std::ios_base::cur
);
428 VERIFY( (off_2
== (off_1
+ 2 + 1 + 1)) );
429 c1
= fb_03
.snextc(); //current in pointer +1
431 c2
= fb_03
.sputc('x'); //test current out pointer
432 c3
= fb_03
.sputc('\n');
438 // 27filebuf-3.txt = "bd2\n456x\n9"
439 pt_2
= fb_03
.pubseekoff(0, std::ios_base::end
,
440 std::ios_base::in
|std::ios_base::out
);
442 VERIFY( off_1
> off_2
); //weak, but don't know exactly where it ends
443 c3
= fb_03
.sputc('\n');
444 strmsz_1
= fb_03
.sputn("because because because. . .", 28);
445 VERIFY( strmsz_1
== 28 );
446 c1
= fb_03
.sungetc();
447 // Defect? retval of sungetc is not necessarily the character ungotten.
458 // pubseekpos(pos_type sp, ios_base::openmode)
459 // alters the stream position to sp
462 pt_1
= fb_03
.pubseekoff(78, std::ios_base::beg
);
465 c1
= fb_03
.snextc(); //current in pointer +1
467 c2
= fb_03
.sputc('\n'); //test current out pointer
469 fb_03
.pubsync(); //resets pointers
470 pt_2
= fb_03
.pubseekpos(pt_1
);
472 VERIFY( off_1
== off_2
);
473 c3
= fb_03
.snextc(); //current in pointer +1
475 pt_1
= fb_03
.pubseekoff(0, std::ios_base::end
);
477 VERIFY( off_1
> off_2
);
478 fb_03
.sputn("\nof the wonderful things he does!!\nok", 37);
484 // VIRTUALS (indirectly tested)
486 // if read position avail, returns *gptr()
488 // pbackfail(int_type c)
489 // put c back into input sequence
492 // appends c to output seq
494 // NB Have to close these suckers. . .
495 // filebuf_type* close()
499 VERIFY( !fb_01
.is_open() );
500 VERIFY( !fb_02
.is_open() );
501 VERIFY( !fb_03
.is_open() );
507 typedef istream::int_type int_type
;
510 ifstream
ifs(name_02
);
511 char buffer
[] = "xxxxxxxxxx";
512 int_type len1
= ifs
.rdbuf()->sgetn(buffer
, sizeof(buffer
));
513 VERIFY( len1
== sizeof(buffer
) );
514 VERIFY( buffer
[0] == 'a' );