From bfabb435e5c5bf313005c4636747fce59eb4ca6f Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Mon, 12 Jun 2023 08:52:15 -0700 Subject: Add list library. --- list/test/list_test.c | 76 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 16 deletions(-) (limited to 'list/test/list_test.c') diff --git a/list/test/list_test.c b/list/test/list_test.c index a11c713..9ff10c1 100644 --- a/list/test/list_test.c +++ b/list/test/list_test.c @@ -4,31 +4,75 @@ #define TEST_LIST_SIZE 10 -// Create an empty list. -TEST_CASE(list_create_empty) { list_make(0, 0); } - -// Create a list of a given size. -TEST_CASE(list_create) { - struct list list[TEST_LIST_SIZE]; - list_make(list, TEST_LIST_SIZE); -} +DEF_LIST(int); // Iterate over a list. TEST_CASE(list_traverse) { - int numbers[TEST_LIST_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - - struct list list[TEST_LIST_SIZE]; - list_make(list, TEST_LIST_SIZE); + int_list list = make_list(int); + for (int i = 0; i < TEST_LIST_SIZE; ++i) { + list_push(list, i + 1); + } int count = 0; - int sum = 0; - list_foreach(list, item) { + int sum = 0; + list_foreach(list, { count++; - sum += numbers[item - list]; - } + sum += *value; + }); TEST_EQUAL(count, TEST_LIST_SIZE); TEST_EQUAL(sum, TEST_LIST_SIZE * (TEST_LIST_SIZE + 1) / 2); + + del_list(list); +} + +// Delete by value. +TEST_CASE(list_remove_by_value) { + int_list list = make_list(int); + for (int i = 0; i < TEST_LIST_SIZE; ++i) { + list_push(list, i + 1); + } + + list_remove(list, 5); + + int count = 0; + int sum = 0; + list_foreach(list, { + count++; + sum += *value; + }); + + TEST_EQUAL(count, TEST_LIST_SIZE - 1); + TEST_EQUAL(sum, (TEST_LIST_SIZE * (TEST_LIST_SIZE + 1) / 2) - 5); + + del_list(list); +} + +// Delete by address. +TEST_CASE(list_remove_by_address) { + const int N = 3; + + int* ptrs[3] = {0}; + + int_list list = make_list(int); + for (int i = 0; i < N; ++i) { + list_push(list, i + 1); + ptrs[i] = &list.head->val; + } + + list_remove_ptr(list, ptrs[1]); // Value 2. + + int count = 0; + int sum = 0; + list_foreach(list, { + count++; + sum += *value; + }); + + TEST_EQUAL(count, 2); + TEST_EQUAL(sum, 1 + 3); + + del_list(list); } int main() { return 0; } -- cgit v1.2.3