jacz@semihalf.com has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/50715 )
Change subject: tests: Add lib/malloc-test test case ......................................................................
tests: Add lib/malloc-test test case
Signed-off-by: Jakub Czapiga jacz@semihalf.com Change-Id: Ic6b10ec382cc807772689e852bad300c75da1fe2 --- M tests/lib/Makefile.inc A tests/lib/malloc-test.c 2 files changed, 134 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/15/50715/1
diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc index 2c80b93..44bbf02 100644 --- a/tests/lib/Makefile.inc +++ b/tests/lib/Makefile.inc @@ -14,6 +14,7 @@ tests-y += imd_cbmem-ramstage-test tests-y += region_file-test tests-y += stack-test +tests-y += malloc-test
string-test-srcs += tests/lib/string-test.c string-test-srcs += src/lib/string.c @@ -76,3 +77,7 @@ stack-test-srcs += tests/lib/stack-test.c stack-test-srcs += src/lib/stack.c stack-test-srcs += tests/stubs/console.c + +malloc-test-srcs += tests/lib/malloc-test.c +#malloc-test-srcs += src/lib/malloc.c +malloc-test-srcs += tests/stubs/console.c diff --git a/tests/lib/malloc-test.c b/tests/lib/malloc-test.c new file mode 100644 index 0000000..91dad77 --- /dev/null +++ b/tests/lib/malloc-test.c @@ -0,0 +1,129 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#define malloc cb_malloc +#define free cb_free +#define memalign cb_memalign +#undef __noreturn +#define __noreturn + +#include "../lib/malloc.c" + +#undef malloc +#undef free +#undef memalign +#undef __noreturn +#define __noreturn __attribute__((noreturn)) + +#include <stdlib.h> +#include <tests/test.h> +#include <commonlib/helpers.h> +#include <types.h> +#include <symbols.h> + +/* 4 MiB */ +#define TEST_HEAP_SZ 0x400000 + +/* Heap region setup */ +__weak extern uint8_t _test_heap[]; +__weak extern uint8_t _etest_heap[]; +TEST_REGION(test_heap, TEST_HEAP_SZ); +TEST_SYMBOL(_heap, _test_heap); +TEST_SYMBOL(_eheap, _etest_heap); + +void die(const char *msg, ...) +{ + function_called(); +} + +static int setup_test(void **state) +{ + free_mem_ptr = &_heap; + free_mem_end_ptr = &_eheap; + free_last_alloc_ptr = &_heap; + + return 0; +} + +static void test_malloc_out_of_memory(void **state) +{ + /* Expect die() call if out of memory */ + expect_function_call(die); + cb_malloc(TEST_HEAP_SZ); +} + +static void test_malloc_zero(void **state) +{ + void *ptr1 = cb_malloc(0); + void *ptr2 = cb_malloc(0); + void *ptr3 = cb_malloc(0); + + /* Expect malloc(0) to return the same pointer as there are no bytes + to be added to the heap */ + assert_ptr_equal(ptr1, ptr2); + assert_ptr_equal(ptr2, ptr3); +} + +static void test_malloc_multiple_small_allocations(void **state) +{ + /* Make multiple small allocations (smaller than alignment) + Expect no call to die(), as this allocations should be small + enough to fit in provided memory */ + void *prev; + void *curr = cb_malloc(3); + assert_non_null(curr); + for (int i = 0; i < 1000; ++i) { + prev = curr; + curr = cb_malloc(3); + assert_non_null(curr); + assert_true(prev < curr); + } +} + +static void test_memalign_out_of_memory(void **state) +{ + expect_function_call(die); + cb_memalign(16, TEST_HEAP_SZ); +} + +static void test_memalign_zero(void **state) +{ + void *ptr1 = cb_memalign(16, 0); + void *ptr2 = cb_memalign(7, 0); + void *ptr3 = cb_memalign(11, 0); + + /* Expect memalign(x, 0) to return the same pointer as there are no bytes + to be added to the heap */ + assert_ptr_equal(ptr1, ptr2); + assert_ptr_equal(ptr2, ptr3); +} + +static void test_memalign_multiple_small_allocations(void **state) +{ + /* Make multiple small allocations (smaller than alignment) + Expect no call to die(), as this allocations should be small + enough to fit in provided memory. There should also be no error + when allocating memory with different align values. */ + void *prev; + void *curr = cb_memalign(3, 3); + assert_non_null(curr); + for (int i = 0; i < 1000; ++i) { + prev = curr; + curr = cb_memalign(1 + i % 16, 3); + assert_non_null(curr); + assert_true(prev < curr); + } +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test_setup(test_malloc_out_of_memory, setup_test), + cmocka_unit_test_setup(test_malloc_zero, setup_test), + cmocka_unit_test_setup(test_malloc_multiple_small_allocations, setup_test), + cmocka_unit_test_setup(test_memalign_out_of_memory, setup_test), + cmocka_unit_test_setup(test_memalign_zero, setup_test), + cmocka_unit_test_setup(test_memalign_multiple_small_allocations, setup_test), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +}