chiark / gitweb /
Further test coverage
[disorder] / lib / t-hash.c
index b5c0094a79e159c8fed96a27941a2e394521ed9d..ad77841c1ccbca55a9d5ed3a1528cdda31e945d3 100644 (file)
  */
 #include "test.h"
 
-void test_hash(void) {
+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;
   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);
+  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);
@@ -44,6 +65,8 @@ void test_hash(void) {
   check_integer(hash_count(h), 0);
 }
 
+TEST(hash);
+
 /*
 Local Variables:
 c-basic-offset:2