Bump clang version to 18 (#14116)
[betaflight.git] / src / test / unit / pwl_unittest.cc
blob9cabce77fab4fcdccb84b9afba6cc7c7c8845e16
1 /*
2 * This file is part of Betaflight.
4 * Betaflight is free software. You can redistribute this software
5 * and/or modify this software under the terms of the GNU General
6 * Public License as published by the Free Software Foundation,
7 * either version 3 of the License, or (at your option) any later
8 * version.
10 * Betaflight is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this software.
19 * If not, see <http://www.gnu.org/licenses/>.
22 #include <stdint.h>
23 #include <stdbool.h>
25 #include <limits.h>
27 #include <math.h>
29 extern "C" {
30 #include "common/pwl.h"
33 #include "unittest_macros.h"
34 #include "gtest/gtest.h"
36 float xSquared(float x, void *args)
38 UNUSED(args);
39 return x * x;
42 PWL_DECLARE(pwlXSquared, 21, 0.0f, 10.0f);
44 TEST(PwlUnittest, TestXSquared21)
46 pwlFill(&pwlXSquared, xSquared, NULL);
48 EXPECT_EQ(pwlInterpolate(&pwlXSquared, -1.0f), 0.0f); // outside of X bounds on the left
49 EXPECT_EQ(pwlInterpolate(&pwlXSquared, 11.0f), 100.0f); // outside of X bounds on the right
50 EXPECT_EQ(pwlInterpolate(&pwlXSquared, 0.0f), 0.0f);
51 EXPECT_EQ(pwlInterpolate(&pwlXSquared, 1.0f), 1.0f);
52 EXPECT_EQ(pwlInterpolate(&pwlXSquared, 2.0f), 4.0f);
53 EXPECT_EQ(pwlInterpolate(&pwlXSquared, 9.0f), 81.0f);
54 EXPECT_EQ(pwlInterpolate(&pwlXSquared, 10.0f), 100.0f);
56 float x = 0.0f;
57 while (x <= 10.0f) {
58 EXPECT_NEAR(pwlInterpolate(&pwlXSquared, x), xSquared(x, NULL), 0.1f);
59 x += 0.1;
64 PWL_DECLARE(pwlXSquaredTwoPoints, 2, 1.0f, 5.0f);
66 TEST(PwlUnittest, TestXSquared2)
68 pwlFill(&pwlXSquaredTwoPoints, xSquared, NULL);
70 EXPECT_EQ(pwlInterpolate(&pwlXSquaredTwoPoints, -1.0f), 1.0f); // outside of X bounds on the left
71 EXPECT_EQ(pwlInterpolate(&pwlXSquaredTwoPoints, 11.0f), 25.0f); // outside of X bounds on the right
72 EXPECT_EQ(pwlInterpolate(&pwlXSquaredTwoPoints, 1.0f), 1.0f);
73 EXPECT_EQ(pwlInterpolate(&pwlXSquaredTwoPoints, 5.0f), 25.0f);
75 EXPECT_EQ(pwlInterpolate(&pwlXSquaredTwoPoints, 3.5f), 16.0f);
79 typedef struct additionalArgs_s {
80 float a;
81 } additionalArgs_t;
83 float xSquaredArgs(float x, void *args)
85 additionalArgs_t *addArgs = (additionalArgs_t*)args;
86 return x * x * addArgs->a;
89 PWL_DECLARE(pwlXSquaredArgs, 21, 0.0f, 10.0f);
91 TEST(PwlUnittest, TestXSquaredArgs)
93 additionalArgs_t args { .a = 2.0 };
94 pwlFill(&pwlXSquaredArgs, xSquaredArgs, &args);
96 EXPECT_EQ(pwlInterpolate(&pwlXSquaredArgs, -1.0f), args.a * 0.0f); // outside of X bounds on the left
97 EXPECT_EQ(pwlInterpolate(&pwlXSquaredArgs, 11.0f), args.a * 100.0f); // outside of X bounds on the right
98 EXPECT_EQ(pwlInterpolate(&pwlXSquaredArgs, 0.0f), args.a * 0.0f);
99 EXPECT_EQ(pwlInterpolate(&pwlXSquaredArgs, 1.0f), args.a * 1.0f);
100 EXPECT_EQ(pwlInterpolate(&pwlXSquaredArgs, 2.0f), args.a * 4.0f);
101 EXPECT_EQ(pwlInterpolate(&pwlXSquaredArgs, 9.0f), args.a * 81.0f);
102 EXPECT_EQ(pwlInterpolate(&pwlXSquaredArgs, 10.0f), args.a * 100.0f);
104 float x = 0.0f;
105 while (x <= 10.0f) {
106 EXPECT_NEAR(pwlInterpolate(&pwlXSquaredArgs, x), xSquaredArgs(x, &args), args.a * 0.1f);
107 x += 0.1;