diff options
author | Tim Wawrzynczak <twawrzynczak@chromium.org> | 2021-03-30 11:49:14 -0600 |
---|---|---|
committer | Patrick Georgi <pgeorgi@google.com> | 2021-04-06 06:50:38 +0000 |
commit | c556dffe985067cc40b817e963dae5cd1c34cc74 (patch) | |
tree | 3215c4fee56bdf4870864e72471d77f508fae32a /tests | |
parent | 8aedb34501450920a663342429e409562431e1c2 (diff) |
lib: Add obvious definition for `calloc`
The calloc() function is useful in addition to malloc and friends, so
add the obvious definition.
Change-Id: I57a568e323344a97b35014b7b8bec16adc2fd720
Signed-off-by: Tim Wawrzynczak <twawrzynczak@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/51949
Reviewed-by: Angel Pons <th3fanbus@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/malloc-test.c | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/tests/lib/malloc-test.c b/tests/lib/malloc-test.c index e358fffd3b..fefacbb92b 100644 --- a/tests/lib/malloc-test.c +++ b/tests/lib/malloc-test.c @@ -2,6 +2,7 @@ /* Include malloc() and memalign() source code and alter its name to indicate the functions source origin. */ +#define calloc cb_calloc #define malloc cb_malloc #define free cb_free #define memalign cb_memalign @@ -10,6 +11,7 @@ #include "../lib/malloc.c" +#undef calloc #undef malloc #undef free #undef memalign @@ -46,6 +48,12 @@ static int setup_test(void **state) return 0; } +static int setup_calloc_test(void **state) +{ + memset(&_heap, 0xFF, TEST_HEAP_SZ); + return setup_test(state); +} + static void test_malloc_out_of_memory(void **state) { /* Expect die() call if out of memory */ @@ -130,6 +138,19 @@ static void test_memalign_multiple_small_allocations(void **state) } } +static void test_calloc_memory_is_zeroed(void **state) +{ + const size_t nitems = 42; + const size_t size = sizeof(uint32_t); + void *ptr = cb_calloc(nitems, size); + assert_non_null(ptr); + + for (size_t i = 0; i < nitems; i++) { + const uint32_t *p = (const uint32_t *)ptr + i; + assert_int_equal(*p, 0); + } +} + int main(void) { const struct CMUnitTest tests[] = { @@ -140,6 +161,7 @@ int main(void) 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), + cmocka_unit_test_setup(test_calloc_memory_is_zeroed, setup_calloc_test), }; return cmocka_run_group_tests(tests, NULL, NULL); |