/* testlist.cc */ #include #include "slist.h" void isInTest(); // Warn compiler of upcoming definitions. void removeTest(); void mergeTest(); int main() { SortedList *l1 = new SortedList(); l1->insert(1); l1->insert(9); l1->insert(3); l1->insert(8); l1->insert(2); SortedList *l2 = new SortedList(*l1); // l2 is a copy of l1. SortedList *l3 = l1; // l3 and l1 are the same object. cout << "All lists should contain 1, 2, 3, 8, 9." << endl; cout << " Here is l1: "; l1->print(); cout << " Here is l2: "; l2->print(); cout << " Here is l3: "; l3->print(); cout << "Show sharing by inserting 4 into l1." << endl; l1->insert(4); cout << " Here is l1: "; l1->print(); cout << " Here is l2: "; l2->print(); cout << " Here is l3: "; l3->print(); // Uncomment the following lines to test your solutions. // isInTest(); // removeTest(); // mergeTest(); } void isInTest() { cout << endl << "* * * Testing isIn * * *" << endl; SortedList *sl = new SortedList(); cout << "Testing isIn on an empty list." << endl; cout << " isIn should return 0: " << sl->isIn(2) << endl; cout << "Testing isIn on a non-empty list." << endl; sl->insert(3); sl->insert(8); sl->insert(2); cout << " isIn should return 1: " << sl->isIn(3) << endl; cout << " isIn should return 1: " << sl->isIn(2) << endl; cout << " isIn should return 0: " << sl->isIn(10) << endl; cout << " isIn should return 0: " << sl->isIn(-1) << endl; // Note: sl and its nodes should be deleted here, but in case your solution // to Part I doesn't work, we've omitted the deletion. } void removeTest() { cout << endl << "* * * Testing remove * * *" << endl; SortedList *sl = new SortedList(); cout << "Testing remove on an empty list." << endl; cout << " remove should do nothing: "; sl->remove(2); sl->print(); cout << "Testing remove on a 1 element list." << endl; sl->insert(3); cout << " remove should produce [ 3 ]: "; sl->remove(1); sl->print(); cout << " remove should produce [ 3 ]: "; sl->remove(5); sl->print(); cout << " remove should produce [ ]: "; sl->remove(3); sl->print(); sl->insert(8); sl->insert(2); sl->insert(4); cout << " remove should produce [ 2 4 ]: "; sl->remove(8); sl->print(); cout << " remove should produce [ 2 ]: "; sl->remove(4); sl->print(); // Note: sl and its nodes should be deleted here, but in case your solution // to Part II doesn't work, we've omitted the deletion. } void mergeTest() { cout << endl << "* * * Testing merge * * *" << endl; SortedList *l1 = new SortedList(); SortedList *l2 = new SortedList(); cout << "Testing merge on two empty lists." << endl; l1->merge(l2); cout << "Merged list: "; l1->print(); cout << "Other list (should be empty): "; l2->print(); cout << "Testing merge on one non-empty and one empty list." << endl; l1 = new SortedList(); l2 = new SortedList(); l1->insert(1); l1->insert(5); l1->insert(3); l1->merge(l2); cout << "Merged list: "; l1->print(); cout << "Other list (should be empty): "; l2->print(); cout << "Testing merge on one empty and one non-empty list." << endl; l2 = l1; l1 = new SortedList(); l1->merge(l2); cout << "Merged list: "; l1->print(); cout << "Other list (should be empty): "; l2->print(); cout << "Testing merge on two non-empty lists." << endl; l2 = new SortedList(); l2->insert(2); l2->insert(6); l2->insert(4); l2->insert(0); l1->merge(l2); cout << "Merged list: "; l1->print(); cout << "Other list (should be empty): "; l2->print(); }