ocarina/tests/core/index.cpp

112 lines
2.5 KiB
C++

/*
* Copyright 2014 (c) Anna Schumaker.
* Test a Database
*/
#include <core/index.h>
#include "test.h"
static void test_entry()
{
index_entry *ie = new index_entry("Link");
unsigned int i;
struct file f;
ie->ie_set.insert(0);
ie->ie_set.insert(1);
ie->ie_set.insert(2);
test_equal(ie->primary_key(), "Link");
test_equal(ie->size(), 3);
for (i = 0; i < 3; i++) {
test_loop_equal(ie->has(i), true, i);
} test_loop_passed();
test_equal(ie->has(3), false);
file_init(&f, "index_entry", 0);
file_open(&f, OPEN_WRITE);
file_writef(&f, "Zelda\n0 \n");
ie->write(f);
file_close(&f);
delete ie;
ie = new index_entry();
test_equal(ie->primary_key(), "");
test_equal(ie->size(), 0);
test_equal(ie->has(42), false);
file_open(&f, OPEN_READ);
ie->read(f);
test_equal(ie->primary_key(), "Zelda");
test_equal(ie->size(), 0);
ie->read(f);
test_equal(ie->primary_key(), "Link");
test_equal(ie->size(), 3);
file_close(&f);
for (i = 0; i < 3; i++) {
test_loop_equal(ie->has(i), true, i);
} test_loop_passed();
delete ie;
}
static void test_stress(unsigned int N)
{
index_entry *ie, *ie2;
std::string key;
unsigned int i;
Index index;
char c;
index_init(&index, "stress.idx", false);
/* index_insert() */
for (c = 'a'; c <= 'z'; c++) {
key = c;
ie = index_insert(&index, key.c_str(), 0);
test_loop_not_equal(ie, NULL, c - 'a');
test_loop_equal(ie->primary_key(), key, c - 'a');
for (i = 0; i < N; i++)
ie2 = index_insert(&index, key.c_str(), i);
test_loop_equal(ie, ie2, c - 'a');
test_loop_equal(ie->ie_set.size(), N, c - 'a');
test_loop_equal(index.db_size, c - 'a' + 1, c - 'a');
} test_loop_passed();
test_equal(index.db_size, 26);
/* index.has() */
for (c = 'a'; c <= 'z'; c += 4) {
key = c;
ie = db_find(&index, key.c_str());
test_loop_not_equal(ie, NULL, i);
for (i = 0; i < N; i++)
test_loop_equal(ie->has(i), true, i);
test_loop_equal(ie->has(N), false, c - 'a');
} test_loop_passed();
/* index_remove() */
for (c = 'a'; c <= 'z'; c += 4) {
key = c;
for (i = 0; i < N; i++)
index_remove(&index, key.c_str(), i);
ie = db_find(&index, key.c_str());
test_loop_not_equal(ie, NULL, c - 'a');
test_loop_equal(ie->size(), 0, c - 'a');
} test_loop_passed();
index_remove(&index, "ZZ", 42);
test_equal(index.db_size, 26);
db_deinit(&index);
}
static void test_basics() { test_stress(10); }
static void test_stress() { test_stress(100000); }
DECLARE_UNIT_TESTS(
UNIT_TEST("Index Entry", test_entry),
UNIT_TEST("Index Basics (N = 10)", test_basics),
UNIT_TEST("Index Stress (N = 100,000)", test_stress),
);