makes GPIO_PIN_RST optional for the sx1276
[ExpressLRS.git] / src / test / fmap_native / test_fmap.cpp
blob7453b4574f2afbe8eafbc39e0b7ed125b2491e1d
1 #include <cstdint>
2 #include <crsf_protocol.h>
3 #include <unity.h>
4 #include <iostream>
5 #include <bitset>
7 static uint16_t fmapf(uint16_t x, float in_min, float in_max, float out_min, float out_max) { return round((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min); };
9 void test_fmap_consistent_with_float(void)
11 for (int i = 172; i <= 1811; i++)
13 uint16_t iv = fmap(i, 172, 1811, 0, 1023);
14 uint16_t fv = fmapf(i, 172, 1811, 0, 1023);
16 // assert that the integer function is equivalent to the floating point function
17 TEST_ASSERT_EQUAL(fv, iv);
21 void test_fmap_consistent_bider(void)
23 for (int i = 172; i <= 1811; i++)
25 uint16_t iv = fmap(i, 172, 1811, 0, 1023);
26 uint16_t iiv = fmap(iv, 0, 1023, 172, 1811);
27 uint16_t fiv = fmapf(iv, 0, 1023, 172, 1811);
29 // make sure that integer and floats return the same values
30 TEST_ASSERT_EQUAL(fiv, iiv);
32 // make sure that the full cycle value is +/- 1 error given that the input range is under 2x the output range
33 TEST_ASSERT_GREATER_OR_EQUAL(i-1, iiv);
34 TEST_ASSERT_LESS_OR_EQUAL(i+1, iiv);
38 int main(int argc, char **argv)
40 UNITY_BEGIN();
41 RUN_TEST(test_fmap_consistent_with_float);
42 RUN_TEST(test_fmap_consistent_bider);
43 UNITY_END();
45 return 0;