tests/core: Update index unit test to the new framework
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
7318363287
commit
a9219d4f22
|
@ -16,9 +16,9 @@ res = [ CoreTest("version", "version.c") ]
|
||||||
res += [ CoreTest("string", "string.cpp") ]
|
res += [ CoreTest("string", "string.cpp") ]
|
||||||
res += [ CoreTest("file", "file.cpp") ]
|
res += [ CoreTest("file", "file.cpp") ]
|
||||||
res += [ CoreTest("database", "database.cpp") ]
|
res += [ CoreTest("database", "database.cpp") ]
|
||||||
|
res += [ CoreTest("index", "index.cpp") ]
|
||||||
Return("res")
|
Return("res")
|
||||||
|
|
||||||
#test( "index" )
|
|
||||||
#test( "filter" )
|
#test( "filter" )
|
||||||
#test( "idle" )
|
#test( "idle" )
|
||||||
#
|
#
|
||||||
|
|
|
@ -5,10 +5,9 @@
|
||||||
#include <core/index.h>
|
#include <core/index.h>
|
||||||
#include <core/print.h>
|
#include <core/print.h>
|
||||||
#include <core/string.h>
|
#include <core/string.h>
|
||||||
#include <tests/test.h>
|
#include "test.h"
|
||||||
|
|
||||||
|
|
||||||
static unsigned int N = 0;
|
|
||||||
static Index *INDEX = NULL;
|
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);
|
test_rm_data_dir();
|
||||||
IndexEntry *it = INDEX->find(std::string(1, (char)i));
|
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 (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);
|
INDEX->insert(std::string(1, c), i);
|
||||||
}
|
}
|
||||||
|
|
||||||
test_equal(INDEX->size(), (N == 0) ? 0 : 26);
|
test_equal(INDEX->size(), (n == 0) ? 0 : 26);
|
||||||
test_for_each('a', 'z' + 1, 1, _test_insertion_check);
|
|
||||||
|
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;
|
IndexEntry *it;
|
||||||
std::string key(1, (char)i);
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < N; i++)
|
for (char c = 'a'; c <= 'z'; c++) {
|
||||||
INDEX->remove(key, i);
|
for (unsigned int i = 0; i < n; i++)
|
||||||
|
INDEX->remove(std::string(1, c), i);
|
||||||
|
|
||||||
it = INDEX->find(key);
|
it = INDEX->find(std::string(1, c));
|
||||||
if (N == 0)
|
if (n == 0) {
|
||||||
return (it == NULL) ? LOOP_PASSED : LOOP_FAILED;
|
test_loop_equal(it, NULL, c - 'a');
|
||||||
if ((it == NULL) || (it->size() != 0))
|
return;
|
||||||
return LOOP_FAILED;
|
}
|
||||||
return LOOP_PASSED;
|
test_loop_not_equal(it, NULL, c - 'a');
|
||||||
}
|
test_loop_equal(it->size(), 0, c - 'a');
|
||||||
|
} test_loop_passed();
|
||||||
static void test_removal()
|
|
||||||
{
|
|
||||||
test_for_each('a', 'z' + 1, 1, _test_removal_check);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static Index *INDEX2 = NULL;
|
static void test_save_load(unsigned int n)
|
||||||
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);
|
Index idx2("index.idx", false);
|
||||||
|
IndexEntry *it1, *it2;
|
||||||
|
|
||||||
idx2.load();
|
idx2.load();
|
||||||
INDEX2 = &idx2;
|
|
||||||
test_equal(idx2.size(), (unsigned)0);
|
test_equal(idx2.size(), (unsigned)0);
|
||||||
|
|
||||||
INDEX->save();
|
INDEX->save();
|
||||||
|
@ -109,32 +86,48 @@ static void test_saving()
|
||||||
test_equal(idx2.size(), INDEX->size());
|
test_equal(idx2.size(), INDEX->size());
|
||||||
test_equal(idx2.actual_size(), INDEX->actual_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;
|
#define INDEX_TEST_FUNCS(n) \
|
||||||
INDEX = &index;
|
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_FUNCS(0)
|
||||||
{
|
INDEX_TEST_FUNCS(10)
|
||||||
index_test(0);
|
INDEX_TEST_FUNCS(100)
|
||||||
index_test(10);
|
INDEX_TEST_FUNCS(1000)
|
||||||
index_test(100);
|
INDEX_TEST_FUNCS(10000)
|
||||||
index_test(1000);
|
INDEX_TEST_FUNCS(100000)
|
||||||
index_test(10000);
|
|
||||||
index_test(100000);
|
#define INDEX_UNIT_TESTS(n) \
|
||||||
return 0;
|
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),
|
||||||
|
);
|
||||||
|
|
Loading…
Reference in New Issue