-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest-address-map.c
109 lines (104 loc) · 3.42 KB
/
test-address-map.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
106
107
108
109
#include <stdio.h>
#include "address-map.h"
#define COUNT (1000 * 1000)
static void add_to_other(uintptr_t addr, uintptr_t val, void *data) {
struct address_map *other = data;
if (addr >= COUNT)
fprintf(stdout, "unexpected address: %zu\n", addr);
if (address_map_contains(other, addr))
fprintf(stdout, "missing: %zu\n", addr);
address_map_add(other, addr, val);
}
int main(int argc, char *arv[]) {
struct address_map set;
address_map_init(&set);
for (size_t i = 0; i < COUNT; i++)
address_map_add(&set, i, -i);
fprintf(stdout, "after initial add, %zu/%zu\n", set.hash_map.n_items,
set.hash_map.size);
for (size_t i = 0; i < COUNT; i++) {
if (!address_map_contains(&set, i)) {
fprintf(stdout, "missing: %zu\n", i);
return 1;
}
if (address_map_lookup(&set, i, -1) != -i) {
fprintf(stdout, "missing: %zu\n", i);
return 1;
}
}
for (size_t i = COUNT; i < COUNT * 2; i++) {
if (address_map_contains(&set, i)) {
fprintf(stdout, "unexpectedly present: %zu\n", i);
return 1;
}
}
address_map_clear(&set);
fprintf(stdout, "after clear, %zu/%zu\n", set.hash_map.n_items,
set.hash_map.size);
for (size_t i = 0; i < COUNT; i++)
address_map_add(&set, i, 0);
// Now update.
fprintf(stdout, "after re-add, %zu/%zu\n", set.hash_map.n_items,
set.hash_map.size);
for (size_t i = 0; i < COUNT; i++)
address_map_add(&set, i, i + 1);
fprintf(stdout, "after idempotent re-add, %zu/%zu\n", set.hash_map.n_items,
set.hash_map.size);
for (size_t i = 0; i < COUNT; i++) {
if (!address_map_contains(&set, i)) {
fprintf(stdout, "missing: %zu\n", i);
return 1;
}
if (address_map_lookup(&set, i, -1) != i + 1) {
fprintf(stdout, "missing: %zu\n", i);
return 1;
}
}
for (size_t i = 0; i < COUNT; i++)
address_map_remove(&set, i);
fprintf(stdout, "after one-by-one removal, %zu/%zu\n", set.hash_map.n_items,
set.hash_map.size);
for (size_t i = COUNT; i < 2 * COUNT; i++) {
if (address_map_contains(&set, i)) {
fprintf(stdout, "unexpectedly present: %zu\n", i);
return 1;
}
}
for (size_t i = 0; i < COUNT; i++)
address_map_add(&set, i, i + 2);
struct address_map set2;
address_map_init(&set2);
address_map_for_each(&set, add_to_other, &set2);
fprintf(stdout, "after for-each set, %zu/%zu\n", set2.hash_map.n_items,
set2.hash_map.size);
for (size_t i = 0; i < COUNT; i++) {
if (address_map_lookup(&set2, i, -1) != i + 2) {
fprintf(stdout, "missing: %zu\n", i);
return 1;
}
}
address_map_destroy(&set2);
size_t burnin = 1000 * 1000 * 1000 / COUNT;
fprintf(stdout, "beginning clear then add %zu items, %zu times\n",
(size_t)COUNT, burnin);
for (size_t j = 0; j < burnin; j++) {
address_map_clear(&set);
for (size_t i = 0; i < COUNT; i++)
address_map_add(&set, i, i + 3);
}
fprintf(stdout, "after burnin, %zu/%zu\n", set.hash_map.n_items,
set.hash_map.size);
fprintf(stdout, "beginning lookup %zu items, %zu times\n",
(size_t)COUNT, burnin);
for (size_t j = 0; j < burnin; j++) {
for (size_t i = 0; i < COUNT; i++) {
if (address_map_lookup(&set, i, -1) != i + 3) {
fprintf(stdout, "missing: %zu\n", i);
return 1;
}
}
}
fprintf(stdout, "after burnin, %zu/%zu\n", set.hash_map.n_items,
set.hash_map.size);
address_map_destroy(&set);
}