/////////////////////////////////////////////////////////////////// // stack.cpp - Test Stack class for errors // // // // This module provides a good example of how you would use // // the low-level test harness, provided by the Test and Suite // // modules. // // // // Language: Visual C++, ver 6.0, SP 4 // // Platform: Dell XPS1000, Windows 2000 Professional, SP 1 // // Author: Jim Fawcett, CST 2-187, Syracuse University // // jfawcett@twcny.rr.com, 315-443-3948 // // Source: // // The origianl design and implementation are by Chuck // // Allison (see ref, below), but I have modified his code // // and so take responsibility for this product. // /////////////////////////////////////////////////////////////////// #include "Stack.h" #include "test.h" #include using namespace std; /////////////////////////////////////////////////////////////////// // test class for stacks // class StackTest : public Test { enum {SIZE = 5}; Stack stk; public: StackTest() : stk(SIZE) {} void run(); void testBadSize(); void testUnderflow(); void testPopulate(); void testOverflow(); void testPop(); }; //----< define all the stack tests to be run >--------------------- inline void StackTest::run() { testUnderflow(); testPopulate(); testOverflow(); testPop(); testBadSize(); } // //----< test constructor error handling >-------------------------- inline void StackTest::testBadSize() { try { Stack s(0); _fail("Bad Size"); } catch (StackError&) { _succeed(); } } //----< test stack underflow error handling >---------------------- inline void StackTest::testUnderflow() { _test(stk.size() == 0); try { stk.top(); _fail("Underflow"); } catch (StackError&) { _succeed(); } try { stk.pop(); _fail("Underflow"); } catch (StackError&) { _succeed(); } } //----< test pushing elements onto stack >------------------------- inline void StackTest::testPopulate() { try { for (int i = 0; i < SIZE; ++i) stk.push(i); _succeed(); } catch (StackError&) { _fail("Populate"); } _test(stk.size() == SIZE); _test(stk.top() == SIZE-1); } // //----< induce intentional overflow to test error handling >------- inline void StackTest::testOverflow() { try { stk.push(SIZE); _fail("Overflow"); } catch (StackError&) { _succeed(); } } //----< pop until empty >------------------------------------------ inline void StackTest::testPop() { for (int i = 0; i < SIZE; ++i) _test(stk.pop() == SIZE-i-1); _test(stk.size() == 0); } //----< test stub >------------------------------------------------ int main() { StackTest t; t.setStream(&cout); t.run(); t.report(); cout << "\n\n"; return t.getNumFailed(); }