blob: 340928207bd8dfda6058fd20c710e481b2c166ec [file] [log] [blame]
Philipp Schrader790cb542023-07-05 21:06:52 -07001#include "frc971/shooter_interpolation/interpolation.h"
2
Philipp Schradere8ad6382017-04-09 21:51:21 +00003#include <unistd.h>
4
5#include <memory>
6#include <random>
7#include <utility>
8
9#include "gtest/gtest.h"
10
Stephan Pleinesf63bde82024-01-13 15:59:33 -080011namespace frc971::shooter_interpolation {
Philipp Schradere8ad6382017-04-09 21:51:21 +000012
Parker Schuh94d56792017-04-13 20:32:50 -070013struct TestShotParams {
14 double angle;
15 double power;
Philipp Schrader790cb542023-07-05 21:06:52 -070016 static TestShotParams BlendY(double x, const TestShotParams &a,
17 const TestShotParams &b) {
18 return TestShotParams{Blend(x, a.angle, b.angle),
19 Blend(x, a.power, b.power)};
Parker Schuh94d56792017-04-13 20:32:50 -070020 }
21};
22
23bool operator==(TestShotParams a1, TestShotParams a2) {
Philipp Schradere8ad6382017-04-09 21:51:21 +000024 return a1.angle == a2.angle && a1.power == a2.power;
25}
26
Parker Schuh94d56792017-04-13 20:32:50 -070027using TestInterpolationTable = InterpolationTable<TestShotParams>;
28
Philipp Schradere8ad6382017-04-09 21:51:21 +000029// Tests to see if distances whose values are on the table are processed
30// correctly
31TEST(InterpolationTable, ExactNumbers) {
Parker Schuh94d56792017-04-13 20:32:50 -070032 ::std::vector<::std::pair<double, TestShotParams>> data = {
Philipp Schrader790cb542023-07-05 21:06:52 -070033 {1, {10, 10}},
34 {3, {20, 20}},
35 {2, {15, 12345678}},
36 {4, {10, 567.323}},
Philipp Schradere8ad6382017-04-09 21:51:21 +000037 };
38
Parker Schuh94d56792017-04-13 20:32:50 -070039 TestInterpolationTable interpolation(data);
40 ASSERT_EQ(data[1].second, interpolation.Get(3));
41 ASSERT_EQ(data[3].second, interpolation.Get(4));
Philipp Schradere8ad6382017-04-09 21:51:21 +000042}
43
44// Tests to see if distances whose values are off the table are processed
45// correctly
46TEST(InterpolationTable, InexactNumbers) {
Parker Schuh94d56792017-04-13 20:32:50 -070047 ::std::vector<::std::pair<double, TestShotParams>> data = {
Philipp Schrader790cb542023-07-05 21:06:52 -070048 {1, {10, 10}},
49 {3, {20, 20}},
50 {2, {15, 15}},
51 {4, {10, 567.323}},
Philipp Schradere8ad6382017-04-09 21:51:21 +000052 };
53
Parker Schuh94d56792017-04-13 20:32:50 -070054 TestInterpolationTable interpolation(data);
55 ASSERT_EQ(TestShotParams({12.5, 12.5}), interpolation.Get(1.5));
56 ASSERT_EQ(TestShotParams({10, 10}), interpolation.Get(0));
Philipp Schradere8ad6382017-04-09 21:51:21 +000057}
58
59// Tests to see if distances whose values are beyond the range of the table are
60// processed correctly
61TEST(InterpolationTable, OutOfScopeNumbers) {
Parker Schuh94d56792017-04-13 20:32:50 -070062 ::std::vector<::std::pair<double, TestShotParams>> data = {
Philipp Schrader790cb542023-07-05 21:06:52 -070063 {1, {10, 10}},
64 {3, {20, 20}},
65 {2, {15, 12345678}},
66 {4, {10, 567.323}},
Philipp Schradere8ad6382017-04-09 21:51:21 +000067 };
68
Parker Schuh94d56792017-04-13 20:32:50 -070069 TestInterpolationTable interpolation(data);
70 ASSERT_EQ(TestShotParams({10, 10}), interpolation.Get(0));
71 ASSERT_EQ(TestShotParams({10, 567.323}), interpolation.Get(5));
Philipp Schradere8ad6382017-04-09 21:51:21 +000072}
73
Stephan Pleinesf63bde82024-01-13 15:59:33 -080074} // namespace frc971::shooter_interpolation