#include #include "StackT.h" #include #include using namespace std; const string POP_ERROR = "Error, can not Pop empty stack.\n"; const string TOP_ERROR = "Error, can not Top empty stack.\n"; bool ConstructorTest(); bool SimplePushPop(); bool StressTest(); void DestructorTest(); bool PassByValueTest(StackT s, vector v, int depth); bool CopyConstructorTest() ; bool AssignmentOperatorTest() ; bool ErrorTest(); int main() { StackT s; vectorv; int i; if (!ConstructorTest()) { cout << "Constructor Test Failed " << endl; } if (!ErrorTest()) { cout << "Error Test Failed " << endl; } if (!SimplePushPop()) { cout << "Simple Push Pop Failed " << endl; } if (!CopyConstructorTest()) { cout << "Copy Constructor Test failed " << endl; } if (!AssignmentOperatorTest()) { cout << "Assignment Operator Test failed " << endl; } if (!StressTest()){ cout << "Stress Test Failed" << endl; } for(i = 3; i < 10; i++) { if (!PassByValueTest(s,v,i)) { cout << "Pass by Value Test Failed " << endl; } } DestructorTest(); return 0; } bool ConstructorTest() { // an empty stack should // 1. have a size of 0 StackT s; if (s.Size() != 0) { return false; } if (!s.IsEmpty() ) { return false; } return true; } bool SimplePushPop() { StackT s; s.Push(0); if (s.Size() != 1) { return false; } if (s.IsEmpty()) { return false; } if (s.Top() != 0) { return false; } s.Pop(); if (s.Size() != 0) { return false; } return true; } bool RandomAction(StackT & s, vector & v) { int tmp; int tmp1; if (rand()/RAND_MAX < 0.5) { tmp = rand() % 10000 - 5000; v.push_back(tmp); s.Push(tmp); } else { if (v.size() > 0) { tmp = v.back(); tmp1 = s.Top(); if (tmp != tmp1) { return false; } s.Pop(); v.pop_back(); } } return true; } bool StressTest() { vector standard; StackT s; for(int i =0; i < 1000; i++) { if (!RandomAction(s,standard)) { return false; } if (s.Size() != standard.size()) { return false; } } return true; } void DestructorTest() { StackT s; for(int i = 0; i < 10; i++) { s.Push(i); } return; } bool CompareStack(StackT s, vector v) { while (s.Size() > 0) { if (v.size() == 0) { return false; } if (s.Size() != v.size()) { return false; } if (s.Top() != v.back()) { return false; } s.Pop(); v.pop_back(); } if (v.size() != 0) { return false; } return true; } bool PassByValueTest( StackT s, vector v, int depth) { int trials; int limit{rand() % 10 + 3}; if (depth >= 0) { if(!CompareStack(s,v)) { return false; } else { for(trials = 0; trials < limit; trials++) { if (!RandomAction(s,v)) { return false; } } } if( !PassByValueTest(s,v, depth-1)) { return false; } } if(!CompareStack(s,v)) { return false; } return true; } bool CompareStacksDestructive(StackT & a, StackT & b) { if (a.Size() != b.Size()) { return false; } while (a.Size() > 0) { if (a.Top() != b.Top()) { return false; } a.Pop(); b.Pop(); } return true; } bool CopyConstructorTest() { StackT s; StackT q(s); int i; if (!CompareStacksDestructive(s,q)) { return false; } for(i = 0; i < 10; i++) { s.Push(i); } StackT t{s}; // I want to test these in place, not calling a function. if (s.Size() != t.Size() ) { return false; } while (s.Size() > 0) { if (s.Top() != t.Top() ) { return false; } s.Pop(); t.Pop(); } return true; } bool AssignmentOperatorTest(){ StackT s; vector copy; StackT t,u,v; int i; for(i =0; i < 1000; i++) { RandomAction(s, copy); t = s; u = v = t; if (!CompareStacksDestructive(u,t)) { return false; } } if(!CompareStack(v,copy)) { return false; } if (!CompareStacksDestructive(v,s)) { return false; } return true; } bool ErrorTest(){ StackT s; stringstream buffer; streambuf *sbuf = std::cout.rdbuf(); cout.rdbuf(buffer.rdbuf()); s.Pop(); if (buffer.str() != "Error, can not Pop empty stack.\n") { cout.rdbuf(sbuf); return false; } buffer.str(""); s.Top(); if (buffer.str() != "Error, can not Top empty stack.\n") { cout.rdbuf(sbuf); return false; } cout.rdbuf(sbuf); return true; }