1 #include "Stack.h"
2 #include "test.h"
3 #include <iostream>
4 using namespace std;
5 
6 /*
7  * C/C++ Users Journal Sept 2000 <br>
8  * The Simplest Automated Unit Test Framework That Could Possibly Work <br>
9  * Chuck Allison <br>
10  */
11 
12 class StackTest : public Test
13 {
14     enum {SIZE = 5};
15     Stack<int> stk;
16 
17 public:
StackTest()18     StackTest() : stk(SIZE)
19     {}
20 
run()21     void run()
22     {
23         testUnderflow();
24         testPopulate();
25         testOverflow();
26         testPop();
27         testBadSize();
28     }
29 
testBadSize()30     void testBadSize()
31     {
32         try
33         {
34             Stack<int> s(0);
35             _fail("Bad Size");
36         }
37         catch (StackError&)
38         {
39             _succeed();
40         }
41     }
42 
testUnderflow()43     void testUnderflow()
44     {
45         _test(stk.size() == 0);
46 
47         try
48         {
49             stk.top();
50             _fail("Underflow");
51         }
52         catch (StackError&)
53         {
54             _succeed();
55         }
56 
57         try
58         {
59             stk.pop();
60             _fail("Underflow");
61         }
62         catch (StackError&)
63         {
64             _succeed();
65         }
66     }
67 
testPopulate()68     void testPopulate()
69     {
70         try
71         {
72             for (int i = 0; i < SIZE; ++i)
73                 stk.push(i);
74             _succeed();
75         }
76         catch (StackError&)
77         {
78             _fail("Populate");
79         }
80 
81         _test(stk.size() == SIZE);
82         _test(stk.top() == SIZE-1);
83     }
84 
testOverflow()85     void testOverflow()
86     {
87         try
88         {
89             stk.push(SIZE);
90             _fail("Overflow");
91         }
92         catch (StackError&)
93         {
94             _succeed();
95         }
96     }
97 
testPop()98     void testPop()
99     {
100         for (int i = 0; i < SIZE; ++i)
101             _test(stk.pop() == SIZE-i-1);
102         _test(stk.size() == 0);
103     }
104 };
105 
main()106 int main()
107 {
108     StackTest t;
109     t.setStream(&cout);
110     t.run();
111     t.report();
112 }
113 
114 /* Output:
115 Test "class StackTest":
116         Passed: 14      Failed: 0
117 */
118 
119