iris: include p_defines.h in iris_bufmgr.h
[mesa.git] / src / util / tests / hash_table / clear.c
1 /*
2 * Copyright (C) 2016 Advanced Micro Devices, Inc.
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #undef NDEBUG
25
26 #include "hash_table.h"
27
28 static void *make_key(uint32_t i)
29 {
30 return (void *)(uintptr_t)(1 + i);
31 }
32
33 static uint32_t key_id(const void *key)
34 {
35 return (uintptr_t)key - 1;
36 }
37
38 static uint32_t key_hash(const void *key)
39 {
40 return (uintptr_t)key;
41 }
42
43 static bool key_equal(const void *a, const void *b)
44 {
45 return a == b;
46 }
47
48 static void delete_function(struct hash_entry *entry)
49 {
50 bool *deleted = (bool *)entry->data;
51 assert(!*deleted);
52 *deleted = true;
53 }
54
55 int main()
56 {
57 struct hash_table *ht;
58 const uint32_t size = 1000;
59 bool flags[size];
60 uint32_t i;
61
62 ht = _mesa_hash_table_create(NULL, key_hash, key_equal);
63
64 for (i = 0; i < size; ++i) {
65 flags[i] = false;
66 _mesa_hash_table_insert(ht, make_key(i), &flags[i]);
67 }
68
69 _mesa_hash_table_clear(ht, delete_function);
70 assert(_mesa_hash_table_next_entry(ht, NULL) == NULL);
71
72 /* Check that delete_function was called and that repopulating the table
73 * works. */
74 for (i = 0; i < size; ++i) {
75 assert(flags[i]);
76 flags[i] = false;
77 _mesa_hash_table_insert(ht, make_key(i), &flags[i]);
78 }
79
80 /* Check that exactly the right set of entries is in the table. */
81 for (i = 0; i < size; ++i) {
82 assert(_mesa_hash_table_search(ht, make_key(i)));
83 }
84
85 hash_table_foreach(ht, entry) {
86 assert(key_id(entry->key) < size);
87 }
88
89 _mesa_hash_table_destroy(ht, NULL);
90
91 return 0;
92 }