1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2016 Advanced Micro Devices, Inc.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#undef NDEBUG
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "hash_table.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#define SIZE 1000
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_cistatic void *make_key(uint32_t i)
31bf215546Sopenharmony_ci{
32bf215546Sopenharmony_ci      return (void *)(uintptr_t)(1 + i);
33bf215546Sopenharmony_ci}
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistatic uint32_t key_id(const void *key)
36bf215546Sopenharmony_ci{
37bf215546Sopenharmony_ci   return (uintptr_t)key - 1;
38bf215546Sopenharmony_ci}
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_cistatic uint32_t key_hash(const void *key)
41bf215546Sopenharmony_ci{
42bf215546Sopenharmony_ci   return (uintptr_t)key;
43bf215546Sopenharmony_ci}
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_cistatic bool key_equal(const void *a, const void *b)
46bf215546Sopenharmony_ci{
47bf215546Sopenharmony_ci   return a == b;
48bf215546Sopenharmony_ci}
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_cistatic void delete_function(struct hash_entry *entry)
51bf215546Sopenharmony_ci{
52bf215546Sopenharmony_ci   bool *deleted = (bool *)entry->data;
53bf215546Sopenharmony_ci   assert(!*deleted);
54bf215546Sopenharmony_ci   *deleted = true;
55bf215546Sopenharmony_ci}
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ciint main()
58bf215546Sopenharmony_ci{
59bf215546Sopenharmony_ci   struct hash_table *ht;
60bf215546Sopenharmony_ci   bool flags[SIZE];
61bf215546Sopenharmony_ci   uint32_t i;
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci   ht = _mesa_hash_table_create(NULL, key_hash, key_equal);
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   for (i = 0; i < SIZE; ++i) {
66bf215546Sopenharmony_ci      flags[i] = false;
67bf215546Sopenharmony_ci      _mesa_hash_table_insert(ht, make_key(i), &flags[i]);
68bf215546Sopenharmony_ci   }
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   _mesa_hash_table_clear(ht, delete_function);
71bf215546Sopenharmony_ci   assert(_mesa_hash_table_next_entry(ht, NULL) == NULL);
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   /* Check that delete_function was called and that repopulating the table
74bf215546Sopenharmony_ci    * works. */
75bf215546Sopenharmony_ci   for (i = 0; i < SIZE; ++i) {
76bf215546Sopenharmony_ci      assert(flags[i]);
77bf215546Sopenharmony_ci      flags[i] = false;
78bf215546Sopenharmony_ci      _mesa_hash_table_insert(ht, make_key(i), &flags[i]);
79bf215546Sopenharmony_ci   }
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   /* Check that exactly the right set of entries is in the table. */
82bf215546Sopenharmony_ci   for (i = 0; i < SIZE; ++i) {
83bf215546Sopenharmony_ci      assert(_mesa_hash_table_search(ht, make_key(i)));
84bf215546Sopenharmony_ci   }
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci   hash_table_foreach(ht, entry) {
87bf215546Sopenharmony_ci      assert(key_id(entry->key) < SIZE);
88bf215546Sopenharmony_ci   }
89bf215546Sopenharmony_ci   _mesa_hash_table_clear(ht, NULL);
90bf215546Sopenharmony_ci   assert(!ht->entries);
91bf215546Sopenharmony_ci   assert(!ht->deleted_entries);
92bf215546Sopenharmony_ci   hash_table_foreach(ht, entry) {
93bf215546Sopenharmony_ci      assert(0);
94bf215546Sopenharmony_ci   }
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   for (i = 0; i < SIZE; ++i) {
97bf215546Sopenharmony_ci      flags[i] = false;
98bf215546Sopenharmony_ci      _mesa_hash_table_insert(ht, make_key(i), &flags[i]);
99bf215546Sopenharmony_ci   }
100bf215546Sopenharmony_ci   hash_table_foreach_remove(ht, entry) {
101bf215546Sopenharmony_ci      assert(key_id(entry->key) < SIZE);
102bf215546Sopenharmony_ci   }
103bf215546Sopenharmony_ci   assert(!ht->entries);
104bf215546Sopenharmony_ci   assert(!ht->deleted_entries);
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   _mesa_hash_table_destroy(ht, NULL);
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   return 0;
109bf215546Sopenharmony_ci}
110