Home | History | Annotate | Download | only in unit
      1 /**************************************************************************
      2  *
      3  * Copyright 2010 VMware, Inc.
      4  * All Rights Reserved.
      5  *
      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:
     13  *
     14  * The above copyright notice and this permission notice (including the
     15  * next paragraph) shall be included in all copies or substantial portions
     16  * of the Software.
     17  *
     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.
     25  *
     26  **************************************************************************/
     27 
     28 
     29 /*
     30  * Test case for u_cache.
     31  */
     32 
     33 
     34 #include <assert.h>
     35 #include <stdio.h>
     36 
     37 #include "util/u_cache.h"
     38 #include "util/u_hash.h"
     39 
     40 
     41 typedef uint32_t cache_test_key;
     42 typedef uint32_t cache_test_value;
     43 
     44 
     45 static uint32_t
     46 cache_test_hash(const void *key)
     47 {
     48    return util_hash_crc32(key, sizeof(cache_test_key));
     49 }
     50 
     51 
     52 static void
     53 cache_test_destroy(void *key, void *value)
     54 {
     55    free(key);
     56    free(value);
     57 }
     58 
     59 
     60 static int
     61 cache_test_compare(const void *key1, const void *key2) {
     62    return !(key1 == key2);
     63 }
     64 
     65 
     66 int main() {
     67    unsigned cache_size;
     68    unsigned cache_count;
     69 
     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;
     73          cache_test_key *key;
     74          cache_test_value *value_in;
     75          cache_test_value *value_out;
     76          int i;
     77 
     78          printf("Testing cache size of %d with %d values.\n", cache_size, cache_count);
     79 
     80          cache = util_cache_create(cache_test_hash,
     81                                    cache_test_compare,
     82                                    cache_test_destroy,
     83                                    cache_size);
     84 
     85          /*
     86           * Retrieve a value from an empty cache.
     87           */
     88          key = malloc(sizeof(cache_test_key));
     89          *key = 0xdeadbeef;
     90          value_out = (cache_test_value *) util_cache_get(cache, key);
     91          assert(value_out == NULL);
     92          free(key);
     93 
     94 
     95          /*
     96           * Repeatedly insert into and retrieve values from the cache.
     97           */
     98          for (i = 0; i < cache_count; i++) {
     99             key = malloc(sizeof(cache_test_key));
    100             value_in = malloc(sizeof(cache_test_value));
    101 
    102             *key = rand();
    103             *value_in = rand();
    104             util_cache_set(cache, key, value_in);
    105 
    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);
    110          }
    111 
    112          /*
    113           * In debug builds, this will trigger a self-check by the cache of
    114           * the distribution of hits in its internal cache entries.
    115           */
    116          util_cache_destroy(cache);
    117       }
    118    }
    119 
    120    return 0;
    121 }
    122