chiark / gitweb /
hash.c tests
authorRichard Kettlewell <rjk@greenend.org.uk>
Sat, 1 Dec 2007 17:05:30 +0000 (17:05 +0000)
committerRichard Kettlewell <rjk@greenend.org.uk>
Sat, 1 Dec 2007 17:05:30 +0000 (17:05 +0000)
lib/Makefile.am
lib/test.c

index 348858866519b0e2e078dcb6de45f7a50708e676..67416b2a0f57548603a9c4f389a1bce82883f554 100644 (file)
@@ -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
index bef8d79475f9139168bebaf0b2c3fbd7e44d9d4f..5454fd327b3d589c10d301f4bff053b4b8298e27 100644 (file)
@@ -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;
 }