-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
72 lines (63 loc) · 1.61 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
60
61
62
63
64
65
66
67
68
69
70
71
#include <assert.h>
#include "map.h"
int main (void) {
Map map = MapAlloc(0);
bool found;
uint32_t keys[10000], cap, occ;
// initialize keys
for (uint32_t i = 0; i < 10000; ++i) keys[i] = i;
// Test Put, Get, and Del without rehashing
cap = map.cap;
occ = map.rem - 1; // one more insertion would trigger a rehash
for (uint32_t i = 0; i < occ; ++i) {
Buf key = { sizeof keys[i], &keys[i] };
Buf val;
found = MapPut(&map, key, key);
assert(!found);
found = MapGet(map, key, &val);
assert(found);
assert(key.size == val.size);
assert(key.p == val.p);
assert(* (uint32_t *) val.p == i);
found = MapPut(&map, key, key);
assert(found);
}
assert(map.cap == cap);
assert(map.rem == 1);
assert(map.occ == occ);
for (uint32_t i = 0; i < occ; ++i) {
Buf key = { sizeof keys[i], &keys[i] };
found = MapDel(&map, key);
assert(found);
found = MapGet(map, key, NULL);
assert(!found);
}
assert(map.cap == cap);
assert(map.rem == occ + 1);
assert(map.occ == 0);
// Test Put, Get, and Del with rehashing
occ = 10000;
for (uint32_t i = 0; i < occ; ++i) {
Buf key = { sizeof keys[i], &keys[i] };
Buf val;
found = MapPut(&map, key, key);
assert(!found);
found = MapGet(map, key, &val);
assert(found);
assert(key.size == val.size);
assert(key.p == val.p);
assert(* (uint32_t *) val.p == i);
found = MapPut(&map, key, key);
assert(found);
}
assert(map.occ == occ);
for (uint32_t i = 0; i < occ; ++i) {
Buf key = { sizeof keys[i], &keys[i] };
found = MapDel(&map, key);
assert(found);
found = MapGet(map, key, NULL);
assert(!found);
}
assert(map.occ == 0);
return 0;
}