1 /*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
27 #undef NDEBUG
28
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <assert.h>
33 #include "hash_table.h"
34
entry_free(struct hash_entry * entry)35 static void entry_free(struct hash_entry *entry)
36 {
37 free((void *)entry->key);
38 }
39
40 int
main(int argc,char ** argv)41 main(int argc, char **argv)
42 {
43 struct hash_table *ht;
44 const char *str1 = strdup("test1");
45 const char *str2 = strdup("test2");
46 const char *str3 = strdup("test3");
47 struct hash_entry *entry1, *entry2;
48 uint32_t bad_hash = 5;
49 int i;
50
51 (void) argc;
52 (void) argv;
53
54 ht = _mesa_hash_table_create(NULL, NULL, _mesa_key_string_equal);
55
56 /* Insert some items. Inserting 3 items forces a rehash and the new
57 * table size is big enough that we don't get rehashes later.
58 */
59 _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str1, NULL);
60 _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str2, NULL);
61 _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str3, NULL);
62
63 entry1 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str1);
64 assert(entry1->key == str1);
65
66 entry2 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str2);
67 assert(entry2->key == str2);
68
69 /* Check that we can still find #1 after inserting #2 */
70 entry1 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str1);
71 assert(entry1->key == str1);
72
73 /* Remove the collided entry and look again. */
74 _mesa_hash_table_remove(ht, entry1);
75 entry2 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str2);
76 assert(entry2->key == str2);
77
78 /* Try inserting #2 again and make sure it gets overwritten */
79 _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str2, NULL);
80 entry2 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str2);
81 hash_table_foreach(ht, search_entry) {
82 assert(search_entry == entry2 || search_entry->key != str2);
83 }
84
85 /* Put str1 back, then spam junk into the table to force a
86 * resize and make sure we can still find them both.
87 */
88 _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str1, NULL);
89 for (i = 0; i < 100; i++) {
90 char *key = malloc(10);
91 sprintf(key, "spam%d", i);
92 _mesa_hash_table_insert_pre_hashed(ht, _mesa_hash_string(key), key, NULL);
93 }
94 entry1 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str1);
95 assert(entry1->key == str1);
96 entry2 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str2);
97 assert(entry2->key == str2);
98
99 _mesa_hash_table_destroy(ht, entry_free);
100
101 return 0;
102 }
103