// // simple program to test correctness of dynamicArray class. // // program takes one command-line argument, the number of // elements to be added to / removed from the array. // #include #include // has EXIT_SUCCESS #include "dynamicArray.h" #include // helps detect memory leaks int main(int argc, char * argv[]) { mtrace(); // turn on memory leak detection if (argc < 2) { cerr << "Usage: " << argv[0] << " num_of_elements\n"; exit(EXIT_FAILURE); } // convert command-line argument to unsigned int. dynamicArray::length_pos max_size = strtoul(argv[1], static_cast(0), 10); dynamicArray a1; // test push_back(). for (dynamicArray::length_pos i = 0; i < max_size; ++i) a1.push_back(2*i+1); // show operation counts. cout << "Elements added.\n"; a1.show_op_counts(cout, "a1"); // test size(), get(). cout << "array size = " << a1.size() << endl; cout << "first element = " << a1.get(0) << endl; cout << "last element = " << a1.get(max_size-1) << endl; cout << "element " << max_size/2 << " = " << a1.get(max_size/2) << endl; // test set(). a1.set(0, -1); a1.set(max_size-1, -2); a1.set(max_size/2, -3); cout << "reset first element = " << a1.get(0) << endl; cout << "reset last element = " << a1.get(max_size-1) << endl; cout << "reset middle element = " << a1.get(max_size/2) << endl; // test copy constructor. dynamicArray a2(a1); // test pop_back(). dynamicArray::length_pos i = a2.size(); while (i > 0) cout << "element " << --i << " = " << a2.pop_back() << endl; cout << "after removing elements, size = " << a2.size() << endl; // show operation counts. a2.show_op_counts(cout, "a2"); a2.push_back(max_size); cout << "reinserted " << a2.get(0) << endl; return EXIT_SUCCESS; }