summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/bloom.c7
-rw-r--r--test/hashtable.c173
2 files changed, 180 insertions, 0 deletions
diff --git a/test/bloom.c b/test/bloom.c
index 8d8c21c24..71a0b013a 100644
--- a/test/bloom.c
+++ b/test/bloom.c
@@ -73,6 +73,13 @@ static void dict_bloom_teardown(void)
/* Tests */
+/**
+ * Test blooom filter creation
+ *
+ * Create a bloom filter, add a single entry and test for presece and
+ * absence of that entry (single entry cannot have false positives).
+ *
+ */
START_TEST(bloom_create_test)
{
struct bloom_filter *b;
diff --git a/test/hashtable.c b/test/hashtable.c
index 2939f4f85..d69b10870 100644
--- a/test/hashtable.c
+++ b/test/hashtable.c
@@ -32,6 +32,96 @@
#include "utils/hashtable.h"
+#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
+
+struct test_pairs {
+ const char* test;
+ const char* res;
+};
+
+static struct hash_table *match_hash_a;
+static struct hash_table *match_hash_b;
+
+static struct hash_table *dict_hash;
+
+static const struct test_pairs match_tests[] = {
+ { "cow", "moo" },
+ { "pig", "oink" },
+ { "chicken", "cluck" },
+ { "dog", "woof" },
+ { "sheep", "baaa" },
+};
+
+/* Fixtures */
+
+static void match_hashtable_create(void)
+{
+ int idx;
+
+ match_hash_a = hash_create(79);
+ ck_assert(match_hash_a != NULL);
+
+ match_hash_b = hash_create(103);
+ ck_assert(match_hash_b != NULL);
+
+ for (idx = 0; idx < NELEMS(match_tests); idx++) {
+ hash_add(match_hash_a,
+ match_tests[idx].test,
+ match_tests[idx].res);
+ hash_add(match_hash_b,
+ match_tests[idx].res,
+ match_tests[idx].test);
+ }
+}
+
+static void match_hashtable_teardown(void)
+{
+ hash_destroy(match_hash_a);
+ hash_destroy(match_hash_b);
+}
+
+
+/**
+ * create dictionary hashtable
+ *
+ * hashtable constructed from the odd/even rows of the
+ * dictionary
+ */
+static void dict_hashtable_create(int dict_hash_size)
+{
+ FILE *dictf;
+ char keybuf[BUFSIZ], valbuf[BUFSIZ];
+
+ dictf = fopen("/usr/share/dict/words", "r");
+ ck_assert(dictf != NULL);
+
+ dict_hash = hash_create(dict_hash_size);
+ ck_assert(dict_hash != NULL);
+
+ while (!feof(dictf)) {
+ fscanf(dictf, "%s", keybuf);
+ fscanf(dictf, "%s", valbuf);
+ hash_add(dict_hash, keybuf, valbuf);
+ }
+
+ fclose(dictf);
+}
+
+static void dicts_hashtable_create(void)
+{
+ dict_hashtable_create(1031);
+}
+
+static void dictl_hashtable_create(void)
+{
+ dict_hashtable_create(7919);
+}
+
+static void dict_hashtable_teardown(void)
+{
+ hash_destroy(dict_hash);
+}
+
/* Tests */
/**
@@ -109,12 +199,60 @@ START_TEST(hashtable_positive_test)
}
END_TEST
+
+START_TEST(hashtable_matcha_test)
+{
+ const char *res;
+
+ res = hash_get(match_hash_a, match_tests[_i].test);
+ ck_assert(res != NULL);
+ ck_assert_str_eq(res, match_tests[_i].res);
+}
+END_TEST
+
+
+START_TEST(hashtable_matchb_test)
+{
+ const char *res;
+
+ res = hash_get(match_hash_b, match_tests[_i].res);
+ ck_assert(res != NULL);
+ ck_assert_str_eq(res, match_tests[_i].test);
+}
+END_TEST
+
+
+START_TEST(hashtable_dict_test)
+{
+ FILE *dictf;
+ char keybuf[BUFSIZ], valbuf[BUFSIZ];
+ const char *res;
+
+ dictf = fopen("/usr/share/dict/words", "r");
+ ck_assert(dictf != NULL);
+
+ while (!feof(dictf)) {
+ fscanf(dictf, "%s", keybuf);
+ fscanf(dictf, "%s", valbuf);
+
+ res = hash_get(dict_hash, keybuf);
+ ck_assert(res != NULL);
+ ck_assert_str_eq(res, valbuf);
+ }
+
+ fclose(dictf);
+}
+END_TEST
+
/* Suite */
Suite *hashtable_suite(void)
{
Suite *s;
TCase *tc_create;
+ TCase *tc_match;
+ TCase *tc_dict_s;
+ TCase *tc_dict_l;
s = suite_create("hash table filter");
@@ -127,7 +265,42 @@ Suite *hashtable_suite(void)
suite_add_tcase(s, tc_create);
+ /* Matching entry tests */
+ tc_match = tcase_create("Match");
+
+ tcase_add_checked_fixture(tc_match,
+ match_hashtable_create,
+ match_hashtable_teardown);
+
+ tcase_add_loop_test(tc_match,
+ hashtable_matcha_test,
+ 0, NELEMS(match_tests));
+ tcase_add_loop_test(tc_match,
+ hashtable_matchb_test,
+ 0, NELEMS(match_tests));
+
+ suite_add_tcase(s, tc_match);
+
+ /* small table dictionary test */
+ tc_dict_s = tcase_create("small table dictionary");
+ tcase_add_checked_fixture(tc_dict_s,
+ dicts_hashtable_create,
+ dict_hashtable_teardown);
+
+ tcase_add_test(tc_dict_s, hashtable_dict_test);
+
+ suite_add_tcase(s, tc_dict_s);
+
+
+ /* large table dictionary test */
+ tc_dict_l = tcase_create("large table dictionary");
+ tcase_add_checked_fixture(tc_dict_l,
+ dictl_hashtable_create,
+ dict_hashtable_teardown);
+
+ tcase_add_test(tc_dict_l, hashtable_dict_test);
+ suite_add_tcase(s, tc_dict_l);
return s;
}