1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
|
/*
Copyright (C) 1993 Free Software Foundation
This file is part of the GNU IO Library. This library is free
software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option)
any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
As a special exception, if you link this library with files
compiled with a GNU compiler to produce an executable, this does not cause
the resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why
the executable file might be covered by the GNU General Public License. */
// This may look like C code, but it is really -*- C++ -*-
/*
* a few tests for streams
*
*/
#include <stream.h>
#include <fstream.h>
#ifndef _OLD_STREAMS
#include <strstream.h>
#include "unistd.h"
#endif
#include <SFile.h>
#include <PlotFile.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
const char *tempfile;
class record
{
public:
char c; int i; double d;
};
ostream& operator<<(ostream& s, record& r)
{
return(s << "(i = " << r.i << " c = " << r.c << " d = " << r.d << ")");
}
void t1()
{
char ch;
assert(cout.good());
assert(cout.writable());
assert(cout.is_open());
cout << "Hello, world via cout\n";
assert(cerr.good());
assert(cerr.writable());
assert(cerr.is_open());
cerr << "Hello, world via cerr\n";
assert(cin.good());
assert(cin.readable());
assert(cin.is_open());
cout << "enter a char:"; cin >> ch;
cout.put('c'); cout.put(' '); cout.put('='); cout.put(' ');
cout.put('"'); cout.put(ch); cout << '"'; cout << char('\n');
assert(cin.good());
assert(cout.good());
}
void t2()
{
int i;
short h;
long l;
float f;
double d;
char s[100];
cout << "enter three integers (short, int, long):";
cin >> h; cin >> i;
// cin.scan("%ld", &l);
cin >> l;
cout << "first = " << h << " via dec = " << dec(h, 8) << "\n";
cout << "second = " << i << form(" via form = %d = 0%o", i, i);
cout.form(" via cout.form = %d = 0x%x\n", i, i);
cout << "third = " << l << " via hex = " << hex(l) << "\n";
assert(cin.good());
assert(cout.good());
cout << "enter a float then a double:"; cin >> f; cin >> d;
cout << "first = " << f << "\n";
cout << "second = " << d << "\n";
assert(cin.good());
assert(cout.good());
cout << "enter 5 characters separated with spaces:"; cin >> s;
cout << "first = " << s << "\n";
cin.get(s, 100);
cout << "rest = " << s << "\n";
assert(cin.good());
cin.width(10);
cin >> s;
cin.clear();
cout << "A 10-character buffer: " << s << endl;
assert(cout.good());
}
void t3()
{
char ch;
cout << "\nMaking streams sout and sin...";
#ifdef _OLD_STREAMS
ostream sout("streamfile", io_writeonly, a_create);
#else
ofstream sout("streamfile");
#endif
assert(sout.good());
assert(sout.is_open());
assert(sout.writable());
assert(!sout.readable());
sout << "This file has one line testing output streams.\n";
sout.close();
assert(!sout.is_open());
#ifdef _OLD_STREAMS
istream sin("streamfile", io_readonly, a_useonly);
#else
ifstream sin("streamfile");
#endif
assert(sin.good());
assert(sin.is_open());
assert(!sin.writable());
assert(sin.readable());
cout << "contents of file:\n";
while(sin >> ch) cout << ch;
sin.close();
assert(!sin.is_open());
}
void t4()
{
char s[100];
char ch;
int i;
cout << "\nMaking File tf ... ";
#ifdef _OLD_STREAMS
File tf(tempfile, io_readwrite, a_create);
#else
fstream tf(tempfile, ios::in|ios::out|ios::trunc);
#endif
assert(tf.good());
assert(tf.is_open());
assert(tf.writable());
assert(tf.readable());
strcpy(s, "This is the first and only line of this file.\n");
#ifdef _OLD_STREAMS
tf.put(s);
tf.seek(0);
#else
tf << s;
tf.rdbuf()->seekoff(0, ios::beg);
#endif
tf.get(s, 100);
assert(tf.good());
cout << "first line of file:\n" << s << "\n";
cout << "next char = ";
tf.get(ch);
cout << (int)ch;
cout.put('\n');
assert(ch == 10);
strcpy(s, "Now there is a second line.\n");
cout << "reopening tempfile, appending: " << s;
#ifdef _OLD_STREAMS
tf.open(tf.name(), io_appendonly, a_use);
#else
tf.close();
tf.open(tempfile, ios::app);
#endif
assert(tf.good());
assert(tf.is_open());
assert(tf.writable());
assert(!tf.readable());
#ifdef _OLD_STREAMS
tf.put(s);
assert(tf.good());
tf.open(tf.name(), io_readonly, a_use);
#else
tf << s;
assert(tf.good());
tf.close();
tf.open(tempfile, ios::in);
#endif
tf.raw();
assert(tf.good());
assert(tf.is_open());
assert(!tf.writable());
assert(tf.readable());
cout << "First 10 chars via raw system read after reopen for input:\n";
read(tf.filedesc(), s, 10);
assert(tf.good());
for (i = 0; i < 10; ++ i)
cout.put(s[i]);
lseek(tf.filedesc(), 5, 0);
cout << "\nContents after raw lseek to pos 5:\n";
while ( (tf.get(ch)) && (cout.put(ch)) );
#ifdef _OLD_STREAMS
tf.remove();
#else
tf.close();
unlink((char*)tempfile);
#endif
assert(!tf.is_open());
}
void t5()
{
record r;
int i;
cout << "\nMaking SFile rf...";
#ifdef _OLD_STREAMS
SFile rf("recfile", sizeof(record), io_readwrite, a_create);
#else
SFile rf("recfile", sizeof(record), ios::in|ios::out|ios::trunc);
#endif
assert(rf.good());
assert(rf.is_open());
assert(rf.writable());
assert(rf.readable());
for (i = 0; i < 10; ++i)
{
r.c = i + 'a';
r.i = i;
r.d = (double)(i) / 1000.0;
rf.put(&r);
}
assert(rf.good());
cout << "odd elements of file in reverse order:\n";
for (i = 9; i >= 0; i -= 2)
{
rf[i].get(&r);
assert(r.c == i + 'a');
assert(r.i == i);
cout << r << "\n";
}
assert(rf.good());
#ifdef _OLD_STREAMS
rf.remove();
#else
rf.close();
unlink("recfile");
#endif
assert(!rf.is_open());
}
void t6()
{
cout << "\nMaking PlotFile pf ...";
static const char plot_name[] = "plot.out";
PlotFile pf(plot_name);
assert(pf.good());
assert(pf.is_open());
assert(pf.writable());
assert(!pf.readable());
pf.move(10,10);
pf.label("Test");
pf.circle(300,300,200);
pf.line(100, 100, 500, 500);
assert(pf.good());
#ifdef _OLD_STREAMS
cout << "(You may delete or attempt to plot " << pf.name() << ")\n";
#else
cout << "(You may delete or attempt to plot " << plot_name << ")\n";
#endif
}
void t7()
{
char ch;
static char t7_line1[] = "This is a string-based stream.\n";
static char t7_line2[] = "With two lines.\n";
char mybuf[60];
char *bufp;
#ifdef _OLD_STREAMS
cout << "creating string-based ostream...\n";
ostream strout(60, mybuf);
#else
cout << "creating ostrstream...\n";
ostrstream strout(mybuf, 60);
#endif
assert(strout.good());
assert(strout.writable());
strout << t7_line1 << t7_line2 << ends;
assert(strout.good());
cout << "with contents:\n";
bufp = strout.str();
assert(bufp == mybuf);
strout.rdbuf()->freeze(0); /* Should be a no-op */
cout << mybuf;
#ifdef _OLD_STREAMS
cout << "using it to create string-based istream...\n";
istream strin(strlen(mybuf), mybuf);
#else
cout << "using it to create istrstream...\n";
istrstream strin(mybuf, strlen(mybuf));
#endif
assert(strin.good());
assert(strin.readable());
cout << "with contents:\n";
#ifndef _OLD_STREAMS
char line[100];
strin.getline(line, 100);
int line1_len = strlen(t7_line1);
assert(strin.tellg() == line1_len);
int line_len = strin.gcount();
assert(line_len == line1_len);
cout.write(line, line1_len - 1);
cout << endl;
#endif
while (strin.get(ch)) cout.put(ch);
strstream str1;
strstream str2;
str1 << "Testing string-based stream using strstream.\n";
str1.seekg(0);
for (;;) {
int i = str1.get();
if (i == EOF)
break;
str2 << (char)i;
}
str2 << ends;
cout << str2.str();
// This should make it overflow.
strout << t7_line1;
assert (strout.bad());
}
void t8()
{
#ifdef _OLD_STREAMS
cout << "\nThe following file open should generate error message:";
cout.flush();
File ef("shouldnotexist", io_readonly, a_useonly);
#else
ifstream ef("shouldnotexist");
#endif
assert(!ef.good());
assert(!ef.is_open());
}
void t9()
{
char ch;
static char ffile_name[] = "ftmp";
{
cout << "\nMaking filebuf streams fout and fin...";
filebuf foutbuf;
#ifdef _OLD_STREAMS
foutbuf.open(ffile_name, output);
#else
foutbuf.open(ffile_name, ios::out);
#endif
ostream fout(&foutbuf);
assert(fout.good());
assert(fout.is_open());
assert(fout.writable());
assert(!fout.readable());
fout << "This file has one line testing output streams.\n";
#ifdef _OLD_STREAMS
fout.close();
assert(!fout.is_open());
#endif
}
filebuf finbuf;
#ifdef _OLD_STREAMS
finbuf.open(ffile_name, input);
#else
finbuf.open(ffile_name, ios::in);
#endif
istream fin(&finbuf);
assert(fin.good());
assert(fin.is_open());
assert(!fin.writable());
assert(fin.readable());
cout << "contents of file:\n";
while(fin >> ch) cout << ch;
#ifndef _OLD_STREAMS
cout << '\n';
#endif
fin.close();
assert(!fin.is_open());
}
void t10()
{
int fileCnt = 3;
char *file_name_pattern = "ftmp%d";
char current_file_name[50];
ifstream inFile;
ofstream outFile;
char c;
int i;
cout << '\n';
// Write some files.
for (i=0; i < fileCnt; i++) {
sprintf(current_file_name, file_name_pattern, i);
outFile.open(current_file_name, ios::out);
if ( outFile.fail() )
cerr << "File " << current_file_name
<< " can't be opened for output" << endl;
else {
outFile << "This is line 1 of " << current_file_name << '\n';
outFile << "This is line 2 of " << current_file_name << endl;
outFile.close();
}
}
// Now read the files back in, and write then out to cout.
for (i=0; i < fileCnt; i++) {
sprintf(current_file_name, file_name_pattern, i);
inFile.open(current_file_name, ios::in);
if ( inFile.fail() )
cerr << "File " << current_file_name
<< " can't be opened for input" << endl;
else {
while ( inFile.get (c))
cout << c;
cout << endl;
inFile.close();
}
}
}
// Test form
void t11()
{
int count1, count2;
cout.form("%.2f+%.2f = %4.3e\n%n", 5.5, 6.25, 5.5+6.25, &count1);
char *text = "Previous line has12345";
char text_length_to_use = strlen(text) - 5;
count2 = cout.rdbuf()->form("%-*.*s%3g characters\n",
text_length_to_use + 1,
text_length_to_use,
text,
(double)(count1-1));
cout.form("%-*.*s%+d characters\n%n",
text_length_to_use + 1, text_length_to_use, text,
count2-1, &count1);
assert(count1 == 33);
}
static void
show_int (long val)
{
cout.setf(ios::showbase);
cout << dec; cout.width (8); cout << val << "(dec) = ";
cout << hex; cout.width (8); cout << (0xFFFF & val) << "(hex) = ";
cout << oct; cout.width (8);
cout << (0xFFFF & val) << "(oct) [showbase on]\n";
cout.unsetf(ios::showbase);
cout << dec; cout.width (8); cout << val << "(dec) = ";
cout << hex; cout.width (8); cout << (0xFFFF & val) << "(hex) = ";
cout << oct; cout.width (8);
cout << (0xFFFF & val) << "(oct) [showbase off]\n";
}
void
t12 ()
{
ios::fmtflags old_flags = cout.setf(ios::showpos);
int fill = cout.fill('_');
cout.unsetf(ios::uppercase);
cout.setf(ios::internal, ios::adjustfield);
show_int(34567);
show_int(-34567);
cout.setf(ios::right, ios::adjustfield);
show_int(0);
cout.setf(ios::uppercase);
cout.unsetf(ios::showpos);
show_int(34567);
cout.setf(ios::left, ios::adjustfield);
show_int(-34567);
cout.fill(fill);
show_int(0);
cout.setf(old_flags,
ios::adjustfield|ios::basefield
|ios::showbase|ios::showpos|ios::uppercase);
}
main(int argc, char **argv)
{
char temp [1024] = "tempfile";
if (argc > 1 && strncmp(argv[1], "-b", 2) == 0) {
streambuf *sb = cout.rdbuf();
streambuf *ret;
int buffer_size = atoi(&argv[1][2]);
if (buffer_size == 0)
ret = sb->setbuf(NULL, 0);
else
ret = sb->setbuf(new char[buffer_size], buffer_size);
if (ret != sb)
cerr << "Warning: cout.rdbuf()->setbuf failed!\n";
strncpy (&temp [8], &argv[1][2], 1000);
temp [1008] = '\0';
}
tempfile = temp;
t1();
t2();
t3();
t4();
t5();
t6();
t7();
t9();
t8();
t10();
t11();
t12();
cout << "Final names & states:\n";
#ifdef _OLD_STREAMS
cout << "cin: " << cin.name() << "\t" << cin.rdstate() << "\n";
cout << "cout: " << cout.name() << "\t" << cout.rdstate() << "\n";
cout << "cerr: " << cerr.name() << "\t" << cerr.rdstate() << "\n";
#else
cout << "cin: " << "(stdin)" << "\t" << cin.rdstate() << "\n";
cout << "cout: " << "(stdout)" << "\t" << cout.rdstate() << "\n";
cout << "cerr: " << "(stderr)" << "\t" << cerr.rdstate() << "\n";
#endif
cout << "\nend of test.\n";
}
|