From 773c0c87c10e8a12c67b42506d4fdd5241f08d6a Mon Sep 17 00:00:00 2001 From: Eric Anholt Date: Mon, 23 Nov 2009 11:59:37 +0100 Subject: New test for expanding the hash table and not losing entries. --- tests/.gitignore | 4 +++ tests/Makefile.am | 1 + tests/insert_many.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 tests/.gitignore create mode 100644 tests/insert_many.c (limited to 'tests') diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..be475a3 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1,4 @@ +delete_and_lookup +insert_and_lookup +insert_many +null_destroy diff --git a/tests/Makefile.am b/tests/Makefile.am index 55a3d20..3e6aaad 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -24,6 +24,7 @@ LDADD = ../libhash_table.la TESTS = \ delete_and_lookup \ insert_and_lookup \ + insert_many \ null_destroy \ $() diff --git a/tests/insert_many.c b/tests/insert_many.c new file mode 100644 index 0000000..ef54e02 --- /dev/null +++ b/tests/insert_many.c @@ -0,0 +1,76 @@ +/* + * Copyright © 2009 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Authors: + * Eric Anholt + */ + +#include +#include +#include +#include +#include "hash_table.h" +#include "fnv_hash.h" + +static uint32_t +uint32_t_key_hash(const void *key) +{ + /* Note that we're using the address of the key instead of the + * perfectly-hashed 32-bit value in the key, in order to trigger + * collisions. + */ + return (uint32_t)(uintptr_t)key; +} + +static int +uint32_t_key_equals(const void *a, const void *b) +{ + return *(uint32_t *)a == *(uint32_t *)b; +} + +int +main(int argc, char **argv) +{ + struct hash_table *ht; + struct hash_entry *entry; + int size = 10000; + uint32_t keys[size]; + uint32_t i; + + ht = hash_table_create(uint32_t_key_hash, uint32_t_key_equals); + + for (i = 0; i < size; i++) { + keys[i] = i; + + hash_table_insert(ht, keys + i, NULL); + } + + for (i = 0; i < size; i++) { + entry = hash_table_search(ht, keys + i); + assert(entry); + assert(*(uint32_t *)entry->key == i); + } + + hash_table_destroy(NULL, NULL); + + return 0; +} -- cgit v1.2.3