ocarina/tests/index/index.cpp

153 lines
2.5 KiB
C++

/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <database.h>
#include <print.h>
void print_index(Database<IndexEntry> &index)
{
std::set<std::string>::iterator s_it;
std::set<unsigned int>::iterator u_it;
print("=== Printing index ===\n");
index.print_keys();
for (unsigned int i = index.first(); i != index.num_rows(); i = index.next(i)) {
print("index[%s] = ", index[i].primary_key.c_str());
index[i].print();
print("\n");
}
print("\n");
}
void populate_index(Database<IndexEntry> &index)
{
std::string key_str;
for (char key = 'a'; key <= 'z'; key++) {
key_str = key;
for (unsigned int value = 0; value < 10; value++)
index_insert(index, key_str, value);
}
}
void remove_keys(Database<IndexEntry> &index)
{
std::string key_str;
for (char key = 'a'; key <= 'z'; key+=2) {
key_str = key;
index.remove(index.find_index(key_str));
if (index.has_key(key_str))
print("Failed to remove key!\n");
}
}
void remove_values(Database<IndexEntry> &index)
{
std::string key_str;
unsigned int limit = 0;
for (char key = 'a'; key <= 'z'; key++) {
key_str = key;
for (unsigned int i = 0; i < limit; i++)
index_remove(index, key_str, i);
limit++;
if (limit == 11) {
limit = 0;
if (index.has_key(key_str))
print("Failed to remove key!\n");
}
}
}
/*
* Test index creation, print key access
*/
void test_0()
{
print("Test 0\n");
Database<IndexEntry> index("");
populate_index(index);
print_index(index);
}
/*
* Test removing keys from an index
*/
void test_1()
{
print("Test 1\n");
Database<IndexEntry> index("");
populate_index(index);
remove_keys(index);
print_index(index);
}
/*
* Test removing values from an index
*/
void test_2()
{
print("Test 2\n");
Database<IndexEntry> index("");
populate_index(index);
remove_values(index);
print_index(index);
}
/*
* Save the database to disk
*/
void test_3()
{
print("Test 3\n");
Database<IndexEntry> index("test.idx");
populate_index(index);
remove_values(index);
index.save();
}
/*
* Reload the database from disk
*/
void test_4()
{
print("Test 4\n");
Database<IndexEntry> index("test.idx");
index.load();
print_index(index);
}
/*
* Test access to keys that don't exist
*/
void test_5()
{
print("Test 5\n");
Database<IndexEntry> index("");
print("index[nokey].size() = ");
try {
print("%u\n", index.find("nokey").values.size());
} catch (...) {
print("0\n");
}
}
int main(int argc, char **argv)
{
test_0();
test_1();
test_2();
test_3();
test_4();
test_5();
return 0;
}