index: Clean up unit test

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-01-04 19:50:40 -05:00
parent 4b0c6121c3
commit afd47ce667
1 changed files with 37 additions and 57 deletions

View File

@ -8,32 +8,10 @@
#include <sstream> #include <sstream>
static IndexEntry *IDX_NULL = NULL;
/*** static unsigned int N = 0;
* static Index *INDEX = NULL;
* Struct for passing around arguments to tests
*
*/
struct TestArgs {
const unsigned int n;
Index *index;
TestArgs(const unsigned int, Index *);
};
TestArgs :: TestArgs(const unsigned int _n, Index *_index)
: n(_n), index(_index)
{}
/***
*
* Run tests with varying index sizes
*
*/
static void test_single_item() static void test_single_item()
{ {
@ -42,7 +20,7 @@ static void test_single_item()
index.insert("a", 0); index.insert("a", 0);
IndexEntry *it = index.find("a"); IndexEntry *it = index.find("a");
test_not_equal(it, IDX_NULL); test_not_equal(it, (IndexEntry *)NULL);
test_equal(index.size(), (unsigned)1); test_equal(index.size(), (unsigned)1);
test_equal(it->size(), (size_t)1); test_equal(it->size(), (size_t)1);
test_equal(*(it->begin()), (unsigned)0); test_equal(*(it->begin()), (unsigned)0);
@ -52,7 +30,7 @@ static void test_single_item()
test_equal(it->size(), (size_t)0); test_equal(it->size(), (size_t)0);
} }
static void test_insertion(struct TestArgs *args) static void test_insertion()
{ {
std::string key; std::string key;
IndexEntry *it; IndexEntry *it;
@ -61,32 +39,32 @@ static void test_insertion(struct TestArgs *args)
for (char c = 'a'; c <= 'z'; c++) { for (char c = 'a'; c <= 'z'; c++) {
key = c; key = c;
for (unsigned int i = 0; i < args->n; i++) for (unsigned int i = 0; i < N; i++)
args->index->insert(key, i); INDEX->insert(key, i);
} }
test :: success(); test :: success();
if (args->n == 0) if (N == 0)
test_equal(args->index->size(), (unsigned int)0); test_equal(INDEX->size(), (unsigned int)0);
else else
test_equal(args->index->size(), (unsigned int)26); test_equal(INDEX->size(), (unsigned int)26);
test :: begin(); test :: begin();
for (char c = 'a'; c <= 'z'; c++) { for (char c = 'a'; c <= 'z'; c++) {
key = c; key = c;
it = args->index->find(key); it = INDEX->find(key);
if (args->n == 0) if (N == 0)
check_equal(it, IDX_NULL); check_equal(it, (IndexEntry *)NULL);
else { else {
check_not_equal(it, IDX_NULL); check_not_equal(it, (IndexEntry *)NULL);
check_equal(it->size(), (size_t)args->n); check_equal(it->size(), (size_t)N);
} }
} }
test :: success(); test :: success();
} }
static void test_removal(struct TestArgs *args) static void test_removal()
{ {
std::string key; std::string key;
IndexEntry *it; IndexEntry *it;
@ -95,20 +73,20 @@ static void test_removal(struct TestArgs *args)
for (char c = 'a'; c <= 'z'; c++) { for (char c = 'a'; c <= 'z'; c++) {
key = c; key = c;
for (unsigned int i = 0; i < args->n; i++) for (unsigned int i = 0; i < N; i++)
args->index->remove(key, i); INDEX->remove(key, i);
it = args->index->find(key); it = INDEX->find(key);
if (args->n == 0) if (N == 0)
check_equal(it, IDX_NULL); check_equal(it, (IndexEntry *)NULL);
else { else {
check_not_equal(it, IDX_NULL); check_not_equal(it, (IndexEntry *)NULL);
check_equal(it->size(), (size_t)0); check_equal(it->size(), (size_t)0);
} }
} }
test :: success(); test :: success();
} }
static void test_saving(struct TestArgs *args) static void test_saving()
{ {
std::string key; std::string key;
IndexEntry *it1, *it2; IndexEntry *it1, *it2;
@ -117,24 +95,24 @@ static void test_saving(struct TestArgs *args)
idx2.load(); idx2.load();
test_equal(idx2.size(), (unsigned)0); test_equal(idx2.size(), (unsigned)0);
args->index->save(); INDEX->save();
idx2.load(); idx2.load();
test_equal(idx2.size(), args->index->size()); test_equal(idx2.size(), INDEX->size());
test_equal(idx2.actual_size(), args->index->actual_size()); test_equal(idx2.actual_size(), INDEX->actual_size());
test :: begin(); test :: begin();
for (char c = 'a'; c <= 'z'; c++) { for (char c = 'a'; c <= 'z'; c++) {
key = c; key = c;
it1 = args->index->find(key); it1 = INDEX->find(key);
it2 = idx2.find(key); it2 = idx2.find(key);
if (args->n == 0) { if (N == 0) {
check_equal(it1, IDX_NULL); check_equal(it1, (IndexEntry *)NULL);
check_equal(it2, IDX_NULL); check_equal(it2, (IndexEntry *)NULL);
} else { } else {
check_not_equal(it1, IDX_NULL); check_not_equal(it1, (IndexEntry *)NULL);
check_not_equal(it2, IDX_NULL); check_not_equal(it2, (IndexEntry *)NULL);
check_equal(it1->size(), it2->size()); check_equal(it1->size(), it2->size());
} }
} }
@ -150,13 +128,15 @@ static void index_test(unsigned int n)
const std::string n_str = ss.str(); const std::string n_str = ss.str();
Index index("index.idx", false); Index index("index.idx", false);
struct TestArgs args(n, &index);
N = n;
INDEX = &index;
if (n == 0) if (n == 0)
run_test("Index Single Item Test", test_single_item); run_test("Index Single Item Test", test_single_item);
run_test("Index Insertion Test" + n_str, test_insertion, &args); run_test("Index Insertion Test" + n_str, test_insertion);
run_test("Index Removal Test" + n_str, test_removal, &args); run_test("Index Removal Test" + n_str, test_removal);
run_test("Index Save and Load Test" + n_str, test_saving, &args); run_test("Index Save and Load Test" + n_str, test_saving);
} }
int main(int argc, char **argv) int main(int argc, char **argv)