From 4167860e015a779f83a17bc8754c7070bdfba336 Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 24 Oct 2025 13:40:15 -0500 Subject: add list prepend --- lichen.test.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'lichen.test.c') diff --git a/lichen.test.c b/lichen.test.c index bca7d27..622f480 100644 --- a/lichen.test.c +++ b/lichen.test.c @@ -81,6 +81,72 @@ LILY_TEST("append to list") { #include LILY_PUSH_TEST() +LILY_TEST("prepend to list") { + struct li_ll_t *list = li_alloc_ll(); + REQUIRE_NEQ(list, NULL, "%p"); + + int a = 1; int b = 2; int c = 3; + + li_ll_prepend(list, &b); + CHECK_EQ(list->count, 1, "%d"); + REQUIRE_NEQ(list->head, NULL, "%p"); + CHECK_EQ(list->head->data, &b, "%p"); + CHECK_EQ(list->head->next, NULL, "%p"); + + li_ll_prepend(list, &a); + CHECK_EQ(list->count, 2, "%d"); + REQUIRE_NEQ(list->head->next, NULL, "%p"); + CHECK_EQ(list->head->data, &a, "%p"); + CHECK_EQ(list->head->next->data, &b, "%p"); + CHECK_EQ(list->head->next->next, NULL, "%p"); + + li_ll_prepend(list, &c); + CHECK_EQ(list->count, 3, "%d"); + REQUIRE_NEQ(list->head->next->next, NULL, "%p"); + CHECK_EQ(list->head->data, &c, "%p"); + CHECK_EQ(list->head->next->data, &a, "%p"); + CHECK_EQ(list->head->next->next->data, &b, "%p"); + CHECK_EQ(list->head->next->next->next, NULL, "%p"); + + li_free_ll(list, do_nothing); +} +#include LILY_PUSH_TEST() + + +LILY_TEST("mix prepends and appends") { + struct li_ll_t *list = li_alloc_ll(); + REQUIRE_NEQ(list, NULL, "%p"); + + int a = 1; int b = 2; int c = 3; + + li_ll_prepend(list, &b); + CHECK_EQ(list->count, 1, "%d"); + REQUIRE_NEQ(list->head, NULL, "%p"); + CHECK_EQ(list->head->data, &b, "%p"); + CHECK_EQ(list->head->next, NULL, "%p"); + + li_ll_prepend(list, &a); + CHECK_EQ(list->count, 2, "%d"); + REQUIRE_NEQ(list->head->next, NULL, "%p"); + CHECK_EQ(list->head->data, &a, "%p"); + CHECK_EQ(list->head->next->data, &b, "%p"); + CHECK_EQ(list->head->next->next, NULL, "%p"); + + li_ll_append(list, &c); + CHECK_EQ(list->count, 3, "%d"); + REQUIRE_NEQ(list->head->next->next, NULL, "%p"); + CHECK_EQ(list->head->data, &a, "%p"); + CHECK_EQ(list->head->next->data, &b, "%p"); + CHECK_EQ(list->head->next->next->data, &c, "%p"); + CHECK_EQ(list->head->next->next->next, NULL, "%p"); + + li_free_ll(list, do_nothing); + +} +#include LILY_PUSH_TEST() + + + #define LILY_FILE_END #include LILY_REGISTER_TESTS() -- cgit v1.2.1