Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / tests / math / mathmishtest.cpp
blobe6523d0c3d8b15513748cefc2e7f9df1a28bb255
1 #include "gtest/gtest.h"
3 #include <stdio.h> /* printf */
4 #include <stdlib.h> /* abort */
5 #include <string.h> /* memset */
7 extern "C" {
8 #include "mathmisc.h"
11 #define epsilon 0.00001f
12 // From pios_math.h
13 #define IS_REAL(f) (!isnan(f) && !isinf(f))
14 #define length(points_array) (sizeof(points_array) / sizeof(points_array[0]))
16 // To use a test fixture, derive a class from testing::Test.
17 class MathTestRaw : public testing::Test {};
19 TEST_F(MathTestRaw, y_on_line0) {
20 pointf points[] = {
21 { 0.0f, -0.30f },
22 { 0.5f, 0.30 }
25 EXPECT_NEAR(-0.60f, y_on_line(-0.25f, &points[0], &points[1]), epsilon);
26 EXPECT_NEAR(-0.30f, y_on_line(0.00f, &points[0], &points[1]), epsilon);
27 EXPECT_NEAR(0.00f, y_on_line(0.25f, &points[0], &points[1]), epsilon);
28 EXPECT_NEAR(0.30f, y_on_line(0.50f, &points[0], &points[1]), epsilon);
29 EXPECT_NEAR(0.60f, y_on_line(0.75f, &points[0], &points[1]), epsilon);
32 TEST_F(MathTestRaw, y_on_line1) {
33 pointf points[] = {
34 { 0.25f, -0.30f },
35 { 0.50f, 0.30 }
38 EXPECT_NEAR(-1.50f, y_on_line(-0.25f, &points[0], &points[1]), epsilon);
39 EXPECT_NEAR(-0.90f, y_on_line(0.00f, &points[0], &points[1]), epsilon);
40 EXPECT_NEAR(-0.30f, y_on_line(0.25f, &points[0], &points[1]), epsilon);
41 EXPECT_NEAR(0.30f, y_on_line(0.50f, &points[0], &points[1]), epsilon);
42 EXPECT_NEAR(0.90f, y_on_line(0.75f, &points[0], &points[1]), epsilon);
45 TEST_F(MathTestRaw, y_on_line2) {
46 pointf points[] = {
47 { -0.25f, -0.30f },
48 { 0.50f, 0.30 }
51 EXPECT_NEAR(-0.30f, y_on_line(-0.25f, &points[0], &points[1]), epsilon);
52 EXPECT_NEAR(-0.10f, y_on_line(0.00f, &points[0], &points[1]), epsilon);
53 EXPECT_NEAR(0.10f, y_on_line(0.25f, &points[0], &points[1]), epsilon);
54 EXPECT_NEAR(0.30f, y_on_line(0.50f, &points[0], &points[1]), epsilon);
55 EXPECT_NEAR(0.50f, y_on_line(0.75f, &points[0], &points[1]), epsilon);
58 TEST_F(MathTestRaw, y_on_line3) {
59 pointf points[] = {
60 { 0.25f, -0.30f },
61 { 0.25f, 0.30 }
65 EXPECT_FALSE(IS_REAL(y_on_line(-0.25f, &points[0], &points[1])));
68 TEST_F(MathTestRaw, y_on_curve0) {
69 pointf points[] = {
70 { 0.00f, -0.40f },
71 { 0.25f, -0.20f },
72 { 0.50f, 0.00f },
73 { 0.75f, 0.20 },
74 { 1.00f, 0.40 }
77 EXPECT_NEAR(-0.50f, y_on_curve(-0.125f, points, length(points)), epsilon);
78 EXPECT_NEAR(-0.40f, y_on_curve(0.000f, points, length(points)), epsilon);
79 EXPECT_NEAR(-0.30f, y_on_curve(0.125f, points, length(points)), epsilon);
80 EXPECT_NEAR(-0.20f, y_on_curve(0.250f, points, length(points)), epsilon);
81 EXPECT_NEAR(-0.10f, y_on_curve(0.375f, points, length(points)), epsilon);
82 EXPECT_NEAR(0.00f, y_on_curve(0.500f, points, length(points)), epsilon);
83 EXPECT_NEAR(0.10f, y_on_curve(0.625f, points, length(points)), epsilon);
84 EXPECT_NEAR(0.20f, y_on_curve(0.750f, points, length(points)), epsilon);
85 EXPECT_NEAR(0.30f, y_on_curve(0.875f, points, length(points)), epsilon);
86 EXPECT_NEAR(0.40f, y_on_curve(1.000f, points, length(points)), epsilon);
87 EXPECT_NEAR(0.50f, y_on_curve(1.125f, points, length(points)), epsilon);
91 TEST_F(MathTestRaw, y_on_curve1) {
92 pointf points[] = {
93 { -0.25f, 0.10f },
94 { 0.00f, 0.20f },
95 { 0.50f, 0.30f },
96 { 1.00f, -0.30 },
97 { 2.00f, -0.50 }
100 EXPECT_NEAR(0.00f, y_on_curve(-0.500f, points, length(points)), epsilon);
101 EXPECT_NEAR(0.10f, y_on_curve(-0.250f, points, length(points)), epsilon);
102 EXPECT_NEAR(0.15f, y_on_curve(-0.125f, points, length(points)), epsilon);
103 EXPECT_NEAR(0.20f, y_on_curve(0.000f, points, length(points)), epsilon);
104 EXPECT_NEAR(0.22f, y_on_curve(0.100f, points, length(points)), epsilon);
105 EXPECT_NEAR(0.30f, y_on_curve(0.500f, points, length(points)), epsilon);
106 EXPECT_NEAR(0.00f, y_on_curve(0.750f, points, length(points)), epsilon);
107 EXPECT_NEAR(-0.30f, y_on_curve(1.000f, points, length(points)), epsilon);
108 EXPECT_NEAR(-0.35f, y_on_curve(1.250f, points, length(points)), epsilon);
109 EXPECT_NEAR(-0.50f, y_on_curve(2.000f, points, length(points)), epsilon);