]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 990117 bkoz test functionality of basic_filebuf for char_type == char |
2 | ||
aefb3380 | 3 | // Copyright (C) 1997, 1998, 1999, 2000, 2001 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 | // NB: this test assumes that _M_buf_size == 40, and not the usual | |
1b4a6975 | 22 | // buffer_size length of BUFSIZ (8192), so that overflow/underflow can be |
b2dad0e3 | 23 | // simulated a bit more readily. |
1b4a6975 | 24 | // NRB (Nota Really Bene): setting it to 40 breaks the test, as intended. |
b2dad0e3 | 25 | |
db353c2c GDR |
26 | // @require@ %-*.tst %-*.txt |
27 | // @diff@ %-*.tst %*.txt | |
28 | ||
1b4a6975 PE |
29 | const int buffer_size = 8192; |
30 | //const int buffer_size = 40; | |
31 | ||
b2dad0e3 | 32 | #include <fstream> |
99e9125d | 33 | #include <iostream> |
fe413112 | 34 | #include <testsuite_hooks.h> |
b2dad0e3 BK |
35 | |
36 | const char carray_01[] = "santa cruz or sandiego?"; | |
37 | const char carray_02[] = "memphis, new orleans, and savanah"; | |
db353c2c GDR |
38 | const char name_01[] = "filebuf-1.txt"; // file with data in it |
39 | const char name_02[] = "filebuf-2.txt"; // empty file, need to create | |
40 | const char name_03[] = "filebuf-3.txt"; // empty file, need to create | |
b2dad0e3 BK |
41 | |
42 | class derived_filebuf: public std::filebuf | |
43 | { | |
44 | public: | |
45 | void | |
d207c3f7 | 46 | set_size(int_type __size) { _M_buf_size_opt = __size; } |
b2dad0e3 BK |
47 | }; |
48 | ||
49 | derived_filebuf fb_01; // in | |
50 | derived_filebuf fb_02; // out | |
51 | derived_filebuf fb_03; // in | out | |
52 | ||
ab30ba5c | 53 | |
b2dad0e3 BK |
54 | // initialize filebufs to be the same size regardless of platform |
55 | void test00() | |
56 | { | |
57 | fb_01.set_size(buffer_size); | |
58 | fb_02.set_size(buffer_size); | |
59 | fb_03.set_size(buffer_size); | |
60 | } | |
61 | ||
ab30ba5c | 62 | |
b2dad0e3 BK |
63 | // test the filebuf/stringbuf locale settings |
64 | bool test01() { | |
65 | std::locale loc_tmp; | |
66 | loc_tmp = fb_01.getloc(); | |
67 | fb_01.pubimbue(loc_tmp); //This should initialize _M_init to true | |
68 | fb_01.getloc(); //This should just return _M_locale | |
69 | ||
70 | return true; | |
71 | } | |
72 | ||
73 | ||
74 | // test member functions functions | |
75 | bool test02() { | |
76 | bool test = true; | |
77 | ||
78 | // bool is_open() | |
aa1b2f7d BV |
79 | VERIFY( !fb_01.is_open() ); |
80 | VERIFY( !fb_02.is_open() ); | |
81 | VERIFY( !fb_03.is_open() ); | |
b2dad0e3 BK |
82 | |
83 | // filebuf_type* open(const char* __s, ios_base::openmode __mode) | |
ab30ba5c | 84 | fb_01.open(name_01, std::ios_base::in | std::ios_base::ate); |
b2dad0e3 BK |
85 | fb_02.open(name_02, std::ios_base::in | std::ios_base::out | std::ios_base::trunc); |
86 | // Try to open two different files without closing the first: | |
87 | // Should keep the old file attached, and disregard attempt to overthrow. | |
88 | fb_02.open(name_03, std::ios_base::in | std::ios_base::out); | |
89 | fb_03.open(name_03, std::ios_base::out | std::ios_base::trunc); | |
aa1b2f7d BV |
90 | VERIFY( fb_01.is_open() ); |
91 | VERIFY( fb_02.is_open() ); | |
92 | VERIFY( fb_03.is_open() ); | |
b2dad0e3 BK |
93 | |
94 | // filebuf_type* close() | |
95 | fb_01.close(); | |
96 | fb_02.close(); | |
97 | fb_03.close(); | |
aa1b2f7d BV |
98 | VERIFY( !fb_01.is_open() ); |
99 | VERIFY( !fb_02.is_open() ); | |
100 | VERIFY( !fb_03.is_open() ); | |
b2dad0e3 BK |
101 | |
102 | #ifdef DEBUG_ASSERT | |
103 | assert(test); | |
104 | #endif | |
105 | ||
106 | return test; | |
107 | } | |
108 | ||
109 | ||
110 | // test overloaded virtual functions | |
111 | bool test03() { | |
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; | |
117 | ||
118 | bool test = true; | |
119 | std::filebuf f_tmp; | |
120 | std::streamsize strmsz_1, strmsz_2; | |
121 | std::streamoff strmof_1, strmof_2; | |
122 | int i = 0, j = 0, k = 0; | |
123 | ||
124 | // GET | |
125 | // int showmanyc() | |
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. | |
132 | ||
133 | // int in_avail() | |
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(); | |
aa1b2f7d BV |
138 | VERIFY( strmof_1 == -1 ); |
139 | VERIFY( strmof_1 == strmof_2 ); //fail because not open | |
b2dad0e3 | 140 | strmof_1 = fb_03.in_avail(); |
aa1b2f7d | 141 | VERIFY( strmof_1 == strmof_2 ); |
b2dad0e3 BK |
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(); | |
aa1b2f7d BV |
147 | VERIFY( strmof_1 != strmof_2 ); |
148 | VERIFY( strmof_1 >= 0 ); | |
149 | VERIFY( strmof_2 == -1 ); // empty file | |
b2dad0e3 | 150 | strmof_1 = fb_03.in_avail(); |
99e9125d | 151 | VERIFY( strmof_1 == 0 ); // empty file |
b2dad0e3 BK |
152 | |
153 | // int_type sbumpc() | |
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(); | |
aa1b2f7d BV |
157 | VERIFY( c1 != c2 ); |
158 | VERIFY( c1 == '/' ); | |
159 | VERIFY( c2 == -1 ); | |
b2dad0e3 BK |
160 | int_type c3 = fb_01.sbumpc(); |
161 | int_type c4 = fb_02.sbumpc(); | |
aa1b2f7d BV |
162 | VERIFY( c3 != c4 ); |
163 | VERIFY( c1 == c3 ); // fluke, both happen to be '/' | |
164 | VERIFY( c2 == c4 ); | |
b2dad0e3 | 165 | int_type c5 = fb_03.sbumpc(); |
aa1b2f7d | 166 | VERIFY( c5 == traits_type::eof() ); |
b2dad0e3 BK |
167 | // XXX should do some kind of test to make sure that internal |
168 | // buffers point ot the same thing, to check consistancy. | |
169 | ||
170 | // int_type sgetc() | |
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(); | |
aa1b2f7d BV |
174 | VERIFY( c6 != c3 ); |
175 | VERIFY( c7 == c4 ); // both -1 | |
b2dad0e3 BK |
176 | int_type c8 = fb_01.sgetc(); |
177 | int_type c9 = fb_02.sgetc(); | |
aa1b2f7d BV |
178 | VERIFY( c6 == c8 ); |
179 | VERIFY( c7 == c9 ); | |
b2dad0e3 | 180 | c5 = fb_03.sgetc(); |
aa1b2f7d | 181 | VERIFY( c5 == traits_type::eof() ); |
b2dad0e3 BK |
182 | |
183 | // int_type snextc() | |
184 | // calls sbumpc and if sbumpc != eof, return sgetc | |
185 | c6 = fb_01.snextc(); | |
186 | c7 = fb_02.snextc(); | |
aa1b2f7d BV |
187 | VERIFY( c6 != c8 ); |
188 | VERIFY( c7 == c9 ); // -1 | |
189 | VERIFY( c6 == '9' ); | |
b2dad0e3 BK |
190 | c6 = fb_01.snextc(); |
191 | c7 = fb_02.snextc(); | |
aa1b2f7d BV |
192 | VERIFY( c6 != c8 ); |
193 | VERIFY( c7 == c9 ); // -1 | |
194 | VERIFY( c6 == '9' ); | |
b2dad0e3 | 195 | c5 = fb_03.snextc(); |
aa1b2f7d | 196 | VERIFY( c5 == traits_type::eof() ); |
b2dad0e3 BK |
197 | |
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); | |
aa1b2f7d BV |
209 | VERIFY( strmsz_1 != strmsz_2 ); |
210 | VERIFY( strmsz_1 == 10 ); | |
211 | VERIFY( strmsz_2 == 0 ); | |
b2dad0e3 BK |
212 | c1 = fb_01.sgetc(); |
213 | c2 = fb_02.sgetc(); | |
aa1b2f7d BV |
214 | VERIFY( c1 == '\n' ); |
215 | VERIFY( c7 == c2 ); // n != i | |
b2dad0e3 | 216 | strmsz_1 = fb_03.sgetn(carray1, 10); |
aa1b2f7d | 217 | VERIFY( !strmsz_1 ); //zero |
b2dad0e3 BK |
218 | strmsz_1 = fb_01.in_avail(); |
219 | strmsz_2 = fb_01.sgetn(carray2, strmsz_1 + 5); | |
aa1b2f7d | 220 | VERIFY( strmsz_1 == strmsz_2 - 5 ); |
b2dad0e3 | 221 | c4 = fb_01.sgetc(); // buffer should have underflowed from above. |
aa1b2f7d | 222 | VERIFY( c4 == 'i' ); |
b2dad0e3 | 223 | strmsz_1 = fb_01.in_avail(); |
aa1b2f7d | 224 | VERIFY( strmsz_1 > 0 ); |
b2dad0e3 | 225 | strmsz_2 = fb_01.sgetn(carray2, strmsz_1 + 5); |
aa1b2f7d | 226 | VERIFY( strmsz_1 == strmsz_2 ); //at the end of the actual file |
b2dad0e3 BK |
227 | strmsz_1 = fb_02.in_avail(); |
228 | strmsz_2 = fb_02.sgetn(carray2, strmsz_1 + 5); | |
aa1b2f7d BV |
229 | VERIFY( strmsz_1 == -1 ); |
230 | VERIFY( strmsz_2 == 0 ); | |
b2dad0e3 | 231 | c4 = fb_02.sgetc(); // should be EOF |
aa1b2f7d | 232 | VERIFY( c4 == traits_type::eof() ); |
b2dad0e3 BK |
233 | |
234 | // PUT | |
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'); | |
aa1b2f7d | 242 | VERIFY( c1 != c2 ); |
b2dad0e3 BK |
243 | c1 = fb_02.sputc('c'); |
244 | c2 = fb_03.sputc('d'); | |
aa1b2f7d | 245 | VERIFY( c1 != c2 ); |
b2dad0e3 | 246 | // strmsz_2 = fb_03.in_avail(); |
aa1b2f7d | 247 | // VERIFY( strmsz_1 != strmsz_2 ); |
b2dad0e3 BK |
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(); | |
aa1b2f7d BV |
256 | // VERIFY( strmsz_1 != strmsz_2 ); |
257 | // VERIFY( strmsz_1 > 0 ); | |
258 | // VERIFY( strmsz_2 > 0 ); | |
b2dad0e3 BK |
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 | |
aa1b2f7d | 262 | VERIFY( c3 == traits_type::eof() ); |
b2dad0e3 BK |
263 | |
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?" | |
aa1b2f7d | 268 | VERIFY( strmsz_1 == 10 ); |
b2dad0e3 | 269 | strmsz_2 = fb_03.sputn(", i wanna reach out and", 10); |
aa1b2f7d BV |
270 | VERIFY( strmsz_2 == 10 ); |
271 | VERIFY( strmsz_1 == strmsz_2 ); | |
b2dad0e3 BK |
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); | |
aa1b2f7d | 275 | VERIFY( strmsz_1 == 10 ); |
b2dad0e3 BK |
276 | // fb_02._M_out_beg = "YZracadabras<=>?@ABCDEFGHIJKLMNOPQRSTUVW" |
277 | // fb_02._M_out_cur = "<=>?@ABCDEFGHIJKLMNOPQRSTUVW" | |
278 | strmsz_1 = fb_01.sputn("racadabra", 10); | |
aa1b2f7d | 279 | VERIFY( strmsz_1 == 0 ); |
b2dad0e3 BK |
280 | |
281 | // PUTBACK | |
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 | |
288 | // determined. | |
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 | |
292 | ||
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(); | |
299 | c3 = fb_03.sgetc(); | |
aa1b2f7d BV |
300 | VERIFY( c3 == c2 ); |
301 | VERIFY( c1 != c3 ); | |
302 | VERIFY( 1 == strmsz_2 ); | |
b2dad0e3 BK |
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); | |
307 | fb_03.sputc('m'); | |
308 | strmsz_1 = fb_03.in_avail(); | |
309 | c1 = fb_03.sgetc(); | |
310 | fb_03.snextc(); | |
311 | c2 = fb_03.sputbackc('z'); | |
312 | strmsz_2 = fb_03.in_avail(); | |
313 | c3 = fb_03.sgetc(); | |
aa1b2f7d BV |
314 | VERIFY( c1 != c2 ); |
315 | VERIFY( c3 == c2 ); | |
316 | VERIFY( c1 != c3 ); | |
317 | VERIFY( c2 == 'z' ); | |
99e9125d | 318 | // VERIFY( strmsz_1 == strmsz_2 ); |
b2dad0e3 BK |
319 | // test for replacing char with identical one |
320 | fb_03.snextc(); | |
321 | fb_03.sputc('u'); | |
322 | fb_03.sputc('v'); | |
323 | fb_03.sputc('a'); | |
324 | strmsz_1 = fb_03.in_avail(); | |
325 | c2 = fb_03.sputbackc('a'); | |
326 | strmsz_2 = fb_03.in_avail(); | |
327 | c3 = fb_03.sgetc(); | |
aa1b2f7d BV |
328 | VERIFY( c3 == c2 ); |
329 | VERIFY( strmsz_1 + 1 == strmsz_2 ); | |
b2dad0e3 BK |
330 | //test for ios_base::out |
331 | c1 = fb_02.sgetc(); // undefined | |
332 | c2 = fb_02.sputbackc('a'); | |
aa1b2f7d BV |
333 | VERIFY( c1 == c2 ); |
334 | VERIFY( c1 == -1 ); | |
b2dad0e3 BK |
335 | |
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(); | |
aa1b2f7d BV |
344 | VERIFY( c2 == 'v' ); // VERIFY( c2 != traits_type::eof() ); |
345 | VERIFY( strmsz_1 + 1 == strmsz_2 ); | |
b2dad0e3 BK |
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); | |
b2dad0e3 | 350 | c1 = fb_02.sgetc(); |
9fbcb61a | 351 | strmsz_1 = fb_02.in_avail(); |
b2dad0e3 | 352 | c2 = fb_02.sungetc(); |
b2dad0e3 | 353 | c3 = fb_02.sgetc(); |
9fbcb61a BK |
354 | strmsz_2 = fb_02.in_avail(); |
355 | VERIFY( c1 != c2 ); | |
356 | VERIFY( c2 == c3 ); | |
357 | VERIFY( c1 == traits_type::eof() ); | |
358 | VERIFY( strmsz_1 != strmsz_2 ); | |
b2dad0e3 BK |
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 | |
362 | c1 = fb_03.sgetc(); | |
363 | c2 = fb_03.sungetc(); | |
364 | strmsz_2 = fb_03.in_avail(); // 1 | |
365 | c3 = fb_03.sgetc(); | |
aa1b2f7d BV |
366 | VERIFY( c1 != c2 ); |
367 | // VERIFY( c2 == c3 || c2 == traits_type::not_eof(int(c3)) ); | |
368 | VERIFY( strmsz_2 != strmsz_1 ); | |
369 | VERIFY( strmsz_2 == 1 ); | |
b2dad0e3 BK |
370 | //test for ios_base::out |
371 | ||
372 | // BUFFER MANAGEMENT & POSITIONING | |
373 | // int sync() | |
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(); | |
377 | fb_01.pubsync(); | |
378 | strmsz_2 = fb_01.in_avail(); | |
aa1b2f7d | 379 | VERIFY( strmsz_2 == strmsz_1 ); |
b2dad0e3 BK |
380 | strmsz_1 = fb_02.in_avail(); |
381 | fb_02.pubsync(); | |
382 | // 27filebuf-2.txt == 53 bytes after this. | |
383 | strmsz_2 = fb_02.in_avail(); | |
9fbcb61a | 384 | VERIFY( strmsz_2 == 1 ); |
aa1b2f7d | 385 | VERIFY( strmsz_2 == strmsz_1 ); |
b2dad0e3 BK |
386 | strmsz_1 = fb_03.in_avail(); |
387 | fb_03.pubsync(); | |
388 | // 27filebuf-3.txt | |
389 | // bd23456789mzuva?@ABCDEFGHIJKLMNOPQRSTUVWXYZracadabras, i wannaz | |
390 | // 63 bytes. | |
391 | strmsz_2 = fb_03.in_avail(); | |
aa1b2f7d | 392 | VERIFY( strmsz_1 == 1 ); |
99e9125d | 393 | // VERIFY( strmsz_2 == 1 ); |
b2dad0e3 BK |
394 | |
395 | // setbuf | |
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. | |
402 | ||
403 | ||
404 | // seekoff | |
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)); | |
409 | off_type off_1 = 0; | |
410 | off_type off_2 = 0; | |
411 | //IN|OUT | |
412 | // 27filebuf-3.txt = bd23456789:;<=>?... | |
413 | //beg | |
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(); | |
97e0a05a | 417 | off_1 = pt_1; |
aa1b2f7d | 418 | VERIFY( off_1 > 0 ); |
b2dad0e3 | 419 | c1 = fb_03.snextc(); //current in pointer +1 |
aa1b2f7d | 420 | VERIFY( c1 == '3' ); |
b2dad0e3 BK |
421 | c2 = fb_03.sputc('\n'); //current in pointer +1 |
422 | c3 = fb_03.sgetc(); | |
aa1b2f7d BV |
423 | VERIFY( c2 != c3 ); |
424 | VERIFY( c3 == '4' ); | |
b2dad0e3 BK |
425 | fb_03.pubsync(); |
426 | c1 = fb_03.sgetc(); | |
aa1b2f7d | 427 | VERIFY( c1 == c3 ); |
b2dad0e3 BK |
428 | //cur |
429 | // 27filebuf-3.txt = bd2\n456789:;<=>?... | |
430 | pt_2 = fb_03.pubseekoff(2, std::ios_base::cur); | |
97e0a05a | 431 | off_2 = pt_2; |
aa1b2f7d | 432 | VERIFY( (off_2 == (off_1 + 2 + 1 + 1)) ); |
b2dad0e3 | 433 | c1 = fb_03.snextc(); //current in pointer +1 |
aa1b2f7d | 434 | VERIFY( c1 == '7' ); |
b2dad0e3 BK |
435 | c2 = fb_03.sputc('x'); //test current out pointer |
436 | c3 = fb_03.sputc('\n'); | |
437 | c1 = fb_03.sgetc(); | |
438 | fb_03.pubsync(); | |
439 | c3 = fb_03.sgetc(); | |
aa1b2f7d | 440 | VERIFY( c1 == c3 ); |
b2dad0e3 BK |
441 | //end |
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); | |
97e0a05a | 445 | off_1 = pt_2; |
aa1b2f7d | 446 | VERIFY( off_1 > off_2 ); //weak, but don't know exactly where it ends |
b2dad0e3 BK |
447 | c3 = fb_03.sputc('\n'); |
448 | strmsz_1 = fb_03.sputn("because because because. . .", 28); | |
aa1b2f7d | 449 | VERIFY( strmsz_1 == 28 ); |
b2dad0e3 BK |
450 | c1 = fb_03.sungetc(); |
451 | fb_03.pubsync(); | |
452 | c3 = fb_03.sgetc(); | |
aa1b2f7d | 453 | VERIFY( c1 == c3 ); |
b2dad0e3 BK |
454 | // IN |
455 | // OUT | |
456 | ||
457 | ||
458 | // seekpos | |
459 | // pubseekpos(pos_type sp, ios_base::openmode) | |
460 | // alters the stream position to sp | |
461 | //IN|OUT | |
462 | //beg | |
463 | pt_1 = fb_03.pubseekoff(78, std::ios_base::beg); | |
97e0a05a | 464 | off_1 = pt_1; |
aa1b2f7d | 465 | VERIFY( off_1 > 0 ); |
b2dad0e3 | 466 | c1 = fb_03.snextc(); //current in pointer +1 |
aa1b2f7d | 467 | VERIFY( c1 == ' ' ); |
b2dad0e3 BK |
468 | c2 = fb_03.sputc('\n'); //test current out pointer |
469 | c3 = fb_03.sgetc(); | |
470 | fb_03.pubsync(); //resets pointers | |
471 | pt_2 = fb_03.pubseekpos(pt_1); | |
97e0a05a | 472 | off_2 = pt_2; |
aa1b2f7d | 473 | VERIFY( off_1 == off_2 ); |
b2dad0e3 | 474 | c3 = fb_03.snextc(); //current in pointer +1 |
aa1b2f7d | 475 | VERIFY( c2 == c3 ); |
b2dad0e3 | 476 | pt_1 = fb_03.pubseekoff(0, std::ios_base::end); |
97e0a05a | 477 | off_1 = pt_1; |
aa1b2f7d | 478 | VERIFY( off_1 > off_2 ); |
b2dad0e3 BK |
479 | fb_03.sputn("\nof the wonderful things he does!!\nok", 37); |
480 | fb_03.pubsync(); | |
481 | ||
482 | // IN | |
483 | // OUT | |
484 | ||
485 | // VIRTUALS (indirectly tested) | |
486 | // underflow | |
487 | // if read position avail, returns *gptr() | |
488 | ||
489 | // pbackfail(int_type c) | |
490 | // put c back into input sequence | |
491 | ||
492 | // overflow | |
493 | // appends c to output seq | |
494 | ||
495 | // NB Have to close these suckers. . . | |
496 | // filebuf_type* close() | |
497 | fb_01.close(); | |
498 | fb_02.close(); | |
499 | fb_03.close(); | |
aa1b2f7d BV |
500 | VERIFY( !fb_01.is_open() ); |
501 | VERIFY( !fb_02.is_open() ); | |
502 | VERIFY( !fb_03.is_open() ); | |
b2dad0e3 BK |
503 | |
504 | #ifdef DEBUG_ASSERT | |
505 | assert(test); | |
506 | #endif | |
507 | ||
508 | return test; | |
509 | } | |
510 | ||
c0b84d79 VE |
511 | bool test04() |
512 | { | |
513 | using namespace std; | |
514 | typedef istream::int_type int_type; | |
515 | ||
516 | bool test = true; | |
517 | ifstream ifs(name_02); | |
518 | char buffer[] = "xxxxxxxxxx"; | |
519 | int_type len1 = ifs.rdbuf()->sgetn(buffer, sizeof(buffer)); | |
aa1b2f7d BV |
520 | VERIFY( len1 == sizeof(buffer) ); |
521 | VERIFY( buffer[0] == 'a' ); | |
b2dad0e3 | 522 | |
c0b84d79 VE |
523 | #ifdef DEBUG_ASSERT |
524 | assert(test); | |
525 | #endif | |
526 | return test; | |
527 | } | |
528 | ||
c0a26060 BK |
529 | // test05 |
530 | // libstdc++/1886 | |
531 | // should be able to instantiate basic_filebuf for non-standard types. | |
532 | template class std::basic_filebuf<short, std::char_traits<short> >; | |
533 | ||
69302d8b BK |
534 | // test06 |
535 | // libstdc++/2020 | |
536 | // should be able to use custom char_type | |
537 | class gnu_char_type | |
538 | { | |
539 | unsigned long character; | |
540 | public: | |
541 | // operator == | |
542 | bool | |
543 | operator==(const gnu_char_type& __lhs) | |
544 | { return character == __lhs.character; } | |
545 | ||
546 | // operator < | |
547 | bool | |
548 | operator<(const gnu_char_type& __lhs) | |
549 | { return character < __lhs.character; } | |
550 | ||
39003c99 BK |
551 | // default ctor |
552 | gnu_char_type() { } | |
553 | ||
69302d8b BK |
554 | // to_char_type |
555 | gnu_char_type(const unsigned long& __l) : character(__l) { } | |
556 | ||
557 | // to_int_type | |
558 | operator unsigned long() const { return character; } | |
559 | }; | |
560 | ||
561 | bool test06() | |
562 | { | |
563 | bool test = true; | |
564 | typedef std::basic_filebuf<gnu_char_type> gnu_filebuf; | |
565 | ||
566 | try | |
567 | { gnu_filebuf obj; } | |
568 | catch(std::exception& obj) | |
569 | { | |
570 | test = false; | |
571 | VERIFY( test ); | |
572 | } | |
573 | return test; | |
574 | } | |
c0a26060 | 575 | |
99e9125d BK |
576 | // libstdc++/3647 |
577 | void test07() | |
578 | { | |
579 | // Should not block. | |
580 | std::cout << std::cin.rdbuf()->in_avail() << std::endl; | |
581 | } | |
582 | ||
c0b84d79 VE |
583 | int main() |
584 | { | |
b2dad0e3 BK |
585 | test00(); |
586 | test01(); | |
587 | test02(); | |
588 | test03(); | |
c0b84d79 | 589 | test04(); |
b2dad0e3 | 590 | |
69302d8b | 591 | test06(); |
99e9125d | 592 | test07(); |
b2dad0e3 BK |
593 | return 0; |
594 | } | |
595 | ||
596 | ||
597 | ||
598 | // more surf!!! |