From 6d2b56469ac5455469d555645996a3b58767c4db Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sun, 12 Dec 2021 21:37:09 -0600 Subject: [PATCH] Update: Add cmocka tests for f_memory project. --- level_0/f_memory/data/build/dependencies-tests | 3 + level_0/f_memory/data/build/settings-tests | 41 +++++++++ level_0/f_memory/tests/c/test-memory-adjust.c | 31 +++++++ level_0/f_memory/tests/c/test-memory-adjust.h | 21 +++++ level_0/f_memory/tests/c/test-memory-delete.c | 100 +++++++++++++++++++++ level_0/f_memory/tests/c/test-memory-delete.h | 47 ++++++++++ level_0/f_memory/tests/c/test-memory-destroy.c | 100 +++++++++++++++++++++ level_0/f_memory/tests/c/test-memory-destroy.h | 47 ++++++++++ level_0/f_memory/tests/c/test-memory-new.c | 22 +++++ level_0/f_memory/tests/c/test-memory-new.h | 20 +++++ level_0/f_memory/tests/c/test-memory-new_aligned.c | 22 +++++ level_0/f_memory/tests/c/test-memory-new_aligned.h | 20 +++++ level_0/f_memory/tests/c/test-memory-resize.c | 31 +++++++ level_0/f_memory/tests/c/test-memory-resize.h | 21 +++++ level_0/f_memory/tests/c/test-memory.c | 43 +++++++++ level_0/f_memory/tests/c/test-memory.h | 74 +++++++++++++++ 16 files changed, 643 insertions(+) create mode 100644 level_0/f_memory/data/build/dependencies-tests create mode 100644 level_0/f_memory/data/build/settings-tests create mode 100644 level_0/f_memory/tests/c/test-memory-adjust.c create mode 100644 level_0/f_memory/tests/c/test-memory-adjust.h create mode 100644 level_0/f_memory/tests/c/test-memory-delete.c create mode 100644 level_0/f_memory/tests/c/test-memory-delete.h create mode 100644 level_0/f_memory/tests/c/test-memory-destroy.c create mode 100644 level_0/f_memory/tests/c/test-memory-destroy.h create mode 100644 level_0/f_memory/tests/c/test-memory-new.c create mode 100644 level_0/f_memory/tests/c/test-memory-new.h create mode 100644 level_0/f_memory/tests/c/test-memory-new_aligned.c create mode 100644 level_0/f_memory/tests/c/test-memory-new_aligned.h create mode 100644 level_0/f_memory/tests/c/test-memory-resize.c create mode 100644 level_0/f_memory/tests/c/test-memory-resize.h create mode 100644 level_0/f_memory/tests/c/test-memory.c create mode 100644 level_0/f_memory/tests/c/test-memory.h diff --git a/level_0/f_memory/data/build/dependencies-tests b/level_0/f_memory/data/build/dependencies-tests new file mode 100644 index 0000000..ef33c6a --- /dev/null +++ b/level_0/f_memory/data/build/dependencies-tests @@ -0,0 +1,3 @@ +# fss-0000 + +cmocka 1.* diff --git a/level_0/f_memory/data/build/settings-tests b/level_0/f_memory/data/build/settings-tests new file mode 100644 index 0000000..1f098be --- /dev/null +++ b/level_0/f_memory/data/build/settings-tests @@ -0,0 +1,41 @@ +# fss-0001 + +project_name test-f_memory + +version_major 0 +version_file major +version_target major + +modes individual +modes_default individual + +build_compiler gcc +build_indexer ar +build_indexer_arguments rcs +build_language c +build_libraries -lc -lcmocka +build_libraries-individual -lf_memory +build_sources_program test-memory-adjust.c test-memory-delete.c test-memory-destroy.c test-memory-new_aligned.c test-memory-new.c test-memory-resize.c test-memory.c +build_script no +build_shared yes +build_static no + +path_headers tests/c +path_headers_preserve no +path_sources tests/c +path_standard no + +search_exclusive yes +search_shared yes +search_static yes + +defines -Ibuild/includes +defines_static -Lbuild/libraries/static +defines_shared -Lbuild/libraries/shared + +flags -O2 -z now -g -fdiagnostics-color=always -Wno-logical-not-parentheses -Wno-logical-op-parentheses -Wno-parentheses +flags_program -fPIE +flags_program_shared +flags_program_static +flags_shared +flags_static diff --git a/level_0/f_memory/tests/c/test-memory-adjust.c b/level_0/f_memory/tests/c/test-memory-adjust.c new file mode 100644 index 0000000..fa6bb47 --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory-adjust.c @@ -0,0 +1,31 @@ +#include "test-memory.h" +#include "test-memory-adjust.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void test__f_memory_adjust__works(void **state) { + + uint16_t *data = 0; + + { + const f_status_t status = f_memory_new(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_adjust(1, 4, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + free((void *) data); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_memory/tests/c/test-memory-adjust.h b/level_0/f_memory/tests/c/test-memory-adjust.h new file mode 100644 index 0000000..63532d9 --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory-adjust.h @@ -0,0 +1,21 @@ +/** + * FLL - Level 0 + * + * Project: Memory + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the memory project. + */ +#ifndef _TEST__F_memory__adjust +#define _TEST__F_memory__adjust + +/** + * Test that reallocation works. + * + * @see f_memory_new() + * @see f_memory_adjust() + */ +extern void test__f_memory_adjust__works(void **state); + +#endif // _TEST__F_memory__adjust diff --git a/level_0/f_memory/tests/c/test-memory-delete.c b/level_0/f_memory/tests/c/test-memory-delete.c new file mode 100644 index 0000000..f2e538c --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory-delete.c @@ -0,0 +1,100 @@ +#include "test-memory.h" +#include "test-memory-delete.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void test__f_memory_delete__frees_adjusted_memory(void **state) { + + uint16_t *data = 0; + + { + const f_status_t status = f_memory_new(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_adjust(1, 4, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_delete(4, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_null(data); + } +} + +void test__f_memory_delete__frees_aligned_memory(void **state) { + + uint16_t *data = 0; + + { + const f_status_t status = f_memory_new_aligned(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_delete(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_null(data); + } +} + +void test__f_memory_delete__frees_memory(void **state) { + + uint16_t *data = 0; + + { + const f_status_t status = f_memory_new(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_delete(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_null(data); + } +} + +void test__f_memory_delete__frees_resized_memory(void **state) { + + uint16_t *data = 0; + + { + const f_status_t status = f_memory_new(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_resize(1, 4, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_delete(4, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_null(data); + } +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_memory/tests/c/test-memory-delete.h b/level_0/f_memory/tests/c/test-memory-delete.h new file mode 100644 index 0000000..c78a5b1 --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory-delete.h @@ -0,0 +1,47 @@ +/** + * FLL - Level 0 + * + * Project: Memory + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the memory project. + */ +#ifndef _TEST__F_memory__delete +#define _TEST__F_memory__delete + +/** + * Test that deallocation works for adjusted memory. + * + * @see f_memory_adjust() + * @see f_memory_delete() + * @see f_memory_new() + */ +extern void test__f_memory_delete__frees_adjusted_memory(void **state); + +/** + * Test that deallocation works for aligned memory. + * + * @see f_memory_delete() + * @see f_memory_new_aligned() + */ +extern void test__f_memory_delete__frees_aligned_memory(void **state); + +/** + * Test that deallocation works. + * + * @see f_memory_delete() + * @see f_memory_new() + */ +extern void test__f_memory_delete__frees_memory(void **state); + +/** + * Test that deallocation works for resized memory. + * + * @see f_memory_delete() + * @see f_memory_new() + * @see f_memory_resize() + */ +extern void test__f_memory_delete__frees_resized_memory(void **state); + +#endif // _TEST__F_memory__delete diff --git a/level_0/f_memory/tests/c/test-memory-destroy.c b/level_0/f_memory/tests/c/test-memory-destroy.c new file mode 100644 index 0000000..cc6732f --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory-destroy.c @@ -0,0 +1,100 @@ +#include "test-memory.h" +#include "test-memory-destroy.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void test__f_memory_destroy__frees_adjusted_memory(void **state) { + + uint16_t *data = 0; + + { + const f_status_t status = f_memory_new(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_adjust(1, 4, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_destroy(4, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_null(data); + } +} + +void test__f_memory_destroy__frees_aligned_memory(void **state) { + + uint16_t *data = 0; + + { + const f_status_t status = f_memory_new_aligned(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_destroy(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_null(data); + } +} + +void test__f_memory_destroy__frees_memory(void **state) { + + uint16_t *data = 0; + + { + const f_status_t status = f_memory_new(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_destroy(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_null(data); + } +} + +void test__f_memory_destroy__frees_resized_memory(void **state) { + + uint16_t *data = 0; + + { + const f_status_t status = f_memory_new(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_resize(1, 4, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_destroy(4, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_null(data); + } +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_memory/tests/c/test-memory-destroy.h b/level_0/f_memory/tests/c/test-memory-destroy.h new file mode 100644 index 0000000..4a57ce6 --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory-destroy.h @@ -0,0 +1,47 @@ +/** + * FLL - Level 0 + * + * Project: Memory + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the memory project. + */ +#ifndef _TEST__F_memory__destroy +#define _TEST__F_memory__destroy + +/** + * Test that deallocation works for adjusted memory. + * + * @see f_memory_adjust() + * @see f_memory_destroy() + * @see f_memory_new() + */ +extern void test__f_memory_destroy__frees_adjusted_memory(void **state); + +/** + * Test that deallocation works for aligned memory. + * + * @see f_memory_destroy() + * @see f_memory_new_aligned() + */ +extern void test__f_memory_destroy__frees_aligned_memory(void **state); + +/** + * Test that deallocation works. + * + * @see f_memory_destroy() + * @see f_memory_new() + */ +extern void test__f_memory_destroy__frees_memory(void **state); + +/** + * Test that deallocation works for resized memory. + * + * @see f_memory_destroy() + * @see f_memory_new() + * @see f_memory_resize() + */ +extern void test__f_memory_destroy__frees_resized_memory(void **state); + +#endif // _TEST__F_memory__destroy diff --git a/level_0/f_memory/tests/c/test-memory-new.c b/level_0/f_memory/tests/c/test-memory-new.c new file mode 100644 index 0000000..c52f195 --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory-new.c @@ -0,0 +1,22 @@ +#include "test-memory.h" +#include "test-memory.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void test__f_memory_new__works(void **state) { + + uint16_t *data = 0; + + const f_status_t status = f_memory_new(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + + free((void *) data); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_memory/tests/c/test-memory-new.h b/level_0/f_memory/tests/c/test-memory-new.h new file mode 100644 index 0000000..08a2807 --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory-new.h @@ -0,0 +1,20 @@ +/** + * FLL - Level 0 + * + * Project: Memory + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the memory project. + */ +#ifndef _TEST__F_memory__new +#define _TEST__F_memory__new + +/** + * Test that allocation works. + * + * @see f_memory_new() + */ +extern void test__f_memory_new__works(void **state); + +#endif // _TEST__F_memory__new diff --git a/level_0/f_memory/tests/c/test-memory-new_aligned.c b/level_0/f_memory/tests/c/test-memory-new_aligned.c new file mode 100644 index 0000000..7849823 --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory-new_aligned.c @@ -0,0 +1,22 @@ +#include "test-memory.h" +#include "test-memory-new_aligned.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void test__f_memory_new_aligned__works(void **state) { + + uint16_t *data = 0; + + const f_status_t status = f_memory_new_aligned(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + + free((void *) data); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_memory/tests/c/test-memory-new_aligned.h b/level_0/f_memory/tests/c/test-memory-new_aligned.h new file mode 100644 index 0000000..a819b5a --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory-new_aligned.h @@ -0,0 +1,20 @@ +/** + * FLL - Level 0 + * + * Project: Memory + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the memory project. + */ +#ifndef _TEST__F_memory__new_aligned +#define _TEST__F_memory__new_aligned + +/** + * Test that allocation works. + * + * @see f_memory_new_aligned() + */ +extern void test__f_memory_new_aligned__works(void **state); + +#endif // _TEST__F_memory__new_aligned diff --git a/level_0/f_memory/tests/c/test-memory-resize.c b/level_0/f_memory/tests/c/test-memory-resize.c new file mode 100644 index 0000000..a0d77dc --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory-resize.c @@ -0,0 +1,31 @@ +#include "test-memory.h" +#include "test-memory-resize.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void test__f_memory_resize__works(void **state) { + + uint16_t *data = 0; + + { + const f_status_t status = f_memory_new(1, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + { + const f_status_t status = f_memory_resize(1, 4, sizeof(uint16_t), (void **) &data); + + assert_int_equal(status, F_none); + assert_non_null(data); + } + + free((void *) data); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_memory/tests/c/test-memory-resize.h b/level_0/f_memory/tests/c/test-memory-resize.h new file mode 100644 index 0000000..fc91c53 --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory-resize.h @@ -0,0 +1,21 @@ +/** + * FLL - Level 0 + * + * Project: Memory + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the memory project. + */ +#ifndef _TEST__F_memory__resize +#define _TEST__F_memory__resize + +/** + * Test that reallocation works. + * + * @see f_memory_new() + * @see f_memory_resize() + */ +extern void test__f_memory_resize__works(void **state); + +#endif // _TEST__F_memory__resize diff --git a/level_0/f_memory/tests/c/test-memory.c b/level_0/f_memory/tests/c/test-memory.c new file mode 100644 index 0000000..9d868c6 --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory.c @@ -0,0 +1,43 @@ +#include "test-memory.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int setup(void **state) { + + return 0; +} + +int setdown(void **state) { + + return 0; +} + +int main(void) { + + const struct CMUnitTest tests[] = { + cmocka_unit_test(test__f_memory_adjust__works), + + cmocka_unit_test(test__f_memory_delete__frees_adjusted_memory), + cmocka_unit_test(test__f_memory_delete__frees_aligned_memory), + cmocka_unit_test(test__f_memory_delete__frees_memory), + cmocka_unit_test(test__f_memory_delete__frees_resized_memory), + + cmocka_unit_test(test__f_memory_destroy__frees_adjusted_memory), + cmocka_unit_test(test__f_memory_destroy__frees_aligned_memory), + cmocka_unit_test(test__f_memory_destroy__frees_memory), + cmocka_unit_test(test__f_memory_destroy__frees_resized_memory), + + cmocka_unit_test(test__f_memory_new__works), + cmocka_unit_test(test__f_memory_new_aligned__works), + + cmocka_unit_test(test__f_memory_resize__works), + }; + + return cmocka_run_group_tests(tests, setup, setdown); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_memory/tests/c/test-memory.h b/level_0/f_memory/tests/c/test-memory.h new file mode 100644 index 0000000..edc1faf --- /dev/null +++ b/level_0/f_memory/tests/c/test-memory.h @@ -0,0 +1,74 @@ +/** + * FLL - Level 0 + * + * Project: Memory + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the memory project. + */ +#ifndef _TEST__F_memory_h +#define _TEST__F_memory_h + +// libc includes +#include +#include +#include +#include + +// cmocka includes +#include + +// fll-0 includes +#include + +// test includes +#include "test-memory-adjust.h" +#include "test-memory-delete.h" +#include "test-memory-destroy.h" +#include "test-memory-new.h" +#include "test-memory-new_aligned.h" +#include "test-memory-resize.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Perform any setup operations. + * + * @param state + * The test state. + * + * @return + * The status of this function, where 0 means success. + */ +extern int setup(void **state); + +/** + * Pefom any setdown operations. + * + * @param state + * The test state. + * + * @return + * The status of this function, where 0 means success. + */ +extern int setdown(void **state); + +/** + * Run all tests. + * + * @return + * The final result of the tests. + * + * @see cmocka_run_group_tests() + * @see cmocka_unit_test() + */ +extern int main(void); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // _TEST__F_memory_h -- 1.8.3.1