-
Notifications
You must be signed in to change notification settings - Fork 18
/
test.c
225 lines (196 loc) · 7.48 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
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
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include "hashset.h"
#include "hashset_itr.h"
static void trivial(void)
{
char *missing = "missing";
char *items[] = {"zero", "one", "two", "three", NULL};
char *foo = "foo";
size_t ii, nitems = 4;
hashset_t set = hashset_create();
if (set == NULL) {
fprintf(stderr, "failed to create hashset instance\n");
abort();
}
for (ii = 0; ii < nitems; ++ii) {
hashset_add(set, items[ii]);
}
for (ii = 0; ii < nitems; ++ii) {
assert(hashset_is_member(set, items[ii]));
}
assert(hashset_is_member(set, missing) == 0);
assert(hashset_remove(set, items[1]) == 1);
assert(hashset_num_items(set) == 3);
assert(hashset_remove(set, items[1]) == 0);
assert(hashset_add(set, foo) == 1);
assert(hashset_add(set, foo) == 0);
hashset_destroy(set);
}
static void test_gaps(void)
{
hashset_t set = hashset_create();
/* fill the hashset */
hashset_add(set, (void *)0xbabe);
hashset_add(set, (void *)0xbeef);
hashset_add(set, (void *)0xbad);
hashset_add(set, (void *)0xf00d);
/* 0xf00d (nil) (nil) (nil) (nil) 0xbad 0xbabe 0xbeef */
/* make a gap */
hashset_remove(set, (void *)0xbeef);
/* 0xf00d (nil) (nil) (nil) (nil) 0xbad 0xbabe 0x1 */
/* check that 0xf00d is still reachable */
assert(hashset_is_member(set, (void *)0xf00d));
/* add 0xbeef back */
hashset_add(set, (void *)0xbeef);
/* 0xf00d (nil) (nil) (nil) (nil) 0xbad 0xbabe 0xbeef */
/* verify */
assert(hashset_is_member(set, (void *)0xbeef));
assert(hashset_is_member(set, (void *)0xf00d));
}
static void test_exceptions(void)
{
hashset_t set = hashset_create();
assert(hashset_add(set, (void *)0) == -1);
assert(hashset_add(set, (void *)1) == -1);
}
static void test_rehashing_items_placed_beyond_nitems(void)
{
hashset_t set = hashset_create();
assert(hashset_add(set, (void *)20644128) == 1);
assert(hashset_add(set, (void *)21747760) == 1);
assert(hashset_add(set, (void *)17204864) == 1);
assert(hashset_add(set, (void *)22937440) == 1);
assert(hashset_add(set, (void *)14734272) == 1);
assert(hashset_add(set, (void *)13948320) == 1);
assert(hashset_add(set, (void *)18116496) == 1);
assert(hashset_add(set, (void *)18229952) == 1);
assert(hashset_add(set, (void *)20390128) == 1);
assert(hashset_add(set, (void *)23523264) == 1);
assert(hashset_add(set, (void *)22866784) == 1);
assert(hashset_add(set, (void *)17501248) == 1);
assert(hashset_add(set, (void *)17168832) == 1);
assert(hashset_add(set, (void *)13389824) == 1);
assert(hashset_add(set, (void *)15795136) == 1);
assert(hashset_add(set, (void *)15154464) == 1);
assert(hashset_add(set, (void *)22507840) == 1);
assert(hashset_add(set, (void *)22977920) == 1);
assert(hashset_add(set, (void *)20527584) == 1);
assert(hashset_add(set, (void *)21557872) == 1);
assert(hashset_add(set, (void *)23089952) == 1);
assert(hashset_add(set, (void *)21606240) == 1);
assert(hashset_add(set, (void *)25168704) == 1);
assert(hashset_add(set, (void *)25198096) == 1);
assert(hashset_add(set, (void *)25248000) == 1);
assert(hashset_add(set, (void *)25260976) == 1);
assert(hashset_add(set, (void *)25905520) == 1);
assert(hashset_add(set, (void *)25934608) == 1);
assert(hashset_add(set, (void *)26015264) == 1);
assert(hashset_add(set, (void *)26044352) == 1);
assert(hashset_add(set, (void *)24784800) == 1);
assert(hashset_add(set, (void *)24813888) == 1);
assert(hashset_add(set, (void *)24663936) == 1);
assert(hashset_add(set, (void *)24693536) == 1);
assert(hashset_add(set, (void *)24743792) == 1);
assert(hashset_add(set, (void *)24756480) == 1);
assert(hashset_is_member(set, (void *)20644128) == 1);
assert(hashset_is_member(set, (void *)21747760) == 1);
assert(hashset_is_member(set, (void *)17204864) == 1);
assert(hashset_is_member(set, (void *)22937440) == 1);
assert(hashset_is_member(set, (void *)14734272) == 1);
assert(hashset_is_member(set, (void *)13948320) == 1);
assert(hashset_is_member(set, (void *)18116496) == 1);
assert(hashset_is_member(set, (void *)18229952) == 1);
assert(hashset_is_member(set, (void *)20390128) == 1);
assert(hashset_is_member(set, (void *)23523264) == 1);
assert(hashset_is_member(set, (void *)22866784) == 1);
assert(hashset_is_member(set, (void *)17501248) == 1);
assert(hashset_is_member(set, (void *)17168832) == 1);
assert(hashset_is_member(set, (void *)13389824) == 1);
assert(hashset_is_member(set, (void *)15795136) == 1);
assert(hashset_is_member(set, (void *)15154464) == 1);
assert(hashset_is_member(set, (void *)22507840) == 1);
assert(hashset_is_member(set, (void *)22977920) == 1);
assert(hashset_is_member(set, (void *)20527584) == 1);
assert(hashset_is_member(set, (void *)21557872) == 1);
assert(hashset_is_member(set, (void *)23089952) == 1);
assert(hashset_is_member(set, (void *)21606240) == 1);
assert(hashset_is_member(set, (void *)25168704) == 1);
assert(hashset_is_member(set, (void *)25198096) == 1);
assert(hashset_is_member(set, (void *)25248000) == 1);
assert(hashset_is_member(set, (void *)25260976) == 1);
assert(hashset_is_member(set, (void *)25905520) == 1);
assert(hashset_is_member(set, (void *)25934608) == 1);
assert(hashset_is_member(set, (void *)26015264) == 1);
assert(hashset_is_member(set, (void *)26044352) == 1);
assert(hashset_is_member(set, (void *)24784800) == 1);
assert(hashset_is_member(set, (void *)24813888) == 1);
assert(hashset_is_member(set, (void *)24663936) == 1);
assert(hashset_is_member(set, (void *)24693536) == 1);
assert(hashset_is_member(set, (void *)24743792) == 1);
assert(hashset_is_member(set, (void *)24756480) == 1);
}
static void test_iterating(void)
{
hashset_t set = hashset_create();
hashset_itr_t iter = hashset_iterator(set);
int step;
/* fill the hashset */
hashset_add(set, (void *)"Bob");
hashset_add(set, (void *)"Steve");
hashset_add(set, (void *)"Karen");
hashset_add(set, (void *)"Ellen");
step = 0;
while(hashset_iterator_has_next(iter))
{
if(step == 0)
{
assert(strncmp((char *)hashset_iterator_value(iter), "Karen", 5) == 0);
}
if(step == 1)
{
assert(strncmp((char *)hashset_iterator_value(iter), "Steve", 5) == 0);
}
if(step == 2)
{
assert(strncmp((char *)hashset_iterator_value(iter), "Bob", 3) == 0);
}
if(step == 3)
{
assert(strncmp((char *)hashset_iterator_value(iter), "Ellen", 5) == 0);
}
hashset_iterator_next(iter);
step++;
}
assert(hashset_iterator_has_next(iter) == 0);
assert(hashset_iterator_next(iter) == -1);
}
static void test_fill_with_deleted_items()
{
char *s = "some string";
hashset_t set = hashset_create();
if (set == NULL)
abort();
/* fill `set` with deleted items */
for (int i = 0; i < 8; ++i)
{
hashset_add(set, s + i);
hashset_remove(set, s + i);
}
/* this should not cause an infinite loop */
assert(hashset_is_member(set, s) == 0);
hashset_destroy(set);
}
int main(int argc, char *argv[])
{
trivial();
test_gaps();
test_exceptions();
test_rehashing_items_placed_beyond_nitems();
test_iterating();
test_fill_with_deleted_items();
(void)argc;
(void)argv;
return 0;
}