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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include "linked_list.h"

void _print_string(void* a_value) {
    char* str = a_value;
    // bad: printf(str);
    // as the caller might have unintentional format codes
    printf("%s", str);
}

int main(int argc, char* argv[]) {
    // linked list of size 0
    // NULL
    Node* head = NULL;
    Node* tail = NULL;
    print_linked_list(head, _print_string);
    assert(head == NULL);
    assert(tail == NULL);

    // linked list of size 1
    // [Generic]->NULL
    append("Generic", &head, &tail);
    print_linked_list(head, _print_string);
    assert(head != NULL);
    assert(tail != NULL);
    assert(head == tail);
    char* tail_value = tail->a_value;
    assert(strcmp(tail_value, "Generic") == 0);

    // linked list of size 2
    // [Generic]->[Hello]->NULL
    append("Hello", &head, &tail);
    print_linked_list(head, _print_string);
    assert(head != tail);
    tail_value = tail->a_value;
    assert(strcmp(tail_value, "Hello") == 0);

    // linked list of size 3
    // [Generic]->[Hello]->[World]->NULL
    append("World", &head, &tail);
    print_linked_list(head, _print_string);
    tail_value = tail->a_value;
    assert(strcmp(tail_value, "World") == 0);

    // linked list of size 4
    // [Generic]->[Hello]->[World]->[%d %s %q]->NULL
    append("%d %s %q", &head, &tail);
    print_linked_list(head, _print_string);
    tail_value = tail->a_value;
    assert(strcmp(tail_value, "%d %s %q") == 0);

    // a later node in a linked list is a valid linked list
    print_linked_list(head->next, _print_string);

    destroy_linked_list(&head, &tail);
    assert(head == NULL);
    assert(tail == NULL);

    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2022 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.