-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_htfc.c
96 lines (80 loc) · 1.89 KB
/
test_htfc.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <check.h>
#include "htfc.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/htfc%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 htfc *htfc = htfc_new(buff, len);
char expected[500];
int *o = order(i);
ck_assert_int_eq(htfc_count(htfc), i);
struct htfc_iter *iter = htfc_iter_init(htfc);
char *s;
for (int j = 0; j < i; ++j) {
sprintf(expected, "sample%d", o[j]);
s = htfc_iter_next(iter);
ck_assert_str_eq(s, expected);
}
cleanup:
htfc_iter_free(iter);
htfc_free(htfc);
free(buff);
free(o);
fclose(fp);
}
START_TEST(test_basic)
{
FILE *fp = fopen("data/htfc-basic-test.dict", "r");
ck_assert_ptr_ne(fp, NULL);
uint8_t *buff = malloc(BUFSIZE);
int len = fread(buff, 1, BUFSIZE, fp);
struct htfc *htfc = htfc_new(buff, len);
struct search_result result;
ck_assert_int_eq(htfc_count(htfc), 4);
htfc_search(htfc, contains, 0, "wo", &result);
ck_assert_int_eq(result.count, 1);
ck_assert_int_eq(result.matches[0], 3);
free(result.matches);
htfc_free(htfc);
free(buff);
fclose(fp);
}
END_TEST
START_TEST(test_boundaries)
{
test_dict(2);
test_dict(255);
test_dict(256);
test_dict(257);
}
END_TEST
void add_htfc(TCase *tc) {
tcase_add_test(tc, test_basic);
tcase_add_test(tc, test_boundaries);
}