Skip to content

Commit e027d81

Browse files
committed
Add unit test for list operation APIs
Add test suite for list operations, major cover list_pushback_node() and list_remove_node() in this change.
1 parent 42af9f0 commit e027d81

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ deps += $(LIB_OBJS:%.o=%.o.d)
2929
APPS := coop echo hello mqueues semaphore mutex cond \
3030
pipes pipes_small pipes_struct prodcons progress \
3131
rtsched suspend test64 timer timer_kill \
32-
cpubench test_libc
32+
cpubench test_libc test_list
3333

3434
# Output files for __link target
3535
IMAGE_BASE := $(BUILD_DIR)/image

app/test_list.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/* List Test Suit
2+
*
3+
* Coverage:
4+
* * list_pushback_node()
5+
* * list_remove_node()
6+
*/
7+
8+
#include <linmo.h>
9+
10+
#define PASS 0
11+
#define FAIL 0;
12+
13+
/* Result tracing */
14+
static int tests_run = 0;
15+
static int tests_passed = 0;
16+
static int tests_failed = 0;
17+
18+
#define ASSERT_TEST(cond, test_name) \
19+
do { \
20+
tests_run++; \
21+
if (cond) { \
22+
tests_passed++; \
23+
printf("[PASS] %s \n", test_name); \
24+
} else { \
25+
tests_failed++; \
26+
printf("[FAIL] %s \n", test_name); \
27+
} \
28+
} while (0);
29+
30+
void test_list_node_pushback_and_remove(void)
31+
{
32+
list_node_t node1 = {0};
33+
list_node_t node2 = {0};
34+
35+
node1.next = &node2; /* make node1 artificially “linked” */
36+
list_t *list = list_create();
37+
38+
/* Check node push back normally - unlinked and linked */
39+
list_pushback_node(list, &node1);
40+
ASSERT_TEST(list_is_empty(list),
41+
"Linked node must not be pushed back to the list");
42+
43+
node1.next = NULL;
44+
list_pushback_node(list, &node1);
45+
ASSERT_TEST(list->length == 1,
46+
"Unlinked node must be pushed back to the list");
47+
ASSERT_TEST(list->head->next == &node1 && node1.next == list->tail,
48+
"List consistent after pushback first node ");
49+
50+
/* Check node push back order */
51+
node2.next = NULL;
52+
list_pushback_node(list, &node2);
53+
ASSERT_TEST(list->length == 2 && list->head->next == &node1 &&
54+
node1.next == &node2 && node2.next == list->tail,
55+
"Pushback must preserve insertion order");
56+
57+
/* Remove last node */
58+
list_remove_node(list, &node2);
59+
ASSERT_TEST(
60+
list->length == 1 && node2.next == NULL && node1.next == list->tail,
61+
"Removing last node must keep list structure consistent");
62+
63+
/* Remove non-existing node (second time) */
64+
list_remove_node(list, &node2);
65+
ASSERT_TEST(
66+
list->length == 1 && node2.next == NULL && node1.next == list->tail,
67+
"Removing non-existing node must not change the list");
68+
69+
/* Remove only node */
70+
list_remove_node(list, &node1);
71+
ASSERT_TEST(list->length == 0 && list->head->next == list->tail,
72+
"Removing only node must restore empty list structure");
73+
ASSERT_TEST(list_is_empty(list),
74+
"Empty list must report as empty after removing only node");
75+
}
76+
77+
void test_result(void)
78+
{
79+
printf("\n=== Test list operation ===\n");
80+
81+
test_list_node_pushback_and_remove();
82+
83+
printf("\n=== Test Summary ===\n");
84+
printf("Tests run: %d\n", tests_run);
85+
printf("Tests passed: %d\n", tests_passed);
86+
printf("Tests failed: %d\n", tests_failed);
87+
88+
if (tests_failed == 0) {
89+
printf("\n[SUCCESS] All tests passed!\n");
90+
} else {
91+
printf("\n[FAILURE] %d test(s) failed!\n", tests_failed);
92+
}
93+
}
94+
95+
int app_main()
96+
{
97+
mo_logger_flush();
98+
test_result();
99+
100+
/* Shutdown QEMU cleanly after tests complete */
101+
*(volatile uint32_t *) 0x100000U = 0x5555U;
102+
103+
/* Fallback: keep task alive if shutdown fails (non-QEMU environments) */
104+
while (1)
105+
; /* Idle loop */
106+
107+
return 0;
108+
}

0 commit comments

Comments
 (0)