-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathblt_test.c
165 lines (147 loc) · 4.16 KB
/
blt_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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "blt.h"
#define EXPECT(_x_) if (_x_); else fprintf(stderr, "%s:%d: FAIL\n", __FILE__, __LINE__)
#define FAIL() fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__), exit(1)
#define F(i, n) for(int i = 0; i < n; i++)
static void split(char *s, void (*fun)(char *)) {
if (!*s) return;
for (char *p = s, *q = p;; q++) if (*q == ' ' || *q == '\0') {
char *tmp = strndup(p, q - p);
fun(tmp);
free(tmp);
if (*q == '\0') break;
p = q + 1;
}
}
struct arr_s {
void **p;
int max, n;
};
typedef struct arr_s *arr_t;
arr_t arr_new() {
arr_t r = malloc(sizeof(*r));
r->n = 0;
r->max = 8;
r->p = malloc(sizeof(*r->p) * r->max);
return r;
}
void arr_add(arr_t r, void *p) {
if (r->n == r->max) r->p = realloc(r->p, sizeof(*r->p) * (r->max *= 2));
r->p[r->n++] = p;
}
BLT *make_blt(char *line) {
BLT* blt = blt_new();
split(line, ({ void _(char *s) { blt_put(blt, s, 0); }_; }));
return blt;
}
arr_t make_arr(char *line) {
arr_t a = arr_new();
split(line, ({ void _(char *s) { arr_add(a, strdup(s)); }_; }));
return a;
}
void nuke_arr(arr_t a) {
F(i, a->n) free(a->p[i]);
free(a->p);
free(a);
}
void test_traverse(char *line) {
BLT* blt = blt_new();
arr_t a = arr_new();
split(line, ({ void _(char *s) {
blt_put(blt, s, 0);
arr_add(a, strdup(s));
}_; }));
qsort(a->p, a->n, sizeof(*a->p), ({ int _(const void *p, const void *q) {
return strcmp(*(char **)p, *(char **)q);
}_; }));
int n = 0, count = 0;
// Check blt_forall() retrieves keys in order.
blt_forall(blt, ({void _(BLT_IT *it){
while (n + 1 < a->n && !strcmp(a->p[n], a->p[n + 1])) n++;
if (n == a->n) FAIL();
EXPECT(!strcmp(it->key, a->p[n++]));
count++;
}_;}));
EXPECT(count == blt_size(blt));
EXPECT(n == a->n);
// Check blt_first() and blt_next().
n = count = 0;
for (BLT_IT *it = blt_first(blt); it; it = blt_next(blt, it)) {
while (n + 1 < a->n && !strcmp(a->p[n], a->p[n + 1])) n++;
if (n == a->n) FAIL();
EXPECT(!strcmp(it->key, a->p[n++]));
count++;
}
EXPECT(count == blt_size(blt));
EXPECT(n == a->n);
// Check blt_last() and blt_prev().
n = a->n - 1; count = 0;
for (BLT_IT *it = blt_last(blt); it; it = blt_prev(blt, it)) {
while (n - 1 >= 0 && !strcmp(a->p[n], a->p[n - 1])) n--;
if (n == -1) FAIL();
EXPECT(!strcmp(it->key, a->p[n--]));
count++;
}
EXPECT(count == blt_size(blt));
EXPECT(n == -1);
nuke_arr(a);
blt_clear(blt);
}
void check_prefix(BLT* blt, char *prefix, char *want) {
arr_t a = make_arr(want);
int n = 0;
blt_allprefixed(blt, prefix, ({int _(BLT_IT *it){
while (n + 1 < a->n && !strcmp(a->p[n], a->p[n + 1])) n++;
if (n == a->n) FAIL();
EXPECT(!strcmp(it->key, a->p[n++]));
return 1;
}_;}));
EXPECT(n == a->n);
nuke_arr(a);
}
int main() {
test_traverse("");
test_traverse("one-string");
test_traverse("two strings");
test_traverse("red string blue string");
test_traverse("the quick brown fox jumps over the lazy dog");
test_traverse(" 2 spaces means empty strings are tested");
// Generate strings like "a aaa a aa aaa".
int n = 32;
char s[n * 16];
srand(time(0));
char *c = s;
F(i, n) {
if (i) *c++ = ' ';
int len = rand() % 12 + 1;
F(j, len) *c++ = 'a';
}
*c = 0;
test_traverse(s);
// Generate strings containing random nonzero chars.
c = s;
F(i, n) {
if (i) *c++ = ' ';
int len = rand() % 12 + 1;
// This could be a space. That's fine.
F(j, len) *c++ = (char) (rand() % 255 + 1);
}
*c = 0;
test_traverse(s);
BLT *blt = make_blt("a aardvark b ben blink bliss blt blynn");
check_prefix(blt, "b", "b ben blink bliss blt blynn");
check_prefix(blt, "bl", "blink bliss blt blynn");
check_prefix(blt, "bli", "blink bliss");
check_prefix(blt, "a", "a aardvark");
check_prefix(blt, "aa", "aardvark");
check_prefix(blt, "c", "");
EXPECT(!strcmp(blt_ceil(blt, "blink")->key, "blink"));
EXPECT(!strcmp(blt_ceil(blt, "blink182")->key, "bliss"));
EXPECT(!strcmp(blt_floor(blt, "blink")->key, "blink"));
EXPECT(!strcmp(blt_floor(blt, "blink182")->key, "blink"));
blt_clear(blt);
return 0;
}