home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / g__lib / tobstack.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  1.6 KB  |  80 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2.  
  3. /*
  4.  a little test of Obstacks
  5.  Thu Feb 18 11:16:28 1988  Doug Lea  (dl at rocky.oswego.edu)
  6. */
  7.  
  8. #include <assert.h>
  9.  
  10. #define tassert(ex) {if ((ex)) cerr << #ex << "\n"; \
  11.                        else _assert(#ex, __FILE__,__LINE__); }
  12.  
  13. #include <stream.h>
  14. #include <Obstack.h>
  15. #include <stddef.h>
  16. #include <ctype.h>
  17.  
  18. main()
  19. {
  20.   char*   s[10000];
  21.   int     n = 0;
  22.   bool    got_one = FALSE;
  23.   Obstack os;
  24.   char    c;
  25.  
  26.   s[n++] = os.copy("\nunique words:");
  27.   assert(os.OK());
  28.   assert(os.contains(s[0]));
  29.  
  30.   cout << "enter anything at all, end with an EOF(^D)\n";
  31.  
  32.   while (cin.good() && n < 10000)
  33.   {
  34.     if (cin.get(c) && isalnum(c))
  35.     {
  36.       got_one = TRUE;
  37.       os.grow(c);
  38.     }
  39.     else if (got_one)
  40.     {
  41.       char* current = os.finish(0);
  42.       for (int i = 0; i < n; ++i) // stupid, but this is only a test.
  43.       {
  44.         if (strcmp(s[i], current) == 0)
  45.         {
  46.           os.free(current);
  47.           current = 0;
  48.           break;
  49.         }
  50.       }
  51.       if (current != 0)
  52.         s[n++] = current;
  53.       got_one = FALSE;
  54.     }
  55.   }
  56.   assert(os.OK());
  57.  
  58.   cout << s[0] << "\n";
  59.  
  60.   for (int i = n - 1; i > 0; -- i)
  61.   {
  62.     assert(os.contains(s[i]));
  63.     cout << s[i] << "\n";
  64.     os.free(s[i]);
  65.   }
  66.  
  67.   assert(os.OK());
  68.   assert(os.contains(s[0]));
  69.  
  70.   cout << "\n\nObstack vars:\n";
  71.   cout << "alignment_mask = " << os.alignment_mask() << "\n";
  72.   cout << "chunk_size = " << os.chunk_size() << "\n";
  73.   cout << "size = " << os.size() << "\n";
  74.   cout << "room = " << os.room() << "\n";
  75.  
  76.   cout << "\nend of test\n";
  77.  
  78. }
  79.  
  80.