2014-03-12 20:23:34 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
* Test a Database
|
|
|
|
*/
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/index.h>
|
2015-08-26 08:49:30 -04:00
|
|
|
#include "test.h"
|
2014-03-12 20:23:34 -04:00
|
|
|
|
|
|
|
|
2015-10-21 10:21:43 -04:00
|
|
|
static void test_entry()
|
2014-05-03 18:42:54 -04:00
|
|
|
{
|
2015-10-21 10:41:42 -04:00
|
|
|
index_entry *ie = new index_entry("Link");
|
2015-10-21 10:21:43 -04:00
|
|
|
unsigned int i;
|
|
|
|
struct file f;
|
|
|
|
|
2015-09-29 16:11:59 -04:00
|
|
|
ie->ie_set.insert(0);
|
|
|
|
ie->ie_set.insert(1);
|
|
|
|
ie->ie_set.insert(2);
|
2015-10-21 10:21:43 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-10-21 10:41:42 -04:00
|
|
|
ie = new index_entry();
|
2015-10-21 10:21:43 -04:00
|
|
|
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();
|
2014-03-12 20:23:34 -04:00
|
|
|
|
2015-10-21 10:21:43 -04:00
|
|
|
delete ie;
|
2014-05-03 18:42:54 -04:00
|
|
|
}
|
2014-03-12 20:23:34 -04:00
|
|
|
|
2015-10-21 10:21:43 -04:00
|
|
|
static void test_stress(unsigned int N)
|
2014-05-03 18:42:54 -04:00
|
|
|
{
|
2015-09-29 16:11:59 -04:00
|
|
|
index_entry *ie, *ie2;
|
2015-10-21 10:21:43 -04:00
|
|
|
std::string key;
|
|
|
|
unsigned int i;
|
2015-09-29 16:25:16 -04:00
|
|
|
Index index;
|
2015-10-21 10:21:43 -04:00
|
|
|
char c;
|
|
|
|
|
2015-09-29 16:25:16 -04:00
|
|
|
index_init(&index, "stress.idx", false);
|
|
|
|
|
2015-09-29 16:11:59 -04:00
|
|
|
/* index_insert() */
|
2015-10-21 10:21:43 -04:00
|
|
|
for (c = 'a'; c <= 'z'; c++) {
|
|
|
|
key = c;
|
2015-09-29 16:11:59 -04:00
|
|
|
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');
|
2015-10-21 10:21:43 -04:00
|
|
|
for (i = 0; i < N; i++)
|
2015-09-29 16:11:59 -04:00
|
|
|
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');
|
2015-10-21 10:21:43 -04:00
|
|
|
test_loop_equal(index.db_size, c - 'a' + 1, c - 'a');
|
2015-08-26 08:49:30 -04:00
|
|
|
} test_loop_passed();
|
2015-10-21 10:21:43 -04:00
|
|
|
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');
|
2015-08-26 08:49:30 -04:00
|
|
|
} test_loop_passed();
|
2014-05-03 18:42:54 -04:00
|
|
|
|
2015-09-29 16:07:34 -04:00
|
|
|
/* index_remove() */
|
2015-10-21 10:21:43 -04:00
|
|
|
for (c = 'a'; c <= 'z'; c += 4) {
|
|
|
|
key = c;
|
|
|
|
for (i = 0; i < N; i++)
|
2015-09-29 16:07:34 -04:00
|
|
|
index_remove(&index, key.c_str(), i);
|
2015-10-21 10:21:43 -04:00
|
|
|
ie = db_find(&index, key.c_str());
|
|
|
|
test_loop_not_equal(ie, NULL, c - 'a');
|
|
|
|
test_loop_equal(ie->size(), 0, c - 'a');
|
2015-08-26 08:49:30 -04:00
|
|
|
} test_loop_passed();
|
2015-09-29 16:07:34 -04:00
|
|
|
index_remove(&index, "ZZ", 42);
|
2015-10-21 10:21:43 -04:00
|
|
|
test_equal(index.db_size, 26);
|
2015-09-29 16:25:16 -04:00
|
|
|
|
|
|
|
db_deinit(&index);
|
2014-05-03 18:42:54 -04:00
|
|
|
}
|
2014-03-12 20:23:34 -04:00
|
|
|
|
2015-10-21 10:21:43 -04:00
|
|
|
static void test_basics() { test_stress(10); }
|
|
|
|
static void test_stress() { test_stress(100000); }
|
2015-08-26 08:49:30 -04:00
|
|
|
|
|
|
|
DECLARE_UNIT_TESTS(
|
2015-10-21 10:21:43 -04:00
|
|
|
UNIT_TEST("Index Entry", test_entry),
|
|
|
|
UNIT_TEST("Index Basics (N = 10)", test_basics),
|
|
|
|
UNIT_TEST("Index Stress (N = 100,000)", test_stress),
|
2015-08-26 08:49:30 -04:00
|
|
|
);
|