From a9219d4f22962762be64daa22e38346d47b2083b Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 26 Aug 2015 08:49:30 -0400 Subject: [PATCH] tests/core: Update index unit test to the new framework Signed-off-by: Anna Schumaker --- tests/core/Sconscript | 2 +- tests/core/index.cpp | 149 ++++++++++++++++++++---------------------- 2 files changed, 72 insertions(+), 79 deletions(-) diff --git a/tests/core/Sconscript b/tests/core/Sconscript index 7e327120..2908eceb 100644 --- a/tests/core/Sconscript +++ b/tests/core/Sconscript @@ -16,9 +16,9 @@ res = [ CoreTest("version", "version.c") ] res += [ CoreTest("string", "string.cpp") ] res += [ CoreTest("file", "file.cpp") ] res += [ CoreTest("database", "database.cpp") ] +res += [ CoreTest("index", "index.cpp") ] Return("res") -#test( "index" ) #test( "filter" ) #test( "idle" ) # diff --git a/tests/core/index.cpp b/tests/core/index.cpp index 02be6d10..02a95b2d 100644 --- a/tests/core/index.cpp +++ b/tests/core/index.cpp @@ -5,10 +5,9 @@ #include #include #include -#include +#include "test.h" -static unsigned int N = 0; static Index *INDEX = NULL; @@ -30,77 +29,55 @@ static void test_single_item() } -static unsigned int _test_insertion_check(unsigned int i) +static void test_insertion(unsigned int n) { - std::string key(1, (char)i); - IndexEntry *it = INDEX->find(std::string(1, (char)i)); + test_rm_data_dir(); + INDEX = new Index("index.idx", false); - 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++) + 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); + test_equal(INDEX->size(), (n == 0) ? 0 : 26); + + for (char c = 'a'; c <= 'z'; c++) { + IndexEntry *it = INDEX->find(std::string(1, c)); + if (n == 0) { + test_loop_equal(it, NULL, c - 'a'); + return; + } + test_loop_not_equal(it, NULL, c - 'a'); + test_loop_equal(it->size(), n, c - 'a'); + } test_loop_passed(); } -static unsigned int _test_removal_check(unsigned int i) +static void test_removal(unsigned int n) { IndexEntry *it; - std::string key(1, (char)i); - for (unsigned int i = 0; i < N; i++) - INDEX->remove(key, i); + for (char c = 'a'; c <= 'z'; c++) { + for (unsigned int i = 0; i < n; i++) + INDEX->remove(std::string(1, c), 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); + it = INDEX->find(std::string(1, c)); + if (n == 0) { + test_loop_equal(it, NULL, c - 'a'); + return; + } + test_loop_not_equal(it, NULL, c - 'a'); + test_loop_equal(it->size(), 0, c - 'a'); + } test_loop_passed(); } -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() +static void test_save_load(unsigned int n) { Index idx2("index.idx", false); + IndexEntry *it1, *it2; + idx2.load(); - INDEX2 = &idx2; test_equal(idx2.size(), (unsigned)0); INDEX->save(); @@ -109,32 +86,48 @@ static void test_saving() test_equal(idx2.size(), INDEX->size()); test_equal(idx2.actual_size(), INDEX->actual_size()); - test_for_each('a', 'z' + 1, 1, _test_saving_check); + for (char c = 'a'; c <= 'z'; c++) { + it1 = INDEX->find(std::string(1, c)); + it2 = idx2.find(std::string(1, c)); + if (n == 0) { + test_loop_equal(it1, NULL, c - 'a'); + test_loop_equal(it2, NULL, c - 'a'); + } else { + test_loop_not_equal(it1, NULL, c - 'a'); + test_loop_not_equal(it2, NULL, c - 'a'); + test_loop_equal(it1->size(), it2->size(), c - 'a'); + } + } test_loop_passed(); + + delete INDEX; } -static void index_test(unsigned int n) -{ - std::string n_str = " (n = " + string :: utos(n) + ")"; - Index index("index.idx", false); - N = n; - INDEX = &index; +#define INDEX_TEST_FUNCS(n) \ + static void test_insertion_##n() { test_insertion(n); } \ + static void test_removal_##n() { test_removal(n); } \ + static void test_save_load_##n() { test_save_load(n); } - 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; -} +INDEX_TEST_FUNCS(0) +INDEX_TEST_FUNCS(10) +INDEX_TEST_FUNCS(100) +INDEX_TEST_FUNCS(1000) +INDEX_TEST_FUNCS(10000) +INDEX_TEST_FUNCS(100000) + +#define INDEX_UNIT_TESTS(n) \ + UNIT_TEST("Index Insertion (n = " #n ")", test_insertion_##n), \ + UNIT_TEST("Index Removal (n = " #n ")", test_removal_##n), \ + UNIT_TEST("Index Save and Load (n = " #n ")", test_save_load_##n) + + +DECLARE_UNIT_TESTS( + UNIT_TEST("Index Single Item", test_single_item), + INDEX_UNIT_TESTS(0), + INDEX_UNIT_TESTS(10), + INDEX_UNIT_TESTS(100), + INDEX_UNIT_TESTS(1000), + INDEX_UNIT_TESTS(10000), + INDEX_UNIT_TESTS(100000), +);