-
Notifications
You must be signed in to change notification settings - Fork 737
/
Copy pathtest-arraylist.c
425 lines (311 loc) · 9.99 KB
/
test-arraylist.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
Copyright (c) 2005-2008, Simon Howard
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* ArrayList test cases */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "alloc-testing.h"
#include "framework.h"
#include "arraylist.h"
#include "compare-int.h"
int variable1, variable2, variable3, variable4;
ArrayList *generate_arraylist(void)
{
ArrayList *arraylist;
int i;
arraylist = arraylist_new(0);
for (i = 0; i < 4; ++i) {
arraylist_append(arraylist, &variable1);
arraylist_append(arraylist, &variable2);
arraylist_append(arraylist, &variable3);
arraylist_append(arraylist, &variable4);
}
return arraylist;
}
void test_arraylist_new_free(void)
{
ArrayList *arraylist;
/* Use a default size when given zero */
arraylist = arraylist_new(0);
assert(arraylist != NULL);
arraylist_free(arraylist);
/* Normal allocated */
arraylist = arraylist_new(10);
assert(arraylist != NULL);
arraylist_free(arraylist);
/* Freeing a null arraylist works */
arraylist_free(NULL);
/* Test low memory scenarios (failed malloc) */
alloc_test_set_limit(0);
arraylist = arraylist_new(0);
assert(arraylist == NULL);
alloc_test_set_limit(1);
arraylist = arraylist_new(100);
assert(arraylist == NULL);
}
void test_arraylist_append(void)
{
ArrayList *arraylist;
int i;
arraylist = arraylist_new(0);
assert(arraylist->length == 0);
/* Append some entries */
assert(arraylist_append(arraylist, &variable1) != 0);
assert(arraylist->length == 1);
assert(arraylist_append(arraylist, &variable2) != 0);
assert(arraylist->length == 2);
assert(arraylist_append(arraylist, &variable3) != 0);
assert(arraylist->length == 3);
assert(arraylist_append(arraylist, &variable4) != 0);
assert(arraylist->length == 4);
assert(arraylist->data[0] == &variable1);
assert(arraylist->data[1] == &variable2);
assert(arraylist->data[2] == &variable3);
assert(arraylist->data[3] == &variable4);
/* Test appending many entries */
for (i = 0; i < 10000; ++i) {
assert(arraylist_append(arraylist, NULL) != 0);
}
arraylist_free(arraylist);
/* Test low memory scenario */
arraylist = arraylist_new(100);
alloc_test_set_limit(0);
for (i = 0; i < 100; ++i) {
assert(arraylist_append(arraylist, NULL) != 0);
}
assert(arraylist->length == 100);
assert(arraylist_append(arraylist, NULL) == 0);
assert(arraylist->length == 100);
arraylist_free(arraylist);
}
void test_arraylist_prepend(void)
{
ArrayList *arraylist;
int i;
arraylist = arraylist_new(0);
assert(arraylist->length == 0);
/* Append some entries */
assert(arraylist_prepend(arraylist, &variable1) != 0);
assert(arraylist->length == 1);
assert(arraylist_prepend(arraylist, &variable2) != 0);
assert(arraylist->length == 2);
assert(arraylist_prepend(arraylist, &variable3) != 0);
assert(arraylist->length == 3);
assert(arraylist_prepend(arraylist, &variable4) != 0);
assert(arraylist->length == 4);
assert(arraylist->data[0] == &variable4);
assert(arraylist->data[1] == &variable3);
assert(arraylist->data[2] == &variable2);
assert(arraylist->data[3] == &variable1);
/* Test prepending many entries */
for (i = 0; i < 10000; ++i) {
assert(arraylist_prepend(arraylist, NULL) != 0);
}
arraylist_free(arraylist);
/* Test low memory scenario */
arraylist = arraylist_new(100);
alloc_test_set_limit(0);
for (i = 0; i < 100; ++i) {
assert(arraylist_prepend(arraylist, NULL) != 0);
}
assert(arraylist->length == 100);
assert(arraylist_prepend(arraylist, NULL) == 0);
assert(arraylist->length == 100);
arraylist_free(arraylist);
}
void test_arraylist_insert(void)
{
ArrayList *arraylist;
int i;
arraylist = generate_arraylist();
/* Check for out of range insert */
assert(arraylist->length == 16);
assert(arraylist_insert(arraylist, 17, &variable1) == 0);
assert(arraylist->length == 16);
/* Insert a new entry at index 5 */
assert(arraylist->length == 16);
assert(arraylist->data[4] == &variable1);
assert(arraylist->data[5] == &variable2);
assert(arraylist->data[6] == &variable3);
assert(arraylist_insert(arraylist, 5, &variable4) != 0);
assert(arraylist->length == 17);
assert(arraylist->data[4] == &variable1);
assert(arraylist->data[5] == &variable4);
assert(arraylist->data[6] == &variable2);
assert(arraylist->data[7] == &variable3);
/* Inserting at the start */
assert(arraylist->data[0] == &variable1);
assert(arraylist->data[1] == &variable2);
assert(arraylist->data[2] == &variable3);
assert(arraylist_insert(arraylist, 0, &variable4) != 0);
assert(arraylist->length == 18);
assert(arraylist->data[0] == &variable4);
assert(arraylist->data[1] == &variable1);
assert(arraylist->data[2] == &variable2);
assert(arraylist->data[3] == &variable3);
/* Inserting at the end */
assert(arraylist->data[15] == &variable2);
assert(arraylist->data[16] == &variable3);
assert(arraylist->data[17] == &variable4);
assert(arraylist_insert(arraylist, 18, &variable1) != 0);
assert(arraylist->length == 19);
assert(arraylist->data[15] == &variable2);
assert(arraylist->data[16] == &variable3);
assert(arraylist->data[17] == &variable4);
assert(arraylist->data[18] == &variable1);
/* Test inserting many entries */
for (i = 0; i < 10000; ++i) {
arraylist_insert(arraylist, 10, &variable1);
}
arraylist_free(arraylist);
}
void test_arraylist_remove_range(void)
{
ArrayList *arraylist;
arraylist = generate_arraylist();
assert(arraylist->length == 16);
assert(arraylist->data[3] == &variable4);
assert(arraylist->data[4] == &variable1);
assert(arraylist->data[5] == &variable2);
assert(arraylist->data[6] == &variable3);
arraylist_remove_range(arraylist, 4, 3);
assert(arraylist->length == 13);
assert(arraylist->data[3] == &variable4);
assert(arraylist->data[4] == &variable4);
assert(arraylist->data[5] == &variable1);
assert(arraylist->data[6] == &variable2);
/* Try some invalid ones and check they don't do anything */
arraylist_remove_range(arraylist, 10, 10);
arraylist_remove_range(arraylist, 0, 16);
assert(arraylist->length == 13);
arraylist_free(arraylist);
}
void test_arraylist_remove(void)
{
ArrayList *arraylist;
arraylist = generate_arraylist();
assert(arraylist->length == 16);
assert(arraylist->data[3] == &variable4);
assert(arraylist->data[4] == &variable1);
assert(arraylist->data[5] == &variable2);
assert(arraylist->data[6] == &variable3);
arraylist_remove(arraylist, 4);
assert(arraylist->length == 15);
assert(arraylist->data[3] == &variable4);
assert(arraylist->data[4] == &variable2);
assert(arraylist->data[5] == &variable3);
assert(arraylist->data[6] == &variable4);
/* Try some invalid removes */
arraylist_remove(arraylist, 15);
assert(arraylist->length == 15);
arraylist_free(arraylist);
}
void test_arraylist_index_of(void)
{
int entries[] = {89, 4, 23, 42, 16, 15, 8, 99, 50, 30};
int num_entries;
ArrayList *arraylist;
int i;
int index;
int val;
/* Generate an arraylist containing the entries in the array */
num_entries = sizeof(entries) / sizeof(int);
arraylist = arraylist_new(0);
for (i = 0; i < num_entries; ++i) {
arraylist_append(arraylist, &entries[i]);
}
/* Check all values get found correctly */
for (i = 0; i < num_entries; ++i) {
val = entries[i];
index = arraylist_index_of(arraylist, int_equal, &val);
assert(index == i);
}
/* Check invalid values */
val = 0;
assert(arraylist_index_of(arraylist, int_equal, &val) < 0);
val = 57;
assert(arraylist_index_of(arraylist, int_equal, &val) < 0);
arraylist_free(arraylist);
}
void test_arraylist_clear(void)
{
ArrayList *arraylist;
arraylist = arraylist_new(0);
/* Emptying an already-empty arraylist */
arraylist_clear(arraylist);
assert(arraylist->length == 0);
/* Add some items and then empty it */
arraylist_append(arraylist, &variable1);
arraylist_append(arraylist, &variable2);
arraylist_append(arraylist, &variable3);
arraylist_append(arraylist, &variable4);
arraylist_clear(arraylist);
assert(arraylist->length == 0);
arraylist_free(arraylist);
}
void test_arraylist_sort(void)
{
ArrayList *arraylist;
int entries[] = {89, 4, 23, 42, 4, 16, 15, 4, 8, 99, 50, 30, 4};
int sorted[] = {4, 4, 4, 4, 8, 15, 16, 23, 30, 42, 50, 89, 99};
unsigned int num_entries = sizeof(entries) / sizeof(int);
unsigned int i;
arraylist = arraylist_new(10);
for (i = 0; i < num_entries; ++i) {
arraylist_prepend(arraylist, &entries[i]);
}
arraylist_sort(arraylist, int_compare);
/* List length is unchanged */
assert(arraylist->length == num_entries);
/* Check the list is sorted */
for (i = 0; i < num_entries; ++i) {
int *value;
value = (int *) arraylist->data[i];
assert(*value == sorted[i]);
}
arraylist_free(arraylist);
/* Check sorting an empty list */
arraylist = arraylist_new(5);
arraylist_sort(arraylist, int_compare);
assert(arraylist->length == 0);
arraylist_free(arraylist);
/* Check sorting a list with 1 entry */
arraylist = arraylist_new(5);
arraylist_prepend(arraylist, &entries[0]);
arraylist_sort(arraylist, int_compare);
assert(arraylist->length == 1);
assert(arraylist->data[0] == &entries[0]);
arraylist_free(arraylist);
}
/* clang-format off */
static UnitTestFunction tests[] = {
test_arraylist_new_free,
test_arraylist_append,
test_arraylist_prepend,
test_arraylist_insert,
test_arraylist_remove,
test_arraylist_remove_range,
test_arraylist_index_of,
test_arraylist_clear,
test_arraylist_sort,
NULL
};
/* clang-format on */
int main(int argc, char *argv[])
{
run_tests(tests);
return 0;
}