blob: e195385e27721181eff107755d95232e09f5d46f [file] [log] [blame]
Austin Schuhc6423e62017-02-11 16:56:30 -08001#include <random>
2
3#include "frc971/zeroing/wrap.h"
4#include "gtest/gtest.h"
5
6namespace frc971 {
7namespace zeroing {
8namespace testing {
9
10// Tests some various positive and negative values for wrap.
11TEST(WrapTest, TestWrap) {
12 EXPECT_NEAR(1.0, Wrap(0.0, 1.0, 10.0), 1e-6);
13 EXPECT_NEAR(-1.0, Wrap(0.0, -1.0, 10.0), 1e-6);
14
15 EXPECT_NEAR(1.0, Wrap(5.0, 1.0, 10.0), 1e-6);
16 EXPECT_NEAR(9.0, Wrap(5.0, -1.0, 10.0), 1e-6);
17
18 EXPECT_NEAR(10.0, Wrap(5.0, 10.0, 10.0), 1e-6);
19 EXPECT_NEAR(1.0, Wrap(5.0, -9.0, 10.0), 1e-6);
20}
21
22// Try a bunch of combinations and verify that the result has the right
23// properties. We can be really inefficient here since it is a test.
24TEST(WrapTest, ExhaustiveWrap) {
25 for (double i = -20; i < 20; ++i) {
26 for (double j = -20; j < 20; ++j) {
27 EXPECT_NEAR(i, Wrap(i, j, 10.0), 5.0);
28 const double wrapped_val = Wrap(i, j, 10.0);
29 bool found_interval = false;
30 for (int k = -5; k < 5; ++k) {
31 if (::std::abs(k * 10 + wrapped_val - j) < 1e-6) {
32 found_interval = true;
33 break;
34 }
35 }
36 EXPECT_TRUE(found_interval) << ": Wrap(" << i << ", " << j
37 << ") = " << wrapped_val;
38 }
39 }
40}
41
42} // namespace testing
43} // namespace zeroing
44} // namespace frc971