2013-08-21 22:12:15 -04:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2013-08-25 10:31:33 -04:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2013-08-27 09:15:49 -04:00
|
|
|
/*
|
|
|
|
* 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());
|
|
|
|
}
|
|
|
|
|
2013-10-06 21:58:22 -04:00
|
|
|
/*
|
|
|
|
* Test the print() function
|
|
|
|
*/
|
|
|
|
void test_6()
|
|
|
|
{
|
|
|
|
print("Test 6\n");
|
|
|
|
Index index("");
|
|
|
|
|
|
|
|
populate_index(index);
|
|
|
|
index.print_keys();
|
|
|
|
index.print();
|
|
|
|
}
|
|
|
|
|
2013-08-21 22:12:15 -04:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
test_0();
|
|
|
|
test_1();
|
|
|
|
test_2();
|
2013-08-25 10:31:33 -04:00
|
|
|
test_3();
|
|
|
|
test_4();
|
2013-08-27 09:15:49 -04:00
|
|
|
test_5();
|
2013-10-06 21:58:22 -04:00
|
|
|
test_6();
|
2013-08-21 22:12:15 -04:00
|
|
|
return 0;
|
|
|
|
}
|