1848b8605Smrg/*
2848b8605Smrg * Copyright © 2012 Intel Corporation
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg * copy of this software and associated documentation files (the "Software"),
6848b8605Smrg * to deal in the Software without restriction, including without limitation
7848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
9848b8605Smrg * Software is furnished to do so, subject to the following conditions:
10848b8605Smrg *
11848b8605Smrg * The above copyright notice and this permission notice (including the next
12848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
13848b8605Smrg * Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19848b8605Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20848b8605Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21848b8605Smrg * IN THE SOFTWARE.
22848b8605Smrg *
23848b8605Smrg * Authors:
24848b8605Smrg *    Eric Anholt <eric@anholt.net>
25848b8605Smrg */
26848b8605Smrg
27b8e80941Smrg#undef NDEBUG
28b8e80941Smrg
29848b8605Smrg#include <stdlib.h>
30848b8605Smrg#include <stdio.h>
31848b8605Smrg#include <string.h>
32848b8605Smrg#include <assert.h>
33848b8605Smrg#include "hash_table.h"
34848b8605Smrg
35848b8605Smrgint
36848b8605Smrgmain(int argc, char **argv)
37848b8605Smrg{
38848b8605Smrg   struct hash_table *ht;
39848b8605Smrg   const char *str1 = "test1";
40848b8605Smrg   const char *str2 = "test2";
41b8e80941Smrg   const char *str3 = "test3";
42848b8605Smrg   struct hash_entry *entry1, *entry2;
43848b8605Smrg   uint32_t bad_hash = 5;
44848b8605Smrg   int i;
45848b8605Smrg
46b8e80941Smrg   (void) argc;
47b8e80941Smrg   (void) argv;
48b8e80941Smrg
49b8e80941Smrg   ht = _mesa_hash_table_create(NULL, NULL, _mesa_key_string_equal);
50848b8605Smrg
51b8e80941Smrg   /* Insert some items.  Inserting 3 items forces a rehash and the new
52b8e80941Smrg    * table size is big enough that we don't get rehashes later.
53b8e80941Smrg    */
54b8e80941Smrg   _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str1, NULL);
55b8e80941Smrg   _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str2, NULL);
56b8e80941Smrg   _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str3, NULL);
57848b8605Smrg
58b8e80941Smrg   entry1 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str1);
59848b8605Smrg   assert(entry1->key == str1);
60848b8605Smrg
61b8e80941Smrg   entry2 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str2);
62848b8605Smrg   assert(entry2->key == str2);
63848b8605Smrg
64848b8605Smrg   /* Check that we can still find #1 after inserting #2 */
65b8e80941Smrg   entry1 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str1);
66848b8605Smrg   assert(entry1->key == str1);
67848b8605Smrg
68848b8605Smrg   /* Remove the collided entry and look again. */
69848b8605Smrg   _mesa_hash_table_remove(ht, entry1);
70b8e80941Smrg   entry2 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str2);
71848b8605Smrg   assert(entry2->key == str2);
72848b8605Smrg
73b8e80941Smrg   /* Try inserting #2 again and make sure it gets overwritten */
74b8e80941Smrg   _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str2, NULL);
75b8e80941Smrg   entry2 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str2);
76b8e80941Smrg   hash_table_foreach(ht, search_entry) {
77b8e80941Smrg      assert(search_entry == entry2 || search_entry->key != str2);
78b8e80941Smrg   }
79b8e80941Smrg
80848b8605Smrg   /* Put str1 back, then spam junk into the table to force a
81848b8605Smrg    * resize and make sure we can still find them both.
82848b8605Smrg    */
83b8e80941Smrg   _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str1, NULL);
84848b8605Smrg   for (i = 0; i < 100; i++) {
85848b8605Smrg      char *key = malloc(10);
86848b8605Smrg      sprintf(key, "spam%d", i);
87b8e80941Smrg      _mesa_hash_table_insert_pre_hashed(ht, _mesa_hash_string(key), key, NULL);
88848b8605Smrg   }
89b8e80941Smrg   entry1 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str1);
90848b8605Smrg   assert(entry1->key == str1);
91b8e80941Smrg   entry2 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str2);
92848b8605Smrg   assert(entry2->key == str2);
93848b8605Smrg
94848b8605Smrg   _mesa_hash_table_destroy(ht, NULL);
95848b8605Smrg
96848b8605Smrg   return 0;
97848b8605Smrg}
98