-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-set.c
148 lines (121 loc) · 3.03 KB
/
test-set.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
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <unit.h>
#include <set.h>
#include <util.h>
static void test_alloc(void);
static void test_add(void);
static void test_free(void);
static void test_query(void);
static void test_remove(void);
static void test_fixed(void);
static void test_prefix(void);
static void test_large_add(void);
static void test_dup(void);
char unit_filename[] = "set.c";
struct test unit_tests[] = {
{ "allocating a set", 0x0, test_alloc, test_free, },
{ "inserting a string", 0x0, test_add, test_free, },
{ "querying the set", test_add, test_query, test_free, },
{ "removing an element", test_add, test_remove, test_free, },
{ "querying with a fixed-size buffer", test_add, test_fixed, test_free, },
{ "testing the prefix check", test_add, test_prefix, test_free, },
{ "adding a large number of strings", test_alloc, test_large_add, test_free, },
{ "attempting to add duplicate strings", test_add, test_dup, test_free, },
{ 0x0 },
};
struct set *set;
char **reply = 0x0;
char *strings[] = { "foo", "bar", "baz", "quux", };
void
test_free(void)
{
set_free(set), set = 0x0;
free(reply), reply = 0;
}
void
test_alloc(void)
{
set = set_alloc();
ok(set != 0x0);
}
void
test_add(void)
{
size_t i = 0;
set = set_alloc();
for (i = 0; i < array_len(strings); ++i) {
expect(0, set_add_string(set, strings[i]));
expect(true, set_contains_string(set, strings[i]));
}
}
void
test_query(void)
{
expect(4, set_query_string(0, 0, set, ""));
expect(1, set_query_string(0x0, 0, set, "foo"));
set_query_string(&reply, 0, set, "f");
ok(reply != 0x0);
ok(reply[0] != 0x0);
ok(!strcmp(reply[0], "foo"));
ok(!reply[1]);
free(reply), reply = 0;
expect(2, set_query_string(&reply, 0, set, "ba"));
ok(reply[0] != 0x0);
ok(reply[1] != 0x0);
ok(reply[2] == 0x0);
ok(!strcmp(reply[0], "bar"));
ok(!strcmp(reply[1], "baz"));
}
void
test_remove(void)
{
size_t i = 0;
for (i = 0; i < array_len(strings); ++i) {
expect(0, set_remove_string(set, strings[i]));
expect(false, set_contains_string(set, strings[i]));
expect(0, set_query_string(0x0, 0, set, strings[i]));
}
}
void
test_prefix(void)
{
ok(set_prefix_string(set, "f"));
ok(set_prefix_string(set, "q"));
ok(set_prefix_string(set, "b"));
}
void
test_fixed(void)
{
char **arr = calloc(3, sizeof *arr);
expect(4, set_query_string(&arr, 3, set, ""));
ok(arr[0] != 0x0);
ok(arr[1] != 0x0);
ok(arr[2] == 0x0);
ok(!strcmp(arr[0], "bar"));
ok(!strcmp(arr[1], "baz"));
free(arr);
}
void
test_large_add(void)
{
char word[256];
FILE *wordlist;
wordlist = fopen("opt/words", "r");
ok(!!wordlist);
while (fgets(word, 256, wordlist)) {
expect(0, set_add_string(set, word));
}
expect(0, fseek(wordlist, 0, SEEK_SET));
while (fgets(word, 256, wordlist)) {
ok(set_contains_string(set, word));
}
}
void
test_dup(void)
{
expect(EEXIST, set_add_string(set, "foo"));
}