using namespace std; #include #include "../Array.h" #include "../Array.cc" #include "../AssocList.h" #include "../AssocList.cc" #include "../Hash.h" #include "../Hash.cc" #include "test_utilities.h" int main(int argc, char *argv[]) { test_init(60, argv[0]); alarm(5); // test basic operations HashTable A(-2); test(3, A[0] == -2, "HashTable declared with default initializer doesn't initialize"); A[0] = 3; test(3, A[0] == 3, "assignment to HashTable operator[] fails"); test(3, A.exists(0), "HashTable exists() fails"); test(3, ! A.exists(1), "HashTable exists() fails"); A.remove(0); test(3, ! A.exists(0), "HashTable remove() fails?"); A[0] = 3; A[2] = 1; Array keys = A.keys(); test(3, keys.size() == 2, "HashTable keys() - wrong # keys"); test(3, (keys[0] == 0 || keys[0] == 2) && (keys[1] == 0 || keys[1] == 2), "HashTable keys() - wrong keys"); Array values = A.values(); test(3, values.size() == 2, "HashTable values() - wrong # values"); test(3, (values[0] == 1 || values[0] == 3) && (values[1] == 1 || values[1] == 3), "HashTable values() - wrong values"); // copy constructor, some const correctness const HashTable & B(A); const HashTable BB(A); HashTable C; test(3, B[2] == 1, "HashTable operator[] const fails?"); test(3, BB[2] == 1, "HashTable copy constructor fails? (HashTable value not properly copied)"); A[2] = 2; test(3, BB[2] == 1, "HashTable copy constructor fails? (new table not allocated?)"); C = A; test(3, C[2] == 2, "HashTable assignment operator fails?"); // test clear function HashTable D; SelfInitInt x; test(3, D[1].i == x.i, "HashTable declared without default value should not be initialized"); D[1].i = 10; D.clear(); test(3, D.keys().size() == 0, "HashTable keys() should return 0 keys after HashTable clear()ed."); test(3, D[1].i == x.i, "HashTable clear() operation should allocate new table (this one is subtle)."); // try to test grow function and running time HashTable E(1); int errors = 0; long int N = 60000; // make sure N*N doesn't overflow for (long int i = 0; i < N; ++i) { if (E.exists(i*i)) { ++errors; std::cout << i << ", " << i*i << std::endl; } E[i*i] = i*i; } test(3, errors == 0, "HashTable grow function failing?"); errors = 0; for (long int i = 0; i < N; ++i) { if (E[i*i] != i*i) ++errors; } test(3, errors == 0, "HashTable incorrect after many inserts"); test(3, E.values().size() == N, "HashTable values().size() incorrect after many inserts"); for (long int i = 0; i < N; ++i) { E.remove(i*i); } test(3, E.keys().size() == 0, "HashTable values().size() incorrect after many inserts and deletions"); // end test_report(); }