diff --git a/test/cmp_decmp/test_cmp_decmp.c b/test/cmp_decmp/test_cmp_decmp.c index cddcf6fb022852dabc2187a84e9fd43dd6b82af2..b356864b17454835d152f024a01f568256c1c81c 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 56d233028906f36724e4d138c6ab98ceb218794a..082ac4c1c8a79e1e30ea3147bf445c513b569da7 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 37644a7f07c820cad0552069460cf8b48e400b89..b2be771719f0f768dcb965faad4b47df5b4ebc7e 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 f0fd84aea743cf4d2f6b3239949977c61b472943..e27d68a860bd17083712c94e05d9476da3fd61bf 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 */