-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
170 lines (137 loc) · 4.46 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// Created by easy on 22.03.24.
//
#include "axhashmap.h"
#include <stdlib.h>
#include <stdio.h>
#include <xoshiro256starstar.h>
#include <sys/random.h>
#include <assert.h>
#include <string.h>
static void shuffleU64(uint64_t *xs, size_t n, struct xsr256ss *seed) {
for (size_t i = 0; i < n - 1; ++i) {
size_t j = i + xsr256ss(seed) % (n - i);
uint64_t tmp = xs[i];
xs[i] = xs[j];
xs[j] = tmp;
}
}
static void shuffleStrings(char **xs, size_t n, struct xsr256ss *seed) {
for (size_t i = 0; i < n - 1; ++i) {
size_t j = i + xsr256ss(seed) % (n - i);
char *tmp = xs[i];
xs[i] = xs[j];
xs[j] = tmp;
}
}
void testRemove(struct xsr256ss *seed) {
puts("Testing remove...");
enum {N = 1000};
uint64_t pool[N];
uint64_t tmp[N];
for (int trials = 0; trials < 1000; ++trials) {
unsigned removeCount = xsr256ss(seed) % (N + 1);
axhashmap *h = axh_new(sizeof(uint64_t));
for (int i = 0; i < N; ++i)
pool[i] = xsr256ss(seed);
for (int i = 0; i < N; ++i)
axh_add(h, &pool[i]);
memcpy(tmp, pool, N * sizeof *tmp);
shuffleU64(tmp, N, seed);
for (unsigned i = 0; i < removeCount; ++i) {
assert(axh_has(h, &tmp[i]));
axh_unmap(h, &tmp[i]);
assert(!axh_has(h, &tmp[i]));
}
for (unsigned i = 0; i < N; ++i) {
if (i < removeCount)
assert(!axh_has(h, &tmp[i]));
else
assert(axh_has(h, &tmp[i]));
}
axh_destroy(h);
}
puts("Removal successful.");
}
/* NOTICE: This test is probabilistic and will fail
frequently if MINLEN is not sufficiently high. */
void testStrings(struct xsr256ss *seed) {
puts("Testing native string functionality...");
enum {N = 1000, ALLOCATED = 32, MINLEN = 16};
char *pool[N];
char *tmp[N];
for (int i = 0; i < N; ++i)
pool[i] = malloc(ALLOCATED);
memcpy(tmp, pool, N * sizeof *tmp);
for (int trials = 0; trials < 1000; ++trials) {
axhashmap *h = axh_new(0);
for (unsigned s = 0; s < N; ++s) {
uint64_t length = MINLEN + xsr256ss(seed) % (ALLOCATED - MINLEN);
for (uint64_t i = 0; i < length; ++i)
pool[s][i] = (char) ('a' + xsr256ss(seed) % 26);
pool[s][length] = '\0';
assert(!axh_has(h, pool[s]));
axh_add(h, pool[s]);
assert(axh_has(h, pool[s]));
}
for (unsigned s = 0; s < N; ++s)
assert(axh_has(h, pool[s]));
unsigned removeCount = xsr256ss(seed) % (N + 1);
shuffleStrings(tmp, N, seed);
for (unsigned s = 0; s < removeCount; ++s) {
assert(axh_has(h, tmp[s]));
axh_unmap(h, tmp[s]);
assert(!axh_has(h, tmp[s]));
}
for (unsigned s = 0; s < N; ++s) {
if (s < removeCount)
assert(!axh_has(h, tmp[s]));
else
assert(axh_has(h, tmp[s]));
}
axh_destroy(h);
}
for (int i = 0; i < N; ++i)
free(pool[i]);
puts("Native string functionality test successful");
}
void playground1(void) {
axhashmap *h = axh_new(0);
char *keys[] = {"+", "-", "&&", "||", "=="};
enum {N = sizeof keys / sizeof *keys};
char *values[N] = {"TOK_PLUS", "TOK_MINUS", "TOK_LOGICAL_AND", "TOK_LOGICAL_OR", "TOK_EQUALS"};
for (int i = 0; i < N; ++i)
axh_map(h, keys[i], values[i]);
for (int i = 0; i < N; ++i)
puts(axh_get(h, keys[i]));
printf("size %lu\n", axh_size(h));
axh_clear(h);
printf("size %lu\n", axh_size(h));
for (int i = 0; i < N; ++i)
puts(axh_has(h, keys[i]) ? "yes" : "no");
axh_destroy(h);
}
void playground2(void) {
axhashmap *h = axh_new(0);
char *keys[] = {"+", "-", "&&", "||", "=="};
enum {N = sizeof keys / sizeof *keys};
char *values[N] = {"TOK_PLUS", "TOK_MINUS", "TOK_LOGICAL_AND", "TOK_LOGICAL_OR", "TOK_EQUALS"};
for (int i = 0; i < N; ++i)
axh_map(h, keys[i], values[i]);
axh_destroy(h);
}
void speedtest(struct xsr256ss *seed) {
axhashmap *h = axh_sizedNew(sizeof(uint64_t), 105300000, 19./20.);
for (uint64_t i = 0; i < 100000000; ++i) {
axh_add(h, &i);
}
//axh_destroy(h);
}
int main(void) {
struct xsr256ss seed;
(void) getrandom(&seed, sizeof seed, 0);
speedtest(&seed);
/*playground1();
testRemove(&seed);
testStrings(&seed);*/
}