database: Add a function for printing

Other tests may need to print out a database.  To make this easier I've
added a print() function to the base database class.  This function will
only exist when CONFIG.TEST == True, so don't use it outside of testing!

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2013-09-29 22:32:08 -04:00 committed by Anna Schumaker
parent 38f97fb85b
commit 79d592ed64
6 changed files with 114 additions and 49 deletions

View File

@ -227,11 +227,12 @@ Database: (lib/database.cpp)
- DatabaseEntry:
class DatabaseEntry { /* let database modify valid flag */
private:
bool valid;
public:
bool valid;
virtual void write(File &) = 0;
virtual void read(File &) = 0;
virtual void print() = 0;
};
File << <CHILD_CLASS_DATA>
@ -246,14 +247,15 @@ Database: (lib/database.cpp)
template <class T>
class Database {
private:
unsigned int _size; /* Number of valid rows */
File filename;
DatabaseFlags flags;
vector<T> db;
unsigned int _size; /* Number of valid rows */
File file;
DatabaseFlags flags;
public:
Database::Database(filename, flags);
void load();
void save();
void print();
unsigned int insert(T);
void delete(unsigned int);
@ -268,49 +270,54 @@ Database: (lib/database.cpp)
File << db.size() << endl
File << INDEX_0 << db[INDEX_0].valid << db[INDEX_0] << endl;
File << Index_1 << db[INDEX_1].valid << db[INDEX_1] << endl;
File << INDEX_1 << db[INDEX_1].valid << db[INDEX_1] << endl;
...
- API:
Database : Database(filename, flags);
Database :: Database(filename, flags);
Initializes database to use ~/.ocarina{-debug}/filename.
Set up flags.
void Database : load();
Reads data from file.
void Database :: load();
Reads a saved database from disk.
void Database : save();
Saves data to file.
void Database :: save();
Saves the database to disk.
void Database :: print()
This function exists only If CONFIG_DEBUG is enabled.
Following a similar format for writing to disk, print the
database to the console in a human-readable format.
template <class T>
unsigned int Database : insert(T &);
unsigned int Database :: insert(T &);
Adds a new item to the db, returns the id of the item.
if DB_UNIQUE is set, check if the item already exists in the
DB and return it's ID instead of adding a new item.
void Database : delete(unsigned int index);
void Database :: delete(unsigned int index);
Mark db[index] as invalid (quick deletion).
unsigned int Database : size();
unsigned int Database :: size();
Returns number of valid rows in the database.
unsigned int Database : num_rows();
unsigned int Database :: num_rows();
Return db.size().
unsigned int Database : first();
unsigned int Database :: first();
Return the id to the first valid row or return db.size()
if there are no valid rows.
unsigned int Database : last();
unsigned int Database :: last();
Return the id of the last valid row or return db.size()
if there are no valid rows.
unsigned int Database : next(unsigned int &id)
unsigned int Database :: next(unsigned int &id)
Return the id of the next valid row or return db.size()
if there are no remaining valid rows.
template <class T>
T &Database : operator[unsigned int index]
T &Database :: operator[unsigned int index]
Return a reference to db[index].

View File

@ -31,11 +31,12 @@ Database: (lib/database.cpp)
- DatabaseEntry:
class DatabaseEntry { /* let database modify valid flag */
private:
bool valid;
public:
bool valid;
virtual void write(File &) = 0;
virtual void read(File &) = 0;
virtual void print() = 0;
};
File << <CHILD_CLASS_DATA>
@ -50,14 +51,15 @@ Database: (lib/database.cpp)
template <class T>
class Database {
private:
unsigned int _size; /* Number of valid rows */
File filename;
DatabaseFlags flags;
vector<T> db;
unsigned int _size; /* Number of valid rows */
File file;
DatabaseFlags flags;
public:
Database::Database(filename, flags);
void load();
void save();
void print();
unsigned int insert(T);
void delete(unsigned int);
@ -72,47 +74,52 @@ Database: (lib/database.cpp)
File << db.size() << endl
File << INDEX_0 << db[INDEX_0].valid << db[INDEX_0] << endl;
File << Index_1 << db[INDEX_1].valid << db[INDEX_1] << endl;
File << INDEX_1 << db[INDEX_1].valid << db[INDEX_1] << endl;
...
- API:
Database : Database(filename, flags);
Database :: Database(filename, flags);
Initializes database to use ~/.ocarina{-debug}/filename.
Set up flags.
void Database : load();
Reads data from file.
void Database :: load();
Reads a saved database from disk.
void Database : save();
Saves data to file.
void Database :: save();
Saves the database to disk.
void Database :: print()
This function exists only If CONFIG_DEBUG is enabled.
Following a similar format for writing to disk, print the
database to the console in a human-readable format.
template <class T>
unsigned int Database : insert(T &);
unsigned int Database :: insert(T &);
Adds a new item to the db, returns the id of the item.
if DB_UNIQUE is set, check if the item already exists in the
DB and return it's ID instead of adding a new item.
void Database : delete(unsigned int index);
void Database :: delete(unsigned int index);
Mark db[index] as invalid (quick deletion).
unsigned int Database : size();
unsigned int Database :: size();
Returns number of valid rows in the database.
unsigned int Database : num_rows();
unsigned int Database :: num_rows();
Return db.size().
unsigned int Database : first();
unsigned int Database :: first();
Return the id to the first valid row or return db.size()
if there are no valid rows.
unsigned int Database : last();
unsigned int Database :: last();
Return the id of the last valid row or return db.size()
if there are no valid rows.
unsigned int Database : next(unsigned int &id)
unsigned int Database :: next(unsigned int &id)
Return the id of the next valid row or return db.size()
if there are no remaining valid rows.
template <class T>
T &Database : operator[unsigned int index]
T &Database :: operator[unsigned int index]
Return a reference to db[index].

View File

@ -17,6 +17,9 @@ public:
DatabaseEntry();
virtual void write(File &) = 0;
virtual void read(File &) = 0;
#ifdef CONFIG_DEBUG
virtual void print() = 0;
#endif /* CONFIG_DEBUG */
};
@ -38,6 +41,9 @@ public:
~Database();
void save();
void load();
#ifdef CONFIG_DEBUG
void print();
#endif /* CONFIG_DEBUG */
unsigned int insert(T);
void remove(unsigned int);

View File

@ -55,6 +55,20 @@ void Database<T> :: load()
file.close();
}
template <class T>
void Database<T> :: print()
{
:: print("Database size: %u\n", db.size());
:: print("Valid rows: %u\n", _size);
for (unsigned int i = 0; i < db.size(); i++) {
if (db[i].valid == true) {
:: print("db[%u] = ", i);
db[i].print();
}
:: print("\n");
}
}
template <class T>
unsigned int Database<T> :: insert(T val)
{

View File

@ -12,6 +12,7 @@ public:
DBTest(unsigned int);
void write(File &);
void read(File &);
void print();
bool operator==(DBTest &);
};
@ -35,6 +36,11 @@ void DBTest :: read(File &file)
file >> value;
}
void DBTest :: print()
{
:: print("%u", value);
}
bool DBTest :: operator==(DBTest &rhs)
{
return value == rhs.value;
@ -44,7 +50,7 @@ bool DBTest :: operator==(DBTest &rhs)
void print_db(Database<DBTest> &db)
{
print("\nDatabase size: %u\n", db.size());
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());
@ -89,7 +95,7 @@ void test_0()
*/
void test_1()
{
print("Test 1\n");
print("\nTest 1\n");
Database<DBTest> db("test.db", DB_NORMAL);
db.load();
print_db(db);
@ -100,7 +106,7 @@ void test_1()
*/
void test_2()
{
print("Test 2\n");
print("\nTest 2\n");
Database<DBTest> db("", DB_NORMAL);
test_insertion(db);
@ -112,7 +118,7 @@ void test_2()
*/
void test_3()
{
print("Test 3\n");
print("\nTest 3\n");
Database<DBTest> db("", DB_NORMAL);
db.load();
}
@ -122,7 +128,7 @@ void test_3()
*/
void test_4()
{
print("Test 4\n");
print("\nTest 4\n");
Database<DBTest> db("", DB_UNIQUE);
for (unsigned int i = 0; i < 100; i++)
@ -131,6 +137,18 @@ void test_4()
print_db(db);
}
/*
* Test the database's built-in print() function
*/
void test_5()
{
print("\nTest 5\n");
Database<DBTest> db("", DB_NORMAL);
for (unsigned int i = 0; i < 10; i++)
db.insert(DBTest(i));
db.print();
}
int main(int argc, char **argv)
{
test_0();
@ -138,6 +156,7 @@ int main(int argc, char **argv)
test_2();
test_3();
test_4();
test_5();
return 0;
}

View File

@ -1,5 +1,4 @@
Test 0
Database size: 100000
Num rows: 100000
First: 0
@ -100004,7 +100003,6 @@ db[99996] = 99997
db[99997] = 99998
db[99998] = 99999
db[99999] = 100000
Database size: 50000
Num rows: 100000
First: 1
@ -150009,7 +150007,6 @@ db[99993] = 99994
db[99995] = 99996
db[99997] = 99998
db[99999] = 100000
Database size: 25000
Num rows: 100000
First: 3
@ -175014,8 +175011,8 @@ db[99987] = 99988
db[99991] = 99992
db[99995] = 99996
db[99999] = 100000
Test 1
Database size: 25000
Num rows: 100000
First: 3
@ -200020,8 +200017,8 @@ db[99987] = 99988
db[99991] = 99992
db[99995] = 99996
db[99999] = 100000
Test 2
Test 2
Database size: 100000
Num rows: 100000
First: 0
@ -300027,10 +300024,11 @@ db[99997] = 99998
db[99998] = 99999
db[99999] = 100000
ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened
Test 3
ERROR: A file with hint = FILE_TYPE_INVALID cannot be opened
Test 4
Test 4
Database size: 5
Num rows: 5
First: 0
@ -300040,3 +300038,17 @@ db[1] = 1
db[2] = 2
db[3] = 3
db[4] = 4
Test 5
Database size: 10
Valid rows: 10
db[0] = 0
db[1] = 1
db[2] = 2
db[3] = 3
db[4] = 4
db[5] = 5
db[6] = 6
db[7] = 7
db[8] = 8
db[9] = 9