blob: 92a2b76338535c935d8b218861200888e6e68030 [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
Philipp Schradere8ad6382017-04-09 21:51:21 +000011namespace frc971 {
12namespace shooter_interpolation {
13
Parker Schuh94d56792017-04-13 20:32:50 -070014struct TestShotParams {
15 double angle;
16 double power;
Philipp Schrader790cb542023-07-05 21:06:52 -070017 static TestShotParams BlendY(double x, const TestShotParams &a,
18 const TestShotParams &b) {
19 return TestShotParams{Blend(x, a.angle, b.angle),
20 Blend(x, a.power, b.power)};
Parker Schuh94d56792017-04-13 20:32:50 -070021 }
22};
23
24bool operator==(TestShotParams a1, TestShotParams a2) {
Philipp Schradere8ad6382017-04-09 21:51:21 +000025 return a1.angle == a2.angle && a1.power == a2.power;
26}
27
Parker Schuh94d56792017-04-13 20:32:50 -070028using TestInterpolationTable = InterpolationTable<TestShotParams>;
29
Philipp Schradere8ad6382017-04-09 21:51:21 +000030// Tests to see if distances whose values are on the table are processed
31// correctly
32TEST(InterpolationTable, ExactNumbers) {
Parker Schuh94d56792017-04-13 20:32:50 -070033 ::std::vector<::std::pair<double, TestShotParams>> data = {
Philipp Schrader790cb542023-07-05 21:06:52 -070034 {1, {10, 10}},
35 {3, {20, 20}},
36 {2, {15, 12345678}},
37 {4, {10, 567.323}},
Philipp Schradere8ad6382017-04-09 21:51:21 +000038 };
39
Parker Schuh94d56792017-04-13 20:32:50 -070040 TestInterpolationTable interpolation(data);
41 ASSERT_EQ(data[1].second, interpolation.Get(3));
42 ASSERT_EQ(data[3].second, interpolation.Get(4));
Philipp Schradere8ad6382017-04-09 21:51:21 +000043}
44
45// Tests to see if distances whose values are off the table are processed
46// correctly
47TEST(InterpolationTable, InexactNumbers) {
Parker Schuh94d56792017-04-13 20:32:50 -070048 ::std::vector<::std::pair<double, TestShotParams>> data = {
Philipp Schrader790cb542023-07-05 21:06:52 -070049 {1, {10, 10}},
50 {3, {20, 20}},
51 {2, {15, 15}},
52 {4, {10, 567.323}},
Philipp Schradere8ad6382017-04-09 21:51:21 +000053 };
54
Parker Schuh94d56792017-04-13 20:32:50 -070055 TestInterpolationTable interpolation(data);
56 ASSERT_EQ(TestShotParams({12.5, 12.5}), interpolation.Get(1.5));
57 ASSERT_EQ(TestShotParams({10, 10}), interpolation.Get(0));
Philipp Schradere8ad6382017-04-09 21:51:21 +000058}
59
60// Tests to see if distances whose values are beyond the range of the table are
61// processed correctly
62TEST(InterpolationTable, OutOfScopeNumbers) {
Parker Schuh94d56792017-04-13 20:32:50 -070063 ::std::vector<::std::pair<double, TestShotParams>> data = {
Philipp Schrader790cb542023-07-05 21:06:52 -070064 {1, {10, 10}},
65 {3, {20, 20}},
66 {2, {15, 12345678}},
67 {4, {10, 567.323}},
Philipp Schradere8ad6382017-04-09 21:51:21 +000068 };
69
Parker Schuh94d56792017-04-13 20:32:50 -070070 TestInterpolationTable interpolation(data);
71 ASSERT_EQ(TestShotParams({10, 10}), interpolation.Get(0));
72 ASSERT_EQ(TestShotParams({10, 567.323}), interpolation.Get(5));
Philipp Schradere8ad6382017-04-09 21:51:21 +000073}
74
75} // namespace shooter_interpolation
76} // namespace frc971