-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_hashmap.c
218 lines (154 loc) · 4.78 KB
/
test_hashmap.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
#include <errno.h> /* errno */
#include <string.h> /* strerror */
#include <unistd.h> /* unlink */
#include "../vendor/unity/unity.h"
#include "../src/hashmap.h"
hashmap_t map;
void setUp()
{
/* Nothing to do */
}
void tearDown()
{
hashmap_destroy(&map);
}
void test_hashmap_init()
{
int rval;
rval = hashmap_init(&map, 0);
TEST_ASSERT_EQUAL_INT(-1, rval);
hashmap_destroy(&map);
rval = hashmap_init(&map, 10);
TEST_ASSERT_EQUAL_INT(0, rval);
/* destroyed in tearDown */
}
void test_hashmap_add_get_indices()
{
int addidx, getidx;
char *value;
char *keys[] = {"a", "b", "c"};
char *values[] = {"1", "2", "3"};
hashmap_init(&map, 10);
for (int i = 0; i < 3; i++) {
addidx = hashmap_add(&map, keys[i], values[i]);
getidx = hashmap_get(&map, keys[i], &value);
TEST_ASSERT_GREATER_OR_EQUAL(0, addidx);
TEST_ASSERT_GREATER_OR_EQUAL(0, getidx);
TEST_ASSERT_EQUAL(addidx, getidx);
TEST_ASSERT_EQUAL_STRING(values[i], value);
free(value);
}
}
/* Allow testing membership without allocating memory for copy of value */
void test_hashmap_get_null()
{
int rval;
hashmap_init(&map, 10);
hashmap_add(&map, "a", "1");
rval = hashmap_get(&map, "a", NULL);
TEST_ASSERT_GREATER_OR_EQUAL(0, rval);
}
void test_hashmap_has_key()
{
hashmap_init(&map, 10);
hashmap_add(&map, "a", "1");
TEST_ASSERT_TRUE(hashmap_has_key(&map, "a"));
}
void test_hashmap_add_get_force_hash_collision()
{
int addidx, getidx;
char *value;
char *keys[] = {"a", "b", "c"};
char *values[] = {"1", "2", "3"};
hashmap_init(&map, 1); /* bucket size 1 will force hash collision */
for (int i = 0; i < 3; i++) {
addidx = hashmap_add(&map, keys[i], values[i]);
getidx = hashmap_get(&map, keys[i], &value);
TEST_ASSERT_EQUAL(0, addidx);
TEST_ASSERT_EQUAL(0, getidx);
TEST_ASSERT_EQUAL(addidx, getidx);
TEST_ASSERT_EQUAL_STRING(values[i], value);
free(value);
}
}
void test_hashmap_size()
{
char *keys[] = {"a", "b", "c"};
char *values[] = {"1", "2", "3"};
hashmap_init(&map, 10);
/* Adding a (key, value) should increase the map size by 1 */
for (int i = 0; i < 3; i++) {
TEST_ASSERT_EQUAL_INT(i, map.size);
hashmap_add(&map, keys[i], values[i]);
TEST_ASSERT_EQUAL_INT(i + 1, map.size);
}
/* Changing the value of an existing key should not change size */
hashmap_add(&map, keys[0], "new value");
TEST_ASSERT_EQUAL_INT(3, map.size);
/* Deleting a (key, value) should decrease the map size by 1 */
for (int i = 2; i >= 0; i--) {
TEST_ASSERT_EQUAL_INT(i + 1, map.size);
hashmap_del(&map, keys[i]);
TEST_ASSERT_EQUAL_INT(i, map.size);
}
/* Deleting a nonexistent key should not change size */
hashmap_del(&map, keys[0]);
TEST_ASSERT_EQUAL_INT(0, map.size);
}
void test_hashmap_empty()
{
hashmap_init(&map, 10);
TEST_ASSERT_TRUE(hashmap_empty(&map));
hashmap_add(&map, "a", "1");
TEST_ASSERT_FALSE(hashmap_empty(&map));
}
void test_hashmap_timeout_0_gc_noop()
{
unsigned long start = time(NULL);
hashmap_init(&map, 10);
map.timeout = 0;
hashmap_add(&map, "a", "1");
while (((unsigned long) time(NULL)) == start)
;
hashmap_gc(&map);
TEST_ASSERT_EQUAL_INT(1, map.size);
map.timeout = 1;
while (((unsigned long) time(NULL)) == start + 1)
;
hashmap_gc(&map);
TEST_ASSERT_EQUAL_INT(0, map.size);
}
void test_hashmap_unlinker()
{
FILE *file;
const char fpath_fmt[] = "/tmp/test_hashmap_%lu";
char *fpath[50];
const char *fname;
hashmap_init(&map, 10);
/* "Touch" a temp file like test_hashmap_1542079712 */
sprintf((char *)fpath, fpath_fmt, (unsigned long) time(NULL));
fname = (const char *)fpath;
file = fopen(fname, "w+");
TEST_ASSERT_NOT_NULL_MESSAGE(file, strerror(errno));
/* Add fname to hashmap and then delete it, triggering the unlinker */
map.unlinker = unlink; /* set `unlink` to be called on value at del */
hashmap_add(&map, "tempfile", fname);
hashmap_del(&map, "tempfile");
/* Verify file no longer exists */
TEST_ASSERT_EQUAL_INT(-1, access(fname, F_OK));
}
int main()
{
UNITY_BEGIN();
RUN_TEST(test_hashmap_init);
RUN_TEST(test_hashmap_add_get_indices);
RUN_TEST(test_hashmap_add_get_force_hash_collision);
RUN_TEST(test_hashmap_size);
RUN_TEST(test_hashmap_empty);
/* Passes but takes about 2 seconds to run - normally disabled */
//RUN_TEST(test_hashmap_timeout_0_gc_noop);
RUN_TEST(test_hashmap_unlinker);
RUN_TEST(test_hashmap_get_null);
RUN_TEST(test_hashmap_has_key);
return UNITY_END();
}