196 lines
2.7 KiB
C++
196 lines
2.7 KiB
C++
/*
|
|
* Copyright 2013 (c) Anna Schumaker.
|
|
*/
|
|
#include <database.h>
|
|
#include <print.h>
|
|
|
|
#include <sstream>
|
|
|
|
class DBTest : public DatabaseEntry {
|
|
public:
|
|
unsigned int value;
|
|
|
|
DBTest();
|
|
DBTest(unsigned int);
|
|
void write(File &);
|
|
void read(File &);
|
|
void print();
|
|
};
|
|
|
|
DBTest :: DBTest()
|
|
{
|
|
}
|
|
|
|
DBTest :: DBTest(unsigned int val)
|
|
{
|
|
std::stringstream ss;
|
|
|
|
value = val;
|
|
ss << value;
|
|
primary_key = ss.str();
|
|
}
|
|
|
|
void DBTest :: write(File &file)
|
|
{
|
|
file << value;
|
|
}
|
|
|
|
void DBTest :: read(File &file)
|
|
{
|
|
file >> value;
|
|
}
|
|
|
|
void DBTest :: print()
|
|
{
|
|
:: print("%u", value);
|
|
}
|
|
|
|
|
|
|
|
void print_db(Database<DBTest> &db)
|
|
{
|
|
print("Database size: %u\n", db.size());
|
|
print("Num rows: %u\n", db.num_rows());
|
|
print("First: %u\n", db.first());
|
|
print("Last: %u\n", db.last());
|
|
|
|
for (unsigned int i = db.first(); i != db.num_rows(); i = db.next(i))
|
|
print("db[%u] = %u\n", i, db[i].value);
|
|
}
|
|
|
|
void test_insertion(Database<DBTest> &db)
|
|
{
|
|
for (unsigned int i = 1; i <= 100000; i++)
|
|
db.insert(DBTest(i));
|
|
print_db(db);
|
|
}
|
|
|
|
void test_deletion(Database<DBTest> &db)
|
|
{
|
|
for (unsigned int i = db.first(); i < db.num_rows(); i = db.next(i)) {
|
|
db.remove(i);
|
|
i = db.next(i);
|
|
}
|
|
print_db(db);
|
|
}
|
|
|
|
/*
|
|
* Test everything except reloading.
|
|
*/
|
|
void test_0()
|
|
{
|
|
print("Test 0\n");
|
|
Database<DBTest> db("test.db");
|
|
|
|
test_insertion(db);
|
|
test_deletion(db);
|
|
test_deletion(db);
|
|
|
|
db.save();
|
|
}
|
|
|
|
/*
|
|
* Reload the database from disk.
|
|
*/
|
|
void test_1()
|
|
{
|
|
print("\nTest 1\n");
|
|
Database<DBTest> db("test.db");
|
|
db.load();
|
|
print_db(db);
|
|
}
|
|
|
|
/*
|
|
* Attempt to save a db with an empty filepath.
|
|
*/
|
|
void test_2()
|
|
{
|
|
print("\nTest 2\n");
|
|
Database<DBTest> db("");
|
|
|
|
test_insertion(db);
|
|
db.save();
|
|
}
|
|
|
|
/*
|
|
* Attempt to load a db with an empty filepath
|
|
*/
|
|
void test_3()
|
|
{
|
|
print("\nTest 3\n");
|
|
Database<DBTest> db("");
|
|
db.load();
|
|
}
|
|
|
|
/*
|
|
* Test unique insertion
|
|
*/
|
|
void test_4()
|
|
{
|
|
print("\nTest 4\n");
|
|
Database<DBTest> db("");
|
|
|
|
for (unsigned int i = 0; i < 100; i++)
|
|
db.insert(DBTest(i % 5));
|
|
|
|
print_db(db);
|
|
}
|
|
|
|
/*
|
|
* Test the database's built-in print() function
|
|
*/
|
|
void test_5()
|
|
{
|
|
print("\nTest 5\n");
|
|
Database<DBTest> db("");
|
|
for (unsigned int i = 0; i < 10; i++)
|
|
db.insert(DBTest(i));
|
|
db.print();
|
|
}
|
|
|
|
/*
|
|
* Test the database's clear() function
|
|
*/
|
|
void test_6()
|
|
{
|
|
print("\nTest 6\n");
|
|
Database<DBTest> db("");
|
|
for (unsigned int i = 0; i < 10; i++)
|
|
db.insert(DBTest(i));
|
|
print_db(db);
|
|
db.clear();
|
|
print_db(db);
|
|
}
|
|
|
|
/*
|
|
* Print databases with no valid rows
|
|
*/
|
|
void test_7()
|
|
{
|
|
print("\nTest 7\n");
|
|
Database<DBTest> db("");
|
|
print_db(db);
|
|
|
|
for (unsigned int i = 0; i < 10; i++)
|
|
db.insert(DBTest(i));
|
|
print_db(db);
|
|
|
|
for (unsigned int i = 0; i < 10; i++)
|
|
db.remove(i);
|
|
print_db(db);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
test_0();
|
|
test_1();
|
|
test_2();
|
|
test_3();
|
|
test_4();
|
|
test_5();
|
|
test_6();
|
|
test_7();
|
|
|
|
return 0;
|
|
}
|