dovecot-2.2: lib: Added some kind of a unit test for hash table.

dovecot at dovecot.org dovecot at dovecot.org
Mon Jun 9 15:16:59 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/ff8402fe378e
changeset: 17444:ff8402fe378e
user:      Timo Sirainen <tss at iki.fi>
date:      Mon Jun 09 18:15:51 2014 +0300
description:
lib: Added some kind of a unit test for hash table.
Just try out some insert+deletes randomly. Mainly I wrote this to check if
there is some obvious problem, but looks like not.

diffstat:

 src/lib/Makefile.am |   1 +
 src/lib/test-hash.c |  48 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/lib/test-lib.c  |   1 +
 src/lib/test-lib.h  |   1 +
 4 files changed, 51 insertions(+), 0 deletions(-)

diffs (85 lines):

diff -r 3e78375be391 -r ff8402fe378e src/lib/Makefile.am
--- a/src/lib/Makefile.am	Mon Jun 09 15:11:50 2014 +0300
+++ b/src/lib/Makefile.am	Mon Jun 09 18:15:51 2014 +0300
@@ -274,6 +274,7 @@
 	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 -r 3e78375be391 -r ff8402fe378e src/lib/test-hash.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/test-hash.c	Mon Jun 09 18:15:51 2014 +0300
@@ -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);
+}
diff -r 3e78375be391 -r ff8402fe378e src/lib/test-lib.c
--- a/src/lib/test-lib.c	Mon Jun 09 15:11:50 2014 +0300
+++ b/src/lib/test-lib.c	Mon Jun 09 18:15:51 2014 +0300
@@ -11,6 +11,7 @@
 		test_bsearch_insert_pos,
 		test_buffer,
 		test_crc32,
+		test_hash,
 		test_hash_format,
 		test_hash_method,
 		test_hex_binary,
diff -r 3e78375be391 -r ff8402fe378e src/lib/test-lib.h
--- a/src/lib/test-lib.h	Mon Jun 09 15:11:50 2014 +0300
+++ b/src/lib/test-lib.h	Mon Jun 09 18:15:51 2014 +0300
@@ -10,6 +10,7 @@
 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);


More information about the dovecot-cvs mailing list