ocarina/tests/index.cpp

172 lines
3.2 KiB
C++

/*
* Copyright 2014 (c) Anna Schumaker.
* Test a Database
*/
#include <core/index.h>
#include "test.h"
#include <sstream>
static IndexEntry *IDX_NULL = NULL;
/***
*
* Struct for passing around arguments to tests
*
*/
struct TestArgs {
const unsigned int n;
Index *index;
TestArgs(const unsigned int, Index *);
};
TestArgs :: TestArgs(const unsigned int _n, Index *_index)
: n(_n), index(_index)
{}
/***
*
* Run tests with varying index sizes
*
*/
static void test_single_item()
{
Index index("index.idx", false);
index.insert("a", 0);
IndexEntry *it = index.find("a");
test_not_equal(it, IDX_NULL);
test_equal(index.size(), (unsigned)1);
test_equal(it->values.size(), (size_t)1);
test_equal(*(it->values.begin()), (unsigned)0);
index.remove("a", 0);
test_equal(index.size(), (unsigned)1);
test_equal(it->values.size(), (size_t)0);
}
static void test_insertion(struct TestArgs *args)
{
std::string key;
IndexEntry *it;
test :: begin();
for (char c = 'a'; c <= 'z'; c++) {
key = c;
for (unsigned int i = 0; i < args->n; i++)
args->index->insert(key, i);
}
test :: success();
if (args->n == 0)
test_equal(args->index->size(), (unsigned int)0);
else
test_equal(args->index->size(), (unsigned int)26);
test :: begin();
for (char c = 'a'; c <= 'z'; c++) {
key = c;
it = args->index->find(key);
if (args->n == 0)
check_equal(it, IDX_NULL);
else {
check_not_equal(it, IDX_NULL);
check_equal(it->values.size(), (size_t)args->n);
}
}
test :: success();
}
static void test_removal(struct TestArgs *args)
{
std::string key;
IndexEntry *it;
test :: begin();
for (char c = 'a'; c <= 'z'; c++) {
key = c;
for (unsigned int i = 0; i < args->n; i++)
args->index->remove(key, i);
it = args->index->find(key);
if (args->n == 0)
check_equal(it, IDX_NULL);
else {
check_not_equal(it, IDX_NULL);
check_equal(it->values.size(), (size_t)0);
}
}
test :: success();
}
static void test_saving(struct TestArgs *args)
{
std::string key;
IndexEntry *it1, *it2;
Index idx2("index.idx", false);
idx2.load();
test_equal(idx2.size(), (unsigned)0);
args->index->save();
idx2.load();
test_equal(idx2.size(), args->index->size());
test_equal(idx2.actual_size(), args->index->actual_size());
test :: begin();
for (char c = 'a'; c <= 'z'; c++) {
key = c;
it1 = args->index->find(key);
it2 = idx2.find(key);
if (args->n == 0) {
check_equal(it1, IDX_NULL);
check_equal(it2, IDX_NULL);
} else {
check_not_equal(it1, IDX_NULL);
check_not_equal(it2, IDX_NULL);
check_equal(it1->values.size(), it2->values.size());
}
}
test :: success();
}
static void index_test(unsigned int n)
{
test :: rm_data_dir();
std::stringstream ss;
ss << " (n = " << n << ")";
const std::string n_str = ss.str();
Index index("index.idx", false);
struct TestArgs args(n, &index);
if (n == 0)
run_test("Index Single Item Test", test_single_item);
run_test("Index Insertion Test" + n_str, test_insertion, &args);
run_test("Index Removal Test" + n_str, test_removal, &args);
run_test("Index Save and Load Test" + n_str, test_saving, &args);
}
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;
}