|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright(c) 2025 Intel Corporation. All rights reserved. |
| 4 | +// |
| 5 | +// Author: GitHub Copilot |
| 6 | +// Submitted by: Tomasz Leman <tomasz.m.leman@intel.com> |
| 7 | + |
| 8 | +#include <zephyr/ztest.h> |
| 9 | +#include <sof/list.h> |
| 10 | + |
| 11 | +/** |
| 12 | + * @brief Test list_init functionality |
| 13 | + * |
| 14 | + * Tests that list.prev and list.next point to the list itself after initialization |
| 15 | + */ |
| 16 | +ZTEST(sof_list_suite, test_list_init) |
| 17 | +{ |
| 18 | + struct list_item list = {.prev = NULL, .next = NULL}; |
| 19 | + |
| 20 | + list_init(&list); |
| 21 | + |
| 22 | + /* Verify that prev and next pointers point to the list itself after initialization */ |
| 23 | + zassert_equal(&list, list.prev, "list.prev should point to itself after list_init"); |
| 24 | + zassert_equal(&list, list.next, "list.next should point to itself after list_init"); |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * @brief Test list_is_empty functionality |
| 29 | + * |
| 30 | + * Tests that list_is_empty returns true for empty lists and false for non-empty lists |
| 31 | + */ |
| 32 | +ZTEST(sof_list_suite, test_list_is_empty) |
| 33 | +{ |
| 34 | + struct list_item list; |
| 35 | + struct list_item item; |
| 36 | + |
| 37 | + /* Test when list is empty */ |
| 38 | + list_init(&list); |
| 39 | + zassert_true(list_is_empty(&list), "list_is_empty should return true for empty list"); |
| 40 | + |
| 41 | + /* Test when list is not empty */ |
| 42 | + list_init(&item); |
| 43 | + list_item_append(&item, &list); |
| 44 | + zassert_false(list_is_empty(&list), "list_is_empty should return false for non-empty list"); |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * @brief Test list_item_append functionality |
| 49 | + * |
| 50 | + * Tests that list_item_append correctly appends an item to the end of the list |
| 51 | + */ |
| 52 | +ZTEST(sof_list_suite, test_list_item_append) |
| 53 | +{ |
| 54 | + struct list_item head; |
| 55 | + struct list_item item1; |
| 56 | + struct list_item item2; |
| 57 | + |
| 58 | + /* Initialize list and items */ |
| 59 | + list_init(&head); |
| 60 | + list_init(&item1); |
| 61 | + list_init(&item2); |
| 62 | + |
| 63 | + /* Append first item */ |
| 64 | + list_item_append(&item1, &head); |
| 65 | + zassert_equal(&item1, head.next, "head->next should point to item1"); |
| 66 | + zassert_equal(&item1, head.prev, "head->prev should point to item1"); |
| 67 | + zassert_equal(&head, item1.next, "item1->next should point to head"); |
| 68 | + zassert_equal(&head, item1.prev, "item1->prev should point to head"); |
| 69 | + |
| 70 | + /* Append second item */ |
| 71 | + list_item_append(&item2, &head); |
| 72 | + zassert_equal(&item1, head.next, "head->next should still point to item1"); |
| 73 | + zassert_equal(&item2, head.prev, "head->prev should now point to item2"); |
| 74 | + zassert_equal(&item2, item1.next, "item1->next should now point to item2"); |
| 75 | + zassert_equal(&head, item1.prev, "item1->prev should still point to head"); |
| 76 | + zassert_equal(&head, item2.next, "item2->next should point to head"); |
| 77 | + zassert_equal(&item1, item2.prev, "item2->prev should point to item1"); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * @brief Test list_item_prepend functionality |
| 82 | + * |
| 83 | + * Tests that list_item_prepend correctly prepends an item to the beginning of the list |
| 84 | + */ |
| 85 | +ZTEST(sof_list_suite, test_list_item_prepend) |
| 86 | +{ |
| 87 | + struct list_item head; |
| 88 | + struct list_item item1; |
| 89 | + struct list_item item2; |
| 90 | + |
| 91 | + /* Initialize list and items */ |
| 92 | + list_init(&head); |
| 93 | + list_init(&item1); |
| 94 | + list_init(&item2); |
| 95 | + |
| 96 | + /* Prepend first item */ |
| 97 | + list_item_prepend(&item1, &head); |
| 98 | + zassert_equal(&item1, head.next, "head->next should point to item1"); |
| 99 | + zassert_equal(&item1, head.prev, "head->prev should point to item1"); |
| 100 | + zassert_equal(&head, item1.next, "item1->next should point to head"); |
| 101 | + zassert_equal(&head, item1.prev, "item1->prev should point to head"); |
| 102 | + |
| 103 | + /* Prepend second item */ |
| 104 | + list_item_prepend(&item2, &head); |
| 105 | + zassert_equal(&item2, head.next, "head->next should now point to item2"); |
| 106 | + zassert_equal(&item1, head.prev, "head->prev should still point to item1"); |
| 107 | + zassert_equal(&item1, item2.next, "item2->next should point to item1"); |
| 108 | + zassert_equal(&head, item2.prev, "item2->prev should point to head"); |
| 109 | + zassert_equal(&head, item1.next, "item1->next should still point to head"); |
| 110 | + zassert_equal(&item2, item1.prev, "item1->prev should now point to item2"); |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * @brief Test list_item_del functionality |
| 115 | + * |
| 116 | + * Tests that list_item_del correctly removes an item from a list |
| 117 | + */ |
| 118 | +ZTEST(sof_list_suite, test_list_item_del) |
| 119 | +{ |
| 120 | + struct list_item head; |
| 121 | + struct list_item item1; |
| 122 | + struct list_item item2; |
| 123 | + |
| 124 | + /* Initialize list and items */ |
| 125 | + list_init(&head); |
| 126 | + list_init(&item1); |
| 127 | + list_init(&item2); |
| 128 | + |
| 129 | + /* Add items to list */ |
| 130 | + list_item_append(&item1, &head); |
| 131 | + list_item_append(&item2, &head); |
| 132 | + |
| 133 | + /* Remove first item */ |
| 134 | + list_item_del(&item1); |
| 135 | + |
| 136 | + /* Check that item1 is properly removed and initialized */ |
| 137 | + zassert_equal(&item1, item1.next, "item1->next should point to itself after deletion"); |
| 138 | + zassert_equal(&item1, item1.prev, "item1->prev should point to itself after deletion"); |
| 139 | + |
| 140 | + /* Check that head and item2 are properly linked */ |
| 141 | + zassert_equal(&item2, head.next, "head->next should point to item2"); |
| 142 | + zassert_equal(&item2, head.prev, "head->prev should point to item2"); |
| 143 | + zassert_equal(&head, item2.next, "item2->next should point to head"); |
| 144 | + zassert_equal(&head, item2.prev, "item2->prev should point to head"); |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * @brief Test list_item_is_last functionality |
| 149 | + * |
| 150 | + * Tests that list_item_is_last correctly identifies the last item in a list |
| 151 | + */ |
| 152 | +ZTEST(sof_list_suite, test_list_item_is_last) |
| 153 | +{ |
| 154 | + struct list_item head; |
| 155 | + struct list_item item1; |
| 156 | + struct list_item item2; |
| 157 | + |
| 158 | + /* Initialize list and items */ |
| 159 | + list_init(&head); |
| 160 | + list_init(&item1); |
| 161 | + list_init(&item2); |
| 162 | + |
| 163 | + /* Add items to list */ |
| 164 | + list_item_append(&item1, &head); |
| 165 | + list_item_append(&item2, &head); |
| 166 | + |
| 167 | + /* Check item positions */ |
| 168 | + zassert_false(list_item_is_last(&item1, &head), |
| 169 | + "item1 should not be the last item in the list"); |
| 170 | + zassert_true(list_item_is_last(&item2, &head), |
| 171 | + "item2 should be the last item in the list"); |
| 172 | +} |
| 173 | + |
| 174 | +ZTEST_SUITE(sof_list_suite, NULL, NULL, NULL, NULL, NULL); |
0 commit comments