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