Skip to content

Commit 1266621

Browse files
committed
Add a unit test for list node helpers
To reuse test suite, test_lib.c is renamed as test_utils.c that consolidate all utilities and helpers. Add a unit test for list_pushback_node() and list_remove_node().
1 parent 42af9f0 commit 1266621

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
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: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
/* LibC Test Suite - Comprehensive tests for standard library functions.
1+
/* Utility Test Suite - Comprehensive tests for utilities and helpers.
22
*
33
* Current Coverage:
44
* - vsnprintf/snprintf: Buffer overflow protection
55
* * C99 semantics, truncation behavior, ISR safety
66
* * Format specifiers: %s, %d, %u, %x, %p, %c, %%
77
* * Edge cases: size=0, size=1, truncation, null termination
88
*
9+
* - list operations: pushback node, remove node
10+
*
911
* Future Tests (Planned):
1012
* - String functions: strlen, strcmp, strcpy, strncpy, memcpy, memset
1113
* - Memory allocation: malloc, free, realloc
@@ -298,6 +300,51 @@ void test_mixed_formats(void)
298300
ASSERT_TEST(buf[test_strlen(buf)] == '\0', "Mixed format null termination");
299301
}
300302

303+
/* Test 11: List node helpers behavior */
304+
void test_list_node_pushback_and_remove(void)
305+
{
306+
list_node_t node1 = {0};
307+
list_node_t node2 = {0};
308+
309+
node1.next = &node2; /* make node1 artificially “linked” */
310+
list_t *list = list_create();
311+
312+
/* Check node push back normally - unlinked and linked */
313+
list_pushback_node(list, &node1);
314+
ASSERT_TEST(list_is_empty(list), "Linked node pushback fail");
315+
316+
node1.next = NULL;
317+
list_pushback_node(list, &node1);
318+
ASSERT_TEST(list->length == 1, "Unlinked node pushback success ");
319+
ASSERT_TEST(list->head->next == &node1 && node1.next == list->tail,
320+
"List consistent after pushback first node ");
321+
322+
/* Check node push back order */
323+
node2.next = NULL;
324+
list_pushback_node(list, &node2);
325+
ASSERT_TEST(list->length == 2 && list->head->next == &node1 &&
326+
node1.next == &node2 && node2.next == list->tail,
327+
"Insertion order preserved ");
328+
329+
/* Remove last node */
330+
list_remove_node(list, &node2);
331+
ASSERT_TEST(
332+
list->length == 1 && node2.next == NULL && node1.next == list->tail,
333+
"Removing last node must keep list structure consistent");
334+
335+
/* Remove non-existing node (second time) */
336+
list_remove_node(list, &node2);
337+
ASSERT_TEST(
338+
list->length == 1 && node2.next == NULL && node1.next == list->tail,
339+
"Removing non-existing node must not change the list");
340+
341+
/* Remove only node */
342+
list_remove_node(list, &node1);
343+
ASSERT_TEST(list->length == 0 && list->head->next == list->tail,
344+
"Removing only node ");
345+
ASSERT_TEST(list_is_empty(list), "Empty list check ");
346+
}
347+
301348
void test_runner(void)
302349
{
303350
printf("\n=== LibC Test Suite ===\n");
@@ -314,6 +361,10 @@ void test_runner(void)
314361
test_isr_safety();
315362
test_mixed_formats();
316363

364+
365+
printf("\n=== List Test Suite ===\n");
366+
test_list_node_pushback_and_remove();
367+
317368
printf("\n=== Test Summary ===\n");
318369
printf("Tests run: %d\n", tests_run);
319370
printf("Tests passed: %d\n", tests_passed);

0 commit comments

Comments
 (0)