blob: fd991249f48ee8d191650f405cfb4b8f76488515 [file] [log] [blame]
Philipp Schradere8ad6382017-04-09 21:51:21 +00001#include <unistd.h>
2
3#include <memory>
4#include <random>
5#include <utility>
6
7#include "gtest/gtest.h"
8
9#include "frc971/shooter_interpolation/interpolation.h"
10
11namespace frc971 {
12namespace shooter_interpolation {
13
Parker Schuh94d56792017-04-13 20:32:50 -070014struct TestShotParams {
15 double angle;
16 double power;
17 static TestShotParams BlendY(double x, const TestShotParams& a, const TestShotParams& b) {
18 return TestShotParams{
19 Blend(x, a.angle, b.angle),
20 Blend(x, a.power, b.power)
21 };
22 }
23};
24
25bool operator==(TestShotParams a1, TestShotParams a2) {
Philipp Schradere8ad6382017-04-09 21:51:21 +000026 return a1.angle == a2.angle && a1.power == a2.power;
27}
28
Parker Schuh94d56792017-04-13 20:32:50 -070029using TestInterpolationTable = InterpolationTable<TestShotParams>;
30
Philipp Schradere8ad6382017-04-09 21:51:21 +000031// Tests to see if distances whose values are on the table are processed
32// correctly
33TEST(InterpolationTable, ExactNumbers) {
Parker Schuh94d56792017-04-13 20:32:50 -070034 ::std::vector<::std::pair<double, TestShotParams>> data = {
Philipp Schradere8ad6382017-04-09 21:51:21 +000035 {1, {10, 10}}, {3, {20, 20}}, {2, {15, 12345678}}, {4, {10, 567.323}},
36 };
37
Parker Schuh94d56792017-04-13 20:32:50 -070038 TestInterpolationTable interpolation(data);
39 ASSERT_EQ(data[1].second, interpolation.Get(3));
40 ASSERT_EQ(data[3].second, interpolation.Get(4));
Philipp Schradere8ad6382017-04-09 21:51:21 +000041}
42
43// Tests to see if distances whose values are off the table are processed
44// correctly
45TEST(InterpolationTable, InexactNumbers) {
Parker Schuh94d56792017-04-13 20:32:50 -070046 ::std::vector<::std::pair<double, TestShotParams>> data = {
Philipp Schradere8ad6382017-04-09 21:51:21 +000047 {1, {10, 10}}, {3, {20, 20}}, {2, {15, 15}}, {4, {10, 567.323}},
48 };
49
Parker Schuh94d56792017-04-13 20:32:50 -070050 TestInterpolationTable interpolation(data);
51 ASSERT_EQ(TestShotParams({12.5, 12.5}), interpolation.Get(1.5));
52 ASSERT_EQ(TestShotParams({10, 10}), interpolation.Get(0));
Philipp Schradere8ad6382017-04-09 21:51:21 +000053}
54
55// Tests to see if distances whose values are beyond the range of the table are
56// processed correctly
57TEST(InterpolationTable, OutOfScopeNumbers) {
Parker Schuh94d56792017-04-13 20:32:50 -070058 ::std::vector<::std::pair<double, TestShotParams>> data = {
Philipp Schradere8ad6382017-04-09 21:51:21 +000059 {1, {10, 10}}, {3, {20, 20}}, {2, {15, 12345678}}, {4, {10, 567.323}},
60 };
61
Parker Schuh94d56792017-04-13 20:32:50 -070062 TestInterpolationTable interpolation(data);
63 ASSERT_EQ(TestShotParams({10, 10}), interpolation.Get(0));
64 ASSERT_EQ(TestShotParams({10, 567.323}), interpolation.Get(5));
Philipp Schradere8ad6382017-04-09 21:51:21 +000065}
66
67} // namespace shooter_interpolation
68} // namespace frc971