1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Test case for u_cache.
37 #include "util/u_cache.h"
38 #include "util/u_hash.h"
41 typedef uint32_t cache_test_key;
42 typedef uint32_t cache_test_value;
46 cache_test_hash(const void *key)
48 return util_hash_crc32(key, sizeof(cache_test_key));
53 cache_test_destroy(void *key, void *value)
61 cache_test_compare(const void *key1, const void *key2) {
62 return !(key1 == key2);
70 for (cache_size = 2; cache_size < (1 << 15); cache_size *= 2) {
71 for (cache_count = (cache_size << 5); cache_count < (cache_size << 10); cache_count *= 2) {
72 struct util_cache * cache;
74 cache_test_value *value_in;
75 cache_test_value *value_out;
78 printf("Testing cache size of %d with %d values.\n", cache_size, cache_count);
80 cache = util_cache_create(cache_test_hash,
86 * Retrieve a value from an empty cache.
88 key = malloc(sizeof(cache_test_key));
90 value_out = (cache_test_value *) util_cache_get(cache, key);
91 assert(value_out == NULL);
96 * Repeatedly insert into and retrieve values from the cache.
98 for (i = 0; i < cache_count; i++) {
99 key = malloc(sizeof(cache_test_key));
100 value_in = malloc(sizeof(cache_test_value));
104 util_cache_set(cache, key, value_in);
106 value_out = util_cache_get(cache, key);
107 assert(value_out != NULL);
108 assert(value_in == value_out);
109 assert(*value_in == *value_out);
113 * In debug builds, this will trigger a self-check by the cache of
114 * the distribution of hits in its internal cache entries.
116 util_cache_destroy(cache);