From 51ec2d6773a69e9046651f2233fc8be351695e54 Mon Sep 17 00:00:00 2001 From: Steveis Date: Tue, 23 Jun 2015 17:41:22 +0100 Subject: [PATCH] MS5611 CRC Unit test --- src/main/drivers/barometer_ms5611.c | 6 ++- src/test/Makefile | 26 +++++++++- src/test/unit/baro_unittest.cc | 99 +++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 3 deletions(-) create mode 100644 src/test/unit/baro_unittest.cc diff --git a/src/main/drivers/barometer_ms5611.c b/src/main/drivers/barometer_ms5611.c index 7ff5b3f0f..897983db7 100644 --- a/src/main/drivers/barometer_ms5611.c +++ b/src/main/drivers/barometer_ms5611.c @@ -26,6 +26,8 @@ #include "system.h" #include "bus_i2c.h" +#include "build_config.h" + // MS5611, Standard address 0x77 #define MS5611_ADDR 0x77 @@ -44,7 +46,7 @@ static void ms5611_reset(void); static uint16_t ms5611_prom(int8_t coef_num); -static int8_t ms5611_crc(uint16_t *prom); +STATIC_UNIT_TESTED int8_t ms5611_crc(uint16_t *prom); static uint32_t ms5611_read_adc(void); static void ms5611_start_ut(void); static void ms5611_get_ut(void); @@ -102,7 +104,7 @@ static uint16_t ms5611_prom(int8_t coef_num) return rxbuf[0] << 8 | rxbuf[1]; } -static int8_t ms5611_crc(uint16_t *prom) +STATIC_UNIT_TESTED int8_t ms5611_crc(uint16_t *prom) { int32_t i, j; uint32_t res = 0; diff --git a/src/test/Makefile b/src/test/Makefile index f46dc69fc..b573acf77 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -58,7 +58,8 @@ TESTS = \ ws2811_unittest \ encoding_unittest \ io_serial_unittest \ - lowpass_unittest + lowpass_unittest \ + baro_unittest # All Google Test headers. Usually you shouldn't change this # definition. @@ -463,6 +464,29 @@ rx_rx_unittest : \ $(CXX) $(CXX_FLAGS) $^ -o $(OBJECT_DIR)/$@ +$(OBJECT_DIR)/drivers/barometer_ms5611.o : \ + $(USER_DIR)/drivers/barometer_ms5611.c \ + $(USER_DIR)/drivers/barometer_ms5611.h \ + $(GTEST_HEADERS) + + @mkdir -p $(dir $@) + $(CC) $(C_FLAGS) $(TEST_CFLAGS) -c $(USER_DIR)/drivers/barometer_ms5611.c -o $@ + +$(OBJECT_DIR)/baro_unittest.o : \ + $(TEST_DIR)/baro_unittest.cc \ + $(USER_DIR)/drivers/barometer_ms5611.h \ + $(GTEST_HEADERS) + + @mkdir -p $(dir $@) + $(CXX) $(CXX_FLAGS) $(TEST_CFLAGS) -c $(TEST_DIR)/baro_unittest.cc -o $@ + +baro_unittest : \ + $(OBJECT_DIR)/drivers/barometer_ms5611.o \ + $(OBJECT_DIR)/baro_unittest.o \ + $(OBJECT_DIR)/gtest_main.a + + $(CXX) $(CXX_FLAGS) $^ -o $(OBJECT_DIR)/$@ + test: $(TESTS) set -e && for test in $(TESTS) ; do \ $(OBJECT_DIR)/$$test; \ diff --git a/src/test/unit/baro_unittest.cc b/src/test/unit/baro_unittest.cc new file mode 100644 index 000000000..b84052c27 --- /dev/null +++ b/src/test/unit/baro_unittest.cc @@ -0,0 +1,99 @@ +/* + * This file is part of Cleanflight. + * + * Cleanflight is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Cleanflight is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Cleanflight. If not, see . + */ +#include + +extern "C" { + +int8_t ms5611_crc(uint16_t *prom); + +} + + +#include "unittest_macros.h" +#include "gtest/gtest.h" + + +TEST(baroTest, TestValidMs5611Crc) +{ + + // given + uint16_t ms5611_prom[] = {0x3132,0x3334,0x3536,0x3738,0x3940,0x4142,0x4344,0x450B}; + + // when + int8_t result = ms5611_crc(ms5611_prom); + + // then + EXPECT_EQ(0, result); + +} + +TEST(baroTest, TestInvalidMs5611Crc) +{ + + // given + uint16_t ms5611_prom[] = {0x3132,0x3334,0x3536,0x3738,0x3940,0x4142,0x4344,0x4500}; + + // when + int8_t result = ms5611_crc(ms5611_prom); + + // then + EXPECT_EQ(-1, result); + +} + +TEST(baroTest, TestMs5611AllZeroProm) +{ + + // given + uint16_t ms5611_prom[] = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000}; + + // when + int8_t result = ms5611_crc(ms5611_prom); + + // then + EXPECT_EQ(-1, result); + +} + +TEST(baroTest, TestMs5611AllOnesProm) +{ + + // given + uint16_t ms5611_prom[] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF,0xFFFF}; + + // when + int8_t result = ms5611_crc(ms5611_prom); + + // then + EXPECT_EQ(-1, result); + +} + +// STUBS + +extern "C" { + +void delay(uint32_t) {} +void delayMicroseconds(uint32_t) {} +bool i2cWrite(uint8_t, uint8_t, uint8_t) { + return 1; +} +bool i2cRead(uint8_t, uint8_t, uint8_t, uint8_t) { + return 1; +} + +} -- 2.11.4.GIT