summaryrefslogtreecommitdiffstats
path: root/src/tests/slinklist-tests.c
blob: 4cbd90aad40f9abfdef7649baf7ef8cf7539b73f (plain)
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
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2013 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <check.h>
#include <stdlib.h>
#include "tests/common_check.h"
#include "util/slinklist.h"

struct slinklist {
    int id;
    struct slinklist *next;
};

START_TEST(test_slinklist_add_end)
{
    struct slinklist nodes[] = {{0, NULL}, {1, NULL}, {2, NULL}, {3, NULL}};
    struct slinklist *list = NULL;
    struct slinklist *node = NULL;
    int len = sizeof(nodes) / sizeof(struct slinklist);
    int i;

    for (i = 0; i != len; i++) {
        SLIST_ADD_END(list, &nodes[i], struct slinklist);
    }

    for (i = 0, node = list;
         i != len && node != NULL;
         i++, node = node->next) {
        fail_if(node->id != i, "Expected %d, got %d", i, node->id);
    }

    fail_if(i != len);
    fail_if(node != NULL);
}
END_TEST

START_TEST(test_slinklist_concatenate)
{
    struct slinklist nodes_a[] = {{0, NULL}, {1, NULL}};
    struct slinklist nodes_b[] = {{2, NULL}, {3, NULL}};
    struct slinklist *node = NULL;
    struct slinklist *list = NULL;
    struct slinklist *list_a = NULL;
    struct slinklist *list_b = NULL;
    int len = (sizeof(nodes_a) + sizeof(nodes_b)) / sizeof(struct slinklist);
    int i;

    list_a = &nodes_a[0];
    list_a->next = &nodes_a[1];

    list_b = &nodes_b[0];
    list_b->next = &nodes_b[1];

    SLIST_CONCATENATE(list, list_a, struct slinklist);
    fail_if(list != list_a);

    SLIST_CONCATENATE(list, list_b, struct slinklist);

    for (i = 0, node = list;
         i != len && node != NULL;
         i++, node = node->next) {
        fail_if(node->id != i, "Expected %d, got %d", i, node->id);
    }

    fail_if(i != len);
    fail_if(node != NULL);
}
END_TEST

Suite *slinklist_suite(void)
{
    Suite *s = suite_create("slinklist");

    TCase *tc_slinklist = tcase_create("slinklist");

    tcase_add_test(tc_slinklist, test_slinklist_add_end);
    tcase_add_test(tc_slinklist, test_slinklist_concatenate);
    tcase_set_timeout(tc_slinklist, 60);

    suite_add_tcase(s, tc_slinklist);

    return s;
}

int main(int argc, const char *argv[])
{
    int number_failed;

    tests_set_cwd();

    Suite *s = slinklist_suite();
    SRunner *sr = srunner_create(s);

    srunner_run_all(sr, CK_NORMAL);
    number_failed = srunner_ntests_failed(sr);
    srunner_free(sr);

    if (number_failed == 0)
        return EXIT_SUCCESS;

    return EXIT_FAILURE;
}