Jakub Czapiga | ea378cc | 2020-11-12 12:10:51 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <tests/test.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
Maximilian Brune | a99b580 | 2023-09-16 19:56:45 +0200 | [diff] [blame] | 6 | #include <commonlib/list.h> |
Jakub Czapiga | ea378cc | 2020-11-12 12:10:51 +0100 | [diff] [blame] | 7 | |
| 8 | struct test_container { |
| 9 | int value; |
| 10 | |
| 11 | struct list_node list_node; |
| 12 | }; |
| 13 | |
| 14 | void test_list_insert_after(void **state) |
| 15 | { |
| 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 */ |
| 23 | |
| 24 | memset(c1, 0, sizeof(*c1)); |
| 25 | memset(c2, 0, sizeof(*c2)); |
| 26 | memset(c2, 0, sizeof(*c3)); |
| 27 | |
| 28 | c1->value = values[0]; |
| 29 | c2->value = values[1]; |
| 30 | c3->value = values[2]; |
| 31 | |
| 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); |
| 35 | |
| 36 | list_for_each(ptr, root, list_node) { |
| 37 | assert_int_equal(values[i], ptr->value); |
| 38 | i++; |
| 39 | } |
| 40 | |
| 41 | assert_int_equal(3, i); |
| 42 | |
| 43 | free(c3); |
| 44 | free(c2); |
| 45 | free(c1); |
| 46 | } |
| 47 | |
| 48 | void test_list_insert_before(void **state) |
| 49 | { |
| 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 */ |
| 57 | |
| 58 | memset(c1, 0, sizeof(*c1)); |
| 59 | memset(c2, 0, sizeof(*c2)); |
| 60 | memset(c2, 0, sizeof(*c3)); |
| 61 | |
| 62 | c1->value = values[0]; |
| 63 | c2->value = values[1]; |
| 64 | c3->value = values[2]; |
| 65 | |
| 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); |
| 69 | |
| 70 | |
| 71 | list_for_each(ptr, root, list_node) { |
| 72 | assert_int_equal(values[i], ptr->value); |
| 73 | i++; |
| 74 | } |
| 75 | |
| 76 | assert_int_equal(3, i); |
| 77 | |
| 78 | free(c3); |
| 79 | free(c2); |
| 80 | free(c1); |
| 81 | } |
| 82 | |
| 83 | void test_list_remove(void **state) |
| 84 | { |
| 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; |
| 90 | |
| 91 | list_insert_after(&c1->list_node, &root); |
| 92 | list_insert_after(&c2->list_node, &c1->list_node); |
| 93 | |
| 94 | len = 0; |
| 95 | list_for_each(ptr, root, list_node) { |
| 96 | len++; |
| 97 | } |
| 98 | assert_int_equal(2, len); |
| 99 | |
| 100 | list_remove(&c1->list_node); |
| 101 | |
| 102 | len = 0; |
| 103 | list_for_each(ptr, root, list_node) { |
| 104 | len++; |
| 105 | } |
| 106 | assert_int_equal(1, len); |
| 107 | |
| 108 | list_remove(&c2->list_node); |
| 109 | len = 0; |
| 110 | list_for_each(ptr, root, list_node) { |
| 111 | len++; |
| 112 | } |
| 113 | assert_int_equal(0, len); |
| 114 | |
| 115 | free(c2); |
| 116 | free(c1); |
| 117 | } |
| 118 | |
Raul E Rangel | 4c8c844 | 2021-11-01 13:40:14 -0600 | [diff] [blame] | 119 | void test_list_append(void **state) |
| 120 | { |
| 121 | size_t idx; |
| 122 | struct test_container *node; |
| 123 | struct list_node root = {}; |
| 124 | struct test_container nodes[] = { |
| 125 | {1}, {2}, {3} |
| 126 | }; |
| 127 | |
| 128 | for (idx = 0; idx < ARRAY_SIZE(nodes); ++idx) |
| 129 | list_append(&nodes[idx].list_node, &root); |
| 130 | |
| 131 | idx = 0; |
| 132 | list_for_each(node, root, list_node) { |
| 133 | assert_ptr_equal(node, &nodes[idx]); |
| 134 | idx++; |
| 135 | } |
| 136 | } |
| 137 | |
Jakub Czapiga | ea378cc | 2020-11-12 12:10:51 +0100 | [diff] [blame] | 138 | int main(void) |
| 139 | { |
| 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), |
Raul E Rangel | 4c8c844 | 2021-11-01 13:40:14 -0600 | [diff] [blame] | 144 | cmocka_unit_test(test_list_append), |
Jakub Czapiga | ea378cc | 2020-11-12 12:10:51 +0100 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | |
Jakub Czapiga | 7c6081e | 2021-08-25 16:27:35 +0200 | [diff] [blame] | 148 | return cb_run_group_tests(tests, NULL, NULL); |
Jakub Czapiga | ea378cc | 2020-11-12 12:10:51 +0100 | [diff] [blame] | 149 | } |