6 /* wrap the whole module */
9 static unsigned int g_verbose_
= 0;
11 struct test_kv_cache_entry
{
12 struct lru_cache_entry lru_
;
17 typedef struct test_kv_cache_entry kve_t
;
21 test_kv_hash_feed(lru_entry_t
*entry
, char **bufp
, size_t *szp
)
23 struct test_kv_cache_entry
*e
= (struct test_kv_cache_entry
*)entry
;
30 test_kv_cmp(lru_entry_t
*a
, lru_entry_t
*b
)
32 kve_t
*kv_a
= (kve_t
*)a
,
35 return strcmp(kv_a
->key
, kv_b
->key
);
40 kv_dump(struct lru_cache
*c
)
42 struct test_kv_cache_entry
*kve
;
46 fprintf(stderr
, "\nLRU, %zu entries\n", c
->num_entries
);
51 fprintf(stderr
, "-- hash --\n");
52 for (i
= 0; i
< c
->hash_sz
; i
++) {
54 for (s
= 0, e
= c
->hash
[i
].entry
; e
; e
= e
->hash_next
, s
++) {
55 kve
= (struct test_kv_cache_entry
*)e
;
57 if (e
->hash_slot
!= i
)
58 fprintf(stderr
, "!!! sanity failure, entry has hash slot %zu, but is in slot %zu !!!\n", e
->hash_slot
, i
);
59 fprintf(stderr
, "%*s[%zu]: (%p) '%s':'%s'\n", s
, "", i
, kve
, kve
->key
, kve
->value
);
64 fprintf(stderr
, "-- queue --\n");
65 for (e
= c
->newest
; e
; e
= e
->next
) {
66 kve
= (struct test_kv_cache_entry
*)e
;
67 fprintf(stderr
, "(%p) '%s':'%s'\n", kve
, kve
->key
, kve
->value
);
70 fprintf(stderr
, "\n");
74 kv_genstats(lru_entry_t
*e
, size_t i
, void *data
)
76 struct test_kv_cache_entry
*kve
= (struct test_kv_cache_entry
*)e
;
77 size_t *hash_depth
= (size_t *)data
;
80 hash_depth
[e
->hash_slot
]++;
83 kv_dumpstats(struct lru_cache
*c
, size_t *stats
, size_t sz
)
87 for (i
= 0; i
< sz
; i
++)
88 fprintf(stderr
, "slot %04zu: %zu (expect: %zu)\n", i
, stats
[i
], c
->hash
[i
].tally
);
92 main(int argc
, char *argv
[])
94 const size_t hash_sz
= 31;
95 const size_t capacity
= 256;
97 struct lru_cache
*cache
;
98 struct test_kv_cache_entry
*e
, *r
, m
;
103 while ( (i
= getopt(argc
, argv
, "vh")) != EOF
) {
115 stats
= malloc(hash_sz
* sizeof *stats
);
116 memset(stats
, 0, hash_sz
* sizeof *stats
);
118 cache
= lru_cache_new(hash_sz
, capacity
, test_kv_hash_feed
, test_kv_cmp
);
119 assert(cache
!= NULL
);
123 e
= malloc(sizeof *e
);
126 strncpy(e
->key
, "key", sizeof e
->key
);
127 strncpy(e
->value
, "value", sizeof e
->value
);
129 lru_cache_insert(cache
, (lru_entry_t
*)e
, (lru_entry_t
**)&r
);
134 memset(&m
, 0, sizeof m
);
135 strncpy(m
.key
, "key", sizeof e
->key
);
136 e
= (struct test_kv_cache_entry
*)lru_cache_locate(cache
, (lru_entry_t
*)&m
);
139 lru_cache_extract(cache
, (lru_entry_t
*)e
);
144 for (i
= 0; i
< 500; i
++) {
146 e
= malloc(sizeof *e
);
148 memset(e
, 0, sizeof *e
);
149 snprintf(e
->key
, sizeof e
->key
, "test %d key", i
);
150 snprintf(e
->value
, sizeof e
->value
, "some %d value", i
);
151 lru_cache_insert(cache
, (lru_entry_t
*)e
, (lru_entry_t
**)&r
);
154 fprintf(stderr
, "freeing (%p) '%s':'%s'\n", r
, r
->key
, r
->value
);
161 for (i
= 0; i
< 500; i
+= 3) {
162 memset(&m
, 0, sizeof m
);
163 snprintf(m
.key
, sizeof m
.key
, "test %d key", i
);
164 e
= (struct test_kv_cache_entry
*)lru_cache_locate(cache
, (lru_entry_t
*)&m
);
167 fprintf(stderr
, "key %d was not cached\n", i
);
170 lru_cache_extract(cache
, (lru_entry_t
*)e
);
172 fprintf(stderr
, "extracted '%s'\n", e
->key
);
178 for (i
= 0; i
< 10; i
+= 2) {
179 e
= malloc(sizeof *e
);
181 memset(e
, 0, sizeof *e
);
182 snprintf(e
->key
, sizeof e
->key
, "new key %d", i
);
183 snprintf(e
->value
, sizeof e
->value
, "new value %d", i
);
184 lru_cache_insert(cache
, (lru_entry_t
*)e
, (lru_entry_t
**)&r
);
187 fprintf(stderr
, "freeing (%p) '%s':'%s'\n", r
, r
->key
, r
->value
);
194 lru_cache_foreach(cache
, kv_genstats
, stats
);
195 kv_dumpstats(cache
, stats
, hash_sz
);