commence breakage
[inav.git] / src / test / unit / baro_bmp280_unittest.cc.txt
blobe937dc494b05923f8b6a97ab0e0fdac4203cf0ae
1 /*
2  * This file is part of Cleanflight.
3  *
4  * Cleanflight is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Cleanflight is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Cleanflight.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include <stdint.h>
19 extern "C" {
21     void bmp280_calculate(int32_t *pressure, int32_t *temperature);
22     extern uint32_t bmp280_up;
23     extern uint32_t bmp280_ut;
25 typedef struct bmp280_calib_param_s {
26     uint16_t dig_T1; /* calibration T1 data */
27     int16_t dig_T2; /* calibration T2 data */
28     int16_t dig_T3; /* calibration T3 data */
29     uint16_t dig_P1; /* calibration P1 data */
30     int16_t dig_P2; /* calibration P2 data */
31     int16_t dig_P3; /* calibration P3 data */
32     int16_t dig_P4; /* calibration P4 data */
33     int16_t dig_P5; /* calibration P5 data */
34     int16_t dig_P6; /* calibration P6 data */
35     int16_t dig_P7; /* calibration P7 data */
36     int16_t dig_P8; /* calibration P8 data */
37     int16_t dig_P9; /* calibration P9 data */
38     int32_t t_fine; /* calibration t_fine data */
39 } bmp280_calib_param_t;
41     bmp280_calib_param_t bmp280_cal;
45 #include "unittest_macros.h"
46 #include "gtest/gtest.h"
49 TEST(baroBmp280Test, TestBmp280Calculate)
52     // given
53     int32_t pressure, temperature;
54     bmp280_up = 415148; // Digital pressure value
55     bmp280_ut = 519888; // Digital temperature value
57     // and
58     bmp280_cal.dig_T1 = 27504;
59     bmp280_cal.dig_T2 = 26435;
60     bmp280_cal.dig_T3 = -1000;
61     bmp280_cal.dig_P1 = 36477;
62     bmp280_cal.dig_P2 = -10685;
63     bmp280_cal.dig_P3 = 3024;
64     bmp280_cal.dig_P4 = 2855;
65     bmp280_cal.dig_P5 = 140;
66     bmp280_cal.dig_P6 = -7;
67     bmp280_cal.dig_P7 = 15500;
68     bmp280_cal.dig_P8 = -14600;
69     bmp280_cal.dig_P9 = 6000;
71     // when
72     bmp280_calculate(&pressure, &temperature);
74     // then
75     EXPECT_EQ(100653, pressure); // 100653 Pa
76     EXPECT_EQ(2508, temperature); // 25.08 degC
80 TEST(baroBmp280Test, TestBmp280CalculateHighP)
83     // given
84     int32_t pressure, temperature;
85     bmp280_up = 215148; // Digital pressure value
86     bmp280_ut = 519888; // Digital temperature value
88     // and
89     bmp280_cal.dig_T1 = 27504;
90     bmp280_cal.dig_T2 = 26435;
91     bmp280_cal.dig_T3 = -1000;
92     bmp280_cal.dig_P1 = 36477;
93     bmp280_cal.dig_P2 = -10685;
94     bmp280_cal.dig_P3 = 3024;
95     bmp280_cal.dig_P4 = 2855;
96     bmp280_cal.dig_P5 = 140;
97     bmp280_cal.dig_P6 = -7;
98     bmp280_cal.dig_P7 = 15500;
99     bmp280_cal.dig_P8 = -14600;
100     bmp280_cal.dig_P9 = 6000;
102     // when
103     bmp280_calculate(&pressure, &temperature);
105     // then
106     EXPECT_EQ(135382, pressure); // 135385 Pa
107     EXPECT_EQ(2508, temperature); // 25.08 degC
111 TEST(baroBmp280Test, TestBmp280CalculateZeroP)
114     // given
115     int32_t pressure, temperature;
116     bmp280_up = 415148; // Digital pressure value
117     bmp280_ut = 519888; // Digital temperature value
119     // and
120     bmp280_cal.dig_T1 = 27504;
121     bmp280_cal.dig_T2 = 26435;
122     bmp280_cal.dig_T3 = -1000;
123     bmp280_cal.dig_P1 = 0;
124     bmp280_cal.dig_P2 = -10685;
125     bmp280_cal.dig_P3 = 3024;
126     bmp280_cal.dig_P4 = 2855;
127     bmp280_cal.dig_P5 = 140;
128     bmp280_cal.dig_P6 = -7;
129     bmp280_cal.dig_P7 = 15500;
130     bmp280_cal.dig_P8 = -14600;
131     bmp280_cal.dig_P9 = 6000;
133     // when
134     bmp280_calculate(&pressure, &temperature);
136     // then
137     EXPECT_EQ(0, pressure); // P1=0 trips pressure to 0 Pa, avoiding division by zero
138     EXPECT_EQ(2508, temperature); // 25.08 degC
142 // STUBS
144 extern "C" {
146     void delay(uint32_t) {}
147     bool i2cWrite(uint8_t, uint8_t, uint8_t) {
148         return 1;
149     }
150     bool i2cRead(uint8_t, uint8_t, uint8_t, uint8_t) {
151         return 1;
152     }