Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 1 | #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 | |
| 11 | namespace frc971 { |
| 12 | namespace shooter_interpolation { |
| 13 | |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 14 | struct 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 | |
| 25 | bool operator==(TestShotParams a1, TestShotParams a2) { |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 26 | return a1.angle == a2.angle && a1.power == a2.power; |
| 27 | } |
| 28 | |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 29 | using TestInterpolationTable = InterpolationTable<TestShotParams>; |
| 30 | |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 31 | // Tests to see if distances whose values are on the table are processed |
| 32 | // correctly |
| 33 | TEST(InterpolationTable, ExactNumbers) { |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 34 | ::std::vector<::std::pair<double, TestShotParams>> data = { |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 35 | {1, {10, 10}}, {3, {20, 20}}, {2, {15, 12345678}}, {4, {10, 567.323}}, |
| 36 | }; |
| 37 | |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 38 | TestInterpolationTable interpolation(data); |
| 39 | ASSERT_EQ(data[1].second, interpolation.Get(3)); |
| 40 | ASSERT_EQ(data[3].second, interpolation.Get(4)); |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // Tests to see if distances whose values are off the table are processed |
| 44 | // correctly |
| 45 | TEST(InterpolationTable, InexactNumbers) { |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 46 | ::std::vector<::std::pair<double, TestShotParams>> data = { |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 47 | {1, {10, 10}}, {3, {20, 20}}, {2, {15, 15}}, {4, {10, 567.323}}, |
| 48 | }; |
| 49 | |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 50 | 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 Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // Tests to see if distances whose values are beyond the range of the table are |
| 56 | // processed correctly |
| 57 | TEST(InterpolationTable, OutOfScopeNumbers) { |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 58 | ::std::vector<::std::pair<double, TestShotParams>> data = { |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 59 | {1, {10, 10}}, {3, {20, 20}}, {2, {15, 12345678}}, {4, {10, 567.323}}, |
| 60 | }; |
| 61 | |
Parker Schuh | 94d5679 | 2017-04-13 20:32:50 -0700 | [diff] [blame] | 62 | TestInterpolationTable interpolation(data); |
| 63 | ASSERT_EQ(TestShotParams({10, 10}), interpolation.Get(0)); |
| 64 | ASSERT_EQ(TestShotParams({10, 567.323}), interpolation.Get(5)); |
Philipp Schrader | e8ad638 | 2017-04-09 21:51:21 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | } // namespace shooter_interpolation |
| 68 | } // namespace frc971 |