• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define SIZE 1000
29 
make_key(uint32_t i)30 static void *make_key(uint32_t i)
31 {
32       return (void *)(uintptr_t)(1 + i);
33 }
34 
key_id(const void * key)35 static uint32_t key_id(const void *key)
36 {
37    return (uintptr_t)key - 1;
38 }
39 
key_hash(const void * key)40 static uint32_t key_hash(const void *key)
41 {
42    return (uintptr_t)key;
43 }
44 
key_equal(const void * a,const void * b)45 static bool key_equal(const void *a, const void *b)
46 {
47    return a == b;
48 }
49 
delete_function(struct hash_entry * entry)50 static void delete_function(struct hash_entry *entry)
51 {
52    bool *deleted = (bool *)entry->data;
53    assert(!*deleted);
54    *deleted = true;
55 }
56 
main()57 int main()
58 {
59    struct hash_table *ht;
60    bool flags[SIZE];
61    uint32_t i;
62 
63    ht = _mesa_hash_table_create(NULL, key_hash, key_equal);
64 
65    for (i = 0; i < SIZE; ++i) {
66       flags[i] = false;
67       _mesa_hash_table_insert(ht, make_key(i), &flags[i]);
68    }
69 
70    _mesa_hash_table_clear(ht, delete_function);
71    assert(_mesa_hash_table_next_entry(ht, NULL) == NULL);
72 
73    /* Check that delete_function was called and that repopulating the table
74     * works. */
75    for (i = 0; i < SIZE; ++i) {
76       assert(flags[i]);
77       flags[i] = false;
78       _mesa_hash_table_insert(ht, make_key(i), &flags[i]);
79    }
80 
81    /* Check that exactly the right set of entries is in the table. */
82    for (i = 0; i < SIZE; ++i) {
83       assert(_mesa_hash_table_search(ht, make_key(i)));
84    }
85 
86    hash_table_foreach(ht, entry) {
87       assert(key_id(entry->key) < SIZE);
88    }
89    _mesa_hash_table_clear(ht, NULL);
90    assert(!ht->entries);
91    assert(!ht->deleted_entries);
92    hash_table_foreach(ht, entry) {
93       assert(0);
94    }
95 
96    for (i = 0; i < SIZE; ++i) {
97       flags[i] = false;
98       _mesa_hash_table_insert(ht, make_key(i), &flags[i]);
99    }
100    hash_table_foreach_remove(ht, entry) {
101       assert(key_id(entry->key) < SIZE);
102    }
103    assert(!ht->entries);
104    assert(!ht->deleted_entries);
105 
106    _mesa_hash_table_destroy(ht, NULL);
107 
108    return 0;
109 }
110