/* * Copyright 2013 (c) Anna Schumaker. */ #include #include void print_index(Database &index) { std::set::iterator s_it; std::set::iterator u_it; print("=== Printing index ===\n"); index.print_keys(); for (unsigned int i = index.first(); i != index.num_rows(); i = index.next(i)) { print("index[%s] = ", index[i].primary_key.c_str()); index[i].print(); print("\n"); } print("\n"); } void populate_index(Database &index) { std::string key_str; for (char key = 'a'; key <= 'z'; key++) { key_str = key; for (unsigned int value = 0; value < 10; value++) index_insert(index, key_str, value); } } void remove_keys(Database &index) { std::string key_str; for (char key = 'a'; key <= 'z'; key+=2) { key_str = key; index.remove(index.find_index(key_str)); if (index.has_key(key_str)) print("Failed to remove key!\n"); } } void remove_values(Database &index) { std::string key_str; unsigned int limit = 0; for (char key = 'a'; key <= 'z'; key++) { key_str = key; for (unsigned int i = 0; i < limit; i++) index_remove(index, key_str, i); limit++; if (limit == 11) { limit = 0; if (index.has_key(key_str)) print("Failed to remove key!\n"); } } } /* * Test index creation, print key access */ void test_0() { print("Test 0\n"); Database index(""); populate_index(index); print_index(index); } /* * Test removing keys from an index */ void test_1() { print("Test 1\n"); Database index(""); populate_index(index); remove_keys(index); print_index(index); } /* * Test removing values from an index */ void test_2() { print("Test 2\n"); Database index(""); populate_index(index); remove_values(index); print_index(index); } /* * Save the database to disk */ void test_3() { print("Test 3\n"); Database index("test.idx"); populate_index(index); remove_values(index); index.save(); } /* * Reload the database from disk */ void test_4() { print("Test 4\n"); Database index("test.idx"); index.load(); print_index(index); } /* * Test access to keys that don't exist */ void test_5() { print("Test 5\n"); Database index(""); print("index[nokey].size() = "); try { print("%u\n", index.find("nokey").values.size()); } catch (...) { print("0\n"); } } int main(int argc, char **argv) { test_0(); test_1(); test_2(); test_3(); test_4(); test_5(); return 0; }