137 lines
2.3 KiB
C++
137 lines
2.3 KiB
C++
/*
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
* Test a Database
|
|
*/
|
|
|
|
#include <index.h>
|
|
#include <print.h>
|
|
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
|
|
unsigned int test_num = 0;
|
|
|
|
void test_results(bool success, unsigned int line)
|
|
{
|
|
print(" %u: ", test_num);
|
|
if (success)
|
|
print("Success!\n");
|
|
else {
|
|
print("FAILED (%u) =(\n", line);
|
|
exit(1);
|
|
}
|
|
test_num++;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
bool autosave = false;
|
|
unsigned int n;
|
|
char c;
|
|
|
|
while ((c = getopt(argc, argv, "a")) != -1) {
|
|
switch (c) {
|
|
case 'a':
|
|
autosave = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
n = atoi(argv[optind]);
|
|
Index index("index.idx", autosave);
|
|
|
|
|
|
/**
|
|
* 0: Test inserting when there is no key
|
|
*/
|
|
index.insert("a", 0);
|
|
Index :: iterator it = index.find("a");
|
|
if (it == index.end())
|
|
test_results(false, __LINE__);
|
|
if (it->values.size() == 0)
|
|
test_results(false, __LINE__);
|
|
test_results(*(it->values.begin()) == 0, __LINE__);
|
|
|
|
|
|
/**
|
|
* 1: Test removing
|
|
*/
|
|
index.remove("a", 0);
|
|
test_results((index.size() == 1) && (index.find("a")->values.size() == 0), __LINE__);
|
|
|
|
|
|
/**
|
|
* 2: Test adding multiple values
|
|
*/
|
|
for (c = 'a'; c <= 'z'; c++) {
|
|
std::string key = "";
|
|
key += c;
|
|
|
|
for (unsigned int i = 0; i < n; i++)
|
|
index.insert(key, i);
|
|
}
|
|
|
|
test_results(index.size() == 26, __LINE__);
|
|
|
|
|
|
/**
|
|
* 3: Make sure we have multiple values for each key
|
|
*/
|
|
for (c = 'a'; c <= 'z'; c++) {
|
|
std::string key = "";
|
|
key += c;
|
|
if (index.find(key)->values.size() != n)
|
|
test_results(false, __LINE__);
|
|
}
|
|
test_results(true, __LINE__);
|
|
|
|
|
|
/**
|
|
* 4: Test removing multiple values
|
|
*/
|
|
for (c = 'a'; c <= 'z'; c+=2) {
|
|
std::string key = "";
|
|
key += c;
|
|
|
|
for (unsigned int i = 0; i < n; i++)
|
|
index.remove(key, i);
|
|
|
|
if (index.find(key)->values.size() > 0)
|
|
test_results(false, __LINE__);
|
|
}
|
|
|
|
test_results(index.size() == 26, __LINE__);
|
|
|
|
|
|
/**
|
|
* Everything after this point tests loading from a file
|
|
*/
|
|
if (autosave == false)
|
|
return 0;
|
|
|
|
Index index2("index.idx", autosave);
|
|
index2.load();
|
|
|
|
|
|
/**
|
|
* 5: Load database from file
|
|
*/
|
|
test_results(index.size() == index2.size(), __LINE__);
|
|
|
|
|
|
/**
|
|
* 6: Check that everything is the right size
|
|
*/
|
|
for (c = 'a'; c <= 'z'; c++) {
|
|
std::string key = "";
|
|
key += c;
|
|
|
|
if (index.find(key)->values.size() != index2.find(key)->values.size())
|
|
test_results(false, __LINE__);
|
|
}
|
|
test_results(true, __LINE__);
|
|
|
|
return 0;
|
|
}
|