MAINTAINERS: Add Maximilian Brune to RISC-V
[coreboot.git] / tests / commonlib / list-test.c
blob4ca48a468fe955bf685ff91b3c78ff5ca419bd84
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <tests/test.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <commonlib/list.h>
8 struct test_container {
9 int value;
11 struct list_node list_node;
14 void test_list_insert_after(void **state)
16 int i = 0;
17 struct list_node root = { .prev = NULL, .next = NULL };
18 struct test_container *c1 = (struct test_container *)malloc(sizeof(*c1));
19 struct test_container *c2 = (struct test_container *)malloc(sizeof(*c2));
20 struct test_container *c3 = (struct test_container *)malloc(sizeof(*c2));
21 struct test_container *ptr;
22 const int values[] = { 5, 10, 13 }; /* Random values */
24 memset(c1, 0, sizeof(*c1));
25 memset(c2, 0, sizeof(*c2));
26 memset(c2, 0, sizeof(*c3));
28 c1->value = values[0];
29 c2->value = values[1];
30 c3->value = values[2];
32 list_insert_after(&c1->list_node, &root);
33 list_insert_after(&c2->list_node, &c1->list_node);
34 list_insert_after(&c3->list_node, &c2->list_node);
36 list_for_each(ptr, root, list_node) {
37 assert_int_equal(values[i], ptr->value);
38 i++;
41 assert_int_equal(3, i);
43 free(c3);
44 free(c2);
45 free(c1);
48 void test_list_insert_before(void **state)
50 int i = 0;
51 struct list_node root = { .prev = NULL, .next = NULL };
52 struct test_container *c1 = (struct test_container *)malloc(sizeof(*c1));
53 struct test_container *c2 = (struct test_container *)malloc(sizeof(*c2));
54 struct test_container *c3 = (struct test_container *)malloc(sizeof(*c2));
55 struct test_container *ptr;
56 const int values[] = { 19, 71, 991 }; /* Random values */
58 memset(c1, 0, sizeof(*c1));
59 memset(c2, 0, sizeof(*c2));
60 memset(c2, 0, sizeof(*c3));
62 c1->value = values[0];
63 c2->value = values[1];
64 c3->value = values[2];
66 list_insert_after(&c3->list_node, &root);
67 list_insert_before(&c2->list_node, &c3->list_node);
68 list_insert_before(&c1->list_node, &c2->list_node);
71 list_for_each(ptr, root, list_node) {
72 assert_int_equal(values[i], ptr->value);
73 i++;
76 assert_int_equal(3, i);
78 free(c3);
79 free(c2);
80 free(c1);
83 void test_list_remove(void **state)
85 struct list_node root = { .prev = NULL, .next = NULL };
86 struct test_container *c1 = (struct test_container *)malloc(sizeof(*c1));
87 struct test_container *c2 = (struct test_container *)malloc(sizeof(*c2));
88 struct test_container *ptr;
89 int len;
91 list_insert_after(&c1->list_node, &root);
92 list_insert_after(&c2->list_node, &c1->list_node);
94 len = 0;
95 list_for_each(ptr, root, list_node) {
96 len++;
98 assert_int_equal(2, len);
100 list_remove(&c1->list_node);
102 len = 0;
103 list_for_each(ptr, root, list_node) {
104 len++;
106 assert_int_equal(1, len);
108 list_remove(&c2->list_node);
109 len = 0;
110 list_for_each(ptr, root, list_node) {
111 len++;
113 assert_int_equal(0, len);
115 free(c2);
116 free(c1);
119 void test_list_append(void **state)
121 size_t idx;
122 struct test_container *node;
123 struct list_node root = {};
124 struct test_container nodes[] = {
125 {1}, {2}, {3}
128 for (idx = 0; idx < ARRAY_SIZE(nodes); ++idx)
129 list_append(&nodes[idx].list_node, &root);
131 idx = 0;
132 list_for_each(node, root, list_node) {
133 assert_ptr_equal(node, &nodes[idx]);
134 idx++;
138 int main(void)
140 const struct CMUnitTest tests[] = {
141 cmocka_unit_test(test_list_insert_after),
142 cmocka_unit_test(test_list_insert_before),
143 cmocka_unit_test(test_list_remove),
144 cmocka_unit_test(test_list_append),
148 return cb_run_group_tests(tests, NULL, NULL);