database: Add a function for clearing databases

Signed-off-by: Anna Schumaker <schumaker.anna@gmail.com>
This commit is contained in:
Anna Schumaker 2013-10-27 17:36:22 -04:00 committed by Anna Schumaker
parent 0d84f92d2a
commit 6d94e78d23
5 changed files with 46 additions and 0 deletions

View File

@ -88,6 +88,11 @@ Database: (lib/database.cpp)
void Database :: save();
Saves the database to disk.
void Database :: clear();
This function exists only if CONFIG_DEBUG is enabled.
Clear the database contents in-memory, but do NOT write
to disk.
void Database :: print()
This function exists only If CONFIG_DEBUG is enabled.
Following a similar format for writing to disk, print the

View File

@ -42,6 +42,7 @@ public:
void save();
void load();
#ifdef CONFIG_DEBUG
void clear();
void print();
#endif /* CONFIG_DEBUG */

View File

@ -55,6 +55,14 @@ void Database<T> :: load()
file.close();
}
#ifdef CONFIG_DEBUG
template <class T>
void Database<T> :: clear()
{
db.clear();
_size = 0;
}
template <class T>
void Database<T> :: print()
{
@ -68,6 +76,7 @@ void Database<T> :: print()
:: print("\n");
}
}
#endif /* CONFIG_DEBUG */
template <class T>
unsigned int Database<T> :: insert(T val)

View File

@ -149,6 +149,20 @@ void test_5()
db.print();
}
/*
* Test the database's clear() function
*/
void test_6()
{
print("\nTest 6\n");
Database<DBTest> db("", DB_NORMAL);
for (unsigned int i = 0; i < 10; i++)
db.insert(DBTest(i));
db.print();
db.clear();
db.print();
}
int main(int argc, char **argv)
{
test_0();
@ -157,6 +171,7 @@ int main(int argc, char **argv)
test_3();
test_4();
test_5();
test_6();
return 0;
}

View File

@ -300052,3 +300052,19 @@ db[6] = 6
db[7] = 7
db[8] = 8
db[9] = 9
Test 6
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
Database size: 0
Valid rows: 0