forked from Stapelzeiger/cvra-socket-handler-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_map_test.c
105 lines (87 loc) · 2.49 KB
/
simple_map_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <assert.h>
#include "simple_map.h"
struct test_data {
int x;
int key;
};
int test_data_cmp(void *key, void *elem)
{
if (*(int *)key == ((struct test_data *)elem)->key)
return SIMPLE_MAP_COMP_EQUAL;
if (*(int *)key > ((struct test_data *)elem)->key)
return SIMPLE_MAP_COMP_GREATER_THAN;
return SIMPLE_MAP_COMP_SMALLER_THAN;
}
int main(void)
{
simple_map_t map;
simple_map_init(&map, sizeof(struct test_data), test_data_cmp);
struct test_data a = {314, 42};
int key;
struct test_data *res;
simple_map_add(&map, &a, &a.key);
key = 42;
res = simple_map_find(&map, &key);
assert(res->x == 314);
key = 24567; // nonexistant key
assert(simple_map_find(&map, &key) == NULL);
assert(simple_map_add(&map, &a, &a.key) == SIMPLE_MAP_KEY_EXISTS);
a.key = 23;
a.x = 3445;
simple_map_add(&map, &a, &a.key);
key = 42;
res = simple_map_find(&map, &key);
assert(res->x == 314);
key = 23;
res = simple_map_find(&map, &key);
assert(res->x == 3445);
key = 24567; // nonexistant key
assert(simple_map_find(&map, &key) == NULL);
a.key = 50;
a.x = 1337;
simple_map_add(&map, &a, &a.key);
key = 42;
res = simple_map_find(&map, &key);
assert(res->x == 314);
key = 23;
res = simple_map_find(&map, &key);
assert(res->x == 3445);
key = 50;
res = simple_map_find(&map, &key);
assert(res->x == 1337);
key = 24567; // nonexistant key
assert(simple_map_find(&map, &key) == NULL);
// test remove
key = 42;
simple_map_remove(&map, &key);
assert(simple_map_find(&map, &key) == NULL);
key = 23;
res = simple_map_find(&map, &key);
assert(res->x == 3445);
key = 50;
res = simple_map_find(&map, &key);
assert(res->x == 1337);
key = 24567; // nonexistant key
assert(simple_map_find(&map, &key) == NULL);
// test remove with memory shrinking
key = 23;
simple_map_remove(&map, &key);
assert(simple_map_find(&map, &key) == NULL);
key = 50;
res = simple_map_find(&map, &key);
assert(res->x == 1337);
simple_map_remove(&map, &key);
assert(simple_map_find(&map, &key) == NULL);
key = 24567; // nonexistant key
assert(simple_map_find(&map, &key) == NULL);
//and add again
a.key = 23;
a.x = 3445;
simple_map_add(&map, &a, &a.key);
key = 23;
res = simple_map_find(&map, &key);
assert(res->x == 3445);
printf("All tests passed\n");
return 0;
}