home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / libg++-2.6-fsf.lha / libg++-2.6 / libio / tests / tiomisc.cc < prev    next >
C/C++ Source or Header  |  1994-02-08  |  1KB  |  74 lines

  1. /* Random regression tests etc. */
  2.  
  3. #include <fstream.h>
  4. #include <stdio.h>
  5. #include <strstream.h>
  6. #include <string.h>
  7.  
  8. #define BUF_SIZE 4096
  9.  
  10. void
  11. test1 ()
  12. {
  13.    fstream f;
  14.    char    buf[BUF_SIZE];
  15.  
  16.    f.setbuf( buf, BUF_SIZE );
  17. }
  18.  
  19. void
  20. test2 ( )
  21. {
  22.    char string[BUF_SIZE];
  23.    ostrstream s( string, BUF_SIZE );
  24.  
  25.    s << "Bla bla bla " << 55 << ' ' << 3.23 << '\0' << endl;
  26.    cout << "Test2: " << string << endl;
  27. }
  28.  
  29.  
  30. /* Test case from Joe Buck <jbuck@Synopsys.COM>. */
  31.  
  32. class special_ofstream : public ofstream {
  33. public:
  34.     special_ofstream() : ofstream() {}
  35.     special_ofstream(int fd) : ofstream(fd) {}
  36.     special_ofstream(const char *name, int mode=ios::out, int prot=0664) {
  37.         open(name,mode,prot);
  38.     }
  39.     void open(const char *name, int mode=ios::out, int prot=0664);
  40. };
  41.  
  42. void special_ofstream::open(const char* name, int mode, int prot) {
  43.     if (strcmp(name, "<cout>") == 0) {
  44.         rdbuf()->attach(1);
  45.     }
  46.     else if (strcmp(name, "<cerr>") == 0) {
  47.         rdbuf()->attach(2);
  48.         setf(unitbuf);
  49.     }
  50.     else ofstream::open(name,mode,prot);
  51. }
  52.  
  53. void
  54. test3 ()
  55. {
  56.     {
  57.         special_ofstream o("<cout>");
  58.         o << "Hello\n";
  59.         // o is destructed now.  This should not close cout
  60.     }
  61.     {
  62.         special_ofstream o("<cout>");
  63.         o << "Line 2\n";
  64.     }
  65. }
  66.  
  67. int main( )
  68. {
  69.   test1 ();
  70.   test2 ();
  71.   test3 ();
  72.   return 0;
  73. }
  74.