#include #include "stackP.h" #include using namespace std; bool VERBOSE = false; void Compare(StackT user, stack rule) { ItemT topUser, topRule; while (rule.size() > 0 ) { topUser = user.Top(); topRule = rule.top(); if (topUser != topRule) { cout << "Error, item mismatch " << endl; cout << "\tUser item: " << topUser << endl; cout << "\tTest item: " << topRule << endl; cout << endl; exit(1); } user.Pop(); rule.pop(); } return; } void TestEm(StackT & user, stack& rule) { ItemT a,b; // check size if (user.Size() != int(rule.size()) ) { cout << "Size error " << endl; cout << "\tShould Be: " << rule.size() << endl; cout << "\tIs : " << user.Size() << endl; cout << endl; exit(1); } // check isempty if (user.IsEmpty() != (rule.size()==0)) { cout << "IsEmpty error" << endl; cout << "\tShould be: " << boolalpha << (rule.size()==0) << endl; cout << "\tIs : " << boolalpha << user.IsEmpty() << endl; cout << endl; exit(0); } // check top if (rule.size() > 0) { a = user.Top(); b = rule.top(); if (a != b) { cout << "Top error " << endl; cout << "\tShould Be: " << b << endl; cout << "\tIs : " << a << endl; cout << endl; exit(0); } } // check contents. Compare(user, rule); return; } int main() { StackT user; stack rule; int i; if (VERBOSE) { cout << "Empty stack test " << endl; } TestEm(user,rule); for(i=0;i<100;i++) { if (VERBOSE) { cout << " Pushing item " << i << " stack holds 0 .. " << i << endl; } user.Push(i); rule.push(i); TestEm(user,rule); } while (user.Size() > 0) { if (VERBOSE) { cout << " Popping item " << rule.top() << " stack holds 0 .. " << rule.top()- 1 << endl; } user.Pop(); rule.pop(); TestEm(user,rule); } cout << " All tests completed " << endl; return 0; }