diff options
author | 3gg <3gg@shellblade.net> | 2023-06-12 08:52:15 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2023-06-12 08:52:15 -0700 |
commit | bfabb435e5c5bf313005c4636747fce59eb4ca6f (patch) | |
tree | a32248966dd2cfa851a1bc731c3b240ebfaf9110 /list/test/list_test.c | |
parent | 0c1eb2535676a6fb2e1def08f9681b2a8b49f6e4 (diff) |
Add list library.
Diffstat (limited to 'list/test/list_test.c')
-rw-r--r-- | list/test/list_test.c | 76 |
1 files changed, 60 insertions, 16 deletions
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 @@ | |||
4 | 4 | ||
5 | #define TEST_LIST_SIZE 10 | 5 | #define TEST_LIST_SIZE 10 |
6 | 6 | ||
7 | // Create an empty list. | 7 | DEF_LIST(int); |
8 | TEST_CASE(list_create_empty) { list_make(0, 0); } | ||
9 | |||
10 | // Create a list of a given size. | ||
11 | TEST_CASE(list_create) { | ||
12 | struct list list[TEST_LIST_SIZE]; | ||
13 | list_make(list, TEST_LIST_SIZE); | ||
14 | } | ||
15 | 8 | ||
16 | // Iterate over a list. | 9 | // Iterate over a list. |
17 | TEST_CASE(list_traverse) { | 10 | TEST_CASE(list_traverse) { |
18 | int numbers[TEST_LIST_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | 11 | int_list list = make_list(int); |
19 | 12 | for (int i = 0; i < TEST_LIST_SIZE; ++i) { | |
20 | struct list list[TEST_LIST_SIZE]; | 13 | list_push(list, i + 1); |
21 | list_make(list, TEST_LIST_SIZE); | 14 | } |
22 | 15 | ||
23 | int count = 0; | 16 | int count = 0; |
24 | int sum = 0; | 17 | int sum = 0; |
25 | list_foreach(list, item) { | 18 | list_foreach(list, { |
26 | count++; | 19 | count++; |
27 | sum += numbers[item - list]; | 20 | sum += *value; |
28 | } | 21 | }); |
29 | 22 | ||
30 | TEST_EQUAL(count, TEST_LIST_SIZE); | 23 | TEST_EQUAL(count, TEST_LIST_SIZE); |
31 | TEST_EQUAL(sum, TEST_LIST_SIZE * (TEST_LIST_SIZE + 1) / 2); | 24 | TEST_EQUAL(sum, TEST_LIST_SIZE * (TEST_LIST_SIZE + 1) / 2); |
25 | |||
26 | del_list(list); | ||
27 | } | ||
28 | |||
29 | // Delete by value. | ||
30 | TEST_CASE(list_remove_by_value) { | ||
31 | int_list list = make_list(int); | ||
32 | for (int i = 0; i < TEST_LIST_SIZE; ++i) { | ||
33 | list_push(list, i + 1); | ||
34 | } | ||
35 | |||
36 | list_remove(list, 5); | ||
37 | |||
38 | int count = 0; | ||
39 | int sum = 0; | ||
40 | list_foreach(list, { | ||
41 | count++; | ||
42 | sum += *value; | ||
43 | }); | ||
44 | |||
45 | TEST_EQUAL(count, TEST_LIST_SIZE - 1); | ||
46 | TEST_EQUAL(sum, (TEST_LIST_SIZE * (TEST_LIST_SIZE + 1) / 2) - 5); | ||
47 | |||
48 | del_list(list); | ||
49 | } | ||
50 | |||
51 | // Delete by address. | ||
52 | TEST_CASE(list_remove_by_address) { | ||
53 | const int N = 3; | ||
54 | |||
55 | int* ptrs[3] = {0}; | ||
56 | |||
57 | int_list list = make_list(int); | ||
58 | for (int i = 0; i < N; ++i) { | ||
59 | list_push(list, i + 1); | ||
60 | ptrs[i] = &list.head->val; | ||
61 | } | ||
62 | |||
63 | list_remove_ptr(list, ptrs[1]); // Value 2. | ||
64 | |||
65 | int count = 0; | ||
66 | int sum = 0; | ||
67 | list_foreach(list, { | ||
68 | count++; | ||
69 | sum += *value; | ||
70 | }); | ||
71 | |||
72 | TEST_EQUAL(count, 2); | ||
73 | TEST_EQUAL(sum, 1 + 3); | ||
74 | |||
75 | del_list(list); | ||
32 | } | 76 | } |
33 | 77 | ||
34 | int main() { return 0; } | 78 | int main() { return 0; } |