chiark / gitweb /
Extend hash.c test coverage
authorRichard Kettlewell <rjk@greenend.org.uk>
Tue, 20 May 2008 19:51:00 +0000 (20:51 +0100)
committerRichard Kettlewell <rjk@greenend.org.uk>
Tue, 20 May 2008 19:51:00 +0000 (20:51 +0100)
lib/t-hash.c

index d9e833689dfa5d5d20c2c4f1918765945d7858b8..ad77841c1ccbca55a9d5ed3a1528cdda31e945d3 100644 (file)
  */
 #include "test.h"
 
  */
 #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;
 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);
   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);
   for(i = 0; i < 10000; ++i) {
     insist((ip = hash_find(h, do_printf("%d", i))) != 0);
     check_integer(*ip, i);