152 lines
2.8 KiB
C++
152 lines
2.8 KiB
C++
/*
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
* Test a Database
|
|
*/
|
|
|
|
#include <core/index.h>
|
|
#include <tests/test.h>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
static unsigned int N = 0;
|
|
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()
|
|
{
|
|
std::string key;
|
|
IndexEntry *it;
|
|
|
|
test :: begin();
|
|
for (char c = 'a'; c <= 'z'; c++) {
|
|
key = c;
|
|
|
|
for (unsigned int i = 0; i < N; i++)
|
|
INDEX->insert(key, i);
|
|
}
|
|
test :: success();
|
|
|
|
if (N == 0)
|
|
test_equal(INDEX->size(), (unsigned int)0);
|
|
else
|
|
test_equal(INDEX->size(), (unsigned int)26);
|
|
|
|
test :: begin();
|
|
for (char c = 'a'; c <= 'z'; c++) {
|
|
key = c;
|
|
it = INDEX->find(key);
|
|
|
|
if (N == 0)
|
|
check_equal(it, (IndexEntry *)NULL);
|
|
else {
|
|
check_not_equal(it, (IndexEntry *)NULL);
|
|
check_equal(it->size(), (size_t)N);
|
|
}
|
|
}
|
|
test :: success();
|
|
}
|
|
|
|
static void test_removal()
|
|
{
|
|
std::string key;
|
|
IndexEntry *it;
|
|
|
|
test :: begin();
|
|
for (char c = 'a'; c <= 'z'; c++) {
|
|
key = c;
|
|
|
|
for (unsigned int i = 0; i < N; i++)
|
|
INDEX->remove(key, i);
|
|
it = INDEX->find(key);
|
|
if (N == 0)
|
|
check_equal(it, (IndexEntry *)NULL);
|
|
else {
|
|
check_not_equal(it, (IndexEntry *)NULL);
|
|
check_equal(it->size(), (size_t)0);
|
|
}
|
|
}
|
|
test :: success();
|
|
}
|
|
|
|
static void test_saving()
|
|
{
|
|
std::string key;
|
|
IndexEntry *it1, *it2;
|
|
|
|
Index idx2("index.idx", false);
|
|
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());
|
|
|
|
test :: begin();
|
|
for (char c = 'a'; c <= 'z'; c++) {
|
|
key = c;
|
|
it1 = INDEX->find(key);
|
|
it2 = idx2.find(key);
|
|
|
|
if (N == 0) {
|
|
check_equal(it1, (IndexEntry *)NULL);
|
|
check_equal(it2, (IndexEntry *)NULL);
|
|
} else {
|
|
check_not_equal(it1, (IndexEntry *)NULL);
|
|
check_not_equal(it2, (IndexEntry *)NULL);
|
|
check_equal(it1->size(), it2->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);
|
|
|
|
N = n;
|
|
INDEX = &index;
|
|
|
|
if (n == 0)
|
|
run_test("Index Single Item Test", test_single_item);
|
|
run_test("Index Insertion Test" + n_str, test_insertion);
|
|
run_test("Index Removal Test" + n_str, test_removal);
|
|
run_test("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;
|
|
}
|