135fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt/* 235fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * Copyright © 2009 Intel Corporation 335fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * 435fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * Permission is hereby granted, free of charge, to any person obtaining a 535fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * copy of this software and associated documentation files (the "Software"), 635fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * to deal in the Software without restriction, including without limitation 735fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * the rights to use, copy, modify, merge, publish, distribute, sublicense, 835fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * and/or sell copies of the Software, and to permit persons to whom the 935fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * Software is furnished to do so, subject to the following conditions: 1035fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * 1135fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * The above copyright notice and this permission notice (including the next 1235fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * paragraph) shall be included in all copies or substantial portions of the 1335fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * Software. 1435fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * 1535fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1635fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1735fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1835fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1935fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2035fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 2135fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * IN THE SOFTWARE. 2235fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * 2335fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * Authors: 2435fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * Eric Anholt <eric@anholt.net> 2535fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt */ 2635fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt 2735fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt#include <stdlib.h> 2835fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt#include <stdio.h> 2935fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt#include <string.h> 3035fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt#include <assert.h> 3135fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt#include "hash_table.h" 3235fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt 3335fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholtstatic uint32_t 3435fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholtkey_value(const void *key) 3535fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt{ 3635fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt return *(const uint32_t *)key; 3735fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt} 3835fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt 3935fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholtstatic bool 4035fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholtuint32_t_key_equals(const void *a, const void *b) 4135fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt{ 4235fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt return key_value(a) == key_value(b); 4335fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt} 4435fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt 4535fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholtstatic bool 4635fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholtuint32_t_key_is_even(struct hash_entry *entry) 4735fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt{ 4835fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt return (key_value(entry->key) & 1) == 0; 4935fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt} 5035fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt 5135fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholtint 5235fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholtmain(int argc, char **argv) 5335fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt{ 5435fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt struct hash_table *ht; 5535fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt struct hash_entry *entry; 561424bbfb572e6165e57554c6a591282743920b5cIan Romanick unsigned size = 10000; 5735fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt uint32_t keys[size]; 5835fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt uint32_t i, random_value; 5935fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt 603d8f9570cdd8a467d76cfffb5f998977dbab8682Ian Romanick (void) argc; 613d8f9570cdd8a467d76cfffb5f998977dbab8682Ian Romanick (void) argv; 623d8f9570cdd8a467d76cfffb5f998977dbab8682Ian Romanick 6394303a0750f9eaae3fcf598b7bf1320e521898fbJason Ekstrand ht = _mesa_hash_table_create(NULL, key_value, uint32_t_key_equals); 6435fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt 6535fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt for (i = 0; i < size; i++) { 6635fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt keys[i] = i; 6735fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt 6894303a0750f9eaae3fcf598b7bf1320e521898fbJason Ekstrand _mesa_hash_table_insert(ht, keys + i, NULL); 6935fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt } 7035fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt 7135fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt /* Test the no-predicate case. */ 7235fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt entry = _mesa_hash_table_random_entry(ht, NULL); 7335fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt assert(entry); 7435fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt 7535fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt /* Check that we're getting different entries and that the predicate 7635fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt * works. 7735fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt */ 7835fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt for (i = 0; i < 100; i++) { 7935fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt entry = _mesa_hash_table_random_entry(ht, uint32_t_key_is_even); 8035fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt assert(entry); 8135fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt assert((key_value(entry->key) & 1) == 0); 8235fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt if (i == 0 || key_value(entry->key) != random_value) 8335fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt break; 8435fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt random_value = key_value(entry->key); 8535fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt } 8635fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt assert(i != 100); 8735fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt 8835fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt _mesa_hash_table_destroy(ht, NULL); 8935fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt 9035fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt return 0; 9135fd61bd99c15c2e13d3945b41c4db7df6e64319Eric Anholt} 92