ocarina/tests/core/index.cpp

134 lines
3.0 KiB
C++

/*
* Copyright 2014 (c) Anna Schumaker.
* Test a Database
*/
#include <core/index.h>
#include <core/print.h>
#include <core/string.h>
#include "test.h"
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 void test_insertion(unsigned int n)
{
test_rm_data_dir();
INDEX = new Index("index.idx", false);
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);
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 void test_removal(unsigned int n)
{
IndexEntry *it;
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(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 void test_save_load(unsigned int n)
{
Index idx2("index.idx", false);
IndexEntry *it1, *it2;
idx2.load();
test_equal(idx2.size(), (unsigned)0);
INDEX->save();
idx2.load();
test_equal(idx2.size(), INDEX->size());
test_equal(idx2.actual_size(), INDEX->actual_size());
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;
}
#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); }
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),
);