summaryrefslogtreecommitdiffstats
path: root/tests/list.c
blob: 2184dd79c399bc4d2b98875027fcc7783c799d15 (plain) (blame)
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
#include "common/FFlist.h"
#include "common/textModifier.h"

#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdnoreturn.h>

noreturn static void testFailed(const FFlist* list, const char* expression, int lineNo) {
    fputs(FASTFETCH_TEXT_MODIFIER_ERROR, stderr);
    fprintf(stderr, "[%d] %s, list:", lineNo, expression);
    for (uint32_t i = 0; i < list->length; ++i) {
        fprintf(stderr, "%u ", *FF_LIST_GET(uint32_t, *list, i));
    }
    fputc('\n', stderr);
    fputs(FASTFETCH_TEXT_MODIFIER_RESET, stderr);
    fputc('\n', stderr);
    exit(1);
}

static bool numEqualsAdapter(const uint32_t* first, const uint32_t* second) {
    return *first == *second;
}

#define VERIFY(expression) \
    if (!(expression)) testFailed(&list, #expression, __LINE__)

int main(void) {
    FFlist list;
    uint32_t n = 0;

    // initA

    ffListInit(&list);

    VERIFY(list.capacity == 0);
    VERIFY(list.length == 0);

    // forEach
    FF_LIST_FOR_EACH (uint32_t, item, list) {
        VERIFY(false);
    }

    // shift
    VERIFY(!FF_LIST_SHIFT(list, &n));
    VERIFY(list.length == 0);

    // pop
    VERIFY(!FF_LIST_POP(list, &n));
    VERIFY(list.length == 0);

    // add
    for (uint32_t i = 1; i <= FF_LIST_DEFAULT_ALLOC + 1; ++i) {
        *FF_LIST_ADD(uint32_t, list) = i;

        VERIFY(list.length == i);

        if (i <= FF_LIST_DEFAULT_ALLOC) {
            VERIFY(list.capacity == FF_LIST_DEFAULT_ALLOC);
        } else {
            VERIFY(list.capacity == FF_LIST_DEFAULT_ALLOC * 2);
        }

        VERIFY(*FF_LIST_GET(uint32_t, list, 0) == 1);
        VERIFY(*FF_LIST_GET(uint32_t, list, i - 1) == i);
    }
    VERIFY(list.length == FF_LIST_DEFAULT_ALLOC + 1);

    uint32_t sum = 0;
    // forEach
    FF_LIST_FOR_EACH (uint32_t, item, list) {
        sum += *item;
    }
    VERIFY(sum == (1 + FF_LIST_DEFAULT_ALLOC + 1) * (FF_LIST_DEFAULT_ALLOC + 1) / 2);

    // ffListFirstIndexComp
    n = 10;
    VERIFY(ffListFirstIndexComp(&list, sizeof(n), &n, (void*) numEqualsAdapter) == 9);
    n = 999;
    VERIFY(ffListFirstIndexComp(&list, sizeof(n), &n, (void*) numEqualsAdapter) == list.length);

    // ffListContains
    n = 10;
    VERIFY(FF_LIST_CONTAINS(list, &n, numEqualsAdapter));
    n = 999;
    VERIFY(!FF_LIST_CONTAINS(list, &n, numEqualsAdapter));

    // shift
    VERIFY(FF_LIST_SHIFT(list, &n));
    VERIFY(n == 1);
    VERIFY(list.length == FF_LIST_DEFAULT_ALLOC);
    VERIFY(*FF_LIST_GET(uint32_t, list, 0) == 2);
    VERIFY(*FF_LIST_GET(uint32_t, list, list.length - 1) == FF_LIST_DEFAULT_ALLOC + 1);

    // pop
    VERIFY(FF_LIST_POP(list, &n));
    VERIFY(n == FF_LIST_DEFAULT_ALLOC + 1);
    VERIFY(list.length == FF_LIST_DEFAULT_ALLOC - 1);
    VERIFY(*FF_LIST_GET(uint32_t, list, 0) == 2);
    VERIFY(*FF_LIST_GET(uint32_t, list, list.length - 1) == FF_LIST_DEFAULT_ALLOC);

    // Destroy
    ffListDestroy(&list);

    VERIFY(list.capacity == 0);
    VERIFY(list.length == 0);

    {
        FF_LIST_AUTO_DESTROY test = ffListCreate();
        VERIFY(test.capacity == 0);
        VERIFY(test.length == 0);
    }

    // Success
    puts("\033[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET);
}