Path: blob/21.2-virgl/src/gallium/tests/unit/u_cache_test.c
4565 views
/**************************************************************************1*2* Copyright 2010 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/262728/*29* Test case for u_cache.30*/313233#include <assert.h>34#include <stdio.h>3536#include "util/u_cache.h"37#include "util/crc32.h"383940typedef uint32_t cache_test_key;41typedef uint32_t cache_test_value;424344static uint32_t45cache_test_hash(const void *key)46{47return util_hash_crc32(key, sizeof(cache_test_key));48}495051static void52cache_test_destroy(void *key, void *value)53{54free(key);55free(value);56}575859static int60cache_test_compare(const void *key1, const void *key2) {61return !(key1 == key2);62}636465int main() {66unsigned cache_size;67unsigned cache_count;6869for (cache_size = 2; cache_size < (1 << 15); cache_size *= 2) {70for (cache_count = (cache_size << 5); cache_count < (cache_size << 10); cache_count *= 2) {71struct util_cache * cache;72cache_test_key *key;73cache_test_value *value_in;74cache_test_value *value_out;75int i;7677printf("Testing cache size of %d with %d values.\n", cache_size, cache_count);7879cache = util_cache_create(cache_test_hash,80cache_test_compare,81cache_test_destroy,82cache_size);8384/*85* Retrieve a value from an empty cache.86*/87key = malloc(sizeof(cache_test_key));88*key = 0xdeadbeef;89value_out = (cache_test_value *) util_cache_get(cache, key);90assert(value_out == NULL);91(void)value_out;92free(key);939495/*96* Repeatedly insert into and retrieve values from the cache.97*/98for (i = 0; i < cache_count; i++) {99key = malloc(sizeof(cache_test_key));100value_in = malloc(sizeof(cache_test_value));101102*key = rand();103*value_in = rand();104util_cache_set(cache, key, value_in);105106value_out = util_cache_get(cache, key);107assert(value_out != NULL);108assert(value_in == value_out);109assert(*value_in == *value_out);110}111112/*113* In debug builds, this will trigger a self-check by the cache of114* the distribution of hits in its internal cache entries.115*/116util_cache_destroy(cache);117}118}119120return 0;121}122123124