Skip to content

Commit 50fe97c

Browse files
committed
Add unit tests for list operations
Add unit tests for general list operations using the new embedded-node design. Rename test_libc to test_utils so it can host unit tests for common helpers and consolidate reusable test code.
1 parent 42ba098 commit 50fe97c

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-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_utils
3333

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

app/test_libc.c renamed to app/test_utils.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,49 @@ void test_mixed_formats(void)
298298
ASSERT_TEST(buf[test_strlen(buf)] == '\0', "Mixed format null termination");
299299
}
300300

301+
/* Test 11: List helpers behavior */
302+
typedef struct {
303+
int val;
304+
list_node_t node;
305+
} list_node_item_t;
306+
307+
void test_list_pushback_and_remove(void)
308+
{
309+
list_t *list = list_create();
310+
311+
list_node_item_t first = {.node.next = NULL, .val = 1};
312+
list_node_item_t second = {.node.next = NULL, .val = 2};
313+
list_node_item_t third = {.node.next = NULL, .val = 3};
314+
315+
/* Check node push back normally - unlinked and linked */
316+
list_pushback(list, &first.node);
317+
ASSERT_TEST(list->length == 1, "Push back first node ");
318+
319+
list_pushback(list, &second.node);
320+
list_node_item_t *item =
321+
container_of(list->head->next, list_node_item_t, node);
322+
ASSERT_TEST(list->length == 2 && item->val == 1,
323+
"Push back second node and order preserved ");
324+
325+
326+
list_pushback(list, &third.node);
327+
item = container_of(list->head->next, list_node_item_t, node);
328+
ASSERT_TEST(list->length == 3 && item->val == 3, "Push back third node ");
329+
330+
/* Remove second node */
331+
list_remove(list, &second.node);
332+
item = container_of(list->head->next, list_node_item_t, node);
333+
ASSERT_TEST(list->length == 2 && item->val == 2, "Remove second node ");
334+
335+
/* Remove non-existing node (second time) */
336+
337+
item = container_of(list_pop(list), list_node_item_t, node);
338+
ASSERT_TEST(list->length == 2 && item->val == 1, "Pop node ");
339+
340+
list_clear(list);
341+
ASSERT_TEST(list_is_empty(list), "List is cleared ");
342+
}
343+
301344
void test_runner(void)
302345
{
303346
printf("\n=== LibC Test Suite ===\n");

0 commit comments

Comments
 (0)