test/: hashtab tests
authorMarko Kreen <markokr@gmail.com>
Thu, 27 Jan 2011 11:03:18 +0000 (13:03 +0200)
committerMarko Kreen <markokr@gmail.com>
Thu, 27 Jan 2011 11:03:18 +0000 (13:03 +0200)
test/Makefile
test/test_common.c
test/test_common.h
test/test_hashtab.c [new file with mode: 0644]

index b228142c49cafed12e159f8f75005962b2fdf505..eb07808f363b24dcd1e48382b8cf5a7792a15d02 100644 (file)
@@ -4,7 +4,7 @@ SRCS = test_string.c test_crypto.c test_aatree.c test_heap.c \
        test_common.c test_list.c tinytest.c test_cbtree.c \
        test_utf8.c test_strpool.c test_pgutil.c test_regex.c \
        test_cxalloc.c test_bits.c test_base.c test_netdb.c \
-       test_cfparser.c test_endian.c
+       test_cfparser.c test_endian.c test_hashtab.c
 OBJS = $(addprefix obj/, $(SRCS:.c=.o))
 HDRS = test_common.h test_config.h tinytest.h tinytest_macros.h
 LIBS =
index cd6b3c5c90df1bdf39b6c0a506ccf7b54157f9fa..d387edcdf336363b381a2b9f4fa6528d05e55335 100644 (file)
@@ -12,6 +12,7 @@ struct testgroup_t groups[] = {
        { "endian/", endian_tests },
        { "string/", string_tests },
        { "heap/", heap_tests },
+       { "hashtab/", hashtab_tests },
        { "list/", list_tests },
        { "utf8/", utf8_tests },
        { "strpool/", strpool_tests },
index 109c41ecd73d8162d268a1dbfa0d25e8e1bc67a7..b4301be60200d4eb6e9effda906f2f34c606ac12 100644 (file)
@@ -24,4 +24,5 @@ extern struct testcase_t base_tests[];
 extern struct testcase_t netdb_tests[];
 extern struct testcase_t cfparser_tests[];
 extern struct testcase_t endian_tests[];
+extern struct testcase_t hashtab_tests[];
 
diff --git a/test/test_hashtab.c b/test/test_hashtab.c
new file mode 100644 (file)
index 0000000..820ccdc
--- /dev/null
@@ -0,0 +1,128 @@
+
+#include <usual/hashtab-impl.h>
+
+#include <usual/list.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+#include "test_common.h"
+
+struct MyNode {
+       int value;
+};
+
+static int cf_size = 64;
+static int cf_ofs = 0;
+static int cf_cnt = 3 * 64;
+static int cf_mod = 13;
+
+static bool mycmp(const htab_val_t curval, const void *arg)
+{
+       const struct MyNode *n1 = curval;
+       const struct MyNode *n2 = arg;
+       return n1->value == n2->value;
+}
+
+
+static struct MyNode *make_node(int v)
+{
+       struct MyNode *n = malloc(sizeof(*n));
+       n->value = v;
+       return n;
+}
+
+/*
+ * checking operations
+ */
+
+static const char *my_insert(struct HashTab *htab, int value)
+{
+       struct MyNode *my = make_node(value);
+       void **p;
+       int key = value % cf_mod;
+       p = hashtab_lookup(htab, key, true, my);
+       if (!p)
+               return "FAIL";
+       if (*p)
+               return "EXISTS?";
+       *p = my;
+       return "OK";
+}
+
+static const char *my_remove(struct HashTab *h, int value)
+{
+       struct MyNode tmp, *my;
+       void **p;
+       int key = value % cf_mod;
+
+       tmp.value = value;
+
+       p = hashtab_lookup(h, key, false, &tmp);
+       if (!p)
+               return "NEXIST";
+       my = *p;
+       if (my->value != value)
+               return "WRONG";
+
+       hashtab_delete(h, key, &tmp);
+       free(my);
+
+       p = hashtab_lookup(h, key, false, &tmp);
+       if (p)
+               return "EXISTS?";
+       return "OK";
+}
+
+static const char *my_lookup(struct HashTab *htab, int value)
+{
+       void **p;
+       struct MyNode tmp, *my;
+       int key = value % cf_mod;
+
+       tmp.value = value;
+       p = hashtab_lookup(htab, key, false, &tmp);
+       if (!p)
+               return "NEXIST";
+       my = *p;
+       if (my->value != value)
+               return "WRONG";
+       return "OK";
+}
+
+/*
+ * Simple operations.
+ */
+
+static void test_hash_basic(void *p)
+{
+       struct HashTab *htab;
+       int i;
+
+       htab = hashtab_create(cf_size, mycmp, USUAL_ALLOC);
+
+       for (i = 0; i < cf_cnt; i++) {
+               int n = i + cf_ofs;
+               str_check(my_lookup(htab, n), "NEXIST");
+               str_check(my_insert(htab, n), "OK");
+               str_check(my_lookup(htab, n), "OK");
+       }
+
+       for (i = 0; i < cf_cnt; i++) {
+               int n = i + cf_ofs;
+               str_check(my_lookup(htab, n), "OK");
+               str_check(my_remove(htab, n), "OK");
+               str_check(my_lookup(htab, n), "NEXIST");
+       }
+
+end:
+       hashtab_destroy(htab);
+}
+
+struct testcase_t hashtab_tests[] = {
+       { "basic", test_hash_basic },
+       END_OF_TESTCASES
+};
+