diff options
author | Eric Anholt <eric@anholt.net> | 2009-11-24 02:44:01 +0100 |
---|---|---|
committer | Eric Anholt <eric@anholt.net> | 2009-11-23 18:08:04 -0800 |
commit | 6d568a14fea277eb1a610f35ad735f7e3828cd30 (patch) | |
tree | 78806bf642180f524fb713bffea277160fef056b /tests | |
parent | 8a4c8f14d40e88239d5dcd9eabb803b8459a4833 (diff) |
API change: pass the hash value in to search/lookup.
This avoids re-hashing the key for the common use case of searching for the
key's presence, then creating the entry if it isn't.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/delete_and_lookup.c | 22 | ||||
-rw-r--r-- | tests/destroy_callback.c | 8 | ||||
-rw-r--r-- | tests/insert_and_lookup.c | 12 | ||||
-rw-r--r-- | tests/insert_many.c | 16 | ||||
-rw-r--r-- | tests/random_entry.c | 14 |
5 files changed, 29 insertions, 43 deletions
diff --git a/tests/delete_and_lookup.c b/tests/delete_and_lookup.c index c4fe555..0515ed5 100644 --- a/tests/delete_and_lookup.c +++ b/tests/delete_and_lookup.c @@ -46,25 +46,27 @@ main(int argc, char **argv) struct hash_table *ht; const char *str1 = "test1"; const char *str2 = "test2"; + uint32_t hash_str1 = badhash(str1); + uint32_t hash_str2 = badhash(str2); struct hash_entry *entry; - ht = hash_table_create(badhash, string_key_equals); + ht = hash_table_create(string_key_equals); - hash_table_insert(ht, str1, NULL); - hash_table_insert(ht, str2, NULL); + hash_table_insert(ht, hash_str1, str1, NULL); + hash_table_insert(ht, hash_str2, str2, NULL); - entry = hash_table_search(ht, str1); - assert(strcmp(entry->key, str1) == 0); - - entry = hash_table_search(ht, str2); + entry = hash_table_search(ht, hash_str2, str2); assert(strcmp(entry->key, str2) == 0); - hash_table_remove(ht, hash_table_search(ht, str1)); + entry = hash_table_search(ht, hash_str1, str1); + assert(strcmp(entry->key, str1) == 0); + + hash_table_remove(ht, entry); - entry = hash_table_search(ht, str1); + entry = hash_table_search(ht, hash_str1, str1); assert(entry == NULL); - entry = hash_table_search(ht, str2); + entry = hash_table_search(ht, hash_str2, str2); assert(strcmp(entry->key, str2) == 0); hash_table_destroy(NULL, NULL); diff --git a/tests/destroy_callback.c b/tests/destroy_callback.c index 4e7ed32..c6d7e3c 100644 --- a/tests/destroy_callback.c +++ b/tests/destroy_callback.c @@ -51,11 +51,13 @@ int main(int argc, char **argv) { struct hash_table *ht; + uint32_t hash_str1 = fnv1_hash_string(str1); + uint32_t hash_str2 = fnv1_hash_string(str2); - ht = hash_table_create(fnv1_hash_string, string_key_equals); + ht = hash_table_create(string_key_equals); - hash_table_insert(ht, str1, NULL); - hash_table_insert(ht, str2, NULL); + hash_table_insert(ht, hash_str1, str1, NULL); + hash_table_insert(ht, hash_str2, str2, NULL); hash_table_destroy(ht, delete_callback); diff --git a/tests/insert_and_lookup.c b/tests/insert_and_lookup.c index 2c287b6..9ed036b 100644 --- a/tests/insert_and_lookup.c +++ b/tests/insert_and_lookup.c @@ -37,17 +37,19 @@ main(int argc, char **argv) struct hash_table *ht; const char *str1 = "test1"; const char *str2 = "test2"; + uint32_t hash_str1 = fnv1_hash_string(str1); + uint32_t hash_str2 = fnv1_hash_string(str2); struct hash_entry *entry; - ht = hash_table_create(fnv1_hash_string, string_key_equals); + ht = hash_table_create(string_key_equals); - hash_table_insert(ht, str1, NULL); - hash_table_insert(ht, str2, NULL); + hash_table_insert(ht, hash_str1, str1, NULL); + hash_table_insert(ht, hash_str2, str2, NULL); - entry = hash_table_search(ht, str1); + entry = hash_table_search(ht, hash_str1, str1); assert(strcmp(entry->key, str1) == 0); - entry = hash_table_search(ht, str2); + entry = hash_table_search(ht, hash_str2, str2); assert(strcmp(entry->key, str2) == 0); hash_table_destroy(NULL, NULL); diff --git a/tests/insert_many.c b/tests/insert_many.c index ef54e02..b027d23 100644 --- a/tests/insert_many.c +++ b/tests/insert_many.c @@ -31,16 +31,6 @@ #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) { @@ -56,16 +46,16 @@ main(int argc, char **argv) uint32_t keys[size]; uint32_t i; - ht = hash_table_create(uint32_t_key_hash, uint32_t_key_equals); + ht = hash_table_create(uint32_t_key_equals); for (i = 0; i < size; i++) { keys[i] = i; - hash_table_insert(ht, keys + i, NULL); + hash_table_insert(ht, i, keys + i, NULL); } for (i = 0; i < size; i++) { - entry = hash_table_search(ht, keys + i); + entry = hash_table_search(ht, i, keys + i); assert(entry); assert(*(uint32_t *)entry->key == i); } diff --git a/tests/random_entry.c b/tests/random_entry.c index 3f51088..796461b 100644 --- a/tests/random_entry.c +++ b/tests/random_entry.c @@ -37,16 +37,6 @@ key_value(const void *key) return *(uint32_t *)key; } -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) { @@ -68,12 +58,12 @@ main(int argc, char **argv) uint32_t keys[size]; uint32_t i, random_value; - ht = hash_table_create(uint32_t_key_hash, uint32_t_key_equals); + ht = hash_table_create(uint32_t_key_equals); for (i = 0; i < size; i++) { keys[i] = i; - hash_table_insert(ht, keys + i, NULL); + hash_table_insert(ht, i, keys + i, NULL); } /* Test the no-predicate case. */ |