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

  1. /*
  2.   test of stacks
  3. */
  4.  
  5. #include <stream.h>
  6. #include <assert.h>
  7. #include "int.Stack.h"
  8.  
  9. #define tassert(ex) {if ((ex)) cerr << #ex << "\n"; \
  10.                        else _assert(#ex, __FILE__,__LINE__); }
  11.  
  12.  
  13. int SIZE;
  14.  
  15. void print(intStack& a)
  16. {
  17.   int maxprint = 20;
  18.   cout << "[";
  19.   int k = 0;
  20.   while (!a.empty() && k++ < maxprint)
  21.     cout << a.pop() << " ";
  22.   if (k == maxprint) 
  23.     cout << "]\n";
  24.   else
  25.   {
  26.     while (!a.empty()) a.del_top();
  27.     cout << "...]\n";
  28.   }
  29.   assert(a.empty());
  30. }
  31.  
  32. #include "int.XPStack.h"
  33.  
  34. void XPtest () 
  35. {
  36.   intXPStack s(SIZE/2);
  37.   assert(s.OK());
  38.   for (int i = 0; i < SIZE; ++i)
  39.     s.push(i);
  40.   assert(s.length() == SIZE);
  41.   assert(s.top() == (SIZE-1));
  42.   assert(!s.full());
  43.   intXPStack s1(SIZE*2);
  44.   for (i = 0; i < SIZE; ++i)
  45.   {
  46.     int x = s.pop();
  47.     assert(x == (SIZE-1) - i);
  48.     s1.push(x);
  49.   }
  50.   assert(s.empty());
  51.   assert(s1.length() == SIZE);
  52.   assert(s1.top() == 0);
  53.   assert(s.OK());
  54.   assert(s1.OK());
  55.   intXPStack s2 (s1);
  56.   assert(s2.length() == SIZE);
  57.   assert(s2.top() == 0);
  58.   assert(s2.OK());
  59.   s1.clear();
  60.   assert(s1.empty());
  61.   s1 = s2;
  62.   assert(s1.length() == SIZE);
  63.   assert(s1.top() == 0);
  64.   assert(s1.OK());
  65.   s1.del_top();
  66.   assert(s1.length() == (SIZE-1));
  67.   assert(s1.top() == 1);
  68.   cout << "s1:"; print(s1);
  69.   assert(s.OK());
  70.   assert(s1.OK());
  71.   assert(s2.OK());
  72. }
  73.  
  74. #include "int.VStack.h"
  75.  
  76.  
  77. void Vtest () 
  78. {
  79.   intVStack s(SIZE);
  80.   assert(s.OK());
  81.   for (int i = 0; i < SIZE; ++i)
  82.     s.push(i);
  83.   assert(s.length() == SIZE);
  84.   assert(s.top() == (SIZE-1));
  85.   assert(s.full());
  86.   intVStack s1(SIZE);
  87.   for (i = 0; i < SIZE; ++i)
  88.   {
  89.     int x = s.pop();
  90.     assert(x == (SIZE-1) - i);
  91.     s1.push(x);
  92.   }
  93.   assert(s.empty());
  94.   assert(s1.length() == SIZE);
  95.   assert(s1.top() == 0);
  96.   assert(s.OK());
  97.   assert(s1.OK());
  98.   intVStack s2 (s1);
  99.   assert(s2.length() == SIZE);
  100.   assert(s2.top() == 0);
  101.   assert(s2.OK());
  102.   s1.clear();
  103.   assert(s1.empty());
  104.   s1 = s2;
  105.   assert(s1.length() == SIZE);
  106.   assert(s1.top() == 0);
  107.   assert(s1.OK());
  108.   s1.del_top();
  109.   assert(s1.length() == (SIZE-1));
  110.   assert(s1.top() == 1);
  111.   cout << "s1:"; print(s1);
  112.  
  113.   assert(s.OK());
  114.   assert(s1.OK());
  115.   assert(s2.OK());
  116. }
  117.  
  118. #include "int.SLStack.h"
  119.  
  120. void SLtest () 
  121. {
  122.   intSLStack s;
  123.   assert(s.OK());
  124.   for (int i = 0; i < SIZE; ++i)
  125.     s.push(i);
  126.   assert(s.length() == SIZE);
  127.   assert(s.top() == (SIZE-1));
  128.   assert(!s.full());
  129.   intSLStack s1;
  130.   for (i = 0; i < SIZE; ++i)
  131.   {
  132.     int x = s.pop();
  133.     assert(x == (SIZE-1) - i);
  134.     s1.push(x);
  135.   }
  136.   assert(s.empty());
  137.   assert(s1.length() == SIZE);
  138.   assert(s1.top() == 0);
  139.   assert(s.OK());
  140.   assert(s1.OK());
  141.   intSLStack s2 (s1);
  142.   assert(s2.length() == SIZE);
  143.   assert(s2.top() == 0);
  144.   assert(s2.OK());
  145.   s1.clear();
  146.   assert(s1.empty());
  147.   s1 = s2;
  148.   assert(s1.length() == SIZE);
  149.   assert(s1.top() == 0);
  150.   assert(s1.OK());
  151.   s1.del_top();
  152.   assert(s1.length() == (SIZE-1));
  153.   assert(s1.top() == 1);
  154.  
  155.   cout << "s1:"; print(s1);
  156.   assert(s.OK());
  157.   assert(s1.OK());
  158.   assert(s2.OK());
  159. }
  160.  
  161.  
  162. main(int argv, char** argc)
  163. {
  164.   if (argv > 1)
  165.   {
  166.     SIZE = abs(atoi(argc[1]));
  167.     SIZE &= ~1;
  168.   }
  169.   else
  170.     SIZE = 100;
  171.   start_timer();
  172.   cout << "XP stacks:\n"; XPtest();
  173.   cout << "\ntime = " << return_elapsed_time(0.0) << "\n";
  174.   start_timer();
  175.   cout << "V stacks:\n"; Vtest();
  176.   cout << "\ntime = " << return_elapsed_time(0.0) << "\n";
  177.   start_timer();
  178.   cout << "SL stacks:\n"; SLtest();
  179.   cout << "\ntime = " << return_elapsed_time(0.0) << "\n";
  180.   cout << "\nEnd of test\n";
  181. }
  182.