/**************************************************************************** * * * Program: ListTest.cpp * * * * Author: Robb T. Koether * * * * Date: Jun 29, 2000 * * * * Abstract: This program tests a list class * * * ****************************************************************************/ #include #include #include #include #include #include "arraylist.h" using namespace std; // Typedef typedef ArrayList List; // Function prototypes List reverse(List lst); /**************************************************************************** * * * Function: main * * * * Purpose: This function will test each member function of a list * * class * * * ****************************************************************************/ int main() { // Test the constructors cout << "Test the constructors" << endl; List list; list.isValid(); cout << endl << "List = " << list << endl; List secondList(3); secondList.isValid(); cout << endl << "Second list = " << secondList << endl; List thirdList(5, 123); thirdList.isValid(); cout << endl << "Third list = " << thirdList << endl; // Test the swap function cout << endl << "Test the swap function" << endl; secondList.swap(thirdList); secondList.isValid(); thirdList.isValid(); cout << endl << "Second list = " << secondList << endl; cout << "Third list = " << thirdList << endl; thirdList.swap(secondList); secondList.isValid(); thirdList.isValid(); cout << endl << "Second list = " << secondList << endl; cout << "Third list = " << thirdList << endl; // Test the pushBack function cout << endl << "Test the pushBack function" << endl; for (int i = 10; i <= 50; i += 10) { list.pushBack(i); list.isValid(); cout << "List = " << list << endl; } // Test the pushFront function cout << endl << "Test the pushFront function" << endl; for (int i = 1; i <= 5; i++) { list.pushFront(i); list.isValid(); cout << "List = " << list << endl; } // Test the popFront function cout << endl << "Test the popFront function" << endl; for (int i = 0; i < 5; i++) { list.popFront(); list.isValid(); cout << "List = " << list << endl; } // Test the setElement function cout << endl << "Test the setElement function" << endl; for (int i = 0; i < 3; i++) { secondList.setElement(i, 100*(i + 1)); secondList.isValid(); cout << "Second list = " << secondList << endl; } // Test the insert function cout << endl << "Test the insert function" << endl; for (int i = 0; i < 6; i++) { list.insert(2*i, 100*(i + 1)); list.isValid(); cout << "List = " << list << endl; } // Test the bracket operator and the GetElement function cout << endl << "Test the bracket operator and the getElement function" << endl; for (int i = 0; i < 6; i++) { list[2*i]++; list.isValid(); cout << "List = " << list << endl; } // Test the remove function cout << endl << "Test the remove function" << endl; for (int i = 0; i < 6; i++) { list.remove(i); list.isValid(); cout << "List = " << list << endl; } // Retrieve each value from the list cout << endl << "Retrieve each value from the list" << endl << endl; for (int i = 0; i < list.size(); i++) { cout << list[i] << endl; list.isValid(); } // Test the copy constructor - Be sure copy is independent of original cout << endl << "Test the copy constructor" << endl << endl; List copyList = list; copyList.isValid(); cout << "Original list = " << list << endl; list = secondList; // Change the original list.isValid(); cout << "Copied list = " << copyList << endl; // Print the copy cout << "Changed original = " << list << endl; // Print the changed original list = copyList; // Restore the original list.isValid(); cout << "Restored original = " << list << endl; // Print the restored original // Test the assignment operator - Be sure copy is independent of original cout << endl << "Test the assignment operator" << endl << endl; List assignList; assignList = list; assignList.isValid(); cout << "Original list = " << list << endl; list = secondList; // Change the original list.isValid(); cout << "Assigned list = " << assignList << endl; // Print the copy cout << "Changed original = " << list << endl; // Print the changed original list = assignList; // Restore the original list.isValid(); cout << "Restored original = " << list << endl; // Print the restored original // Test the makeEmpty and isEmpty functions cout << endl << "Test the makeEmpty function" << endl << endl; secondList.makeEmpty(); secondList.isValid(); cout << "secondList = " << secondList << endl; cout << "The second list length is " << secondList.size() << endl; if (secondList.isEmpty()) cout << "The second list is empty" << endl; else cout << "The second list is not empty. Oops!" << endl; // Test the search function cout << endl << "Test the search function" << endl; for (int i = 5; i <= 55; i += 5) { int pos = list.search(i); list.isValid(); if (pos == -1) cout << i << " was not found in list" << endl; else cout << i << " was found in position " << pos << endl; } // Test the sort function cout << endl << "Test the sort function" << endl; cout << "Scramble the list" << endl; srand((unsigned int) time(0)); for (int i = 0; i < list.size(); i++) { // Pick a random position int j = rand() % list.size(); // Swap the i-th and j-th elements int temp = list[i]; list[i] = list[j]; list[j] = temp; } list.isValid(); cout << "Unsorted list = " << list << endl; list.sort(); // Sort the list list.isValid(); cout << "Sorted list = " << list << endl; // Test the input function cout << endl << "Test the input function" << endl; cout << endl << "Enter a list, delimited by braces {}, with "; cout << "items separated by commas" << endl; cin >> list; list.isValid(); cout << endl << "list = " << list << endl; // Test the popback function cout << endl << "Test the popback function" << endl; while (!list.isEmpty()) { list.popBack(); list.isValid(); cout << "list = " << list << endl; } // Make an array of lists cout << endl << "Make an array of lists" << endl; List listArray[3]; for (int i = 0; i < 3; i++) cout << "listArray[" << i << "] = " << listArray[i] << endl; listArray[0] = List(); listArray[0].isValid(); listArray[1] = List(3); listArray[1].isValid(); listArray[2] = List(5, 123); listArray[2].isValid(); cout << endl << "Initialize the lists" << endl; for (int i = 0; i < 3; i++) cout << "listArray[" << i << "] = " << listArray[i] << endl; // Pass a list to the reverse function cout << endl << "Pass a list to the reverse function" << endl; List revList(5); revList[0] = 10; revList[1] = 20; revList[2] = 30; revList[3] = 40; revList[4] = 50; revList.isValid(); cout << "revList = " << revList << endl; List returnList = reverse(revList); returnList.isValid(); cout << "returnList = " << returnList << endl; cout << endl << "Good-bye" << endl; return 0; } /**************************************************************************** * * * Function: reverse * * * * Purpose: This function will test reverse the sequence of elements * * in a list * * * * Note: This function intentionally passes the list by value in * * order to test the copy constructor * * * ****************************************************************************/ List reverse(List lst) { lst.isValid(); for (int i = 0; i < lst.size()/2; i++) { int j = lst.size() - i; int temp = lst[i]; lst[i] = lst[j]; lst[j] = temp; } lst.isValid(); return lst; }