From: Timo Sirainen Date: Mon, 9 Jun 2014 15:15:51 +0000 (+0300) Subject: lib: Added some kind of a unit test for hash table. X-Git-Tag: 2.2.14.rc1~426 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=44af44edbffb19d33e109b4eb74d41f4fbb2e04a;p=thirdparty%2Fdovecot%2Fcore.git 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. --- diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 9d74ef9019..67aa4affd5 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -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 index 0000000000..9441bfcaba --- /dev/null +++ b/src/lib/test-hash.c @@ -0,0 +1,48 @@ +/* Copyright (c) 2014 Dovecot authors, see the included COPYING file */ + +#include "test-lib.h" +#include "hash.h" + +#include + +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 --git a/src/lib/test-lib.c b/src/lib/test-lib.c index 3a444abe4c..6ab85aace4 100644 --- a/src/lib/test-lib.c +++ b/src/lib/test-lib.c @@ -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, diff --git a/src/lib/test-lib.h b/src/lib/test-lib.h index 40d90daaa2..db9281ac99 100644 --- a/src/lib/test-lib.h +++ b/src/lib/test-lib.h @@ -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);