#include #include "StackT.h" #include using namespace std; bool Compare(StackT s, vector v); bool ConstructorTest(); bool CopyConstructorTest(); bool PushPopTest(); void DestructorTest(); int main() { if (not ConstructorTest()) { cout << "Constructor test failed." << endl; return 0; } CopyConstructorTest(); if (not PushPopTest()) { cout << "PushPop test failed." << endl; } DestructorTest(); return 0; } bool CopyConstructorTest(){ StackT s; vector v; for(char c ='a'; c != 'f'; ++c) { s.Push(c); v.push_back(c); StackT b(s); StackT a{b}; StackT d; d.Push(c); d.Push(c); d = d; Compare(b,v); Compare(a,v); } return true; } void DestructorTest(){ StackT s; s.Push('a'); s.Push('b'); return; } bool PushPopTest(){ StackT s; vector v; char c; for(c ='a'; c != 'f'; ++c) { s.Push(c); v.push_back(c); if(not Compare(s,v)) { cout << "\tPush Pop failed for push of " << c << endl; return false; } } while (v.size() != 0) { char fromStack = s.Top(); char fromVector = v.back(); s.Pop(); v.pop_back(); if(not Compare(s,v)) { cout << "\tPush Pop failed for pop of " << fromVector << endl; return false; } if (fromStack != fromVector) { cout << "\tPush Pop value failed for pop of " << fromVector << endl; cout << "\t got " << fromStack << "." << endl; return false; } } if(not Compare(s,v)) { cout << "\tPush Pop Test error on empty." << endl; return false; } return true; } bool ConstructorTest(){ StackT s; cout << "Constructor Test" << endl; if (s.Size() != 0) { cout << "\tSize of a new stack is " << s.Size() << " but should be 0." << endl; return false; } // check for top and pop errors. if (s.Top() != DEFAULT_ITEM_T) { cout << "\tTop of an empty stack is wrong." << endl; cout << "\tShould be " << DEFAULT_ITEM_T << " but is " << s.Top() << "." << endl; return false; } return true; } bool Compare(StackT s, vector v){ if (s.Size() != v.size() ) { cout << "Compare function, sizes not the same." << endl; cout << "Stack = " << s.Size() << " should be " << v.size() << endl; return false; } while (v.size() != 0) { char fromStack = s.Top(); char fromVector = v.back(); s.Pop(); v.pop_back(); if (fromStack != fromVector) { cout << "Compare function, top values not equal." << endl; cout << "\tShould be " << fromVector << " but is " << fromStack << "." << endl; return false; } if (v.size() != s.Size() ) { cout << "Compare function, sizes not the same." << endl; cout << "Stack = " << s.Size() << " should be " << v.size() << endl; return false; } } return true; }