-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
59 lines (53 loc) · 1.31 KB
/
test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "lru.h"
bool my_strcmp(void *a, void *b)
{
if (strcmp((char *)a, (char *)b) == 0) {
return true;
}
return false;
}
// a bad hash func
uint32_t my_str_hash(void *a)
{
uint32_t hash = 0;
char *str = (char *)a;
while (*str) {
hash += *str;
str++;
}
return hash;
}
void my_free(void *key, void *data)
{
free(data);
}
void my_print_item(void *a)
{
char *str = (char *)a;
printf("%s", str);
}
int main(void)
{
struct lru_cache *cache = lru_init(2, my_str_hash, my_strcmp, my_strcmp, my_free);
lru_insert(cache, "foo", strdup("bar"), true);
lru_insert(cache, "bar", strdup("foo"), true);
lru_dump(cache, my_print_item, my_print_item);
lru_get(cache, "foo");
lru_dump(cache, my_print_item, my_print_item);
lru_insert(cache, "foobar", strdup("foobar"), true);
lru_dump(cache, my_print_item, my_print_item);
//hash collision
lru_insert(cache, "barfoo", strdup("barfoo"), true);
lru_dump(cache, my_print_item, my_print_item);
lru_insert(cache, "deadbeef", strdup("deadbeef"), true);
lru_dump(cache, my_print_item, my_print_item);
lru_get(cache, "foo");
lru_get(cache, "barfoo");
lru_dump(cache, my_print_item, my_print_item);
lru_insert(cache, "deadbeef", strdup("newdata"), true);
lru_dump(cache, my_print_item, my_print_item);
return 0;
}