#include #include #include #include "QueueT.h" #include #include using namespace std; void ConstructorTest(); void SimpleTest(); bool Compare( QueueT q, vector standard); void EqualTest(); void DeepCopyTest(); void Level1(QueueT q, vector standard); void Level2(QueueT q, vector standard); void ErrorTest(); void DestructorTest(); void StressTest(); const int TRIALS{100'000}; const float PUSH_CHANCE{.65}; int main() { cout << "Constructor Test " << endl; ConstructorTest(); cout << "Simple Test " << endl; SimpleTest(); cout << "Equal Test " << endl; EqualTest(); cout << "Deep Copy Test" << endl; DeepCopyTest(); cout << "Error Test" << endl; ErrorTest(); cout << "Destructor Test" << endl; DestructorTest(); cout << "Stress Test" << endl; StressTest(); cout << "End of tests" << endl; return 0; } void StressTest() { QueueT a; int i; vector v; float chance; int num; int enqueue{0}; int dequeue{0}; size_t longest{0}; srand(time(nullptr)); for(i =0; i < TRIALS; ++i) { chance = rand()/ static_cast(RAND_MAX); if (chance > PUSH_CHANCE) { if (a.Size() > 0) { num = a.Front(); a.Dequeue(); if (v[0] != num) { cout << "Error in StressTest, num = " << num << " i = " << i << endl; return; } v.erase(v.begin(), v.begin()+1); ++dequeue; } } else { num = rand(); a.Enqueue(num); v.push_back(num); enqueue++; } longest = max(longest, a.Size()); } Compare(a,v); cout << "\tTest Complete" << endl; cout << "\tEnqueue calls: " << setw(10) << enqueue << endl; cout << "\tDequeue calls: " << setw(10) << dequeue << endl; cout << "\tLongest Queue: " << setw(10) << longest << endl; } // this one will not report an error, but will cause problems if there is a // problem in the destructor. IE It provides fodder for valgrind. // Added after class discussion void DestructorTest(){ QueueT a,b,c; b.Enqueue(1); c.Enqueue(2); c.Enqueue(3); } void ErrorTest() { string tmp; std::stringstream stream; auto old = cerr.rdbuf(stream.rdbuf()); QueueT q; q.Dequeue(); tmp = stream.str(); if (tmp != "Attempt to call dequeue on an empty queue.\n") { cout << "Failed in Error Test dequeue" << endl; } stream.str(""); q.Front(); tmp = stream.str(); if (tmp != "Attempt to call front on an empty queue.\n") { cout << "Failed in Error Test front" << endl; } cerr.rdbuf(old); } void DeepCopyTest() { QueueT a; vectorstandard; int i; for(i = 0; i < 10; i++) { a.Enqueue(i); standard.push_back(i); Level1(a, standard); if (!Compare(a, standard)) { cout << "Failed in Deep Copy Test part Ia, i = " << i << endl; } } while (standard.size() > 0) { a.Dequeue(); standard.erase(begin(standard), begin(standard)+1); if (!Compare(a, standard)) { cout << "Failed in Deep Copy Test part IIa, i = " << i << endl; } } return; } void Level1(QueueT q, vector standard) { if(!Compare(q,standard)) { cout << "Failed in Level 1 I" << endl; } Level2(q, standard); if(!Compare(q,standard)) { cout << "Failed in Level 1 II" << endl; } } void Level2(QueueT q, vector standard){ if(!Compare(q,standard)){ cout << "Failed in Level 2" << endl; } } void EqualTest() { QueueT a,b,c; vector standard; int i; for(i = 0; i < 10; i++) { a.Enqueue(i); b = c = a; // added post class discussion a = a; standard.push_back(i); if (!Compare(a, standard)) { cout << "Failed in EqualTest part Ia, i = " << i << endl; } if (!Compare(b, standard)) { cout << "Failed in EqualTest part Ib, i = " << i << endl; } if (!Compare(c, standard)) { cout << "Failed in EqualTest part Ic, i = " << i << endl; } } while (standard.size() > 0) { a.Dequeue(); b = a; c = b; // added post class discussion a = a; standard.erase(begin(standard), begin(standard)+1); if (!Compare(a, standard)) { cout << "Failed in EqualTest part IIa, i = " << i << endl; } if (!Compare(b, standard)) { cout << "Failed in EqualTest part IIb, i = " << i << endl; } if (!Compare(c, standard)) { cout << "Failed in EqualTest part IIc, i = " << i << endl; } } } void SimpleTest() { QueueT q; vector standard; size_t i; size_t tmp; if (!Compare(q,standard)) { cout << "Failed in SimpleTest part I"<< endl; } for(i = 0; i < 10; i++) { q.Enqueue(i); standard.push_back(i); if (!Compare(q,standard)) { cout << "Failed in SimpleTest part II, i = " << i << endl; } } i = 0; while(standard.size() > 0 ) { if (q.Size() == 0) { cout << "Failed in SimpleTest part IIIa, size = " << standard.size() << endl; } else { tmp = q.Front(); q.Dequeue(); if (tmp != i) { cout << "Failed in SimpleTest part IIIb, i = " << i << " tmp = " << tmp << endl; } standard.erase(standard.begin(), standard.begin()+1); if (!Compare(q,standard)) { cout << "Failed in SimpleTest part IIIc, i = " << i << endl; } } ++i; } if (q.Size() != 0) { cout << "Failed in SimpleTest part IV, size = " << q.Size() << endl; } } bool Compare(QueueT q, vector standard) { int tmp; if (q.Size() != standard.size()) { cout << "Compare, Sizes are different" << endl; return false; } for(size_t i = 0; i < standard.size(); ++i) { tmp = q.Front(); q.Dequeue(); if (tmp != standard[i]) { cout << "Compare, " << standard[i] << " != " << tmp << endl; return false; } } if (q.Size() != 0) { cout << "Compare, queue not empty" << endl; return false; } return true; } void ConstructorTest(){ QueueT q; if (q.Size() != 0) { cout << "Error, invalid Size for new queue" << endl; } }