blob: a6f50285f9d768b80712d4e715d0c902ebbadf61 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
3/* Open Source Software - may be modified and shared by FRC teams. The code */
4/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
6/*----------------------------------------------------------------------------*/
7
8#include "frc/SpeedControllerGroup.h" // NOLINT(build/include_order)
9
10#include <memory>
11#include <vector>
12
13#include "MockSpeedController.h"
14#include "gtest/gtest.h"
15
16using namespace frc;
17
18enum SpeedControllerGroupTestType { TEST_ONE, TEST_TWO, TEST_THREE };
19
20std::ostream& operator<<(std::ostream& os,
21 const SpeedControllerGroupTestType& type) {
22 switch (type) {
23 case TEST_ONE:
24 os << "SpeedControllerGroup with one speed controller";
25 break;
26 case TEST_TWO:
27 os << "SpeedControllerGroup with two speed controllers";
28 break;
29 case TEST_THREE:
30 os << "SpeedControllerGroup with three speed controllers";
31 break;
32 }
33
34 return os;
35}
36
37/**
38 * A fixture used for SpeedControllerGroup testing.
39 */
40class SpeedControllerGroupTest
41 : public testing::TestWithParam<SpeedControllerGroupTestType> {
42 protected:
43 std::vector<MockSpeedController> m_speedControllers;
44 std::unique_ptr<SpeedControllerGroup> m_group;
45
46 void SetUp() override {
47 switch (GetParam()) {
48 case TEST_ONE: {
49 m_speedControllers.emplace_back();
50 m_group = std::make_unique<SpeedControllerGroup>(m_speedControllers[0]);
51 break;
52 }
53
54 case TEST_TWO: {
55 m_speedControllers.emplace_back();
56 m_speedControllers.emplace_back();
57 m_group = std::make_unique<SpeedControllerGroup>(m_speedControllers[0],
58 m_speedControllers[1]);
59 break;
60 }
61
62 case TEST_THREE: {
63 m_speedControllers.emplace_back();
64 m_speedControllers.emplace_back();
65 m_speedControllers.emplace_back();
66 m_group = std::make_unique<SpeedControllerGroup>(m_speedControllers[0],
67 m_speedControllers[1],
68 m_speedControllers[2]);
69 break;
70 }
71 }
72 }
73};
74
75TEST_P(SpeedControllerGroupTest, Set) {
76 m_group->Set(1.0);
77
78 for (auto& speedController : m_speedControllers) {
79 EXPECT_FLOAT_EQ(speedController.Get(), 1.0);
80 }
81}
82
83TEST_P(SpeedControllerGroupTest, GetInverted) {
84 m_group->SetInverted(true);
85
86 EXPECT_TRUE(m_group->GetInverted());
87}
88
89TEST_P(SpeedControllerGroupTest, SetInvertedDoesNotModifySpeedControllers) {
90 for (auto& speedController : m_speedControllers) {
91 speedController.SetInverted(false);
92 }
93 m_group->SetInverted(true);
94
95 for (auto& speedController : m_speedControllers) {
96 EXPECT_EQ(speedController.GetInverted(), false);
97 }
98}
99
100TEST_P(SpeedControllerGroupTest, SetInvertedDoesInvert) {
101 m_group->SetInverted(true);
102 m_group->Set(1.0);
103
104 for (auto& speedController : m_speedControllers) {
105 EXPECT_FLOAT_EQ(speedController.Get(), -1.0);
106 }
107}
108
109TEST_P(SpeedControllerGroupTest, Disable) {
110 m_group->Set(1.0);
111 m_group->Disable();
112
113 for (auto& speedController : m_speedControllers) {
114 EXPECT_FLOAT_EQ(speedController.Get(), 0.0);
115 }
116}
117
118TEST_P(SpeedControllerGroupTest, StopMotor) {
119 m_group->Set(1.0);
120 m_group->StopMotor();
121
122 for (auto& speedController : m_speedControllers) {
123 EXPECT_FLOAT_EQ(speedController.Get(), 0.0);
124 }
125}
126
127TEST_P(SpeedControllerGroupTest, PIDWrite) {
128 m_group->PIDWrite(1.0);
129
130 for (auto& speedController : m_speedControllers) {
131 EXPECT_FLOAT_EQ(speedController.Get(), 1.0);
132 }
133}
134
135INSTANTIATE_TEST_CASE_P(Test, SpeedControllerGroupTest,
136 testing::Values(TEST_ONE, TEST_TWO, TEST_THREE), );