From: Richard Kettlewell Date: Tue, 20 May 2008 19:51:00 +0000 (+0100) Subject: Extend hash.c test coverage X-Git-Tag: 4.0~72 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/commitdiff_plain/ae3a68fa64e62cb72377e9fb6bd8923727188b8f Extend hash.c test coverage --- diff --git a/lib/t-hash.c b/lib/t-hash.c index d9e8336..ad77841 100644 --- a/lib/t-hash.c +++ b/lib/t-hash.c @@ -19,6 +19,20 @@ */ #include "test.h" +static int count; + +static int test_hash_callback(const char attribute((unused)) *key, + void attribute((unused)) *value, + void attribute((unused)) *u) { + if(u) + insist(count < 100); + ++count; + if(u) + return count >= 100 ? 99 : 0; + else + return 0; +} + static void test_hash(void) { hash *h; int i, *ip; @@ -28,6 +42,14 @@ static void test_hash(void) { for(i = 0; i < 10000; ++i) insist(hash_add(h, do_printf("%d", i), &i, HASH_INSERT) == 0); check_integer(hash_count(h), 10000); + i = hash_foreach(h, test_hash_callback, NULL); + check_integer(i, 0); + check_integer(count, 10000); + count = 0; + i = hash_foreach(h, test_hash_callback, h); + check_integer(i, 99); + check_integer(count, 100); + for(i = 0; i < 10000; ++i) { insist((ip = hash_find(h, do_printf("%d", i))) != 0); check_integer(*ip, i);