Annotation of GNUtools/libg++/libio/tests/tFile.cc, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.