From 1bf4d3773cf4043fe897bfff6a1a04f905a329fc Mon Sep 17 00:00:00 2001 From: Dominik Loidolt <dominik.loidolt@univie.ac.at> Date: Tue, 12 Mar 2024 15:05:22 +0100 Subject: [PATCH] Add TEST_malloc function for round trip test --- test/cmp_decmp/test_cmp_decmp.c | 6 +++--- test/test_common/meson.build | 2 +- test/test_common/test_common.c | 28 +++++++++++++++++++++++++--- test/test_common/test_common.h | 3 +++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/test/cmp_decmp/test_cmp_decmp.c b/test/cmp_decmp/test_cmp_decmp.c index cddcf6f..b356864 100644 --- a/test/cmp_decmp/test_cmp_decmp.c +++ b/test/cmp_decmp/test_cmp_decmp.c @@ -884,7 +884,7 @@ static int32_t chunk_round_trip(void *data, uint32_t data_size, */ if (model) { if (up_model == model) { - model_cpy = cmp_test_malloc(data_size); + model_cpy = TEST_malloc(data_size); memcpy(model_cpy, model, data_size); } else { model_cpy = model; @@ -925,9 +925,9 @@ static int32_t chunk_round_trip(void *data, uint32_t data_size, TEST_ASSERT_EQUAL((uint32_t)decmp_size, data_size); if (use_decmp_buf) - decmp_data = cmp_test_malloc(data_size); + decmp_data = TEST_malloc(data_size); if (use_decmp_up_model) - up_model_decmp = cmp_test_malloc(data_size); + up_model_decmp = TEST_malloc(data_size); decmp_size = decompress_cmp_entiy((struct cmp_entity *)cmp_data, model_cpy, up_model_decmp, decmp_data); diff --git a/test/test_common/meson.build b/test/test_common/meson.build index 56d2330..082ac4c 100644 --- a/test/test_common/meson.build +++ b/test/test_common/meson.build @@ -4,5 +4,5 @@ pcb_dep = pcg_proj.get_variable('libpcg_basic_dep') test_common_lib = static_library( 'test_common', 'test_common.c', - dependencies: pcb_dep, + dependencies: [pcb_dep, unity_dep] ) diff --git a/test/test_common/test_common.c b/test/test_common/test_common.c index 37644a7..b2be771 100644 --- a/test/test_common/test_common.c +++ b/test/test_common/test_common.c @@ -1,7 +1,10 @@ -#include <assert.h> +#include <stddef.h> +#include <stdlib.h> +#include <string.h> #include "pcg_basic.h" +#include <unity.h> void cmp_rand_seed(uint64_t seed) { @@ -27,7 +30,7 @@ uint32_t cmp_rand32(void) uint32_t cmp_rand_between(uint32_t min, uint32_t max) { - assert(min < max); + TEST_ASSERT(min < max); return min + pcg32_boundedrand(max-min+1); } @@ -35,7 +38,26 @@ uint32_t cmp_rand_between(uint32_t min, uint32_t max) uint32_t cmp_rand_nbits(unsigned int nbits) { - assert(nbits > 0); + TEST_ASSERT(nbits > 0); return cmp_rand32() >> (32 - nbits); } + + +/** + * @brief allocates memory safely for tests + * + * @param size The size of memory to allocate. + * + * @returns a pointer to the allocated memory, or NULL if allocation fails + */ + +void* TEST_malloc(size_t size) +{ + if (size > 0) { + void* const mem = malloc(size); + TEST_ASSERT(mem); + return mem; + } + return NULL; +} diff --git a/test/test_common/test_common.h b/test/test_common/test_common.h index f0fd84a..e27d68a 100644 --- a/test/test_common/test_common.h +++ b/test/test_common/test_common.h @@ -2,6 +2,7 @@ #define TEST_COMMON_H #include <stdint.h> +#include <stddef.h> void cmp_rand_seed(uint64_t seed); @@ -11,4 +12,6 @@ uint32_t cmp_rand_between(uint32_t min, uint32_t max); uint32_t cmp_rand_nbits(unsigned int nbits); +void* TEST_malloc(size_t size); + #endif /* TEST_COMMON_H */ -- GitLab