diff options
author | Jakub Czapiga <jacz@semihalf.com> | 2020-11-12 12:10:51 +0100 |
---|---|---|
committer | Patrick Georgi <pgeorgi@google.com> | 2020-11-25 09:14:07 +0000 |
commit | ea378ccc8f61b64fcc8be603e5d1c76555086108 (patch) | |
tree | c790821d23b9ebdc172581364d49c1c9fcf6a2fd | |
parent | 5a41b0db20348c6a6b83132d40f825daa2dedc9a (diff) | |
download | coreboot-ea378ccc8f61b64fcc8be603e5d1c76555086108.tar.xz |
tests: Add lib/list-test test case
Change-Id: If74f241b2bb788b3e2fd1b9062fc74819f7be31e
Signed-off-by: Jakub Czapiga <jacz@semihalf.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/47507
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org>
-rw-r--r-- | tests/lib/Makefile.inc | 7 | ||||
-rw-r--r-- | tests/lib/list-test.c | 129 |
2 files changed, 135 insertions, 1 deletions
diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc index c49828962d..5783f18919 100644 --- a/tests/lib/Makefile.inc +++ b/tests/lib/Makefile.inc @@ -8,6 +8,7 @@ tests-y += timestamp-test tests-y += edid-test tests-y += cbmem_console-romstage-test tests-y += cbmem_console-ramstage-test +tests-y += list-test string-test-srcs += tests/lib/string-test.c string-test-srcs += src/lib/string.c @@ -38,4 +39,8 @@ cbmem_console-romstage-test-srcs += tests/stubs/console.c cbmem_console-ramstage-test-stage := ramstage cbmem_console-ramstage-test-srcs += tests/lib/cbmem_console-test.c -cbmem_console-ramstage-test-srcs += tests/stubs/console.c
\ No newline at end of file +cbmem_console-ramstage-test-srcs += tests/stubs/console.c + +list-test-srcs += tests/lib/list-test.c +list-test-srcs += src/lib/list.c + diff --git a/tests/lib/list-test.c b/tests/lib/list-test.c new file mode 100644 index 0000000000..6688c4faeb --- /dev/null +++ b/tests/lib/list-test.c @@ -0,0 +1,129 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <tests/test.h> +#include <stdlib.h> +#include <string.h> +#include <list.h> + +struct test_container { + int value; + + struct list_node list_node; +}; + +void test_list_insert_after(void **state) +{ + int i = 0; + struct list_node root = { .prev = NULL, .next = NULL }; + struct test_container *c1 = (struct test_container *)malloc(sizeof(*c1)); + struct test_container *c2 = (struct test_container *)malloc(sizeof(*c2)); + struct test_container *c3 = (struct test_container *)malloc(sizeof(*c2)); + struct test_container *ptr; + const int values[] = { 5, 10, 13 }; /* Random values */ + + memset(c1, 0, sizeof(*c1)); + memset(c2, 0, sizeof(*c2)); + memset(c2, 0, sizeof(*c3)); + + c1->value = values[0]; + c2->value = values[1]; + c3->value = values[2]; + + list_insert_after(&c1->list_node, &root); + list_insert_after(&c2->list_node, &c1->list_node); + list_insert_after(&c3->list_node, &c2->list_node); + + list_for_each(ptr, root, list_node) { + assert_int_equal(values[i], ptr->value); + i++; + } + + assert_int_equal(3, i); + + free(c3); + free(c2); + free(c1); +} + +void test_list_insert_before(void **state) +{ + int i = 0; + struct list_node root = { .prev = NULL, .next = NULL }; + struct test_container *c1 = (struct test_container *)malloc(sizeof(*c1)); + struct test_container *c2 = (struct test_container *)malloc(sizeof(*c2)); + struct test_container *c3 = (struct test_container *)malloc(sizeof(*c2)); + struct test_container *ptr; + const int values[] = { 19, 71, 991 }; /* Random values */ + + memset(c1, 0, sizeof(*c1)); + memset(c2, 0, sizeof(*c2)); + memset(c2, 0, sizeof(*c3)); + + c1->value = values[0]; + c2->value = values[1]; + c3->value = values[2]; + + list_insert_after(&c3->list_node, &root); + list_insert_before(&c2->list_node, &c3->list_node); + list_insert_before(&c1->list_node, &c2->list_node); + + + list_for_each(ptr, root, list_node) { + assert_int_equal(values[i], ptr->value); + i++; + } + + assert_int_equal(3, i); + + free(c3); + free(c2); + free(c1); +} + +void test_list_remove(void **state) +{ + struct list_node root = { .prev = NULL, .next = NULL }; + struct test_container *c1 = (struct test_container *)malloc(sizeof(*c1)); + struct test_container *c2 = (struct test_container *)malloc(sizeof(*c2)); + struct test_container *ptr; + int len; + + list_insert_after(&c1->list_node, &root); + list_insert_after(&c2->list_node, &c1->list_node); + + len = 0; + list_for_each(ptr, root, list_node) { + len++; + } + assert_int_equal(2, len); + + list_remove(&c1->list_node); + + len = 0; + list_for_each(ptr, root, list_node) { + len++; + } + assert_int_equal(1, len); + + list_remove(&c2->list_node); + len = 0; + list_for_each(ptr, root, list_node) { + len++; + } + assert_int_equal(0, len); + + free(c2); + free(c1); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_list_insert_after), + cmocka_unit_test(test_list_insert_before), + cmocka_unit_test(test_list_remove), + }; + + + return cmocka_run_group_tests(tests, NULL, NULL); +} |