diff options
author | David Phillips <david@yeah.nah.nz> | 2021-02-20 17:43:24 +1300 |
---|---|---|
committer | David Phillips <david@yeah.nah.nz> | 2021-02-27 21:54:56 +1300 |
commit | 27d6d2fc5a1647395a7a9074faf8362d6d0c358a (patch) | |
tree | 81f81c6b4a5a835e01968e0059271bb90c3768ab /test_data_manager.c | |
parent | e843e9da835f058e09810cfeba7c60d0b270e7b3 (diff) | |
download | altimeter-27d6d2fc5a1647395a7a9074faf8362d6d0c358a.tar.xz |
Use more generic interface for barometer and timer
This patch abstracts the global symbols for getting barometer readings behind
a "struct of function pointers" interface as popular in Linux Kernel, U-Boot
and others. This means that unit testing can take place with mocked hardware
peripherals. The old "global" drivers are still available, behind explicit
function calls required to access the now-static functions.
This patch also introduces a timer peripheral software module with the same
model, to support future unit testing of altitude rate etc.
Diffstat (limited to 'test_data_manager.c')
-rw-r--r-- | test_data_manager.c | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/test_data_manager.c b/test_data_manager.c index f21cc97..00b5a2a 100644 --- a/test_data_manager.c +++ b/test_data_manager.c @@ -2,32 +2,41 @@ #include "data_manager.h" #include "test_runner.h" +#include "barometer.h" #include "unity.h" +static int mock_baro_called; + +static void reset_mock_get_baro(void) { + mock_baro_called = 0; +} + +static float set_mock_get_baro(void) { + mock_baro_called = 1; + return 1.0; +}; + +static struct barometer mock_barometer = { + .read_pressure = set_mock_get_baro, +}; + RUNNER_DECLARE_TEST(test_data_manager_init_setting) { struct data_ctx ctx; - data_manager_init(&ctx); - TEST_ASSERT_FLOAT_WITHIN(FLT_EPSILON, 1013.25, ctx.setting); + reset_mock_get_baro(); + data_manager_init(&ctx, NULL, &mock_barometer); + TEST_ASSERT_EQUAL(1, mock_baro_called); } RUNNER_DECLARE_TEST(test_data_manager_first_readings) { struct data_ctx ctx; - data_manager_init(&ctx); - TEST_ASSERT_FLOAT_WITHIN(FLT_EPSILON, 1019.5, ctx.pressure); - data_manager_tick(&ctx); - TEST_ASSERT_FLOAT_WITHIN(FLT_EPSILON, 1019.45, ctx.pressure); -} + reset_mock_get_baro(); + data_manager_init(&ctx, NULL, &mock_barometer); + TEST_ASSERT_EQUAL(1, mock_baro_called); -RUNNER_DECLARE_TEST(test_data_manager_reinit) -{ - struct data_ctx ctx; - /* init 1 */ - data_manager_init(&ctx); - TEST_ASSERT_FLOAT_WITHIN(FLT_EPSILON, 1019.5, ctx.pressure); - /* check re-init */ - data_manager_init(&ctx); - TEST_ASSERT_FLOAT_WITHIN(FLT_EPSILON, 1019.5, ctx.pressure); + reset_mock_get_baro(); + data_manager_tick(&ctx); + TEST_ASSERT_EQUAL(1, mock_baro_called); } |