From 1b9856c1925674397f1255dd996ab9693d0eb871 Mon Sep 17 00:00:00 2001 From: jake Date: Wed, 30 Aug 2023 20:16:47 -0700 Subject: [PATCH] added tests --- tests/CMakeLists.txt | 1 + tests/gy521_driver/CMakeLists.txt | 13 +++++++ tests/gy521_driver/test_gy521_driver.c | 50 ++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 tests/gy521_driver/CMakeLists.txt create mode 100644 tests/gy521_driver/test_gy521_driver.c diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 35b495e..65bf0dc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,3 +2,4 @@ # add_subdirectory(led_driver) add_subdirectory(simple_test) +add_subdirectory(gy521_driver) diff --git a/tests/gy521_driver/CMakeLists.txt b/tests/gy521_driver/CMakeLists.txt new file mode 100644 index 0000000..73525af --- /dev/null +++ b/tests/gy521_driver/CMakeLists.txt @@ -0,0 +1,13 @@ +# The gy521_driver module tests +list(APPEND TEST_LIBS "${CMOCKA_LIBRARIES}") +list(APPEND TEST_LIBS gy521_driver) + +list(APPEND TEST_DIRS "${CMOCKA_INCLUDE_DIRS}") +list(APPEND TEST_DIRS "${MyProject_SOURCE_DIR}/src") + +add_cmocka_test(test_gy521_driver + SOURCES test_gy521_driver.c + COMPILE_OPTIONS ${DEFAULT_C_COMPILE_FLAGS} + LINK_LIBRARIES "${TEST_LIBS}") +add_cmocka_test_environment(test_gy521_driver) +target_include_directories(test_gy521_driver PUBLIC "${TEST_DIRS}") diff --git a/tests/gy521_driver/test_gy521_driver.c b/tests/gy521_driver/test_gy521_driver.c new file mode 100644 index 0000000..2784f1b --- /dev/null +++ b/tests/gy521_driver/test_gy521_driver.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include "gy521_driver.h" + + +/* A test case that does nothing and succeeds. */ +static void null_test_success(void **state) { + (void) state; /* unused */ +} + +/* Fake Object for TWI_TX*/ +void fake_twi_tx(uint8_t slave_addr, uint8_t *data, uint8_t size) +{ + +} + +/* Tests the donothing function */ +static void test_gy521_init(void **state) { + + _Bool result = init_gy521(0); + assert_false(result); + + result = init_gy521(1); + assert_false(result); + + result = init_gy521((uint8_t) TWI_GY521_ADDR1); + assert_true(result); + +} + +static void test_gy521_module_methods(void **state) { + gy521_module *module = malloc(sizeof(gy521_module)); + assert_true(module_test(*module)); +} + + + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(null_test_success), + cmocka_unit_test(test_gy521_init), + cmocka_unit_test(test_gy521_module_methods), + }; + return cmocka_run_group_tests(tests, NULL, NULL); +}