From: Richard Kettlewell Date: Sat, 1 Dec 2007 17:05:30 +0000 (+0000) Subject: hash.c tests X-Git-Tag: 1.5.99+dev10~32 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/commitdiff_plain/fe5755370c53c05c091ced1c62525d17878ad7a9?hp=65f5780f8a94f2bad8146b6e85b8085efdf06feb hash.c tests --- diff --git a/lib/Makefile.am b/lib/Makefile.am index 3488588..67416b2 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -88,8 +88,8 @@ definitions.h: Makefile defs.o: definitions.h defs.lo: definitions.h -test_SOURCES=test.c -test_LDADD=libdisorder.a $(LIBPCRE) $(LIBICONV) +test_SOURCES=test.c memgc.c +test_LDADD=libdisorder.a $(LIBPCRE) $(LIBICONV) $(LIBGC) test_DEPENDENCIES=libdisorder.a check: test #test.i diff --git a/lib/test.c b/lib/test.c index bef8d79..5454fd3 100644 --- a/lib/test.c +++ b/lib/test.c @@ -1227,7 +1227,33 @@ static void test_split(void) { check_string(quoteutf8("wibble'wobble"), "\"wibble'wobble\""); } +static void test_hash(void) { + hash *h; + int i, *ip; + char **keys; + + fprintf(stderr, "test_hash\n"); + h = hash_new(sizeof(int)); + for(i = 0; i < 10000; ++i) + insist(hash_add(h, do_printf("%d", i), &i, HASH_INSERT) == 0); + check_integer(hash_count(h), 10000); + for(i = 0; i < 10000; ++i) { + insist((ip = hash_find(h, do_printf("%d", i))) != 0); + check_integer(*ip, i); + insist(hash_add(h, do_printf("%d", i), &i, HASH_REPLACE) == 0); + } + check_integer(hash_count(h), 10000); + keys = hash_keys(h); + for(i = 0; i < 10000; ++i) + insist(keys[i] != 0); + insist(keys[10000] == 0); + for(i = 0; i < 10000; ++i) + insist(hash_remove(h, do_printf("%d", i)) == 0); + check_integer(hash_count(h), 0); +} + int main(void) { + mem_init(); fail_first = !!getenv("FAIL_FIRST"); insist('\n' == 0x0A); insist('\r' == 0x0D); @@ -1289,6 +1315,7 @@ int main(void) { test_cache(); /* selection.c */ test_selection(); + test_hash(); fprintf(stderr, "%d errors out of %d tests\n", errors, tests); return !!errors; }