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
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/>.
30 #include "common/pwl.h"
33 #include "unittest_macros.h"
34 #include "gtest/gtest.h"
36 float xSquared(float x
, void *args
)
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
);
58 EXPECT_NEAR(pwlInterpolate(&pwlXSquared
, x
), xSquared(x
, NULL
), 0.1f
);
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
{
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
);
106 EXPECT_NEAR(pwlInterpolate(&pwlXSquaredArgs
, x
), xSquaredArgs(x
, &args
), args
.a
* 0.1f
);