mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
Merge "Fixes CounterTest for C++"
This commit is contained in:
@@ -9,89 +9,147 @@
|
||||
#include "gtest/gtest.h"
|
||||
#include "TestBench.h"
|
||||
|
||||
enum CounterTestType { TEST_VICTOR, TEST_JAGUAR, TEST_TALON };
|
||||
static const double kMotorDelay = 2.5;
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, CounterTestType const &type) {
|
||||
switch(type) {
|
||||
case TEST_VICTOR: os << "Victor"; break;
|
||||
case TEST_JAGUAR: os << "Jaguar"; break;
|
||||
case TEST_TALON: os << "Talon"; break;
|
||||
}
|
||||
static const double kMaxPeriod = 2.0;
|
||||
|
||||
return os;
|
||||
}
|
||||
class CounterTest : public testing::Test {
|
||||
protected:
|
||||
Counter *m_talonCounter;
|
||||
Counter *m_victorCounter;
|
||||
Counter *m_jaguarCounter;
|
||||
Talon *m_talon;
|
||||
Victor *m_victor;
|
||||
Jaguar *m_jaguar;
|
||||
|
||||
static constexpr double kMotorTime = 0.5;
|
||||
virtual void SetUp() {
|
||||
m_talonCounter = new Counter(TestBench::kTalonEncoderChannelA);
|
||||
m_victorCounter = new Counter(TestBench::kVictorEncoderChannelA);
|
||||
m_jaguarCounter = new Counter(TestBench::kJaguarEncoderChannelA);
|
||||
m_victor = new Victor(TestBench::kVictorChannel);
|
||||
m_talon = new Talon(TestBench::kTalonChannel);
|
||||
m_jaguar = new Jaguar(TestBench::kJaguarChannel);
|
||||
}
|
||||
|
||||
virtual void TearDown() {
|
||||
delete m_talonCounter;
|
||||
delete m_victorCounter;
|
||||
delete m_jaguarCounter;
|
||||
delete m_victor;
|
||||
delete m_talon;
|
||||
delete m_jaguar;
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
m_talonCounter->Reset();
|
||||
m_victorCounter->Reset();
|
||||
m_jaguarCounter->Reset();
|
||||
m_talon->Set(0.0f);
|
||||
m_victor->Set(0.0f);
|
||||
m_jaguar->Set(0.0f);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Tests the counter by moving the motor and determining if the
|
||||
* counter is counting.
|
||||
* counter is counting.
|
||||
*/
|
||||
TEST_F(CounterTest, CountTalon) {
|
||||
Reset();
|
||||
m_talonCounter->Start();
|
||||
/* Run the motor forward and determine if the counter is counting. */
|
||||
m_talon->Set(1.0f);
|
||||
Wait(0.5);
|
||||
EXPECT_NE(0.0f, m_talonCounter->Get())
|
||||
<< "The counter did not count (talon)";
|
||||
/* Set the motor to 0 and determine if the counter resets to 0. */
|
||||
m_talon->Set(0.0f);
|
||||
Wait(0.5);
|
||||
m_talonCounter->Reset();
|
||||
EXPECT_FLOAT_EQ(0.0f, m_talonCounter->Get())
|
||||
<< "The counter did not restart to 0 (talon)";
|
||||
}
|
||||
|
||||
class CounterTest : public testing::TestWithParam<CounterTestType> {
|
||||
protected:
|
||||
SpeedController *m_speedController;
|
||||
Counter *m_counter;
|
||||
|
||||
virtual void SetUp() {
|
||||
switch(GetParam()) {
|
||||
case TEST_VICTOR:
|
||||
m_counter = new Counter(TestBench::kVictorEncoderChannelA);
|
||||
m_speedController = new Victor(TestBench::kVictorChannel);
|
||||
break;
|
||||
|
||||
case TEST_JAGUAR:
|
||||
m_counter = new Counter(TestBench::kJaguarEncoderChannelA);
|
||||
m_speedController = new Jaguar(TestBench::kJaguarChannel);
|
||||
break;
|
||||
|
||||
case TEST_TALON:
|
||||
m_counter = new Counter(TestBench::kTalonEncoderChannelA);
|
||||
m_speedController = new Talon(TestBench::kTalonChannel);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
virtual void TearDown() {
|
||||
delete m_counter;
|
||||
delete m_speedController;
|
||||
}
|
||||
|
||||
void Reset() {
|
||||
m_counter->Reset();
|
||||
m_speedController->Set(0.0f);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Test resetting a counter
|
||||
*/
|
||||
TEST_P(CounterTest, Reset) {
|
||||
Reset();
|
||||
EXPECT_FLOAT_EQ(0.0f, m_counter->Get()) << "Counter did not restart to 0.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the counter by running the motor forward and determining if
|
||||
* the counter value has changed.
|
||||
*/
|
||||
TEST_P(CounterTest, Count) {
|
||||
Reset();
|
||||
m_counter->Start();
|
||||
|
||||
/*Run the motor forward and determine if the counter is counting. */
|
||||
m_speedController->Set(1.0f);
|
||||
Wait(kMotorTime);
|
||||
m_speedController->Set(0.0f);
|
||||
EXPECT_NE(0.0f, m_counter->Get()) << "Counter did not count.";
|
||||
}
|
||||
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(Test, CounterTest,
|
||||
testing::Values(TEST_VICTOR, TEST_JAGUAR, TEST_TALON));
|
||||
TEST_F(CounterTest, CountVictor) {
|
||||
Reset();
|
||||
m_victorCounter->Start();
|
||||
/* Run the motor forward and determine if the counter is counting. */
|
||||
m_victor->Set(1.0f);
|
||||
Wait(0.5);
|
||||
EXPECT_NE(0.0f, m_victorCounter->Get())
|
||||
<< "The counter did not count (victor)";
|
||||
/* Set the motor to 0 and determine if the counter resets to 0. */
|
||||
m_victor->Set(0.0f);
|
||||
Wait(0.5);
|
||||
m_victorCounter->Reset();
|
||||
EXPECT_FLOAT_EQ(0.0f, m_victorCounter->Get())
|
||||
<< "The counter did not restart to 0 (jaguar)";
|
||||
}
|
||||
|
||||
TEST_F(CounterTest, CountJaguar) {
|
||||
Reset();
|
||||
m_jaguarCounter->Start();
|
||||
/* Run the motor forward and determine if the counter is counting. */
|
||||
m_jaguar->Set(1.0f);
|
||||
Wait(0.5);
|
||||
EXPECT_NE(0.0f, m_jaguarCounter->Get())
|
||||
<< "The counter did not count (jaguar)";
|
||||
/* Set the motor to 0 and determine if the counter resets to 0. */
|
||||
m_jaguar->Set(0.0f);
|
||||
Wait(0.5);
|
||||
m_jaguarCounter->Reset();
|
||||
EXPECT_FLOAT_EQ(0.0f, m_jaguarCounter->Get())
|
||||
<< "The counter did not restart to 0 (jaguar)";
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the GetStopped and SetMaxPeriod methods by setting the Max Period and
|
||||
* getting the value after a period of time.
|
||||
*/
|
||||
TEST_F(CounterTest, TalonGetStopped) {
|
||||
Reset();
|
||||
m_talonCounter->Start();
|
||||
/* Set the Max Period of the counter and run the motor */
|
||||
m_talonCounter->SetMaxPeriod(kMaxPeriod);
|
||||
m_talon->Set(1.0f);
|
||||
Wait(0.5);
|
||||
EXPECT_FALSE(m_talonCounter->GetStopped())
|
||||
<< "The counter did not count.";
|
||||
/* Stop the motor and wait until the Max Period is exceeded */
|
||||
m_talon->Set(0.0f);
|
||||
Wait(kMotorDelay);
|
||||
EXPECT_TRUE(m_talonCounter->GetStopped())
|
||||
<< "The counter did not stop counting.";
|
||||
}
|
||||
|
||||
TEST_F(CounterTest, VictorGetStopped) {
|
||||
Reset();
|
||||
m_victorCounter->Start();
|
||||
/* Set the Max Period of the counter and run the motor */
|
||||
m_victorCounter->SetMaxPeriod(kMaxPeriod);
|
||||
m_victor->Set(1.0f);
|
||||
Wait(0.5);
|
||||
EXPECT_FALSE(m_victorCounter->GetStopped())
|
||||
<< "The counter did not count.";
|
||||
/* Stop the motor and wait until the Max Period is exceeded */
|
||||
m_victor->Set(0.0f);
|
||||
Wait(kMotorDelay);
|
||||
EXPECT_TRUE(m_victorCounter->GetStopped())
|
||||
<< "The counter did not stop counting.";
|
||||
}
|
||||
|
||||
TEST_F(CounterTest, JaguarGetStopped) {
|
||||
Reset();
|
||||
m_jaguarCounter->Start();
|
||||
/* Set the Max Period of the counter and run the motor */
|
||||
m_jaguarCounter->SetMaxPeriod(kMaxPeriod);
|
||||
m_jaguar->Set(1.0f);
|
||||
Wait(0.5);
|
||||
EXPECT_FALSE(m_jaguarCounter->GetStopped())
|
||||
<< "The counter did not count.";
|
||||
/* Stop the motor and wait until the Max Period is exceeded */
|
||||
m_jaguar->Set(0.0f);
|
||||
Wait(kMotorDelay);
|
||||
EXPECT_TRUE(m_jaguarCounter->GetStopped())
|
||||
<< "The counter did not stop counting.";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user