Repository navigation
Expand file tree
/
Copy pathlinked_list_test_program.c
More file actions
581 lines (487 loc) · 17.2 KB
/
Copy pathlinked_list_test_program.c
File metadata and controls
581 lines (487 loc) · 17.2 KB
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "linked_list.h"
#include "queue.h"
// Check that valid compiler defines have been passed in.
//
#ifdef TEST_LINKED_LIST
#define VALID_TEST
#endif
#ifdef TEST_QUEUE
#define VALID_TEST
#endif
#ifndef VALID_TEST
#error "Improper set of compiler defines specified, check Makefile"
#endif
#define TEST(x) printf("Running test " #x "\n"); fflush(stdout);
#define SUBTEST(x) printf(" Executing subtest " #x "\n"); fflush(stdout); \
alarm(1);
#define FAIL(cond, msg) if (cond) {\
printf(" FAIL! "); \
printf(#msg "\n"); \
exit(-1);\
}
#define PASS(x) printf("PASS!\n"); alarm(0);
bool instrumented_malloc_fail_next = false;
bool instrumented_malloc_last_alloc_successful = false;
void gracefully_exit_on_suspected_infinite_loop(int signal_number) {
// Use write() to tell the tester that they're probably stuck
// in an infinite loop.
//
// Why not printf()/fprintf()? It goes against POSIX rules for
// signal handlers to call a non-reentrant function, of which both
// of those are. I had no about that constraint prior to writing
// this function. Cool!
//
const char* err_msg = " Likely stuck in infinite loop! Exiting.\n";
ssize_t retval = write(STDOUT_FILENO, err_msg, strlen(err_msg));
fflush(stdout);
// We really don't care about whether write() succeeded or failed
// or whether a partial write occurred. Further, we only install
// this function to one signal handler, so we can ignore that as well.
//
(void)retval;
(void)signal_number;
// Exit.
//
exit(1);
}
void * instrumented_malloc(size_t size) {
if (instrumented_malloc_fail_next) {
instrumented_malloc_fail_next = false;
instrumented_malloc_last_alloc_successful = false;
return NULL;
}
void * ptr = malloc(size);
instrumented_malloc_last_alloc_successful = (ptr != NULL);
return ptr;
}
// Tests linked list and queue handling of being passed NULL pointers.
//
void check_null_handling(void) {
bool status = false;
#ifdef TEST_LINKED_LIST
TEST(linked_list_check_null_handling)
SUBTEST(linked_list_delete)
status = linked_list_delete(NULL);
FAIL(status != false,
"linked_list_delete(NULL) did not return false")
SUBTEST(linked_list_insert_front)
status = linked_list_insert_front(NULL, 0);
FAIL(status != false,
"linked_list_insert_front(NULL, 0) did not return false")
SUBTEST(linked_list_insert_end)
status = linked_list_insert_end(NULL, 0);
FAIL(status != false,
"linked_list_insert_end(NULL, 0) did not return false")
SUBTEST(linked_list_insert)
status = linked_list_insert(NULL, 0, 0);
FAIL(status != false,
"linked_list_insert(NULL, 0, 0) did not return false")
SUBTEST(linked_list_create_iterator)
struct iterator * iter = linked_list_create_iterator(NULL, 0);
FAIL(iter != NULL,
"linked_list_create_iterator(NULL, 0) did not return NULL")
SUBTEST(linked_list_delete_iterator)
status = linked_list_delete_iterator(NULL);
FAIL(status != false,
"linked_list_delete_iterator(NULL) did not return false")
SUBTEST(linked_list_iterate)
status = linked_list_iterate(NULL);
FAIL(status != false,
"linked_list_iterate(NULL) did not return false")
SUBTEST(linked_list_find)
size_t index = linked_list_find(NULL, 0);
FAIL(index != SIZE_MAX,
"linked_list_find(NULL, 0) did not return SIZE_MAX");
PASS(linked_list_check_null_handling)
#endif
#ifdef TEST_QUEUE
TEST(queue_check_null_handling)
SUBTEST(queue_delete)
status = queue_delete(NULL);
FAIL(status != false,
"queue_delete(NULL) did not return false");
SUBTEST(queue_push)
status = queue_push(NULL, 1);
FAIL(status != false,
"queue_push(NULL, 1) did not return false");
SUBTEST(queue_pop_1)
unsigned int popped_value = 7;
status = queue_pop(NULL, &popped_value);
FAIL(status != false,
"queue_pop(NULL, &popped_value) did not return false");
SUBTEST(queue_pop_2)
FAIL(popped_value != 7,
"queue_pop(NULL, &popped_value) modified the variable popped_value")
SUBTEST(queue_size)
size_t size = queue_size(NULL);
FAIL(size != SIZE_MAX,
"queue_size(NULL) did not return SIZE_MAX");
SUBTEST(queue_has_next)
status = queue_has_next(NULL);
FAIL(status != false,
"queue_has_next(NULL) did not return false");
SUBTEST(queue_next_1)
unsigned int next_value = 7;
status = queue_next(NULL, &next_value);
FAIL(status != false,
"queue_next(NULL, &next_value) did not return false")
SUBTEST(queue_next_2)
FAIL(next_value != 7,
"queue_next(NULL, &next_value) modified the variable next_value");
PASS(queue_check_null_handling)
#endif
}
void check_empty_list_and_queue_properties(void) {
#ifdef TEST_LINKED_LIST
TEST(check_empty_list_properties)
SUBTEST(linked_list_create)
struct linked_list * ll = linked_list_create();
// Sanity check that linked_list_create() works on memory allocation
// success.
//
FAIL((instrumented_malloc_last_alloc_successful && (ll == NULL)),
"linked_list_create() failed when malloc() returned a valid pointer")
// Check invariant that head is null when empty.
//
FAIL((ll->head != NULL),
"ll->head is non-null in empty linked_list");
linked_list_delete(ll);
// Force the memory allocator fail, ensure that NULL is returned.
//
SUBTEST(linked_list_memory_alloc_fail)
instrumented_malloc_fail_next = true;
ll = linked_list_create();
FAIL(ll != NULL,
"linked_list_create() returns non-null pointer on allocation failure")
// Attempt to create an iterator for index 0.
//
SUBTEST(empty_linked_list_iterator)
ll = linked_list_create();
struct iterator * iter = linked_list_create_iterator(ll, 0);
FAIL (iter != NULL,
"linked_list_create_iterator returned an iterator for an empty linked_list")
// Cleanup.
//
linked_list_delete_iterator(iter);
linked_list_delete(ll);
PASS(check_empty_list_properties)
#endif
#ifdef TEST_QUEUE
// Check empty queue properties.
//
TEST(check_empty_queue_properties)
SUBTEST(queue_create)
struct queue * queue = queue_create();
FAIL(queue == NULL,
"Failed to create queue.");
SUBTEST(queue_size)
size_t size = queue_size(queue);
FAIL(size != 0,
"queue_size() returned non-zero for empty queue.")
SUBTEST(queue_has_next)
bool status = queue_has_next(queue);
FAIL(status != false,
"queue_has_next() on empty queue did not return false")
SUBTEST(queue_pop)
unsigned int data = 0;
status = queue_pop(queue, &data);
FAIL(status != false,
"queue_pop() on empty queue did not return false")
queue_delete(queue);
PASS(check_empty_queue_properties)
#endif
}
void check_insertion_functionality(void) {
bool status = false;
#ifdef TEST_LINKED_LIST
TEST(check_linked_list_insertion_functionality)
SUBTEST(check_insert_end)
// Check insertion at end with an iterator.
// Inserts 1, 2, 3, 4 into the list, verifies
// data.
//
struct linked_list * ll = linked_list_create();
size_t ll_size = SIZE_MAX;
FAIL(ll == NULL,
"Failed to create new linked_list (#1)")
for (size_t i = 1; i <= 4; i++) {
bool status = linked_list_insert_end(ll, i);
FAIL(status == false,
"Failed to insert node into linked_list #1")
}
struct iterator * iter = linked_list_create_iterator(ll, 0);
FAIL(iter == NULL,
"Failed to create new iterator for linked_list (#1)")
SUBTEST(iterate_over_linked_list_1)
for (size_t i = 1; i <= 4; i++) {
FAIL(iter->data != i,
"Iterator does not contain correct data for linked_list (#1)")
FAIL(iter->current_index != (i - 1),
"Iterator does not contain correct index for linked_list (#1)")
// Next element.
//
linked_list_iterate(iter);
}
linked_list_delete(ll);
linked_list_delete_iterator(iter);
// Check insertion at front with an iterator.
// Inserts 4, 3, 2, 1 into the list, verifies data.
//
SUBTEST(check_insert_front)
ll = linked_list_create();
ll_size = linked_list_size(ll);
FAIL(ll_size != 0,
"linked_list (#2) size is non-zero when created")
FAIL(ll == NULL,
"Failed to create new linked_list (#2)")
for (size_t i = 4; i != 0; i--) {
bool status = linked_list_insert_front(ll, i);
FAIL(status == false,
"Failed to insert node into linked_list #2")
}
ll_size = linked_list_size(ll);
FAIL(ll_size != 4,
"linked_list (#2) size was not equal to 4")
SUBTEST(iterate_over_linked_list_2)
iter = linked_list_create_iterator(ll, 0);
for (size_t i = 1; i <= 4; i++) {
FAIL(iter->data != i,
"Iterator does not contain correct data for linked_list (#2)")
FAIL(iter->current_index != (i - 1),
"Iterator does not contain correct index for linked_list (#2)")
// Next element.
//
linked_list_iterate(iter);
}
ll_size = linked_list_size(ll);
FAIL(ll_size != 4,
"linked_list (#2) size was not equal to 4")
linked_list_delete(ll);
linked_list_delete_iterator(iter);
SUBTEST(check_insert_function_at_end)
// Checck that insertion of a single element at an out of bounds
// index works on an empty linked list.
//
ll = linked_list_create();
ll_size = linked_list_size(ll);
FAIL(ll_size != 0,
"linked_list (#3) size is non-zero when created")
FAIL(ll == NULL,
"Failed to create new linked_list (#3)")
FAIL(linked_list_insert(ll, 1, 0) != false,
"Inserted index in empty linked_list (#3) at index 1.")
// Check insertion at end, same 1-4 test.
//
for (size_t i = 0; i < 4 ; i++) {
bool status = linked_list_insert(ll, i, i + 1);
FAIL(status != true,
"linked_list_insert() failed for linked_list (#3) unexpectedly")
}
iter = linked_list_create_iterator(ll, 0);
for (size_t i = 1; i <= 4; i++) {
FAIL(iter->data != i,
"Iterator does not contain correct data for linked_list (#3)")
FAIL(iter->current_index != (i - 1),
"Iterator does not contain correct index for linked_list (#3)")
linked_list_iterate(iter);
}
linked_list_delete(ll);
linked_list_delete_iterator(iter);
SUBTEST(check_insert_function_at_beginning)
// Check insertion at front, same 4-1 test.
//
ll = linked_list_create();
// Not checking size at this point. It likely works.
//
FAIL(ll == NULL,
"Failed to create new linked_list (#4)")
for (size_t i = 4; i != 0; i--) {
bool status = linked_list_insert(ll, 0, i);
FAIL(status != true,
"linked_list_insert() failed for linked_list (#4) unexpectedly")
}
iter = linked_list_create_iterator(ll, 0);
for (size_t i = 1; i <= 4; i++) {
FAIL(iter->data != i,
"Iterator does not contain correct data for linked_list (#4)")
FAIL(iter->current_index != (i - 1),
"Iterator does not contain correct index for linked_list (#4)")
linked_list_iterate(iter);
}
linked_list_delete(ll);
linked_list_delete_iterator(iter);
SUBTEST(check_insert_function_at_middle)
// Insert 1,2,4 and then insert 3.
//
ll = linked_list_create();
FAIL(ll == NULL,
"Failed to create new linked_list (#5)")
status = linked_list_insert(ll, 0, 1);
FAIL(status == false,
"Failed to insert 1 at the beginning of linked_list (#5)")
status = linked_list_insert(ll, 1, 2);
FAIL(status == false,
"Failed to insert 2 at the end of linked_list (#5)")
status = linked_list_insert(ll, 2, 4);
FAIL(status == false,
"Failed to insert 4 at the end of linked_list (#5)")
status = linked_list_insert(ll, 2, 3);
FAIL(status == false,
"Failed to insert 3 at the middle of linked_list (#5)")
// Standard 1-4 check.
//
iter = linked_list_create_iterator(ll, 0);
for (size_t i = 1; i <= 4; i++) {
FAIL(iter->data != i,
"Iterator does not contain correct data for linked_list (#5)")
FAIL(iter->current_index != (i - 1),
"Iterator does not contain correct index for linked_list (#5)")
linked_list_iterate(iter);
}
linked_list_delete(ll);
linked_list_delete_iterator(iter);
PASS(check_linked_list_insertion_functionality)
#endif
#ifdef TEST_QUEUE
TEST(check_queue_push_functionality)
SUBTEST(queue_push)
struct queue * queue = queue_create();
FAIL(queue == NULL,
"Failed to create new queue.")
// Push 1 through 5 into the queue.
//
for (size_t i = 1; i <= 5; i++) {
bool status = queue_push(queue, i);
FAIL(status == false,
"Failed to push into queue.")
}
// Check size, has_next.
//
size_t size = queue_size(queue);
FAIL(size != 5,
"queue_size() did not return 5")
bool has_next = queue_has_next(queue);
FAIL(has_next == false,
"queue_has_next() did not return true on non-empty queue")
SUBTEST(queue_next_value)
unsigned int data = 0;
status = queue_next(queue, &data);
FAIL(status == false,
"queue_next() did not return true on non-empty queue")
FAIL(data != 1,
"Head of queue's data was not equal to 1")
PASS(check_queue_push_functionality)
TEST(queue_pop_values)
SUBTEST(pop_all_values)
for (size_t i = 1; i <= 5; i++) {
bool status = queue_pop(queue, &data);
FAIL(status == false,
"queue_pop() return false on non-empty queue")
FAIL(data != i,
"queue_pop() did not return correct data on pop")
}
SUBTEST(queue_empty_after_pop_checks)
size = queue_size(queue);
FAIL(size != 0,
"queue size non-zero after all values popped")
status = queue_has_next(queue);
FAIL(status != false,
"queue_has_next() returned true after all values popped from queue")
status = queue_pop(queue, &data);
FAIL(status == true,
"queue_pop() returned true after all values popped from queue")
status = queue_delete(queue);
FAIL(status == false,
"Failed to delete queue");
PASS(queue_pop_values)
#endif
}
void check_linked_list_find_functionality(void) {
#ifdef TEST_LINKED_LIST
TEST(check_linked_list_find_functionality)
// Single linked_list, find elements.
//
struct linked_list * ll = linked_list_create();
// Create list of ints 1 to 10.
//
for (size_t i = 0; i < 10; i++) {
linked_list_insert_end(ll, i + 1);
}
// Find beginning.
//
SUBTEST(find_beginning)
size_t index = linked_list_find(ll, 1);
FAIL(index != 0,
"Did not find 1 at beginning of linked_list")
// Find end.
//
SUBTEST(find_end)
index = linked_list_find(ll, 10);
FAIL(index != 9,
"Did not find 10 at end of linked_list")
// Find middle.
//
SUBTEST(find_middle)
index = linked_list_find(ll, 5);
FAIL(index != 4,
"Did not find 5 at end of linked_list")
// Find non-existant data
//
SUBTEST(find_nonexistant)
index = linked_list_find(ll, 11);
FAIL(index != SIZE_MAX,
"Found 11 when it is not in the linked_list")
linked_list_insert_end(ll, 6);
// Ensure the first instance of 6 is found.
//
SUBTEST(find_first_of_duplicate_data)
index = linked_list_find(ll, 6);
FAIL(index != 5,
"Failed to find 6, when duplicate added to end of linked_list")
linked_list_delete(ll);
PASS(check_linked_list_find_functionality)
#endif
}
void check_linked_list_additional_delete_tests(void) {
#ifdef TEST_LINKED_LIST
TEST(linked_list_additional_delete_tests)
SUBTEST(insert_and_delete_non_empty_linked_list)
// Add 5 values onto the list and delete it.
// Not expected that this will catch any functional
// failures, but it might catch memory leak ones.
//
struct linked_list * ll = linked_list_create();
for (size_t i = 1; i <= 5; i++) {
bool status = linked_list_insert_end(ll, i);
FAIL(status == false,
"linked_list_insert_end() failed.")
}
bool status = linked_list_delete(ll);
FAIL(status == false,
"Failed to delete non-empty linked_list.")
PASS(linked_list_additional_delete_tests)
#endif
}
int main(void) {
// Set up signal handler for catching infinite loops.
//
signal(SIGALRM, gracefully_exit_on_suspected_infinite_loop);
// Setup instrumented memory allocation/deallocation.
//
linked_list_register_malloc(&instrumented_malloc);
linked_list_register_free(&free);
queue_register_malloc(&instrumented_malloc);
queue_register_free(&free);
check_null_handling();
check_empty_list_and_queue_properties();
check_insertion_functionality();
check_linked_list_find_functionality();
check_linked_list_additional_delete_tests();
return 0;
}