From 67085fc2b808122f80a26ee9335936c9054bbd07 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Thu, 27 Jan 2011 13:03:18 +0200 Subject: [PATCH] test/: hashtab tests --- test/Makefile | 2 +- test/test_common.c | 1 + test/test_common.h | 1 + test/test_hashtab.c | 128 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 test/test_hashtab.c diff --git a/test/Makefile b/test/Makefile index b228142..eb07808 100644 --- a/test/Makefile +++ b/test/Makefile @@ -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 = diff --git a/test/test_common.c b/test/test_common.c index cd6b3c5..d387edc 100644 --- a/test/test_common.c +++ b/test/test_common.c @@ -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 }, diff --git a/test/test_common.h b/test/test_common.h index 109c41e..b4301be 100644 --- a/test/test_common.h +++ b/test/test_common.h @@ -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 index 0000000..820ccdc --- /dev/null +++ b/test/test_hashtab.c @@ -0,0 +1,128 @@ + +#include + +#include + +#include +#include +#include +#include + +#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 +}; + -- 2.39.5