diff options
author | Tom Hughes <tomhughes@chromium.org> | 2023-03-27 15:44:52 -0700 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-04-07 22:33:28 +0000 |
commit | 4dcdae2474cdab1cf7dffc2828d73336a9b4260a (patch) | |
tree | 04aa5f5d7def491ece3021061e9e729933c661a4 /test/malloc.c | |
parent | b5eb344ca4965a798b8c849a9294f2f5798fcc3d (diff) | |
download | chrome-ec-4dcdae2474cdab1cf7dffc2828d73336a9b4260a.tar.gz |
test: Add a test for malloc/free
BRANCH=none
BUG=b:274737509
TEST=./test/run_device_tests.py --board bloonchipper -t malloc
=> PASS
TEST=./test/run_device_tests.py --board dartmonkey -t malloc
=> PASS
TEST=make run-malloc
Change-Id: Ida58335dcacbeefad56804246897810936c98546
Signed-off-by: Tom Hughes <tomhughes@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4390609
Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org>
Diffstat (limited to 'test/malloc.c')
-rw-r--r-- | test/malloc.c | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/test/malloc.c b/test/malloc.c new file mode 100644 index 0000000000..37c566b2ec --- /dev/null +++ b/test/malloc.c @@ -0,0 +1,94 @@ +/* Copyright 2023 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "common.h" +#include "shared_mem.h" +#include "stdlib.h" +#include "test_util.h" + +#include <malloc.h> + +struct malloc_data { + uint32_t size; + uint8_t val; + uint8_t *data; +}; + +static struct malloc_data test_data[] = { + { .size = 15, .val = 1, .data = NULL }, + { .size = 1024, .val = 2, .data = NULL }, + { .size = 86096, .val = 3, .data = NULL } +}; + +test_static int test_malloc_different_sizes(void) +{ + /* Trim to make sure that previous tests haven't fragmented the heap. */ + malloc_trim(0); + + for (int i = 0; i < ARRAY_SIZE(test_data); i++) { + uint8_t *volatile ptr = malloc(test_data[i].size); + TEST_NE(ptr, NULL, "%p"); + test_data[i].data = ptr; + for (int j = 0; j < test_data[i].size; j++) { + ptr[j] = test_data[i].val; + } + } + + for (int i = 0; i < ARRAY_SIZE(test_data); i++) { + uint8_t *ptr = test_data[i].data; + /* Using TEST_EQ results in too much logging. */ + for (int j = 0; j < test_data[i].size; j++) { + if (ptr[j] != test_data[i].val) { + TEST_ASSERT(false); + } + } + } + + for (int i = 0; i < ARRAY_SIZE(test_data); i++) { + free(test_data[i].data); + } + + return EC_SUCCESS; +} + +test_static int test_free_null(void) +{ + free(NULL); + return EC_SUCCESS; +} + +test_static int test_malloc_large(void) +{ + /* Trim to make sure that previous tests haven't fragmented the heap. */ + malloc_trim(0); + uint8_t *volatile ptr = malloc(shared_mem_size() * 0.8); + TEST_NE(ptr, NULL, "%p"); + free(ptr); + return EC_SUCCESS; +} + +test_static int test_malloc_too_large(void) +{ + /* Trim to make sure that previous tests haven't fragmented the heap. */ + malloc_trim(0); + uint8_t *volatile ptr = malloc(shared_mem_size() + 1); + TEST_EQ(ptr, NULL, "%p"); + free(ptr); + return EC_SUCCESS; +} + +void run_test(int argc, const char **argv) +{ + test_reset(); + + RUN_TEST(test_free_null); + RUN_TEST(test_malloc_different_sizes); + RUN_TEST(test_malloc_large); + + if (!IS_ENABLED(BOARD_HOST)) + RUN_TEST(test_malloc_too_large); + + test_print_result(); +} |