1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <tests/test.h>
8 struct test_container
{
11 struct list_node list_node
;
14 void test_list_insert_after(void **state
)
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
);
41 assert_int_equal(3, i
);
48 void test_list_insert_before(void **state
)
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
);
76 assert_int_equal(3, i
);
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
;
91 list_insert_after(&c1
->list_node
, &root
);
92 list_insert_after(&c2
->list_node
, &c1
->list_node
);
95 list_for_each(ptr
, root
, list_node
) {
98 assert_int_equal(2, len
);
100 list_remove(&c1
->list_node
);
103 list_for_each(ptr
, root
, list_node
) {
106 assert_int_equal(1, len
);
108 list_remove(&c2
->list_node
);
110 list_for_each(ptr
, root
, list_node
) {
113 assert_int_equal(0, len
);
119 void test_list_append(void **state
)
122 struct test_container
*node
;
123 struct list_node root
= {};
124 struct test_container nodes
[] = {
128 for (idx
= 0; idx
< ARRAY_SIZE(nodes
); ++idx
)
129 list_append(&nodes
[idx
].list_node
, &root
);
132 list_for_each(node
, root
, list_node
) {
133 assert_ptr_equal(node
, &nodes
[idx
]);
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
);