-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hfc.c
278 lines (243 loc) · 6.64 KB
/
test_hfc.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <check.h>
#include "hfc.h"
#include "bytes.h"
#define BUFSIZE 1000
static int contains(const char *a, const char *b) {
return strstr(a, b) ? 1 : 0;
}
static int intstrcmp(const void *a, const void *b) {
char astr[100];
char bstr[100];
sprintf(astr, "%d", *(const int *)a);
sprintf(bstr, "%d", *(const int *)b);
return strcmp(astr, bstr);
}
static int *order(int n) {
int *r = malloc(n * sizeof(int));
for (int i = 0; i < n; ++i) {
r[i] = i;
}
qsort(r, n, sizeof(int), intstrcmp);
return r;
}
static char filename[500];
static void test_dict(int i) {
sprintf(filename, "data/hfc%d-test.dict", i);
FILE *fp = fopen(filename, "r");
ck_assert_ptr_ne(fp, NULL);
uint8_t *buff = malloc(BUFSIZE);
int len = fread(buff, 1, BUFSIZE, fp);
struct hfc *hfc = hfc_new(buff, len);
char expected[500];
int *o = order(i);
ck_assert_int_eq(hfc_count(hfc), i);
struct hfc_iter *iter = hfc_iter_init(hfc);
char *s;
for (int j = 0; j < i; ++j) {
sprintf(expected, "sample%d", o[j]);
s = hfc_iter_next(iter);
ck_assert_str_eq(s, expected);
}
cleanup:
hfc_iter_free(iter);
hfc_free(hfc);
free(o);
fclose(fp);
}
START_TEST(test_basic)
{
FILE *fp = fopen("data/hfc-basic-test.dict", "r");
ck_assert_ptr_ne(fp, NULL);
uint8_t *buff = malloc(BUFSIZE);
int len = fread(buff, 1, BUFSIZE, fp);
struct hfc *hfc = hfc_new(buff, len);
struct search_result result;
ck_assert_int_eq(hfc_count(hfc), 4);
hfc_search_method(hfc, contains, 0, "wo", &result);
ck_assert_int_eq(result.count, 1);
ck_assert_int_eq(result.matches[0], 3);
free(result.matches);
hfc_free(hfc);
fclose(fp);
}
END_TEST
START_TEST(test_boundaries)
{
test_dict(2);
test_dict(255);
test_dict(256);
test_dict(257);
}
END_TEST
START_TEST(test_encode_basic)
{
char *strings[] = {"foo", "bar", "baz"};
struct bytes *b = hfc_compress(3, strings);
struct hfc *hfc = hfc_new(b->bytes, b->len);
ck_assert_int_eq(hfc_count(hfc), 3);
struct hfc_iter *it = hfc_iter_init(hfc);
ck_assert_str_eq(hfc_iter_next(it), "bar");
ck_assert_str_eq(hfc_iter_next(it), "baz");
ck_assert_str_eq(hfc_iter_next(it), "foo");
hfc_free(hfc);
free(b);
hfc_iter_free(it);
}
END_TEST
START_TEST(test_empty)
{
char *strings[] = {};
struct bytes *b = hfc_compress(0, strings);
struct hfc *hfc = hfc_new(b->bytes, b->len);
ck_assert_int_eq(hfc_count(hfc), 0);
struct hfc_iter *it = hfc_iter_init(hfc);
ck_assert_ptr_eq(hfc_iter_next(it), NULL);
hfc_iter_free(it);
hfc_free(hfc);
free(b);
}
END_TEST
START_TEST(test_one)
{
char *strings[] = {"foo"};
struct bytes *b = hfc_compress(1, strings);
struct hfc *hfc = hfc_new(b->bytes, b->len);
ck_assert_int_eq(hfc_count(hfc), 1);
struct hfc_iter *it = hfc_iter_init(hfc);
ck_assert_str_eq(hfc_iter_next(it), "foo");
ck_assert_ptr_eq(hfc_iter_next(it), NULL);
hfc_iter_free(it);
hfc_free(hfc);
free(b);
}
END_TEST
START_TEST(test_merge)
{
char *strings0[] = {"foo", "bar", "baz"};
struct bytes *b0 = hfc_compress(3, strings0);
struct hfc *hfc0 = hfc_new(b0->bytes, b0->len);
char *strings1[] = {"foo", "bin", "baz"};
struct bytes *b1 = hfc_compress(3, strings1);
struct hfc *hfc1 = hfc_new(b1->bytes, b1->len);
struct hfc *merged = hfc_merge_two(hfc0, hfc1);
ck_assert_int_eq(hfc_count(merged), 4);
struct hfc_iter *it = hfc_iter_init(merged);
ck_assert_str_eq(hfc_iter_next(it), "bar");
ck_assert_str_eq(hfc_iter_next(it), "baz");
ck_assert_str_eq(hfc_iter_next(it), "bin");
ck_assert_str_eq(hfc_iter_next(it), "foo");
hfc_free(merged);
hfc_free(hfc0);
hfc_free(hfc1);
free(b0);
free(b1);
hfc_iter_free(it);
}
END_TEST
START_TEST(test_filter)
{
char *strings0[] = {"one", "two", "three", "four", "five", "six", "seven", "eight"};
struct bytes *b0 = hfc_compress(8, strings0);
hfc_set(b0->bytes, b0->len);
free(b0);
char *x = strdup("i");
struct search_result *result = hfc_search(x, CONTAINS);
hfc_filter(result->matches, result->count);
struct hfc_iter *it = hfc_iter_init(hfc_get_cache());
// sorted: eight five four one seven six three two
// matches: 0, 1, 5
ck_assert_str_eq(hfc_iter_next(it), "eight");
ck_assert_str_eq(hfc_iter_next(it), "five");
ck_assert_str_eq(hfc_iter_next(it), "six");
hfc_iter_free(it);
free(x);
hfc_clear_cache();
}
END_TEST
START_TEST(test_larger_filter)
{
int count = 512;
char *strings0[count];
for (int i = 0; i < count; ++i) {
strings0[i] = malloc(4);
sprintf(strings0[i], "%d", i);
}
struct bytes *b0 = hfc_compress(count, strings0);
hfc_set(b0->bytes, b0->len);
free(b0);
char *x = strdup("78");
struct search_result *result = hfc_search(x, CONTAINS);
hfc_filter(result->matches, result->count);
struct hfc_iter *it = hfc_iter_init(hfc_get_cache());
ck_assert_str_eq(hfc_iter_next(it), "178");
ck_assert_str_eq(hfc_iter_next(it), "278");
ck_assert_str_eq(hfc_iter_next(it), "378");
ck_assert_str_eq(hfc_iter_next(it), "478");
ck_assert_str_eq(hfc_iter_next(it), "78");
hfc_iter_free(it);
free(x);
hfc_clear_cache();
for (int i = 0; i < count; ++i) {
free(strings0[i]);
}
}
END_TEST
START_TEST(test_lookup)
{
char *strings0[] = {"one", "two", "three", "four", "five", "six", "seven", "eight"};
struct bytes *b0 = hfc_compress(8, strings0);
hfc_set(b0->bytes, b0->len);
free(b0);
// sorted: eight five four one seven six three two
ck_assert_str_eq(hfc_lookup(0), "eight");
ck_assert_str_eq(hfc_lookup(1), "five");
ck_assert_str_eq(hfc_lookup(2), "four");
ck_assert_str_eq(hfc_lookup(3), "one");
ck_assert_str_eq(hfc_lookup(4), "seven");
ck_assert_str_eq(hfc_lookup(5), "six");
ck_assert_str_eq(hfc_lookup(6), "three");
ck_assert_str_eq(hfc_lookup(7), "two");
hfc_clear_cache();
}
END_TEST
START_TEST(test_set_empty)
{
hfc_set_empty();
ck_assert_int_eq(hfc_length(), 0);
}
END_TEST
// we keep a static reference to the current sample list.
// Testing some operations on that object.
START_TEST(test_singleton)
{
hfc_set_empty();
ck_assert_int_eq(hfc_length(), 0);
char *strings0[] = {"foo", "bar", "baz"};
struct bytes *b0 = hfc_compress(3, strings0);
hfc_merge(b0->bytes, b0->len);
ck_assert_int_eq(hfc_length(), 3);
char *strings1[] = {"foo", "bin", "baz"};
struct bytes *b1 = hfc_compress(3, strings1);
hfc_merge(b1->bytes, b1->len);
ck_assert_int_eq(hfc_length(), 4);
free(b0);
free(b1);
}
END_TEST
void add_hfc(TCase *tc) {
tcase_add_test(tc, test_basic);
tcase_add_test(tc, test_boundaries);
tcase_add_test(tc, test_encode_basic);
tcase_add_test(tc, test_empty);
tcase_add_test(tc, test_one);
tcase_add_test(tc, test_merge);
tcase_add_test(tc, test_filter);
tcase_add_test(tc, test_larger_filter);
tcase_add_test(tc, test_lookup);
tcase_add_test(tc, test_set_empty);
tcase_add_test(tc, test_singleton);
}