Skip to content

Commit 4f3be6a

Browse files
tmlemanlgirdwood
authored andcommitted
test: convert LIST unit tests from CMocka to Ztest framework
This commit converts the legacy LIST unit tests from the CMocka/Unity test framework to the Zephyr Ztest framework. The converted tests maintain the same test coverage while adhering to SOF and Zephyr coding standards. The test code was generated with assistance from GitHub Copilot AI. Test flow and test coverage scope is based on the existing CMocka tests located at sof/test/cmocka/src/list. All list operations are covered: - list_init - list_is_empty - list_item_append - list_item_prepend - list_item_del - list_item_is_last Tests can be run with: west twister --testsuite-root sof/test/ztest/unit/ --platform \ native_sim --verbose --inline-logs Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
1 parent c743cbc commit 4f3be6a

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
4+
project(test_list)
5+
6+
set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../..")
7+
8+
target_include_directories(app PRIVATE ${SOF_ROOT}/src/include)
9+
10+
target_sources(app PRIVATE test_list_ztest.c)

test/ztest/unit/list/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ZTEST=y
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
//
3+
// Copyright(c) 2025 Intel Corporation. All rights reserved.
4+
//
5+
// These contents may have been developed with support from one or more Intel-operated
6+
// generative artificial intelligence solutions.
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_item_append(&item, &list);
43+
zassert_false(list_is_empty(&list), "list_is_empty should return false for non-empty list");
44+
}
45+
46+
/**
47+
* @brief Test list_item_append functionality
48+
*
49+
* Tests that list_item_append correctly appends an item to the end of the list
50+
*/
51+
ZTEST(sof_list_suite, test_list_item_append)
52+
{
53+
struct list_item head;
54+
struct list_item item1;
55+
struct list_item item2;
56+
57+
/* Initialize list */
58+
list_init(&head);
59+
60+
/* Append first item */
61+
list_item_append(&item1, &head);
62+
zassert_equal(&item1, head.next, "head->next should point to item1");
63+
zassert_equal(&item1, head.prev, "head->prev should point to item1");
64+
zassert_equal(&head, item1.next, "item1->next should point to head");
65+
zassert_equal(&head, item1.prev, "item1->prev should point to head");
66+
67+
/* Append second item */
68+
list_item_append(&item2, &head);
69+
zassert_equal(&item1, head.next, "head->next should still point to item1");
70+
zassert_equal(&item2, head.prev, "head->prev should now point to item2");
71+
zassert_equal(&item2, item1.next, "item1->next should now point to item2");
72+
zassert_equal(&head, item1.prev, "item1->prev should still point to head");
73+
zassert_equal(&head, item2.next, "item2->next should point to head");
74+
zassert_equal(&item1, item2.prev, "item2->prev should point to item1");
75+
}
76+
77+
/**
78+
* @brief Test list_item_prepend functionality
79+
*
80+
* Tests that list_item_prepend correctly prepends an item to the beginning of the list
81+
*/
82+
ZTEST(sof_list_suite, test_list_item_prepend)
83+
{
84+
struct list_item head;
85+
struct list_item item1;
86+
struct list_item item2;
87+
88+
/* Initialize list */
89+
list_init(&head);
90+
91+
/* Prepend first item */
92+
list_item_prepend(&item1, &head);
93+
zassert_equal(&item1, head.next, "head->next should point to item1");
94+
zassert_equal(&item1, head.prev, "head->prev should point to item1");
95+
zassert_equal(&head, item1.next, "item1->next should point to head");
96+
zassert_equal(&head, item1.prev, "item1->prev should point to head");
97+
98+
/* Prepend second item */
99+
list_item_prepend(&item2, &head);
100+
zassert_equal(&item2, head.next, "head->next should now point to item2");
101+
zassert_equal(&item1, head.prev, "head->prev should still point to item1");
102+
zassert_equal(&item1, item2.next, "item2->next should point to item1");
103+
zassert_equal(&head, item2.prev, "item2->prev should point to head");
104+
zassert_equal(&head, item1.next, "item1->next should still point to head");
105+
zassert_equal(&item2, item1.prev, "item1->prev should now point to item2");
106+
}
107+
108+
/**
109+
* @brief Test list_item_del functionality
110+
*
111+
* Tests that list_item_del correctly removes an item from a list
112+
*/
113+
ZTEST(sof_list_suite, test_list_item_del)
114+
{
115+
struct list_item head;
116+
struct list_item item1;
117+
struct list_item item2;
118+
119+
/* Initialize list */
120+
list_init(&head);
121+
122+
/* Add items to list */
123+
list_item_append(&item1, &head);
124+
list_item_append(&item2, &head);
125+
126+
/* Remove first item */
127+
list_item_del(&item1);
128+
129+
/* Check that item1 is properly removed and initialized */
130+
zassert_equal(&item1, item1.next, "item1->next should point to itself after deletion");
131+
zassert_equal(&item1, item1.prev, "item1->prev should point to itself after deletion");
132+
133+
/* Check that head and item2 are properly linked */
134+
zassert_equal(&item2, head.next, "head->next should point to item2");
135+
zassert_equal(&item2, head.prev, "head->prev should point to item2");
136+
zassert_equal(&head, item2.next, "item2->next should point to head");
137+
zassert_equal(&head, item2.prev, "item2->prev should point to head");
138+
}
139+
140+
/**
141+
* @brief Test list_item_is_last functionality
142+
*
143+
* Tests that list_item_is_last correctly identifies the last item in a list
144+
*/
145+
ZTEST(sof_list_suite, test_list_item_is_last)
146+
{
147+
struct list_item head;
148+
struct list_item item1;
149+
struct list_item item2;
150+
151+
/* Initialize list */
152+
list_init(&head);
153+
154+
/* Add items to list */
155+
list_item_append(&item1, &head);
156+
list_item_append(&item2, &head);
157+
158+
/* Check item positions */
159+
zassert_false(list_item_is_last(&item1, &head),
160+
"item1 should not be the last item in the list");
161+
zassert_true(list_item_is_last(&item2, &head),
162+
"item2 should be the last item in the list");
163+
}
164+
165+
ZTEST_SUITE(sof_list_suite, NULL, NULL, NULL, NULL, NULL);

test/ztest/unit/list/testcase.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
#
3+
# Copyright(c) 2025 Intel Corporation. All rights reserved.
4+
#
5+
# These contents may have been developed with support from one or more Intel-operated
6+
# generative artificial intelligence solutions.
7+
8+
tests:
9+
sof.list:
10+
platform_allow: native_sim
11+
harness: ztest
12+
tags: unit

0 commit comments

Comments
 (0)