blob: 672ee6916ab674b8cf99c37eeb3852cd5b6a2657 [file] [log] [blame]
Brian Silverman8d3816a2017-07-03 18:52:15 -07001#include "motors/math.h"
2
3#include "gtest/gtest.h"
4#include "gmock/gmock.h"
5
6namespace frc971 {
7namespace salsa {
8namespace testing {
9
10class SinCosIntTest : public ::testing::Test {
11 public:
12 void SetUp() override {
13 MathInit();
14 }
15
16 template <class Rotation>
17 void CheckSinCos() {
18 static constexpr float kTolerance = 0.004;
19 for (uint32_t theta = 0; theta < Rotation::den; ++theta) {
20 EXPECT_THAT(
21 FastSinInt<Rotation>(theta),
22 ::testing::FloatNear(sin(ThetaToFloat<Rotation>(theta)), kTolerance));
23 EXPECT_THAT(
24 FastCosInt<Rotation>(theta),
25 ::testing::FloatNear(cos(ThetaToFloat<Rotation>(theta)), kTolerance));
26 }
27 }
28
29 private:
30 template <class Rotation>
31 double ThetaToFloat(uint32_t theta) {
32 const double rotation_double =
33 static_cast<double>(Rotation::num) / static_cast<double>(Rotation::den);
34 return (static_cast<double>(theta) + 0.5) * rotation_double * 2.0 * M_PI;
35 }
36};
37
38TEST_F(SinCosIntTest, Numerator1) {
39 CheckSinCos<::std::ratio<1, 2>>();
40 CheckSinCos<::std::ratio<1, 4>>();
41 CheckSinCos<::std::ratio<1, 8>>();
42 CheckSinCos<::std::ratio<1, 128>>();
43 CheckSinCos<::std::ratio<1, 1024>>();
44}
45
46TEST_F(SinCosIntTest, Numerator5) {
47 CheckSinCos<::std::ratio<5, 8>>();
48 CheckSinCos<::std::ratio<5, 128>>();
49 CheckSinCos<::std::ratio<5, 1024>>();
50}
51
52class SinCosFloatTest : public ::testing::Test {
53 public:
54 void SetUp() override {
55 MathInit();
56 }
57
58 void CheckSinCos(float theta) {
59 ASSERT_GE(theta, -0.2f);
60 ASSERT_LE(theta, 0.2f);
61
62 static constexpr float kTolerance = 0.002;
63 EXPECT_THAT(FastSinFloat(theta),
64 ::testing::FloatNear(sin(theta), kTolerance));
65 EXPECT_THAT(FastCosFloat(theta),
66 ::testing::FloatNear(cos(theta), kTolerance));
67 }
68};
69
70TEST_F(SinCosFloatTest, Endpoints) {
71 CheckSinCos(0);
72 CheckSinCos(-0.2);
73 CheckSinCos(0.2);
74}
75
76} // namespace testing
77} // namespace salsa
78} // namespace frc971