ocarina/tests/index/index.cpp

175 lines
2.6 KiB
C++
Raw Normal View History

/*
* Copyright 2013 (c) Anna Schumaker.
*/
#include <index.h>
#include <print.h>
void print_keys(Index &index)
{
std::set<std::string>::iterator it;
print("Found keys:");
for (it = index.keys_begin(); it != index.keys_end(); it++)
print(" %s", it->c_str());
print("\n");
}
void print_index(Index &index)
{
std::set<std::string>::iterator s_it;
std::set<unsigned int>::iterator u_it;
print("=== Printing index ===\n");
print_keys(index);
for (s_it = index.keys_begin(); s_it != index.keys_end(); s_it++) {
std::string key = *s_it;
print("index[%s] = {", key.c_str());
for (u_it = index[key].begin(); u_it != index[key].end(); u_it++) {
if (u_it != index[key].begin())
print(" ");
print("%d", *u_it);
}
print("}\n");
}
print("\n");
}
void populate_index(Index &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(key_str, value);
}
}
}
void remove_keys(Index &index)
{
std::string key_str;
for (char key = 'a'; key <= 'z'; key+=2) {
key_str = key;
index.remove(key_str);
}
}
void remove_values(Index &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(key_str, i);
}
limit++;
if (limit == 11)
limit = 0;
}
}
/*
* Test index creation, print key access
*/
void test_0()
{
print("Test 0\n");
Index index("");
populate_index(index);
print_index(index);
}
/*
* Test removing keys from an index
*/
void test_1()
{
print("Test 1\n");
Index index("");
populate_index(index);
remove_keys(index);
print_index(index);
}
/*
* Test removing values from an index
*/
void test_2()
{
print("Test 2\n");
Index index("");
populate_index(index);
remove_values(index);
print_index(index);
}
/*
* Save the database to disk
*/
void test_3()
{
print("Test 3\n");
Index index("test.idx");
populate_index(index);
remove_values(index);
index.save();
}
/*
* Reload the database from disk
*/
void test_4()
{
print("Test 4\n");
Index index("test.idx");
index.load();
print_index(index);
}
/*
* Test access to keys that don't exist
*/
void test_5()
{
print("Test 5\n");
Index index("");
print("index[nokey].size() = %u\n", index["nokey"].size());
}
/*
* Test the print() function
*/
void test_6()
{
print("Test 6\n");
Index index("");
populate_index(index);
index.print_keys();
index.print();
}
int main(int argc, char **argv)
{
test_0();
test_1();
test_2();
test_3();
test_4();
test_5();
test_6();
return 0;
}