tests/core: Update database test to the new framework

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-08-25 09:27:01 -04:00
parent da8131aa6d
commit 7318363287
2 changed files with 109 additions and 119 deletions

View File

@ -1,22 +1,23 @@
#!/usr/bin/python
import os
Import("UnitTest")
Import("env", "UnitTest")
objs = []
def CoreTest(name, source):
src_list = [ source ]
global objs
if os.path.exists("../../core/%s" % source):
src_list += [ "../../core/%s" % source ]
run = UnitTest("core/%s" % name, src_list)
objs += [ env.Object("../../core/%s" % source) ]
run = UnitTest("core/%s" % name, [ source ] + objs)
Alias("tests/core", run)
return run
res = [ CoreTest("version", "version.c") ]
res += [ CoreTest("string", "string.cpp") ]
res += [ CoreTest("file", "file.cpp") ]
res = [ CoreTest("version", "version.c") ]
res += [ CoreTest("string", "string.cpp") ]
res += [ CoreTest("file", "file.cpp") ]
res += [ CoreTest("database", "database.cpp") ]
Return("res")
#test( "database" )
#test( "index" )
#test( "filter" )
#test( "idle" )

View File

@ -4,7 +4,7 @@
*/
#include <core/database.h>
#include <core/string.h>
#include <tests/test.h>
#include "test.h"
#include <vector>
@ -29,127 +29,102 @@ public:
};
static unsigned int N = 0;
static bool AUTOSAVE = false;
static Database<IntEntry> *DB = NULL;
static Database<IntEntry>::iterator IT;
static std::vector<IntEntry *> *POINTERS;
static unsigned int _test_insertion_check(unsigned int i)
static void test_insertion(unsigned int n, bool autosave)
{
IntEntry *it = DB->insert(IntEntry(i));
if ((it == NULL) || (it->index() != i) || (it->val != i))
return LOOP_FAILED;
POINTERS->push_back(it);
test_rm_data_dir();
DB = new Database<IntEntry>("database.db", autosave);
std::vector<IntEntry *> pointers;
return (DB->insert(IntEntry(i)) == NULL) ? LOOP_PASSED : LOOP_FAILED;
}
static unsigned int _test_insertion_pointer_check(unsigned int i)
{
return (POINTERS->at(i) == DB->at(i)) ? LOOP_PASSED : LOOP_FAILED;
}
static void test_insertion()
{
/* Check initial sizes. */
test_equal(DB->size(), (unsigned)0);
test_equal(DB->actual_size(), (unsigned)0);
test_for_each(0, N, 1, _test_insertion_check);
test_equal(DB->size(), N);
test_equal(DB->actual_size(), N);
for (unsigned int i = 0; i < n; i++) {
IntEntry *it = DB->insert(IntEntry(i));
test_loop_not_equal(it, NULL, i);
test_loop_equal(it->index(), i, i);
test_loop_equal(it->val, i, i);
test_loop_equal(DB->insert(IntEntry(i)), NULL, i);
pointers.push_back(it);
} test_loop_passed();
test_equal(DB->size(), n);
test_equal(DB->actual_size(), n);
/* Pointers should still be valid. */
test_for_each(0, N, 1, _test_insertion_pointer_check);
for (unsigned int i = 0; i < n; i++)
test_loop_equal(pointers.at(i), DB->at(i), i);
test_loop_passed();
}
static void test_removal()
static void test_removal(unsigned int n, bool autosave)
{
/* Size should have only changed once each iteration. */
for (unsigned int i = 0; i < N; i+=2) {
for (unsigned int i = 0; i < n; i+=2) {
DB->remove(i);
DB->remove(i);
}
test_equal(DB->size(), N / 2);
test_equal(DB->actual_size(), N);
test_equal(DB->size(), n / 2);
test_equal(DB->actual_size(), n);
/* Test out-of-bounds removal. */
for (unsigned int i = 0; i < 10; i++)
DB->remove(N + i);
DB->remove(n + i);
test_equal(DB->size(), N / 2);
test_equal(DB->actual_size(), N);
test_equal(DB->size(), n / 2);
test_equal(DB->actual_size(), n);
}
static unsigned int _test_access_null(unsigned int i)
static void test_access(unsigned int n, bool autosave)
{
return (DB->at(i) == NULL) ? LOOP_PASSED : LOOP_FAILED;
Database<IntEntry>::iterator it;
IntEntry *ie;
for (unsigned int i = 0; i < n; i += 2)
test_loop_equal(DB->at(i), NULL, i);
test_loop_passed();
it = DB->begin();
for (unsigned int i = 1; i < n; i += 2) {
ie = DB->at(i);
test_loop_not_equal(ie, NULL, i);
test_loop_equal(ie->val, i, i);
test_loop_equal(ie->index(), i, i);
test_loop_equal(*it, ie, i);
it = DB->next(it);
} test_loop_passed();
for (unsigned int i = n; i < n + 10; i++)
test_loop_equal(DB->at(i), NULL, i);
test_loop_passed();
}
static unsigned int _test_access_valid(unsigned int i)
static void test_reinsertion(unsigned int n, bool autosave)
{
IntEntry *ie = DB->at(i);
IntEntry *it;
if ((ie == NULL) || (*IT != ie) ||
(ie->val != i) || (ie->index() != i))
return LOOP_FAILED;
IT = DB->next(IT);
return LOOP_PASSED;
}
for (unsigned int i = 0; i < n; i += 2) {
it = DB->insert(IntEntry(i));
test_loop_equal(it->index(), (n + (i / 2)), i);
} test_loop_passed();
static void test_access()
{
IT = DB->begin();
test_for_each(0, N, 2, _test_access_null);
test_for_each(1, N, 2, _test_access_valid);
test_for_each(N, N + 10, 1, _test_access_null);
test_equal(DB->size(), n);
test_equal(DB->actual_size(), n + (n / 2));
}
static unsigned int _test_reinsert_check(unsigned int i)
{
IntEntry *it = DB->insert(IntEntry(i));
return (it->index() == (N + (i / 2))) ? LOOP_PASSED : LOOP_FAILED;
}
static void test_reinsert()
{
test_for_each(0, N, 2, _test_reinsert_check);
test_equal(DB->size(), N);
test_equal(DB->actual_size(), N + (N / 2));
}
static Database<IntEntry> *DB2;
static unsigned int _test_save_load_check(unsigned int i)
{
IntEntry *ie1 = DB->at(i);
IntEntry *ie2 = DB2->at(i);
if (ie1 == NULL)
return (ie2 == NULL) ? LOOP_PASSED : LOOP_FAILED;
else {
if ((ie2 == NULL) || (ie1->index() != ie2->index()) ||
(ie1->val != ie2->val))
return LOOP_FAILED;
}
return LOOP_PASSED;
}
static void test_save_load()
static void test_save_load(unsigned int n, bool autosave)
{
Database<IntEntry> db2("database.db", false);
IntEntry *ie1, *ie2;
db2.load();
DB2 = &db2;
if (AUTOSAVE == false) {
if (autosave == false) {
test_equal(db2.size(), (unsigned)0);
test_equal(db2.actual_size(), (unsigned)0);
@ -159,35 +134,49 @@ static void test_save_load()
test_equal(db2.size(), DB->size());
test_equal(db2.actual_size(), DB->actual_size());
test_for_each(0, DB->actual_size(), 1, _test_save_load_check);
for (unsigned int i = 0; i < DB->actual_size(); i++) {
ie1 = DB->at(i);
ie2 = db2.at(i);
if (ie1) {
test_loop_equal(ie2->index(), ie1->index(), i);
test_loop_equal(ie2->val, ie1->val, i);
} else
test_loop_equal(ie2, NULL, i);
} test_loop_passed();
delete DB;
}
static void db_test(unsigned int n, bool autosave)
{
std::string n_str = " (n = " + string :: utos(n) + ")";
Database<IntEntry> db("database.db", autosave);
std::vector<IntEntry *> pointers;
N = n;
DB = &db;
AUTOSAVE = autosave;
POINTERS = &pointers;
#define DB_TEST_FUNCS(n, autosave) \
static void test_insertion_##n( ) { test_insertion(n, autosave); } \
static void test_removal_##n() { test_removal(n, autosave); } \
static void test_access_##n() { test_access(n, autosave); } \
static void test_reinsertion_##n() { test_reinsertion(n, autosave); } \
static void test_save_load_##n() { test_save_load(n, autosave); }
test :: rm_data_dir();
test :: run("Database Insertion Test" + n_str, test_insertion);
test :: run("Database Removal Test" + n_str, test_removal);
test :: run("Database Access Test" + n_str, test_access);
test :: run("Database Reinsertion Test" + n_str, test_reinsert);
test :: run("Database Save and Load Test" + n_str, test_save_load);
}
int main(int argc, char **argv)
{
db_test(0, true);
db_test(10, true);
db_test(100, true);
db_test(1000, true);
db_test(10000, false);
db_test(100000, false);
return 0;
}
DB_TEST_FUNCS(0, true)
DB_TEST_FUNCS(10, true)
DB_TEST_FUNCS(100, true)
DB_TEST_FUNCS(1000, true)
DB_TEST_FUNCS(10000, false)
DB_TEST_FUNCS(100000, false)
#define DB_UNIT_TESTS(n) \
UNIT_TEST("Database Insertion (n = " #n ")", test_insertion_##n), \
UNIT_TEST("Database Removal (n = " #n ")", test_removal_##n), \
UNIT_TEST("Database Access (n = " #n ")", test_access_##n), \
UNIT_TEST("Database Reinsertion (n = " #n ")", test_reinsertion_##n), \
UNIT_TEST("Database Save and Load (n = " #n ")", test_save_load_##n)
DECLARE_UNIT_TESTS(
DB_UNIT_TESTS(0),
DB_UNIT_TESTS(10),
DB_UNIT_TESTS(100),
DB_UNIT_TESTS(1000),
DB_UNIT_TESTS(10000),
DB_UNIT_TESTS(100000),
);