#include #include "DNodeT.h" #include "DListT.h" using namespace std; class Javert { public: Javert() { runTests(); } private: void runTests() { cout << "********Testing DListT********" << endl; cout << "Testing Constructor:" << endl; if (!testConstructor()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Copy Constructor:" << endl; if (!testCopyConstructor()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Assignment Operator:" << endl; if (!testAssignmentOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Clear:" << endl; if (!testClear()) { cout << "\t****Test Failed****" << endl; } cout << "Testing IsEmpty: " << endl; if (!testIsEmpty()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Begin:" << endl; if (!testBegin()) { cout << "\t****Test Failed****" << endl; } cout << "Testing End: " << endl; if (!testEnd()) { cout << "\tTestFailed" << endl; } cout << "Testing ConstBegin:" << endl; if (!testConstBegin()) { cout << "\t****Test Failed****" << endl; } cout << "Testing ConstEnd:" << endl; if (!testConstEnd()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Insert:" << endl; if (!testInsert()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Find:" << endl; if (!testFind()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Const Find:" << endl; if (!testConstFind()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Delete:" << endl; if (!testDelete()) { cout << "\t****Test Failed****" << endl; } cout << endl << "********Testing Iterator********" << endl; cout << "Testing Default Constructor" << endl; if (!testItrDefaultConstructor()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Private Constructor" << endl; if (!testItrPrivateConstructor()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Prefix Add Operator" << endl; if (!testItrPrefixAddOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Postfix Add Operator" << endl; if (!testItrPostfixAddOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Prefix Sub Operator" << endl; if (!testItrPrefixSubOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Postfix Sub Operator" << endl; if (!testItrPostfixSubOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Equals Operator" << endl; if (!testItrEqualsOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Not Equals Operator" << endl; if (!testItrNotEqualsOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Derefrence Operator" << endl; if (!testItrDerefrenceOperator()) { cout << "\t****Test Failed****" << endl; } cout << endl << "********Testing Constant Iterator********" << endl; cout << "Testing Default Constructor" << endl; if (!testConstItrDefaultConstructor()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Private Constructor" << endl; if (!testConstItrPrivateConstructor()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Prefix Add Operator" << endl; if (!testConstItrPrefixAddOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Postfix Add Operator" << endl; if (!testConstItrPostfixAddOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Prefix Sub Operator" << endl; if (!testConstItrPrefixSubOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Postfix Sub Operator" << endl; if (!testConstItrPostfixSubOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Equals Operator" << endl; if (!testConstItrEqualsOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Not Equals Operator" << endl; if (!testConstItrNotEqualsOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Derefrence Operator" << endl; if (!testConstItrDerefrenceOperator()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Copy Constructor" << endl; if (!testConstItrCopyConstructor()) { cout << "\t****Test Failed****" << endl; } cout << "Testing Assignment Operator" << endl; if (!testConstItrAssignmentOperator()) { cout << "\t****Test Failed****" << endl; } } bool testConstructor() { bool passed = true; try { bool passed = true; DListT list; if (list.Fore->Next != list.Aft) { cout << "\tError at 1" << endl; passed = false; } if (list.Fore->Prev != list.Fore) { cout << "\tError at 2" << endl; passed = false; } if (list.Aft->Next != list.Aft) { cout << "\tError at 3" << endl; passed = false; } if (list.Aft->Prev != list.Fore) { cout << "\tError at 4" << endl; passed = false; } } catch (...) { passed = false; cout << "Your code blew up when it wasn't supposed to." << endl; } return passed; } bool testCopyConstructor() { bool passed = true; try { DListT list; //test empty copy DListT list2(list); if (list2.Fore->Next != list2.Aft) { cout << "\tError at 1" << endl; passed = false; } if (list2.Fore->Prev != list2.Fore) { cout << "\tError at 2" << endl; passed = false; } if (list2.Aft->Next != list2.Aft) { cout << "\tError at 3" << endl; passed = false; } if (list2.Aft->Prev != list2.Fore) { cout << "\tError at 4" << endl; passed = false; } list.Insert(list.end(), 5); list.Insert(list.end(), 6); list.Insert(list.end(), 7); list.Insert(list.end(), 8); //test data copy DListT list3(list); if (list3.Fore->Prev != list3.Fore) { cout << "\tError at 5" << endl; passed = false; } if (list3.Aft->Next != list3.Aft) { cout << "\tError at 6" << endl; passed = false; } DNodeT *p = list3.Fore->Next; if (p->Element != 5) { cout << "\tError at 9" << endl; passed = false; } p = p->Next; if (p->Element != 6) { cout << "\tError at 10" << endl; passed = false; } p = p->Next; if (p->Element != 7) { cout << "\tError at 11" << endl; passed = false; } p = p->Next; if (p->Element != 8) { cout << "\tError at 12" << endl; passed = false; } p = p->Next; if (p != list3.Aft) { cout << "\tError at 13" << endl; passed = false; } //test deep copy list.Delete(list.begin()); if (*(list3.begin()) != 5) { cout << "\tError at 14" << endl; passed = false; } } catch (...) { cout << "Your code blew up when it wasn't supposed to" << endl; passed = false; } return passed; } bool testAssignmentOperator() { bool passed = true; try { DListT list; //test empty copy DListT list2; list2 = list; if (list2.Fore->Next != list2.Aft) { cout << "\tError at 1" << endl; passed = false; } if (list2.Fore->Prev != list2.Fore) { cout << "\tError at 2" << endl; passed = false; } if (list2.Aft->Next != list2.Aft) { cout << "\tError at 3" << endl; passed = false; } if (list2.Aft->Prev != list2.Fore) { cout << "\tError at 4" << endl; passed = false; } list.Insert(list.end(), 5); list.Insert(list.end(), 6); list.Insert(list.end(), 7); list.Insert(list.end(), 8); //test data copy DListT list3; list3 = list; if (list3.Fore->Prev != list3.Fore) { cout << "\tError at 5" << endl; passed = false; } if (list3.Aft->Next != list3.Aft) { cout << "\tError at 6" << endl; passed = false; } DNodeT *p = list3.Fore->Next; if (p->Element != 5) { cout << "\tError at 9" << endl; passed = false; } p = p->Next; if (p->Element != 6) { cout << "\tError at 10" << endl; passed = false; } p = p->Next; if (p->Element != 7) { cout << "\tError at 11" << endl; passed = false; } p = p->Next; if (p->Element != 8) { cout << "\tError at 12" << endl; passed = false; } p = p->Next; if (p != list3.Aft) { cout << "\tError at 13" << endl; passed = false; } //test deep copy list.Delete(list.begin()); if (*(list3.begin()) != 5) { cout << "\tError at 14" << endl; passed = false; } //test self assignment DListT list4; list4.Insert(list4.begin(), 5); list4 = list4; if (list4.Fore->Prev != list4.Fore) { cout << "\tError at 15" << endl; passed = false; } if (list4.Aft->Next != list4.Aft) { cout << "\tError at 16" << endl; passed = false; } if (list4.Fore->Next->Element != 5) { cout << "\tError at 17" << endl; passed = false; } if (list4.Fore->Next->Next != list4.Aft) { cout << "\tError at 18" << endl; passed = false; } } catch (...) { cout << "Your code blew up when it wasn't supposed to." << endl; passed = false; } return passed; } bool testClear() { bool passed = true; try { //test empty list DListT list; list.Clear(); if (list.Fore->Next != list.Aft) { cout << "\tError at 1" << endl; passed = false; } if (list.Fore->Prev != list.Fore) { cout << "\tError at 2" << endl; passed = false; } if (list.Aft->Next != list.Aft) { cout << "\tError at 3" << endl; passed = false; } if (list.Aft->Prev != list.Fore) { cout << "\tError at 4" << endl; passed = false; } //test list with data list.Insert(list.begin(), 1); list.Insert(list.begin(), 2); list.Insert(list.begin(), 3); list.Clear(); if (list.Fore->Next != list.Aft) { cout << "\tError at 5" << endl; passed = false; } if (list.Fore->Prev != list.Fore) { cout << "\tError at 6" << endl; passed = false; } if (list.Aft->Next != list.Aft) { cout << "\tError at 7" << endl; passed = false; } if (list.Aft->Prev != list.Fore) { cout << "\tError at 8" << endl; passed = false; } } catch (...) { cout << "Your code blew up when it wasn't supposed to." << endl; passed = false; } return passed; } bool testIsEmpty() { bool passed = true; try { DListT list; //test empty list if (!list.isEmpty()) { cout << "Error at 1" << endl; passed = false; } //test non-empty list list.Insert(list.begin(), 1); if (list.isEmpty()) { cout << "Error at 2" << endl; passed = false; } //test cleared list list.Clear(); if (!list.isEmpty()) { cout << "Error at 3" << endl; passed = false; } } catch (...) { cout << "Your code blew up when it wasn't supposed to." << endl; passed = false; } return passed; } bool testBegin() { bool passed = true; try { DListT list; //test empty list DListT::iterator itr = list.begin(); if (itr.Position != list.Aft) { cout << "Error at 1" << endl; passed = false; } //test list with data list.Insert(list.begin(), 1); itr = list.begin(); if (itr.Position != list.Fore->Next) { cout << "Error at 2" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testEnd() { bool passed = true; try { DListT list; //test empty list DListT::iterator itr = list.end(); if (itr.Position != list.Aft) { cout << "Error at 1" << endl; passed = false; } //test list with data list.Insert(list.begin(), 1); list.Insert(list.begin(), 2); list.Insert(list.begin(), 3); itr = list.end(); if (itr.Position != list.Aft) { cout << "Error at 2" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstBegin() { bool passed = true; try { const DListT list; //test empty list DListT::const_iterator itr = list.begin(); if (itr.Position != list.Aft) { cout << "Error at 1" << endl; passed = false; } //test list with data DListT list2; list2.Insert(list2.begin(), 1); const DListT list3(list2); itr = list3.begin(); if (itr.Position != list3.Fore->Next) { cout << "Error at 2" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstEnd() { bool passed = true; try { const DListT list; //test empty list DListT::const_iterator itr = list.end(); if (itr.Position != list.Aft) { cout << "Error at 1" << endl; passed = false; } DListT list2; //test list with data list2.Insert(list2.begin(), 1); list2.Insert(list2.begin(), 2); list2.Insert(list2.begin(), 3); const DListT list3(list2); itr = list3.end(); if (itr.Position != list3.Aft) { cout << "Error at 2" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testInsert() { bool passed = true; try { DListT list; //test inserting at the beginning list.Insert(list.begin(), 2); if (list.Fore->Next->Element != 2) { cout << "Error at 1" << endl; passed = false; } if (list.Fore->Next->Next != list.Aft) { cout << "Error at 2" << endl; passed = false; } if (list.Aft->Prev->Element != 2) { cout << "Error at 4" << endl; passed = false; } if (list.Aft->Prev->Prev != list.Fore) { cout << "Error at 5" << endl; passed = false; } list.Insert(list.begin(), 1); if (list.Fore->Next->Element != 1) { cout << "Error at 6" << endl; passed = false; } if (list.Fore->Next->Next->Element != 2) { cout << "Error at 7" << endl; passed = false; } if (list.Fore->Next->Next->Next != list.Aft) { cout << "Error at 8" << endl; passed = false; } if (list.Aft->Prev->Element != 2) { cout << "Error at 9" << endl; passed = false; } if (list.Aft->Prev->Prev->Element != 1) { cout <<"Error at 10" << endl; } if (list.Aft->Prev->Prev->Prev != list.Fore) { cout << "Error at 11" << endl; passed = false; } list.Insert(list.end(), 3); if (list.Fore->Next->Element != 1) { cout << "Error at 12" << endl; passed = false; } if (list.Fore->Next->Next->Element != 2) { cout << "Error at 13" << endl; passed = false; } if (list.Fore->Next->Next->Next->Element != 3) { cout << "Error at 14" << endl; passed = false; } if (list.Fore->Next->Next->Next->Next != list.Aft) { cout << "Error at 15" << endl; passed = false; } if (list.Aft->Prev->Element != 3) { cout << "Error at 16" << endl; passed = false; } if (list.Aft->Prev->Prev->Element != 2) { cout <<"Error at 17" << endl; } if (list.Aft->Prev->Prev->Prev->Element != 1) { cout << "Error at 18" << endl; passed = false; } if (list.Aft->Prev->Prev->Prev->Prev != list.Fore) { cout << "Error at 19" << endl; passed = false; } DListT::iterator itr = list.begin(); itr--; bool result = false; //try to insert at the Fore node try { list.Insert(itr, 5); } catch (range_error e) { result = true; } if (!result) { cout << "Error at 20" << endl; passed = false; } itr.Position = NULL; result = false; //try to insert at a NULL iterator try { list.Insert(itr, 5); } catch (range_error e) { result = true; } if (!result) { cout << "Error at 21" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; } return passed; } bool testFind() { bool passed = true; try { DListT list; list.Insert(list.begin(), 1); list.Insert(list.end(), 2); list.Insert(list.end(), 3); if ((list.Find(1)).Position->Element != 1) { cout << "Error at 1" << endl; passed = false; } if ((list.Find(2)).Position->Element != 2) { cout << "Error at 2" << endl; passed = false; } if ((list.Find(3)).Position->Element != 3) { cout << "Error at 3" << endl; passed = false; } if ((list.Find(4)).Position != list.Aft) { cout << "Error at 4" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstFind() { bool passed = true; try { DListT list2; list2.Insert(list2.begin(), 1); list2.Insert(list2.end(), 2); list2.Insert(list2.end(), 3); const DListT list = list2; DListT::const_iterator itr = list.Find(1); if (itr.Position->Element != 1) { cout << "Error at 1" << endl; passed = false; } itr = list.Find(2); if (itr.Position->Element != 2) { cout << "Error at 2" << endl; passed = false; } itr = list.Find(3); if (itr.Position->Element != 3) { cout << "Error at 3" << endl; passed = false; } itr = list.Find(4); if (itr.Position != list.Aft) { cout << "Error at 4" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testDelete() { bool passed = true; try { DListT list; list.Insert(list.end(), 1); list.Insert(list.end(), 2); list.Insert(list.end(), 3); DListT::iterator itr = list.begin(); itr++; //remove from middle list.Delete(itr); if (list.begin().Position->Prev != list.Fore) { cout << "Error at 1" << endl; passed = false; } if (list.begin().Position->Element != 1) { cout << "Error at 2" << endl; passed = false; } if (list.begin().Position->Next->Element != 3) { cout << "Error at 3" << endl; passed = false; } if (list.begin().Position->Next->Next != list.Aft) { cout << "Error at 4" << endl; passed = false; } if (list.end().Position->Prev->Element != 3) { cout << "Error at 5" << endl; passed = false; } if (list.end().Position->Prev->Prev->Element != 1) { cout << "Error at 6" << endl; passed = false; } if (list.end().Position->Prev->Prev->Prev != list.Fore) { cout << "Error at 7" << endl; passed = false; } //test the end of the list itr = list.end(); itr--; list.Delete(itr); if (list.begin().Position->Prev != list.Fore) { cout << "Error at 8" << endl; passed = false; } if (list.begin().Position->Element != 1) { cout << "Error at 9" << endl; passed = false; } if (list.begin().Position->Next != list.Aft) { cout << "Error at 10" << endl; passed = false; } if (list.end().Position->Prev->Element != 1) { cout << "Error at 11" << endl; passed = false; } if (list.end().Position->Prev->Prev != list.Fore) { cout << "Error at 12" << endl; passed = false; } //test the beginning of the list itr = list.begin(); list.Delete(itr); if (list.begin().Position->Prev != list.Fore) { cout << "Error at 13" << endl; passed = false; } if (list.begin().Position->Next != list.Aft) { cout << "Error at 14" << endl; passed = false; } if (list.end().Position->Prev != list.Fore) { cout << "Error at 15" << endl; passed = false; } itr = list.begin(); itr--; bool result = false; //try to delete the Fore node try { list.Delete(itr); } catch (range_error e) { result = true; } if (!result) { cout << "Error at 16" << endl; passed = false; } itr = list.end(); result = false; //try to delete the Aft node try { list.Delete(itr); } catch (range_error e) { result = true; } if (!result) { cout << "Error at 17" << endl; passed = false; } itr.Position = NULL; result = false; //try to delete a NULL iterator try { list.Delete(itr); } catch (range_error e) { result = true; } if (!result) { cout << "Error at 18" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testItrDefaultConstructor() { bool passed = true; try { DListT::iterator itr; if (itr.Position != NULL) { cout << "\tError at 1" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testItrPrivateConstructor() { bool passed = true; try { DListT list; DListT::iterator itr(list.Fore); if (itr.Position != list.Fore) { cout << "\tError at 1" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testItrPrefixAddOperator() { bool passed = true; try { DListT list; list.Insert(list.begin(), 1); list.Insert(list.end(), 2); list.Insert(list.end(), 3); DListT::iterator itr = list.begin(); if (itr.Position->Element != 1) { cout << "\tError at 1" << endl; passed = false; } ++itr; if (itr.Position->Element != 2) { cout << "\tError at 2" << endl; passed = false; } DListT::iterator itr2 = ++itr; if (itr2.Position->Element != 3) { cout << "\tError at 3" << endl; passed = false; } ++itr; if (itr.Position != list.Aft) { cout << "\tError at 4" << endl; passed = false; } itr.Position = NULL; ++itr; if (itr.Position != NULL) { cout << "\tError at 5" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testItrPostfixAddOperator() { bool passed = true; try { DListT list; list.Insert(list.begin(), 1); list.Insert(list.end(), 2); list.Insert(list.end(), 3); DListT::iterator itr = list.begin(); if (itr.Position->Element != 1) { cout << "\tError at 1" << endl; passed = false; } itr++; if (itr.Position->Element != 2) { cout << "\tError at 2" << endl; passed = false; } DListT::iterator itr2 = itr++; if (itr2.Position->Element != 2) { cout << "\tError at 3" << endl; passed = false; } itr++; if (itr.Position != list.Aft) { cout << "\tError at 4" << endl; passed = false; } itr.Position = NULL; itr++; if (itr.Position != NULL) { cout << "\tError at 5" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testItrPrefixSubOperator() { bool passed = true; try { DListT list; list.Insert(list.begin(), 1); list.Insert(list.end(), 2); list.Insert(list.end(), 3); DListT::iterator itr = list.end(); if (itr.Position != list.Aft) { cout << "\tError at 1" << endl; passed = false; } --itr; if (itr.Position->Element != 3) { cout << "\tError at 2" << endl; passed = false; } DListT::iterator itr2 = --itr; if (itr2.Position->Element != 2) { cout << "\tError at 3" << endl; passed = false; } --itr; if (itr.Position->Element != 1) { cout << "\tError at 4" << endl; passed = false; } --itr; if (itr.Position != list.Fore) { cout << "\tError at 4" << endl; passed = false; } itr.Position = NULL; --itr; if (itr.Position != NULL) { cout << "\tError at 5" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testItrPostfixSubOperator() { bool passed = true; try { DListT list; list.Insert(list.begin(), 1); list.Insert(list.end(), 2); list.Insert(list.end(), 3); DListT::iterator itr = list.end(); if (itr.Position != list.Aft) { cout << "\tError at 1" << endl; passed = false; } itr--; if (itr.Position->Element != 3) { cout << "\tError at 2" << endl; passed = false; } DListT::iterator itr2 = itr--; if (itr2.Position->Element != 3) { cout << "\tError at 3" << endl; passed = false; } itr--; if (itr.Position->Element != 1) { cout << "\tError at 4" << endl; passed = false; } itr--; if (itr.Position != list.Fore) { cout << "\tError at 4" << endl; passed = false; } itr.Position = NULL; itr--; if (itr.Position != NULL) { cout << "\tError at 5" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testItrEqualsOperator() { bool passed = true; try { DListT::iterator itr; DListT::iterator itr2; //test NULL iterator if (!(itr == itr2)) { cout << "Error at 1" << endl; passed = false; } DListT list; DListT list2; list.Insert(list.begin(), 1); list2.Insert(list2.begin(), 2); itr = list.begin(); itr2 = list.begin(); if (!(itr == itr2)) { cout << "Error at 2" << endl; passed = false; } itr2 = list2.begin(); if (itr == itr2) { cout << "Error at 3" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testItrNotEqualsOperator() { bool passed = true; try { DListT::iterator itr; DListT::iterator itr2; //test NULL iterator if (itr != itr2) { cout << "Error at 1" << endl; passed = false; } DListT list; DListT list2; list.Insert(list.begin(), 1); list2.Insert(list2.begin(), 2); itr = list.begin(); itr2 = list.begin(); if (itr != itr2) { cout << "Error at 2" << endl; passed = false; } itr2 = list2.begin(); if (!(itr != itr2)) { cout << "Error at 3" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testItrDerefrenceOperator() { bool passed = true; try { DListT list; list.Insert(list.begin(), 1); list.Insert(list.end(), 2); DListT::iterator itr = list.begin(); itr--; bool result = false; try { *itr; } catch (range_error e) { result = true; } if (!result) { cout << "Error at 1" << endl; passed = false; } itr++; if (*itr != 1) { cout << "Error at 2" << endl; passed = false; } itr++; if (*itr != 2) { cout << "Error at 3" << endl; passed = false; } itr++; result = false; try { *itr; } catch (range_error e) { result = true; } if (!result) { cout << "Error at 4" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstItrDefaultConstructor() { bool passed = true; try { DListT::const_iterator itr; if (itr.Position != NULL) { cout << "\tError at 1" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstItrPrivateConstructor() { bool passed = true; try { const DListT list; DListT::const_iterator itr(list.Fore); if (itr.Position != list.Fore) { cout << "\tError at 1" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstItrPrefixAddOperator() { bool passed = true; try { DListT list2; list2.Insert(list2.begin(), 1); list2.Insert(list2.end(), 2); list2.Insert(list2.end(), 3); const DListT list(list2); DListT::const_iterator itr = list.begin(); if (itr.Position->Element != 1) { cout << "\tError at 1" << endl; passed = false; } ++itr; if (itr.Position->Element != 2) { cout << "\tError at 2" << endl; passed = false; } DListT::const_iterator itr2 = ++itr; if (itr2.Position->Element != 3) { cout << "\tError at 3" << endl; passed = false; } ++itr; if (itr.Position != list.Aft) { cout << "\tError at 4" << endl; passed = false; } itr.Position = NULL; ++itr; if (itr.Position != NULL) { cout << "\tError at 5" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstItrPostfixAddOperator() { bool passed = true; try { DListT list2; list2.Insert(list2.begin(), 1); list2.Insert(list2.end(), 2); list2.Insert(list2.end(), 3); const DListT list(list2); DListT::const_iterator itr = list.begin(); if (itr.Position->Element != 1) { cout << "\tError at 1" << endl; passed = false; } itr++; if (itr.Position->Element != 2) { cout << "\tError at 2" << endl; passed = false; } DListT::const_iterator itr2 = itr++; if (itr2.Position->Element != 2) { cout << "\tError at 3" << endl; passed = false; } itr++; if (itr.Position != list.Aft) { cout << "\tError at 4" << endl; passed = false; } itr.Position = NULL; itr++; if (itr.Position != NULL) { cout << "\tError at 5" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstItrPrefixSubOperator() { bool passed = true; try { DListT list2; list2.Insert(list2.begin(), 1); list2.Insert(list2.end(), 2); list2.Insert(list2.end(), 3); const DListT list(list2); DListT::const_iterator itr = list.end(); if (itr.Position != list.Aft) { cout << "\tError at 1" << endl; passed = false; } --itr; if (itr.Position->Element != 3) { cout << "\tError at 2" << endl; passed = false; } DListT::const_iterator itr2 = --itr; if (itr2.Position->Element != 2) { cout << "\tError at 3" << endl; passed = false; } --itr; if (itr.Position->Element != 1) { cout << "\tError at 4" << endl; passed = false; } --itr; if (itr.Position != list.Fore) { cout << "\tError at 4" << endl; passed = false; } itr.Position = NULL; --itr; if (itr.Position != NULL) { cout << "\tError at 5" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstItrPostfixSubOperator() { bool passed = true; try { DListT list2; list2.Insert(list2.begin(), 1); list2.Insert(list2.end(), 2); list2.Insert(list2.end(), 3); const DListT list(list2); DListT::const_iterator itr = list.end(); if (itr.Position != list.Aft) { cout << "\tError at 1" << endl; passed = false; } itr--; if (itr.Position->Element != 3) { cout << "\tError at 2" << endl; passed = false; } DListT::const_iterator itr2 = itr--; if (itr2.Position->Element != 3) { cout << "\tError at 3" << endl; passed = false; } itr--; if (itr.Position->Element != 1) { cout << "\tError at 4" << endl; passed = false; } itr--; if (itr.Position != list.Fore) { cout << "\tError at 4" << endl; passed = false; } itr.Position = NULL; itr--; if (itr.Position != NULL) { cout << "\tError at 5" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstItrEqualsOperator() { bool passed = true; try { DListT::const_iterator itr; DListT::const_iterator itr2; //test NULL iterator if (!(itr == itr2)) { cout << "Error at 1" << endl; passed = false; } DListT list3; DListT list4; list3.Insert(list3.begin(), 1); list4.Insert(list4.begin(), 2); const DListT list(list3); const DListT list2(list4); itr = list.begin(); itr2 = list.begin(); if (!(itr == itr2)) { cout << "Error at 2" << endl; passed = false; } itr2 = list2.begin(); if (itr == itr2) { cout << "Error at 3" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstItrNotEqualsOperator() { bool passed = true; try { DListT::const_iterator itr; DListT::const_iterator itr2; //test NULL iterator if (itr != itr2) { cout << "Error at 1" << endl; passed = false; } DListT list3; DListT list4; list3.Insert(list3.begin(), 1); list4.Insert(list4.begin(), 2); const DListT list(list3); const DListT list2(list4); itr = list.begin(); itr2 = list.begin(); if (itr != itr2) { cout << "Error at 2" << endl; passed = false; } itr2 = list2.begin(); if (!(itr != itr2)) { cout << "Error at 3" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstItrDerefrenceOperator() { bool passed = true; try { DListT list2; list2.Insert(list2.begin(), 1); list2.Insert(list2.end(), 2); DListT list(list2); DListT::const_iterator itr = list.begin(); itr--; bool result = false; try { *itr; } catch (range_error e) { result = true; } if (!result) { cout << "Error at 1" << endl; passed = false; } itr++; if (*itr != 1) { cout << "Error at 2" << endl; passed = false; } itr++; if (*itr != 2) { cout << "Error at 3" << endl; passed = false; } itr++; result = false; try { *itr; } catch (range_error e) { result = true; } if (!result) { cout << "Error at 4" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstItrCopyConstructor() { bool passed = true; try { DListT::const_iterator itr; DListT::const_iterator itr2(itr); if (itr2.Position != NULL) { cout << "Error at 1" << endl; passed = false; } DListT list; DListT::const_iterator itr3(list.begin()); if (itr3.Position != list.Aft) { cout << "Error at 2" << endl; passed = false; } list.Insert(list.begin(), 1); DListT::const_iterator itr4(list.begin()); if (itr4.Position->Element != 1) { cout << "Error at 3" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } bool testConstItrAssignmentOperator() { bool passed = true; try { DListT::const_iterator itr; DListT::const_iterator itr2; itr = itr2; if (itr.Position != NULL) { cout << "Error at 1" << endl; passed = false; } DListT list; itr = list.begin(); if (itr.Position != list.Aft) { cout << "Error at 2" << endl; passed = false; } list.Insert(list.begin(), 1); itr = list.begin(); if (itr.Position->Element != 1) { cout << "Error at 3" << endl; passed = false; } } catch (...) { cout << "Your code bombed when it wasn't supposed to." << endl; passed = false; } return passed; } }; //memory leak stuff #define _CHK_MLEAK #ifdef _CHK_MLEAK #define CRTDBG_MAP_ALLOC #include #include #endif int main() { #ifdef _CHK_MLEAK _CrtMemState s1, s2, s3; //memory leak sstuff #endif // MEMORY LEAK: begin #ifdef _CHK_MLEAK //AfxMessageBox("Starting leak check."); _CrtMemCheckpoint( &s1 ); cout << "ML: Memory Leak Test Initiated\n"; #endif Javert j; #ifdef _CHK_MLEAK _CrtMemCheckpoint( &s2 ); //std::cerr << "done\n"; //cerr("Dump the changes that occurred between two memory checkpoints" ); if ( _CrtMemDifference( &s3, &s1, &s2 ) ) { //std::cerr << "Memory meaks detected!\n"; _CrtDumpMemoryLeaks(); //_CrtMemDumpStatistics(&s3); cout << "ML: *** Leak Detected!\n"; } cout << "ML: Memory Leak Test Complete\n"; //AfxMessageBox("Done with leak check."); #endif getchar(); return 0; }