101e04c3fSmrg/* 201e04c3fSmrg * Copyright (C) 2016 Advanced Micro Devices, Inc. 301e04c3fSmrg * 401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 501e04c3fSmrg * copy of this software and associated documentation files (the "Software"), 601e04c3fSmrg * to deal in the Software without restriction, including without limitation 701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the 901e04c3fSmrg * Software is furnished to do so, subject to the following conditions: 1001e04c3fSmrg * 1101e04c3fSmrg * The above copyright notice and this permission notice (including the next 1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the 1301e04c3fSmrg * Software. 1401e04c3fSmrg * 1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 1801e04c3fSmrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, 1901e04c3fSmrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 2001e04c3fSmrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 2101e04c3fSmrg * USE OR OTHER DEALINGS IN THE SOFTWARE. 2201e04c3fSmrg */ 2301e04c3fSmrg 248a1362adSmaya#undef NDEBUG 258a1362adSmaya 2601e04c3fSmrg#include "hash_table.h" 2701e04c3fSmrg 288a1362adSmaya#define SIZE 1000 298a1362adSmaya 3001e04c3fSmrgstatic void *make_key(uint32_t i) 3101e04c3fSmrg{ 3201e04c3fSmrg return (void *)(uintptr_t)(1 + i); 3301e04c3fSmrg} 3401e04c3fSmrg 3501e04c3fSmrgstatic uint32_t key_id(const void *key) 3601e04c3fSmrg{ 3701e04c3fSmrg return (uintptr_t)key - 1; 3801e04c3fSmrg} 3901e04c3fSmrg 4001e04c3fSmrgstatic uint32_t key_hash(const void *key) 4101e04c3fSmrg{ 4201e04c3fSmrg return (uintptr_t)key; 4301e04c3fSmrg} 4401e04c3fSmrg 4501e04c3fSmrgstatic bool key_equal(const void *a, const void *b) 4601e04c3fSmrg{ 4701e04c3fSmrg return a == b; 4801e04c3fSmrg} 4901e04c3fSmrg 5001e04c3fSmrgstatic void delete_function(struct hash_entry *entry) 5101e04c3fSmrg{ 5201e04c3fSmrg bool *deleted = (bool *)entry->data; 5301e04c3fSmrg assert(!*deleted); 5401e04c3fSmrg *deleted = true; 5501e04c3fSmrg} 5601e04c3fSmrg 5701e04c3fSmrgint main() 5801e04c3fSmrg{ 5901e04c3fSmrg struct hash_table *ht; 608a1362adSmaya bool flags[SIZE]; 6101e04c3fSmrg uint32_t i; 6201e04c3fSmrg 6301e04c3fSmrg ht = _mesa_hash_table_create(NULL, key_hash, key_equal); 6401e04c3fSmrg 658a1362adSmaya for (i = 0; i < SIZE; ++i) { 6601e04c3fSmrg flags[i] = false; 6701e04c3fSmrg _mesa_hash_table_insert(ht, make_key(i), &flags[i]); 6801e04c3fSmrg } 6901e04c3fSmrg 7001e04c3fSmrg _mesa_hash_table_clear(ht, delete_function); 7101e04c3fSmrg assert(_mesa_hash_table_next_entry(ht, NULL) == NULL); 7201e04c3fSmrg 7301e04c3fSmrg /* Check that delete_function was called and that repopulating the table 7401e04c3fSmrg * works. */ 758a1362adSmaya for (i = 0; i < SIZE; ++i) { 7601e04c3fSmrg assert(flags[i]); 7701e04c3fSmrg flags[i] = false; 7801e04c3fSmrg _mesa_hash_table_insert(ht, make_key(i), &flags[i]); 7901e04c3fSmrg } 8001e04c3fSmrg 8101e04c3fSmrg /* Check that exactly the right set of entries is in the table. */ 828a1362adSmaya for (i = 0; i < SIZE; ++i) { 8301e04c3fSmrg assert(_mesa_hash_table_search(ht, make_key(i))); 8401e04c3fSmrg } 8501e04c3fSmrg 8601e04c3fSmrg hash_table_foreach(ht, entry) { 878a1362adSmaya assert(key_id(entry->key) < SIZE); 8801e04c3fSmrg } 897ec681f3Smrg _mesa_hash_table_clear(ht, NULL); 907ec681f3Smrg assert(!ht->entries); 917ec681f3Smrg assert(!ht->deleted_entries); 927ec681f3Smrg hash_table_foreach(ht, entry) { 937ec681f3Smrg assert(0); 947ec681f3Smrg } 957ec681f3Smrg 967ec681f3Smrg for (i = 0; i < SIZE; ++i) { 977ec681f3Smrg flags[i] = false; 987ec681f3Smrg _mesa_hash_table_insert(ht, make_key(i), &flags[i]); 997ec681f3Smrg } 1007ec681f3Smrg hash_table_foreach_remove(ht, entry) { 1017ec681f3Smrg assert(key_id(entry->key) < SIZE); 1027ec681f3Smrg } 1037ec681f3Smrg assert(!ht->entries); 1047ec681f3Smrg assert(!ht->deleted_entries); 10501e04c3fSmrg 10601e04c3fSmrg _mesa_hash_table_destroy(ht, NULL); 10701e04c3fSmrg 10801e04c3fSmrg return 0; 10901e04c3fSmrg} 110