2 Copyright (C) 1993 Free Software Foundation
4 This file is part of the GNU IO Library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License. */
25 // This may look like C code, but it is really -*- C++ -*-
28 * a few tests for streams
35 #include <strstream.h>
51 char c
; int i
; double d
;
54 ostream
& operator<<(ostream
& s
, record
& r
)
56 return(s
<< "(i = " << r
.i
<< " c = " << r
.c
<< " d = " << r
.d
<< ")");
64 assert(cout
.writable());
65 assert(cout
.is_open());
66 cout
<< "Hello, world via cout\n";
68 assert(cerr
.writable());
69 assert(cerr
.is_open());
70 cerr
<< "Hello, world via cerr\n";
73 assert(cin
.readable());
74 assert(cin
.is_open());
76 cout
<< "enter a char:"; cin
>> ch
;
77 cout
.put('c'); cout
.put(' '); cout
.put('='); cout
.put(' ');
78 cout
.put('"'); cout
.put(ch
); cout
<< '"'; cout
<< char('\n');
92 cout
<< "enter three integers (short, int, long):";
94 // cin.scan("%ld", &l);
96 cout
<< "first = " << h
<< " via dec = " << dec(h
, 8) << "\n";
97 cout
<< "second = " << i
<< form(" via form = %d = 0%o", i
, i
);
98 cout
.form(" via cout.form = %d = 0x%x\n", i
, i
);
99 cout
<< "third = " << l
<< " via hex = " << hex(l
) << "\n";
103 cout
<< "enter a float then a double:"; cin
>> f
; cin
>> d
;
104 cout
<< "first = " << f
<< "\n";
105 cout
<< "second = " << d
<< "\n";
109 cout
<< "enter 5 characters separated with spaces:"; cin
>> s
;
110 cout
<< "first = " << s
<< "\n";
112 cout
<< "rest = " << s
<< "\n";
119 cout
<< "A 10-character buffer: " << s
<< endl
;
128 cout
<< "\nMaking streams sout and sin...";
130 ostream
sout("streamfile", io_writeonly
, a_create
);
132 ofstream
sout("streamfile");
135 assert(sout
.is_open());
136 assert(sout
.writable());
137 assert(!sout
.readable());
138 sout
<< "This file has one line testing output streams.\n";
140 assert(!sout
.is_open());
142 istream
sin("streamfile", io_readonly
, a_useonly
);
144 ifstream
sin("streamfile");
147 assert(sin
.is_open());
148 assert(!sin
.writable());
149 assert(sin
.readable());
150 cout
<< "contents of file:\n";
151 while(sin
>> ch
) cout
<< ch
;
153 assert(!sin
.is_open());
163 cout
<< "\nMaking File tf ... ";
165 File
tf(tempfile
, io_readwrite
, a_create
);
167 fstream
tf(tempfile
, ios::in
|ios::out
|ios::trunc
);
170 assert(tf
.is_open());
171 assert(tf
.writable());
172 assert(tf
.readable());
173 strcpy(s
, "This is the first and only line of this file.\n");
179 tf
.rdbuf()->seekoff(0, ios::beg
);
183 cout
<< "first line of file:\n" << s
<< "\n";
184 cout
<< "next char = ";
189 strcpy(s
, "Now there is a second line.\n");
190 cout
<< "reopening tempfile, appending: " << s
;
192 tf
.open(tf
.name(), io_appendonly
, a_use
);
195 tf
.open(tempfile
, ios::app
);
198 assert(tf
.is_open());
199 assert(tf
.writable());
200 assert(!tf
.readable());
204 tf
.open(tf
.name(), io_readonly
, a_use
);
209 tf
.open(tempfile
, ios::in
);
213 assert(tf
.is_open());
214 assert(!tf
.writable());
215 assert(tf
.readable());
216 cout
<< "First 10 chars via raw system read after reopen for input:\n";
217 read(tf
.filedesc(), s
, 10);
219 for (i
= 0; i
< 10; ++ i
)
221 lseek(tf
.filedesc(), 5, 0);
222 cout
<< "\nContents after raw lseek to pos 5:\n";
223 while ( (tf
.get(ch
)) && (cout
.put(ch
)) );
228 unlink((char*)tempfile
);
230 assert(!tf
.is_open());
237 cout
<< "\nMaking SFile rf...";
239 SFile
rf("recfile", sizeof(record
), io_readwrite
, a_create
);
241 SFile
rf("recfile", sizeof(record
), ios::in
|ios::out
|ios::trunc
);
244 assert(rf
.is_open());
245 assert(rf
.writable());
246 assert(rf
.readable());
247 for (i
= 0; i
< 10; ++i
)
251 r
.d
= (double)(i
) / 1000.0;
255 cout
<< "odd elements of file in reverse order:\n";
256 for (i
= 9; i
>= 0; i
-= 2)
259 assert(r
.c
== i
+ 'a');
270 assert(!rf
.is_open());
275 cout
<< "\nMaking PlotFile pf ...";
276 static const char plot_name
[] = "plot.out";
277 PlotFile
pf(plot_name
);
279 assert(pf
.is_open());
280 assert(pf
.writable());
281 assert(!pf
.readable());
284 pf
.circle(300,300,200);
285 pf
.line(100, 100, 500, 500);
288 cout
<< "(You may delete or attempt to plot " << pf
.name() << ")\n";
290 cout
<< "(You may delete or attempt to plot " << plot_name
<< ")\n";
297 static char t7_line1
[] = "This is a string-based stream.\n";
298 static char t7_line2
[] = "With two lines.\n";
302 cout
<< "creating string-based ostream...\n";
303 ostream
strout(60, mybuf
);
305 cout
<< "creating ostrstream...\n";
306 ostrstream
strout(mybuf
, 60);
308 assert(strout
.good());
309 assert(strout
.writable());
310 strout
<< t7_line1
<< t7_line2
<< ends
;
311 assert(strout
.good());
312 cout
<< "with contents:\n";
314 assert(bufp
== mybuf
);
315 strout
.rdbuf()->freeze(0); /* Should be a no-op */
318 cout
<< "using it to create string-based istream...\n";
319 istream
strin(strlen(mybuf
), mybuf
);
321 cout
<< "using it to create istrstream...\n";
322 istrstream
strin(mybuf
, strlen(mybuf
));
324 assert(strin
.good());
325 assert(strin
.readable());
326 cout
<< "with contents:\n";
329 strin
.getline(line
, 100);
330 int line1_len
= strlen(t7_line1
);
331 assert(strin
.tellg() == line1_len
);
332 int line_len
= strin
.gcount();
333 assert(line_len
== line1_len
);
334 cout
.write(line
, line1_len
- 1);
337 while (strin
.get(ch
)) cout
.put(ch
);
341 str1
<< "Testing string-based stream using strstream.\n";
352 // This should make it overflow.
354 assert (strout
.bad());
360 cout
<< "\nThe following file open should generate error message:";
362 File
ef("shouldnotexist", io_readonly
, a_useonly
);
364 ifstream
ef("shouldnotexist");
367 assert(!ef
.is_open());
373 static char ffile_name
[] = "ftmp";
375 cout
<< "\nMaking filebuf streams fout and fin...";
378 foutbuf
.open(ffile_name
, output
);
380 foutbuf
.open(ffile_name
, ios::out
);
382 ostream
fout(&foutbuf
);
384 assert(fout
.is_open());
385 assert(fout
.writable());
386 assert(!fout
.readable());
387 fout
<< "This file has one line testing output streams.\n";
390 assert(!fout
.is_open());
395 finbuf
.open(ffile_name
, input
);
397 finbuf
.open(ffile_name
, ios::in
);
399 istream
fin(&finbuf
);
401 assert(fin
.is_open());
402 assert(!fin
.writable());
403 assert(fin
.readable());
404 cout
<< "contents of file:\n";
405 while(fin
>> ch
) cout
<< ch
;
410 assert(!fin
.is_open());
416 char *file_name_pattern
= "ftmp%d";
417 char current_file_name
[50];
426 for (i
=0; i
< fileCnt
; i
++) {
427 sprintf(current_file_name
, file_name_pattern
, i
);
428 outFile
.open(current_file_name
, ios::out
);
430 if ( outFile
.fail() )
431 cerr
<< "File " << current_file_name
432 << " can't be opened for output" << endl
;
434 outFile
<< "This is line 1 of " << current_file_name
<< '\n';
435 outFile
<< "This is line 2 of " << current_file_name
<< endl
;
440 // Now read the files back in, and write then out to cout.
441 for (i
=0; i
< fileCnt
; i
++) {
442 sprintf(current_file_name
, file_name_pattern
, i
);
443 inFile
.open(current_file_name
, ios::in
);
447 cerr
<< "File " << current_file_name
448 << " can't be opened for input" << endl
;
450 while ( inFile
.get (c
))
463 cout
.form("%.2f+%.2f = %4.3e\n%n", 5.5, 6.25, 5.5+6.25, &count1
);
464 char *text
= "Previous line has12345";
465 char text_length_to_use
= strlen(text
) - 5;
466 count2
= cout
.rdbuf()->form("%-*.*s%3g characters\n",
467 text_length_to_use
+ 1,
471 cout
.form("%-*.*s%+d characters\n%n",
472 text_length_to_use
+ 1, text_length_to_use
, text
,
474 assert(count1
== 33);
480 cout
.setf(ios::showbase
);
481 cout
<< dec
; cout
.width (8); cout
<< val
<< "(dec) = ";
482 cout
<< hex
; cout
.width (8); cout
<< (0xFFFF & val
) << "(hex) = ";
483 cout
<< oct
; cout
.width (8);
484 cout
<< (0xFFFF & val
) << "(oct) [showbase on]\n";
485 cout
.unsetf(ios::showbase
);
486 cout
<< dec
; cout
.width (8); cout
<< val
<< "(dec) = ";
487 cout
<< hex
; cout
.width (8); cout
<< (0xFFFF & val
) << "(hex) = ";
488 cout
<< oct
; cout
.width (8);
489 cout
<< (0xFFFF & val
) << "(oct) [showbase off]\n";
495 ios::fmtflags old_flags
= cout
.setf(ios::showpos
);
496 int fill
= cout
.fill('_');
497 cout
.unsetf(ios::uppercase
);
498 cout
.setf(ios::internal
, ios::adjustfield
);
501 cout
.setf(ios::right
, ios::adjustfield
);
503 cout
.setf(ios::uppercase
);
504 cout
.unsetf(ios::showpos
);
506 cout
.setf(ios::left
, ios::adjustfield
);
511 ios::adjustfield
|ios::basefield
512 |ios::showbase
|ios::showpos
|ios::uppercase
);
515 main(int argc
, char **argv
)
517 char temp
[1024] = "tempfile";
519 if (argc
> 1 && strncmp(argv
[1], "-b", 2) == 0) {
520 streambuf
*sb
= cout
.rdbuf();
522 int buffer_size
= atoi(&argv
[1][2]);
523 if (buffer_size
== 0)
524 ret
= sb
->setbuf(NULL
, 0);
526 ret
= sb
->setbuf(new char[buffer_size
], buffer_size
);
528 cerr
<< "Warning: cout.rdbuf()->setbuf failed!\n";
530 strncpy (&temp
[8], &argv
[1][2], 1000);
547 cout
<< "Final names & states:\n";
549 cout
<< "cin: " << cin
.name() << "\t" << cin
.rdstate() << "\n";
550 cout
<< "cout: " << cout
.name() << "\t" << cout
.rdstate() << "\n";
551 cout
<< "cerr: " << cerr
.name() << "\t" << cerr
.rdstate() << "\n";
553 cout
<< "cin: " << "(stdin)" << "\t" << cin
.rdstate() << "\n";
554 cout
<< "cout: " << "(stdout)" << "\t" << cout
.rdstate() << "\n";
555 cout
<< "cerr: " << "(stderr)" << "\t" << cerr
.rdstate() << "\n";
557 cout
<< "\nend of test.\n";