core/index: Rewrite unit test

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-10-21 10:21:43 -04:00
parent 2c5b14c7f3
commit 1244531f0d
2 changed files with 76 additions and 106 deletions

View File

@ -57,7 +57,7 @@ void IndexEntry :: read(file &file)
unsigned int num, val;
gchar *key = file_readl(&file);
file_readf(&file, "%u", &num);
file_readf(&file, "%u ", &num);
_key = key;
g_free(key);

View File

@ -3,131 +3,101 @@
* 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()
static void test_entry()
{
Index index("index.idx", false);
IndexEntry *ie = new struct IndexEntry("Link");
unsigned int i;
struct file f;
index.insert("a", 0);
IndexEntry *it = db_get(&index, "a");
ie->insert(0);
ie->insert(1);
ie->insert(2);
test_not_equal(it, (IndexEntry *)NULL);
test_equal(index.db_size, (unsigned)1);
test_equal(it->size(), (size_t)1);
test_equal(*(it->begin()), (unsigned)0);
index.remove("a", 0);
test_equal(index.db_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->db_size, (n == 0) ? 0 : 26);
for (char c = 'a'; c <= 'z'; c++) {
IndexEntry *it = db_get(INDEX, std::string(1, c).c_str());
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_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;
static void test_removal(unsigned int n)
{
IndexEntry *it;
ie = new IndexEntry();
test_equal(ie->primary_key(), "");
test_equal(ie->size(), 0);
test_equal(ie->has(42), false);
for (char c = 'a'; c <= 'z'; c++) {
for (unsigned int i = 0; i < n; i++)
INDEX->remove(std::string(1, c), i);
file_open(&f, OPEN_READ);
ie->read(f);
test_equal(ie->primary_key(), "Zelda");
test_equal(ie->size(), 0);
it = db_get(INDEX, std::string(1, c).c_str());
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();
}
ie->read(f);
test_equal(ie->primary_key(), "Link");
test_equal(ie->size(), 3);
file_close(&f);
static void test_save_load(unsigned int n)
{
Index idx2("index.idx", false);
IndexEntry *it1, *it2;
db_load(&idx2);
test_equal(idx2.db_size, (unsigned)0);
db_save(INDEX);
db_load(&idx2);
test_equal(idx2.db_size, INDEX->db_size);
test_equal(db_actual_size(&idx2), db_actual_size(INDEX));
for (char c = 'a'; c <= 'z'; c++) {
it1 = db_get(INDEX, std::string(1, c).c_str());
it2 = db_get(&idx2, std::string(1, c).c_str());
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');
}
for (i = 0; i < 3; i++) {
test_loop_equal(ie->has(i), true, i);
} test_loop_passed();
delete INDEX;
delete ie;
}
static void test_stress(unsigned int N)
{
Index index("stress.idx", false);
std::string key;
IndexEntry *ie;
unsigned int i;
char c;
#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.insert() */
for (c = 'a'; c <= 'z'; c++) {
key = c;
index.insert(key, 0);
for (i = 0; i < N; i++)
index.insert(key, i);
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_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)
/* index.remove() */
for (c = 'a'; c <= 'z'; c += 4) {
key = c;
for (i = 0; i < N; i++)
index.remove(key, 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("ZZ", 42);
test_equal(index.db_size, 26);
}
static void test_basics() { test_stress(10); }
static void test_stress() { test_stress(100000); }
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),
UNIT_TEST("Index Entry", test_entry),
UNIT_TEST("Index Basics (N = 10)", test_basics),
UNIT_TEST("Index Stress (N = 100,000)", test_stress),
);