/* * Copyright 2014 (c) Anna Schumaker. * Test a Database */ #include #include #include #include static unsigned int N = 0; static Index *INDEX = NULL; static void test_single_item() { Index index("index.idx", false); index.insert("a", 0); IndexEntry *it = index.find("a"); test_not_equal(it, (IndexEntry *)NULL); test_equal(index.size(), (unsigned)1); test_equal(it->size(), (size_t)1); test_equal(*(it->begin()), (unsigned)0); index.remove("a", 0); test_equal(index.size(), (unsigned)1); test_equal(it->size(), (size_t)0); } static unsigned int _test_insertion_check(unsigned int i) { std::string key(1, (char)i); IndexEntry *it = INDEX->find(std::string(1, (char)i)); if (N == 0) return (it == NULL) ? LOOP_PASSED : LOOP_FAILED; if (it == NULL) return LOOP_FAILED; if (it->size() != N) return LOOP_FAILED; return LOOP_PASSED; } static void test_insertion() { for (char c = 'a'; c <= 'z'; c++) { for (unsigned int i = 0; i < N; i++) INDEX->insert(std::string(1, c), i); } test_equal(INDEX->size(), (N == 0) ? 0 : 26); test_for_each('a', 'z' + 1, 1, _test_insertion_check); } static unsigned int _test_removal_check(unsigned int i) { IndexEntry *it; std::string key(1, (char)i); for (unsigned int i = 0; i < N; i++) INDEX->remove(key, i); it = INDEX->find(key); if (N == 0) return (it == NULL) ? LOOP_PASSED : LOOP_FAILED; if ((it == NULL) || (it->size() != 0)) return LOOP_FAILED; return LOOP_PASSED; } static void test_removal() { test_for_each('a', 'z' + 1, 1, _test_removal_check); } static Index *INDEX2 = NULL; static unsigned int _test_saving_check(unsigned int i) { std::string key(1, (char)i); IndexEntry *it1 = INDEX->find(key); IndexEntry *it2 = INDEX2->find(key); if (N == 0) { if ((it1 != NULL) || (it2 != NULL)) return LOOP_FAILED; } else { if ((it1 == NULL) || (it2 == NULL) || (it1->size() != it2->size())) return LOOP_FAILED; } return LOOP_PASSED; } static void test_saving() { Index idx2("index.idx", false); idx2.load(); INDEX2 = &idx2; test_equal(idx2.size(), (unsigned)0); INDEX->save(); idx2.load(); test_equal(idx2.size(), INDEX->size()); test_equal(idx2.actual_size(), INDEX->actual_size()); test_for_each('a', 'z' + 1, 1, _test_saving_check); } static void index_test(unsigned int n) { std::string n_str = " (n = " + string :: utos(n) + ")"; Index index("index.idx", false); N = n; INDEX = &index; test :: rm_data_dir(); if (n == 0) test :: run("Index Single Item Test", test_single_item); test :: run("Index Insertion Test" + n_str, test_insertion); test :: run("Index Removal Test" + n_str, test_removal); test :: run("Index Save and Load Test" + n_str, test_saving); } int main(int argc, char **argv) { index_test(0); index_test(10); index_test(100); index_test(1000); index_test(10000); index_test(100000); return 0; }