home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / g / gnu_c / gpplib22.zoo / libtests / tobstack.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-30  |  1.6 KB  |  83 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. extern "C" { unsigned long _stksize = 64*1024; };
  14.  
  15. #include <stream.h>
  16. #include <xobstack.h>
  17. #include <stddef.h>
  18. #include <ctype.h>
  19.  
  20. int
  21. main()
  22. {
  23.   char*   s[10000];
  24.   int     n = 0;
  25.   int     got_one = 0;
  26.   Obstack os;
  27.   char    c;
  28.  
  29.   s[n++] = (char *)os.copy("\nunique words:");
  30.   assert(os.OK());
  31.   assert(os.contains(s[0]));
  32.  
  33.   cout << "enter anything at all, end with an EOF(^D)\n";
  34.  
  35.   while (cin.good() && n < 10000)
  36.   {
  37.     if (cin.get(c) && isalnum(c))
  38.     {
  39.       got_one = 1;
  40.       os.grow(c);
  41.     }
  42.     else if (got_one)
  43.     {
  44.       char* current = (char *)os.finish(0);
  45.       for (int i = 0; i < n; ++i) // stupid, but this is only a test.
  46.       {
  47.         if (strcmp(s[i], current) == 0)
  48.         {
  49.           os.free(current);
  50.           current = 0;
  51.           break;
  52.         }
  53.       }
  54.       if (current != 0)
  55.         s[n++] = current;
  56.       got_one = 0;
  57.     }
  58.   }
  59.   assert(os.OK());
  60.  
  61.   cout << s[0] << "\n";
  62.  
  63.   for (int i = n - 1; i > 0; -- i)
  64.   {
  65.     assert(os.contains(s[i]));
  66.     cout << s[i] << "\n";
  67.     os.free(s[i]);
  68.   }
  69.  
  70.   assert(os.OK());
  71.   assert(os.contains(s[0]));
  72.  
  73.   cout << "\n\nObstack vars:\n";
  74.   cout << "alignment_mask = " << os.alignment_mask() << "\n";
  75.   cout << "chunk_size = " << os.chunk_size() << "\n";
  76.   cout << "size = " << os.size() << "\n";
  77.   cout << "room = " << os.room() << "\n";
  78.  
  79.   cout << "\nend of test\n";
  80.  
  81.   return 0;
  82. }
  83.