home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libio / tests / tFile.cc < prev    next >
C/C++ Source or Header  |  1994-05-06  |  13KB  |  551 lines

  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 =