]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Added some kind of a unit test for hash table.
authorTimo Sirainen <tss@iki.fi>
Mon, 9 Jun 2014 15:15:51 +0000 (18:15 +0300)
committerTimo Sirainen <tss@iki.fi>
Mon, 9 Jun 2014 15:15:51 +0000 (18:15 +0300)
Just try out some insert+deletes randomly. Mainly I wrote this to check if
there is some obvious problem, but looks like not.

src/lib/Makefile.am
src/lib/test-hash.c [new file with mode: 0644]
src/lib/test-lib.c
src/lib/test-lib.h

index 9d74ef90197fedd0036c5faa49de47e4cb97d210..67aa4affd5a2748b3c413698103346d28e0b11a1 100644 (file)
@@ -274,6 +274,7 @@ test_lib_SOURCES = \
        test-bsearch-insert-pos.c \
        test-buffer.c \
        test-crc32.c \
+       test-hash.c \
        test-hash-format.c \
        test-hash-method.c \
        test-hex-binary.c \
diff --git a/src/lib/test-hash.c b/src/lib/test-hash.c
new file mode 100644 (file)
index 0000000..9441bfc
--- /dev/null
@@ -0,0 +1,48 @@
+/* Copyright (c) 2014 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+#include "hash.h"
+
+#include <stdlib.h>
+
+static void test_hash_random_pool(pool_t pool)
+{
+#define KEYMAX 100000
+       HASH_TABLE(void *, void *) hash;
+       unsigned int *keys;
+       unsigned int i, key, keyidx, delidx;
+
+       keys = i_new(unsigned int, KEYMAX); keyidx = 0;
+       hash_table_create_direct(&hash, pool, 0);
+       for (i = 0; i < KEYMAX; i++) {
+               key = (rand() % KEYMAX) + 1;
+               if (rand() % 5 > 0) {
+                       if (hash_table_lookup(hash, POINTER_CAST(key)) == NULL) {
+                               hash_table_insert(hash, POINTER_CAST(key),
+                                                 POINTER_CAST(1));
+                               keys[keyidx++] = key;
+                       }
+               } else if (keyidx > 0) {
+                       delidx = rand() % keyidx;
+                       hash_table_remove(hash, POINTER_CAST(keys[delidx]));
+                       memmove(&keys[delidx], &keys[delidx+1],
+                               (keyidx-delidx-1) * sizeof(*keys));
+                       keyidx--;
+               }
+       }
+       for (i = 0; i < keyidx; i++)
+               hash_table_remove(hash, POINTER_CAST(keys[i]));
+       hash_table_destroy(&hash);
+       i_free(keys);
+}
+
+void test_hash(void)
+{
+       pool_t pool;
+
+       test_hash_random_pool(default_pool);
+
+       pool = pool_alloconly_create("test hash", 1024);
+       test_hash_random_pool(pool);
+       pool_unref(&pool);
+}
index 3a444abe4c01d31a50c8868d6ad127c14feac0bb..6ab85aace4e8f868982d47e5299715574a817714 100644 (file)
@@ -11,6 +11,7 @@ int main(void)
                test_bsearch_insert_pos,
                test_buffer,
                test_crc32,
+               test_hash,
                test_hash_format,
                test_hash_method,
                test_hex_binary,
index 40d90daaa256c18477280f0bef1db094b74f5d3b..db9281ac99b52bb9bbd9a519df171a49e42af643 100644 (file)
@@ -10,6 +10,7 @@ void test_base64(void);
 void test_bsearch_insert_pos(void);
 void test_buffer(void);
 void test_crc32(void);
+void test_hash(void);
 void test_hash_format(void);
 void test_hash_method(void);
 void test_hex_binary(void);