database: Clean up the unit test
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
ae1f0300f7
commit
4b0c6121c3
|
@ -7,261 +7,179 @@
|
|||
#include <tests/test.h>
|
||||
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
|
||||
/***
|
||||
*
|
||||
* Derive a DatabaseEntry for storing ints
|
||||
*
|
||||
/*
|
||||
* Derive a DatabaseEntry for storing integerss
|
||||
*/
|
||||
|
||||
class IntEntry : public DatabaseEntry {
|
||||
public:
|
||||
unsigned int val;
|
||||
|
||||
IntEntry();
|
||||
IntEntry(unsigned int);
|
||||
const std::string primary_key() const;
|
||||
void write(File &);
|
||||
void read(File &);
|
||||
IntEntry() : val(0) {};
|
||||
IntEntry(unsigned int v) : val(v) {};
|
||||
|
||||
const std::string primary_key() const
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << val;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
void write(File &f) { f << val; }
|
||||
void read(File &f) { f >> val; }
|
||||
};
|
||||
|
||||
IntEntry :: IntEntry() : val(0) {}
|
||||
IntEntry :: IntEntry(unsigned int v) : val(v) {}
|
||||
|
||||
const std::string IntEntry :: primary_key() const
|
||||
static unsigned int N = 0;
|
||||
static bool AUTOSAVE = false;
|
||||
static Database<IntEntry> *DB = NULL;
|
||||
|
||||
|
||||
static void test_insertion()
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << val;
|
||||
return ss.str();
|
||||
}
|
||||
std::vector<IntEntry *> pointers;
|
||||
std::vector<IntEntry *>::iterator p_it;
|
||||
|
||||
void IntEntry :: write(File &f) { f << val; }
|
||||
void IntEntry :: read(File &f) { f >> val; }
|
||||
|
||||
static IntEntry *INT_NULL = NULL;
|
||||
|
||||
|
||||
|
||||
/***
|
||||
*
|
||||
* Struct for passing around arguments to tests
|
||||
*
|
||||
*/
|
||||
|
||||
struct TestArgs {
|
||||
const unsigned int n;
|
||||
unsigned int size;
|
||||
unsigned int actual;
|
||||
bool autosave;
|
||||
|
||||
IntEntry *one;
|
||||
IntEntry *three;
|
||||
Database<IntEntry> *db;
|
||||
|
||||
TestArgs(const unsigned int, bool, Database<IntEntry> *);
|
||||
};
|
||||
|
||||
TestArgs :: TestArgs(const unsigned int _n, bool _asv, Database<IntEntry> *_db)
|
||||
: n(_n), size(0), actual(0), autosave(_asv),
|
||||
one(NULL), three(NULL), db(_db)
|
||||
{}
|
||||
|
||||
|
||||
/***
|
||||
*
|
||||
* Run tests with varying database sizes
|
||||
*
|
||||
*/
|
||||
|
||||
static void test_insertion(struct TestArgs *args)
|
||||
{
|
||||
/*
|
||||
* Initial size should be 0
|
||||
* Check initial sizes.
|
||||
*/
|
||||
test_equal(args->db->size(), args->size);
|
||||
test_equal(args->db->actual_size(), args->actual);
|
||||
test_equal(DB->size(), (unsigned)0);
|
||||
test_equal(DB->actual_size(), (unsigned)0);
|
||||
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < args->n; i++) {
|
||||
IntEntry *it = args->db->insert(IntEntry(i));
|
||||
check_not_equal(it, INT_NULL);
|
||||
for (unsigned int i = 0; i < N; i++) {
|
||||
IntEntry *it = DB->insert(IntEntry(i));
|
||||
check_not_equal(it, (IntEntry *)NULL);
|
||||
check_equal(it->index(), i);
|
||||
check_equal(it->val, i);
|
||||
|
||||
pointers.push_back(it);
|
||||
|
||||
it = DB->insert(IntEntry(i));
|
||||
check_equal(it, (IntEntry *)NULL);
|
||||
}
|
||||
test :: success();
|
||||
|
||||
/*
|
||||
* Size should change
|
||||
*/
|
||||
test_equal(DB->size(), N);
|
||||
test_equal(DB->actual_size(), N);
|
||||
|
||||
/*
|
||||
* Pointers should still be valid
|
||||
*/
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < N; i++)
|
||||
check_equal(pointers[i], DB->at(i));
|
||||
test :: success();
|
||||
}
|
||||
|
||||
static void test_removal()
|
||||
{
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < N; i+=2) {
|
||||
/*
|
||||
* Pointers should still be valid after more insertions.
|
||||
* These will be checked later
|
||||
* Database size should only decrease once.
|
||||
*/
|
||||
if (i == 1)
|
||||
args->one = it;
|
||||
if (i == 3)
|
||||
args->three = it;
|
||||
|
||||
args->size++;
|
||||
args->actual++;
|
||||
DB->remove(i);
|
||||
DB->remove(i);
|
||||
}
|
||||
test :: success();
|
||||
|
||||
/*
|
||||
* Size should change
|
||||
*/
|
||||
test_equal(args->db->size(), args->size);
|
||||
test_equal(args->db->actual_size(), args->actual);
|
||||
}
|
||||
|
||||
static void test_insertion2(struct TestArgs *args)
|
||||
{
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < args->n; i++) {
|
||||
IntEntry *it = args->db->insert(IntEntry(i));
|
||||
check_equal(it, INT_NULL);
|
||||
}
|
||||
test :: success();
|
||||
|
||||
/*
|
||||
* Size should not change
|
||||
*/
|
||||
test_equal(args->db->size(), args->size);
|
||||
test_equal(args->db->actual_size(), args->actual);
|
||||
}
|
||||
|
||||
static void test_removal(struct TestArgs *args)
|
||||
{
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < args->n; i+=2) {
|
||||
args->db->remove(i);
|
||||
args->size--;
|
||||
}
|
||||
test :: success();
|
||||
|
||||
/*
|
||||
* Size should change
|
||||
*/
|
||||
test_equal(args->db->size(), args->size);
|
||||
test_equal(args->db->actual_size(), args->actual);
|
||||
test_equal(DB->size(), N / 2);
|
||||
test_equal(DB->actual_size(), N);
|
||||
|
||||
/*
|
||||
* Test out-of-bounds removal
|
||||
*/
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < 10; i++)
|
||||
args->db->remove(args->n + i);
|
||||
DB->remove(N + i);
|
||||
test :: success();
|
||||
|
||||
/*
|
||||
* Size should not change
|
||||
*/
|
||||
test_equal(args->db->size(), args->size);
|
||||
test_equal(args->db->actual_size(), args->actual);
|
||||
test_equal(DB->size(), N / 2);
|
||||
test_equal(DB->actual_size(), N);
|
||||
}
|
||||
|
||||
static void test_removal2(struct TestArgs *args)
|
||||
static void test_access()
|
||||
{
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < args->n; i+=2)
|
||||
args->db->remove(i);
|
||||
test :: success();
|
||||
|
||||
/*
|
||||
* Size should not change
|
||||
*/
|
||||
test_equal(args->db->size(), args->size);
|
||||
test_equal(args->db->actual_size(), args->actual);
|
||||
}
|
||||
|
||||
static void test_iterator(struct TestArgs *args)
|
||||
{
|
||||
unsigned int index = 1;
|
||||
Database<IntEntry>::iterator it;
|
||||
|
||||
test :: begin();
|
||||
for (it = args->db->begin(); it != args->db->end(); it = args->db->next(it)) {
|
||||
check_not_equal((*it), INT_NULL);
|
||||
check_equal((*it)->val, index);
|
||||
check_equal((*it)->index(), index);
|
||||
index += 2;
|
||||
};
|
||||
for (unsigned int i = 0; i < N; i+=2)
|
||||
check_equal(DB->at(i), (IntEntry *)NULL);
|
||||
test :: success();
|
||||
}
|
||||
|
||||
static void test_access(struct TestArgs *args)
|
||||
{
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < args->n; i+=2) {
|
||||
IntEntry *it = args->db->at(i);
|
||||
check_equal(it, INT_NULL);
|
||||
it = DB->begin();
|
||||
for (unsigned int i = 1; i < N; i+=2) {
|
||||
IntEntry *ie = DB->at(i);
|
||||
|
||||
check_not_equal(ie, (IntEntry *)NULL);
|
||||
check_equal(*it, ie);
|
||||
check_equal(ie->val, i);
|
||||
check_equal(ie->index(), i);
|
||||
|
||||
it = DB->next(it);
|
||||
}
|
||||
test :: success();
|
||||
|
||||
test :: begin();
|
||||
for (unsigned int i = 1; i < args->n; i+=2) {
|
||||
IntEntry *it = args->db->at(i);
|
||||
check_not_equal(it, INT_NULL);
|
||||
}
|
||||
test :: success();
|
||||
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < 10; i++) {
|
||||
IntEntry *it = args->db->at(args->n + i);
|
||||
check_equal(it, INT_NULL);
|
||||
}
|
||||
for (unsigned int i = 0; i < 10; i++)
|
||||
check_equal(DB->at(N + i), (IntEntry *)NULL);
|
||||
test :: success();
|
||||
}
|
||||
|
||||
static void test_pointers(struct TestArgs *args)
|
||||
{
|
||||
IntEntry *it = args->db->at(1);
|
||||
test_equal(it, args->one);
|
||||
|
||||
it = args->db->at(3);
|
||||
test_equal(it, args->three);
|
||||
}
|
||||
|
||||
static void test_insertion3(struct TestArgs *args)
|
||||
static void test_reinsert()
|
||||
{
|
||||
test :: begin();
|
||||
for (unsigned int i = 0; i < args->n; i+=2) {
|
||||
IntEntry *it = args->db->insert(IntEntry(i));
|
||||
|
||||
args->size++;
|
||||
args->actual++;
|
||||
check_equal(it->index(), args->n + (i / 2));
|
||||
for (unsigned int i = 0; i < N; i+=2) {
|
||||
IntEntry *it = DB->insert(IntEntry(i));
|
||||
check_equal(it->index(), N + (i / 2));
|
||||
}
|
||||
test :: success();
|
||||
|
||||
/*
|
||||
* Size should change
|
||||
*/
|
||||
test_equal(args->db->size(), args->size);
|
||||
test_equal(args->db->actual_size(), args->actual);
|
||||
test_equal(DB->size(), N);
|
||||
test_equal(DB->actual_size(), N + (N / 2));
|
||||
}
|
||||
|
||||
static void test_autosave(struct TestArgs *args)
|
||||
static void test_save_load()
|
||||
{
|
||||
Database<IntEntry> db2("database.db", args->autosave);
|
||||
Database<IntEntry> db2("database.db", false);
|
||||
db2.load();
|
||||
|
||||
if (args->autosave == false) {
|
||||
if (AUTOSAVE == false) {
|
||||
test_equal(db2.size(), (unsigned)0);
|
||||
test_equal(db2.actual_size(), (unsigned)0);
|
||||
|
||||
args->db->save();
|
||||
DB->save();
|
||||
db2.load();
|
||||
}
|
||||
|
||||
test_equal(db2.size(), args->db->size());
|
||||
test_equal(db2.actual_size(), args->db->actual_size());
|
||||
test_equal(db2.size(), DB->size());
|
||||
test_equal(db2.actual_size(), DB->actual_size());
|
||||
|
||||
Database<IntEntry>::iterator it1;
|
||||
Database<IntEntry>::iterator it2 = db2.begin();
|
||||
|
||||
test :: begin();
|
||||
for (it1 = args->db->begin(); it1 != args->db->end(); it1++) {
|
||||
if (*it1 == INT_NULL)
|
||||
check_equal(*it2, INT_NULL);
|
||||
for (it1 = DB->begin(); it1 != DB->end(); it1++) {
|
||||
if (*it1 == (IntEntry *)NULL)
|
||||
check_equal(*it2, (IntEntry *)NULL);
|
||||
else {
|
||||
check_not_equal(*it2, INT_NULL);
|
||||
check_not_equal(*it2, (IntEntry *)NULL);
|
||||
check_equal((*it1)->index(), (*it2)->index());
|
||||
check_equal((*it1)->val, (*it2)->val);
|
||||
}
|
||||
|
@ -279,17 +197,16 @@ static void db_test(unsigned int n, bool autosave)
|
|||
const std::string n_str = ss.str();
|
||||
|
||||
Database<IntEntry> db("database.db", autosave);
|
||||
struct TestArgs args(n, autosave, &db);
|
||||
|
||||
run_test("Database Insertion Test" + n_str, test_insertion, &args);
|
||||
run_test("Database Second Insertion Test" + n_str, test_insertion2, &args);
|
||||
run_test("Database Removal Test" + n_str, test_removal, &args);
|
||||
run_test("Database Second Removal Test" + n_str, test_removal2, &args);
|
||||
run_test("Database Iterator Test" + n_str, test_iterator, &args);
|
||||
run_test("Database Access Test" + n_str, test_access, &args);
|
||||
run_test("Database Pointer Test" + n_str, test_pointers, &args);
|
||||
run_test("Database Third Insertion Test" + n_str, test_insertion3, &args);
|
||||
run_test("Database Save and Load Test" + n_str, test_autosave, &args);
|
||||
N = n;
|
||||
DB = &db;
|
||||
AUTOSAVE = autosave;
|
||||
|
||||
run_test("Database Insertion Test" + n_str, test_insertion);
|
||||
run_test("Database Removal Test" + n_str, test_removal);
|
||||
run_test("Database Access Test" + n_str, test_access);
|
||||
run_test("Database Reinsertion Test" + n_str, test_reinsert);
|
||||
run_test("Database Save and Load Test" + n_str, test_save_load);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
|
|
Loading…
Reference in New Issue