-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_vector.c
319 lines (270 loc) · 7.08 KB
/
test_vector.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "hashmap.h"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
// Vector tests
void *int_ptr_cpy (const void *p)
{
if (p == NULL)
return NULL;
int **a = malloc (sizeof (int *));
if (a == NULL)
return NULL;
*a = *((int **) p);
return a;
}
int int_ptr_cmp (const void *p1, const void *p2)
{
if (p1 == NULL || p2 == NULL)
return 0;
return *((int **) p1) == *((int **) p2);
}
void int_ptr_free (void **p)
{
if (p == NULL || *p == NULL)
return;
free (*p);
*p = NULL;
}
void *int_cpy (const void *p)
{
if (p == NULL)
return NULL;
int *a = malloc (sizeof (int));
if (a == NULL)
return NULL;
*a = *((int *) p);
return a;
}
int int_cmp (const void *p1, const void *p2)
{
if (p1 == NULL || p2 == NULL)
return 0;
return *((int *) p1) == *((int *) p2);
}
void int_free (void **p)
{
if (p == NULL || *p == NULL)
return;
free (*p);
*p = NULL;
}
void test_vector_alloc ()
{
// Test any function is NULL
vector *v = vector_alloc (NULL, int_ptr_cmp, int_ptr_free);
assert(v == NULL);
v = vector_alloc (int_ptr_cpy, NULL, int_ptr_free);
assert(v == NULL);
v = vector_alloc (int_ptr_cpy, int_ptr_cmp, NULL);
assert(v == NULL);
v = vector_alloc (int_ptr_cpy, int_ptr_cmp, int_ptr_free);
if (v == NULL)
exit (EXIT_FAILURE);
assert(v->size == 0);
assert(v->capacity == VECTOR_INITIAL_CAP);
assert(v->data != NULL);
assert(v->elem_copy_func == int_ptr_cpy);
assert(v->elem_cmp_func == int_ptr_cmp);
assert(v->elem_free_func == int_ptr_free);
vector_free (&v);
}
void *null_cpy (const void *p)
{
if (p == NULL)
return (void *)p;
return NULL;
}
void test_vector_push_back_failed_allocations()
{
vector *v = vector_alloc (null_cpy, int_ptr_cmp, int_ptr_free);
if (v == NULL)
exit (EXIT_FAILURE);
for (int i = 1; i <= 10; ++i)
{
int *p = &i;
assert(vector_push_back (v, &p) == 0);
assert(v->size == 0);
}
vector_free (&v);
}
void test_vector_push_back ()
{
test_vector_push_back_failed_allocations();
vector *v = vector_alloc (int_ptr_cpy, int_ptr_cmp, int_ptr_free);
if (v == NULL)
exit (EXIT_FAILURE);
// Check pointers are inserted correctly
for (int i = 1; i <= 10; ++i)
{
int *p = &i;
assert(vector_push_back (v, &p) == 1);
assert(v->size == (size_t) i);
}
// Check that NULL can be inserted correctly **as a value**
int *a = NULL;
assert(vector_push_back (v, &a) == 1);
assert(v->size == 11);
// Check that NULL cannot be inserted directly
assert(vector_push_back (v, NULL) == 0);
assert(v->size == 11);
assert(vector_push_back (NULL, &a) == 0);
assert(v->size == 11);
// Check that the vector resizes correctly
int b = 5;
a = &b;
while (vector_get_load_factor (v) < VECTOR_MAX_LOAD_FACTOR)
{
assert(v->capacity == VECTOR_INITIAL_CAP);
assert(vector_push_back (v, &a) == 1);
}
assert(vector_push_back (v, &a) == 1);
assert(v->capacity == VECTOR_INITIAL_CAP * VECTOR_GROWTH_FACTOR);
vector_free (&v);
}
void test_vector_get_load_factor ()
{
vector *v = vector_alloc (int_ptr_cpy, int_ptr_cmp, int_ptr_free);
if (v == NULL)
exit (EXIT_FAILURE);
assert(vector_get_load_factor (NULL) == -1);
for (int i = 1; i <= 10; ++i)
{
int *p = &i;
vector_push_back (v, &p);
assert(vector_get_load_factor (v)
== ((double) (i) / (double) VECTOR_INITIAL_CAP));
}
vector_free (&v);
}
void test_vector_at (void)
{
vector *v = vector_alloc (int_cpy, int_cmp, int_free);
if (v == NULL)
exit (EXIT_FAILURE);
for (int i = 0; i < 10; ++i)
{
vector_push_back (v, &i);
}
for (int i = 0; i < 10; ++i)
{
assert(*((int *) vector_at (v, i)) == i);
}
// Check invalid indexes
assert(vector_at (v, -1) == NULL);
assert(vector_at (NULL, 0) == NULL);
assert(vector_at (v, v->size) == NULL);
vector_free (&v);
}
void test_vector_find ()
{
vector *v = vector_alloc (int_ptr_cpy, int_ptr_cmp, int_ptr_free);
if (v == NULL)
exit (EXIT_FAILURE);
for (int i = 1; i <= 10; ++i)
{
int *p = &i;
vector_push_back (v, &p);
}
// Check that NULL can be found
int *a = NULL;
vector_push_back (v, &a);
assert(vector_find (v, &a) == 10);
vector_free (&v);
v = vector_alloc (int_cpy, int_cmp, int_free);
if (v == NULL)
exit (EXIT_FAILURE);
for (int i = 0; i < 10; ++i)
{
vector_push_back (v, &i);
}
for (int i = 0; i < 10; ++i)
{
assert(vector_find (v, &i) == i);
}
// Check invalid values
int t = 5;
assert(vector_find (NULL, &t) == -1);
t = -1;
char x[] = "This should not be found";
assert(vector_find (v, &t) == -1);
assert(vector_find (v, NULL) == -1);
assert(vector_find (v, x) == -1);
vector_free (&v);
}
void test_vector_erase (void)
{
vector *v = vector_alloc (int_cpy, int_cmp, int_free);
if (v == NULL)
exit (EXIT_FAILURE);
for (int i = 0; i < 7; ++i)
{
vector_push_back (v, &i);
}
assert(vector_erase (v, 0) == 1);
assert(vector_erase (v, 0) == 1);
assert(v->size == 5);
for (int i = 0; i < 5; ++i)
{
assert(*((int *) vector_at (v, i)) == i + 2);
}
int i = 0;
assert(vector_find (v, &i) == -1);
i = 1;
assert(vector_find (v, &i) == -1);
// Check invalid indexes
assert(vector_erase (v, -1) == 0);
assert(vector_erase (NULL, 0) == 0);
assert(v->size == 5);
assert(vector_erase (v, v->size) == 0);
assert(v->size == 5);
// Check vector changes size properly
while (vector_get_load_factor (v) > VECTOR_MIN_LOAD_FACTOR)
{
assert(v->capacity == VECTOR_INITIAL_CAP);
assert(vector_erase (v, 0) == 1);
}
assert(vector_erase (v, 0) == 1);
assert(v->capacity == VECTOR_INITIAL_CAP / VECTOR_GROWTH_FACTOR);
vector_free (&v);
}
void test_vector_clear (void)
{
vector *v = vector_alloc (int_cpy, int_cmp, int_free);
if (v == NULL)
exit (EXIT_FAILURE);
for (int i = 0; i < 7; ++i)
{
vector_push_back (v, &i);
}
// Just to make sure the program does not crash
vector_clear (NULL);
vector_clear (v);
assert(v->size == 0);
assert(vector_erase (v, 0) == 0);
assert(vector_at (v, 0) == NULL);
int i = 0;
assert(vector_find (v, &i) == -1);
assert(vector_push_back(v, &i) == 1);
vector_free (&v);
}
int main ()
{
printf ("Vector tests start:\n\n");
test_vector_alloc ();
printf ("Passed alloc test.\n");
test_vector_get_load_factor ();
printf ("Passed get_load_factor test.\n");
test_vector_push_back ();
printf ("Passed push_back test.\n");
test_vector_at ();
printf ("Passed vector_at test.\n");
test_vector_find ();
printf ("Passed vector_find test.\n");
test_vector_erase ();
printf ("Passed vector_erase test.\n");
test_vector_clear ();
printf ("Passed vector_clear test.\n");
printf ("\nPassed all tests :)\n");
return 0;
}