00001 /* -*- Mode:C++ -*- */ 00002 #include "StackTest.hh" 00003 #include <iostream> 00004 00005 StackTest::StackTest() : 00006 stk(SIZE), 00007 UnitTest("StackTest") 00008 { 00009 } 00010 00011 void 00012 StackTest::run_tests() 00013 { 00014 testUnderflow(); 00015 testPopulate(); 00016 testOverflow(); 00017 testPop(); 00018 testBadSize(); 00019 } 00020 00024 void 00025 StackTest::testBadSize() 00026 { 00027 try { 00028 Stack<int> s(0); 00029 _fail("Bad Size"); 00030 } catch (StackError&) { 00031 _succeed(); 00032 } 00033 } 00034 00038 void 00039 StackTest::testUnderflow() 00040 { 00041 00042 _test(stk.size() == 0); 00043 00044 try { 00045 stk.top(); 00046 _fail("Underflow"); 00047 } catch (StackError&) { 00048 _succeed(); 00049 } 00050 00051 try { 00052 stk.pop(); 00053 _fail("Underflow"); 00054 } catch (StackError&) { 00055 _succeed(); 00056 } 00057 } 00058 00062 void 00063 StackTest::testPopulate() 00064 { 00065 try 00066 { 00067 for (int i = 0; i < SIZE; ++i) 00068 stk.push(i); 00069 _succeed(); 00070 } 00071 catch (StackError&) 00072 { 00073 _fail("Populate"); 00074 } 00075 00076 _test(stk.size() == SIZE); 00077 _test(stk.top() == SIZE-1); 00078 } 00079 00083 void 00084 StackTest::testOverflow() 00085 { 00086 try 00087 { 00088 stk.push(SIZE); 00089 _fail("Overflow"); 00090 } 00091 catch (StackError&) 00092 { 00093 _succeed(); 00094 } 00095 } 00096 00100 void 00101 StackTest::testPop() 00102 { 00103 for (int i = 0; i < SIZE; ++i) 00104 _test(stk.pop() == SIZE-i-1); 00105 _test(stk.size() == 0); 00106 }