1af69d88dSmrg/*
2af69d88dSmrg * Copyright © 2012 Intel Corporation
3af69d88dSmrg *
4af69d88dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
5af69d88dSmrg * copy of this software and associated documentation files (the "Software"),
6af69d88dSmrg * to deal in the Software without restriction, including without limitation
7af69d88dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8af69d88dSmrg * and/or sell copies of the Software, and to permit persons to whom the
9af69d88dSmrg * Software is furnished to do so, subject to the following conditions:
10af69d88dSmrg *
11af69d88dSmrg * The above copyright notice and this permission notice (including the next
12af69d88dSmrg * paragraph) shall be included in all copies or substantial portions of the
13af69d88dSmrg * Software.
14af69d88dSmrg *
15af69d88dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16af69d88dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17af69d88dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19af69d88dSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20af69d88dSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21af69d88dSmrg * IN THE SOFTWARE.
22af69d88dSmrg *
23af69d88dSmrg * Authors:
24af69d88dSmrg *    Eric Anholt <eric@anholt.net>
25af69d88dSmrg */
26af69d88dSmrg
278a1362adSmaya#undef NDEBUG
288a1362adSmaya
29af69d88dSmrg#include <stdlib.h>
30af69d88dSmrg#include <stdio.h>
31af69d88dSmrg#include <string.h>
32af69d88dSmrg#include <assert.h>
33af69d88dSmrg#include "hash_table.h"
34af69d88dSmrg
357ec681f3Smrgstatic void entry_free(struct hash_entry *entry)
367ec681f3Smrg{
377ec681f3Smrg   free((void *)entry->key);
387ec681f3Smrg}
397ec681f3Smrg
40af69d88dSmrgint
41af69d88dSmrgmain(int argc, char **argv)
42af69d88dSmrg{
43af69d88dSmrg   struct hash_table *ht;
447ec681f3Smrg   const char *str1 = strdup("test1");
457ec681f3Smrg   const char *str2 = strdup("test2");
467ec681f3Smrg   const char *str3 = strdup("test3");
47af69d88dSmrg   struct hash_entry *entry1, *entry2;
48af69d88dSmrg   uint32_t bad_hash = 5;
49af69d88dSmrg   int i;
50af69d88dSmrg
5101e04c3fSmrg   (void) argc;
5201e04c3fSmrg   (void) argv;
53af69d88dSmrg
5401e04c3fSmrg   ht = _mesa_hash_table_create(NULL, NULL, _mesa_key_string_equal);
55af69d88dSmrg
5601e04c3fSmrg   /* Insert some items.  Inserting 3 items forces a rehash and the new
5701e04c3fSmrg    * table size is big enough that we don't get rehashes later.
5801e04c3fSmrg    */
5901e04c3fSmrg   _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str1, NULL);
6001e04c3fSmrg   _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str2, NULL);
6101e04c3fSmrg   _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str3, NULL);
6201e04c3fSmrg
6301e04c3fSmrg   entry1 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str1);
64af69d88dSmrg   assert(entry1->key == str1);
65af69d88dSmrg
6601e04c3fSmrg   entry2 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str2);
67af69d88dSmrg   assert(entry2->key == str2);
68af69d88dSmrg
69af69d88dSmrg   /* Check that we can still find #1 after inserting #2 */
7001e04c3fSmrg   entry1 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str1);
71af69d88dSmrg   assert(entry1->key == str1);
72af69d88dSmrg
73af69d88dSmrg   /* Remove the collided entry and look again. */
74af69d88dSmrg   _mesa_hash_table_remove(ht, entry1);
7501e04c3fSmrg   entry2 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str2);
76af69d88dSmrg   assert(entry2->key == str2);
77af69d88dSmrg
7801e04c3fSmrg   /* Try inserting #2 again and make sure it gets overwritten */
7901e04c3fSmrg   _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str2, NULL);
8001e04c3fSmrg   entry2 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str2);
8101e04c3fSmrg   hash_table_foreach(ht, search_entry) {
8201e04c3fSmrg      assert(search_entry == entry2 || search_entry->key != str2);
8301e04c3fSmrg   }
8401e04c3fSmrg
85af69d88dSmrg   /* Put str1 back, then spam junk into the table to force a
86af69d88dSmrg    * resize and make sure we can still find them both.
87af69d88dSmrg    */
8801e04c3fSmrg   _mesa_hash_table_insert_pre_hashed(ht, bad_hash, str1, NULL);
89af69d88dSmrg   for (i = 0; i < 100; i++) {
90af69d88dSmrg      char *key = malloc(10);
91af69d88dSmrg      sprintf(key, "spam%d", i);
9201e04c3fSmrg      _mesa_hash_table_insert_pre_hashed(ht, _mesa_hash_string(key), key, NULL);
93af69d88dSmrg   }
9401e04c3fSmrg   entry1 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str1);
95af69d88dSmrg   assert(entry1->key == str1);
9601e04c3fSmrg   entry2 = _mesa_hash_table_search_pre_hashed(ht, bad_hash, str2);
97af69d88dSmrg   assert(entry2->key == str2);
98af69d88dSmrg
997ec681f3Smrg   _mesa_hash_table_destroy(ht, entry_free);
100af69d88dSmrg
101af69d88dSmrg   return 0;
102af69d88dSmrg}
103