[tools] Translate unit tests to catch2 (#9006)

This commit is contained in:
Peter Johnson
2026-06-21 19:24:30 -07:00
committed by GitHub
parent edbf2db5a7
commit 912938b434
16 changed files with 217 additions and 159 deletions

View File

@@ -2,10 +2,8 @@
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include <gtest/gtest.h>
#include <catch2/catch_session.hpp>
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();
return ret;
return Catch::Session().run(argc, argv);
}

View File

@@ -4,11 +4,11 @@
#include "wpi/sysid/analysis/AnalysisType.hpp"
#include <gtest/gtest.h>
#include <catch2/catch_test_macros.hpp>
TEST(AnalysisTypeTest, FromName) {
EXPECT_EQ(sysid::analysis::kElevator, sysid::analysis::FromName("Elevator"));
EXPECT_EQ(sysid::analysis::kArm, sysid::analysis::FromName("Arm"));
EXPECT_EQ(sysid::analysis::kSimple, sysid::analysis::FromName("Simple"));
EXPECT_EQ(sysid::analysis::kSimple, sysid::analysis::FromName("Random"));
TEST_CASE("AnalysisTypeTest FromName", "[sysid]") {
CHECK(sysid::analysis::kElevator == sysid::analysis::FromName("Elevator"));
CHECK(sysid::analysis::kArm == sysid::analysis::FromName("Arm"));
CHECK(sysid::analysis::kSimple == sysid::analysis::FromName("Simple"));
CHECK(sysid::analysis::kSimple == sysid::analysis::FromName("Random"));
}

View File

@@ -4,11 +4,12 @@
#include "wpi/sysid/analysis/FeedbackAnalysis.hpp"
#include <gtest/gtest.h>
#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
#include "wpi/sysid/analysis/FeedbackControllerPreset.hpp"
TEST(FeedbackAnalysisTest, VelocitySystem1) {
TEST_CASE("FeedbackAnalysisTest VelocitySystem1", "[sysid]") {
auto Kv = 3.060;
auto Ka = 0.327;
@@ -17,11 +18,11 @@ TEST(FeedbackAnalysisTest, VelocitySystem1) {
auto [Kp, Kd] = sysid::CalculateVelocityFeedbackGains(
sysid::presets::kDefault, params, Kv, Ka);
EXPECT_NEAR(Kp, 2.11, 0.05);
EXPECT_NEAR(Kd, 0.00, 0.05);
CHECK(Kp == Catch::Approx(2.11).margin(0.05));
CHECK(Kd == Catch::Approx(0.00).margin(0.05));
}
TEST(FeedbackAnalysisTest, VelocitySystem2) {
TEST_CASE("FeedbackAnalysisTest VelocitySystem2", "[sysid]") {
auto Kv = 0.0693;
auto Ka = 0.1170;
@@ -30,11 +31,11 @@ TEST(FeedbackAnalysisTest, VelocitySystem2) {
auto [Kp, Kd] = sysid::CalculateVelocityFeedbackGains(
sysid::presets::kDefault, params, Kv, Ka);
EXPECT_NEAR(Kp, 3.11, 0.05);
EXPECT_NEAR(Kd, 0.00, 0.05);
CHECK(Kp == Catch::Approx(3.11).margin(0.05));
CHECK(Kd == Catch::Approx(0.00).margin(0.05));
}
TEST(FeedbackAnalysisTest, VelocitySystemWithSmallKa) {
TEST_CASE("FeedbackAnalysisTest VelocitySystemWithSmallKa", "[sysid]") {
auto Kv = 3.060;
auto Ka = 0.0;
@@ -43,11 +44,11 @@ TEST(FeedbackAnalysisTest, VelocitySystemWithSmallKa) {
auto [Kp, Kd] = sysid::CalculateVelocityFeedbackGains(
sysid::presets::kDefault, params, Kv, Ka);
EXPECT_NEAR(Kp, 0.00, 0.05);
EXPECT_NEAR(Kd, 0.00, 0.05);
CHECK(Kp == Catch::Approx(0.00).margin(0.05));
CHECK(Kd == Catch::Approx(0.00).margin(0.05));
}
TEST(FeedbackAnalysisTest, VelocityConversion) {
TEST_CASE("FeedbackAnalysisTest VelocityConversion", "[sysid]") {
auto Kv = 0.0693;
auto Ka = 0.1170;
@@ -58,11 +59,11 @@ TEST(FeedbackAnalysisTest, VelocityConversion) {
// This should have the same Kp as the test above, but scaled by a factor of 3
// * 1023.
EXPECT_NEAR(Kp, 3.11 / (3 * 1023), 0.005);
EXPECT_NEAR(Kd, 0.00, 0.05);
CHECK(Kp == Catch::Approx(3.11 / (3 * 1023)).margin(0.005));
CHECK(Kd == Catch::Approx(0.00).margin(0.05));
}
TEST(FeedbackAnalysisTest, VelocityCTRE) {
TEST_CASE("FeedbackAnalysisTest VelocityCTRE", "[sysid]") {
auto Kv = 1.97;
auto Ka = 0.179;
@@ -71,11 +72,11 @@ TEST(FeedbackAnalysisTest, VelocityCTRE) {
auto [Kp, Kd] = sysid::CalculateVelocityFeedbackGains(sysid::presets::kCTREv5,
params, Kv, Ka);
EXPECT_NEAR(Kp, 259.21276731541178, 0.00005);
EXPECT_NEAR(Kd, 0.00, 0.05);
CHECK(Kp == Catch::Approx(259.21276731541178).margin(0.00005));
CHECK(Kd == Catch::Approx(0.00).margin(0.05));
}
TEST(FeedbackAnalysisTest, VelocityCTREConversion) {
TEST_CASE("FeedbackAnalysisTest VelocityCTREConversion", "[sysid]") {
auto Kv = 1.97;
auto Ka = 0.179;
@@ -86,11 +87,11 @@ TEST(FeedbackAnalysisTest, VelocityCTREConversion) {
// This should have the same Kp as the test above, but scaled by a factor
// of 3.
EXPECT_NEAR(Kp, 259.21276731541178 / 3, 0.00005);
EXPECT_NEAR(Kd, 0.00, 0.05);
CHECK(Kp == Catch::Approx(259.21276731541178 / 3).margin(0.00005));
CHECK(Kd == Catch::Approx(0.00).margin(0.05));
}
TEST(FeedbackAnalysisTest, VelocityREV) {
TEST_CASE("FeedbackAnalysisTest VelocityREV", "[sysid]") {
auto Kv = 1.97;
auto Ka = 0.179;
@@ -99,11 +100,11 @@ TEST(FeedbackAnalysisTest, VelocityREV) {
auto [Kp, Kd] = sysid::CalculateVelocityFeedbackGains(
sysid::presets::kREVNEOBuiltIn, params, Kv, Ka);
EXPECT_NEAR(Kp, 0.00241, 0.005);
EXPECT_NEAR(Kd, 0.00, 0.05);
CHECK(Kp == Catch::Approx(0.00241).margin(0.005));
CHECK(Kd == Catch::Approx(0.00).margin(0.05));
}
TEST(FeedbackAnalysisTest, VelocityREVConversion) {
TEST_CASE("FeedbackAnalysisTest VelocityREVConversion", "[sysid]") {
auto Kv = 1.97;
auto Ka = 0.179;
@@ -114,11 +115,11 @@ TEST(FeedbackAnalysisTest, VelocityREVConversion) {
// This should have the same Kp as the test above, but scaled by a factor
// of 3.
EXPECT_NEAR(Kp, 0.00241 / 3, 0.005);
EXPECT_NEAR(Kd, 0.00, 0.05);
CHECK(Kp == Catch::Approx(0.00241 / 3).margin(0.005));
CHECK(Kd == Catch::Approx(0.00).margin(0.05));
}
TEST(FeedbackAnalysisTest, Position) {
TEST_CASE("FeedbackAnalysisTest Position", "[sysid]") {
auto Kv = 3.060;
auto Ka = 0.327;
@@ -127,11 +128,11 @@ TEST(FeedbackAnalysisTest, Position) {
auto [Kp, Kd] = sysid::CalculatePositionFeedbackGains(
sysid::presets::kDefault, params, Kv, Ka);
EXPECT_NEAR(Kp, 6.41, 0.05);
EXPECT_NEAR(Kd, 2.48, 0.05);
CHECK(Kp == Catch::Approx(6.41).margin(0.05));
CHECK(Kd == Catch::Approx(2.48).margin(0.05));
}
TEST(FeedbackAnalysisTest, PositionWithSmallKa) {
TEST_CASE("FeedbackAnalysisTest PositionWithSmallKa", "[sysid]") {
auto Kv = 3.060;
auto Ka = 1e-10;
@@ -140,11 +141,11 @@ TEST(FeedbackAnalysisTest, PositionWithSmallKa) {
auto [Kp, Kd] = sysid::CalculatePositionFeedbackGains(
sysid::presets::kDefault, params, Kv, Ka);
EXPECT_NEAR(Kp, 19.97, 0.05);
EXPECT_NEAR(Kd, 0.00, 0.05);
CHECK(Kp == Catch::Approx(19.97).margin(0.05));
CHECK(Kd == Catch::Approx(0.00).margin(0.05));
}
TEST(FeedbackAnalysisTest, PositionWithLatencyCompensation) {
TEST_CASE("FeedbackAnalysisTest PositionWithLatencyCompensation", "[sysid]") {
auto Kv = 3.060;
auto Ka = 0.327;
@@ -154,11 +155,11 @@ TEST(FeedbackAnalysisTest, PositionWithLatencyCompensation) {
preset.measurementDelay = 10_ms;
auto [Kp, Kd] = sysid::CalculatePositionFeedbackGains(preset, params, Kv, Ka);
EXPECT_NEAR(Kp, 5.92, 0.05);
EXPECT_NEAR(Kd, 2.12, 0.05);
CHECK(Kp == Catch::Approx(5.92).margin(0.05));
CHECK(Kd == Catch::Approx(2.12).margin(0.05));
}
TEST(FeedbackAnalysisTest, PositionREV) {
TEST_CASE("FeedbackAnalysisTest PositionREV", "[sysid]") {
auto Kv = 3.060;
auto Ka = 0.327;
@@ -167,6 +168,6 @@ TEST(FeedbackAnalysisTest, PositionREV) {
auto [Kp, Kd] = sysid::CalculatePositionFeedbackGains(
sysid::presets::kREVNEOBuiltIn, params, Kv, Ka);
EXPECT_NEAR(Kp, 0.30202, 0.05);
EXPECT_NEAR(Kd, 48.518, 0.05);
CHECK(Kp == Catch::Approx(0.30202).margin(0.05));
CHECK(Kd == Catch::Approx(48.518).margin(0.05));
}

View File

@@ -9,8 +9,12 @@
#include <bitset>
#include <cmath>
#include <span>
#include <sstream>
#include <string>
#include <gtest/gtest.h>
#include <catch2/catch_approx.hpp>
#include <catch2/catch_message.hpp>
#include <catch2/catch_test_macros.hpp>
#include "wpi/sysid/analysis/AnalysisManager.hpp"
#include "wpi/sysid/analysis/AnalysisType.hpp"
@@ -113,30 +117,35 @@ sysid::Storage CollectData(Model& model, std::bitset<4> movements) {
}
/**
* Asserts success if the gains contain NaNs or are too far from their expected
* Returns true if the gains contain NaNs or are too far from their expected
* values.
*
* @param expectedGains The expected feedforward gains.
* @param actualGains The calculated feedforward gains.
* @param tolerances The tolerances for the coefficient comparisons.
*/
testing::AssertionResult FitIsBad(std::span<const double> expectedGains,
std::span<const double> actualGains,
std::span<const double> tolerances) {
bool FitIsBad(std::span<const double> expectedGains,
std::span<const double> actualGains,
std::span<const double> tolerances) {
// Check for NaN
for (const auto& coeff : actualGains) {
if (std::isnan(coeff)) {
return testing::AssertionSuccess();
return true;
}
}
for (size_t i = 0; i < expectedGains.size(); ++i) {
if (std::abs(expectedGains[i] - actualGains[i]) >= tolerances[i]) {
return testing::AssertionSuccess();
return true;
}
}
auto result = testing::AssertionFailure();
return false;
}
std::string DescribeFit(std::span<const double> expectedGains,
std::span<const double> actualGains) {
std::ostringstream result;
result << "\n";
for (size_t i = 0; i < expectedGains.size(); ++i) {
@@ -158,7 +167,7 @@ testing::AssertionResult FitIsBad(std::span<const double> expectedGains,
result << " diff " << std::abs(expectedGains[i] - actualGains[i]) << "\n";
}
return result;
return result.str();
}
/**
@@ -173,12 +182,13 @@ void ExpectArrayNear(std::span<const double> expected,
std::span<const double> tolerances) {
// Check size
const size_t size = expected.size();
EXPECT_EQ(size, actual.size());
EXPECT_EQ(size, tolerances.size());
REQUIRE(size == actual.size());
REQUIRE(size == tolerances.size());
// Check elements
for (size_t i = 0; i < size; ++i) {
EXPECT_NEAR(expected[i], actual[i], tolerances[i]) << "where i = " << i;
UNSCOPED_INFO("i = " << i);
CHECK(expected[i] == Catch::Approx(actual[i]).margin(tolerances[i]));
}
}
@@ -205,14 +215,18 @@ void RunTests(Model& model, const sysid::AnalysisType& type,
// doesn't match
auto ff = sysid::CalculateFeedforwardGains(CollectData(model, movements),
type, false);
EXPECT_TRUE(FitIsBad(expectedGains, ff.coeffs, tolerances));
bool fitIsBad = FitIsBad(expectedGains, ff.coeffs, tolerances);
if (!fitIsBad) {
UNSCOPED_INFO(DescribeFit(expectedGains, ff.coeffs));
}
CHECK(fitIsBad);
}
}
}
} // namespace
TEST(FeedforwardAnalysisTest, Arm) {
TEST_CASE("FeedforwardAnalysisTest Arm", "[sysid]") {
{
constexpr double Ks = 1.01;
constexpr double Kv = 3.060;
@@ -242,7 +256,7 @@ TEST(FeedforwardAnalysisTest, Arm) {
}
}
TEST(FeedforwardAnalysisTest, Elevator) {
TEST_CASE("FeedforwardAnalysisTest Elevator", "[sysid]") {
{
constexpr double Ks = 1.01;
constexpr double Kv = 3.060;
@@ -268,7 +282,7 @@ TEST(FeedforwardAnalysisTest, Elevator) {
}
}
TEST(FeedforwardAnalysisTest, Simple) {
TEST_CASE("FeedforwardAnalysisTest Simple", "[sysid]") {
{
constexpr double Ks = 1.01;
constexpr double Kv = 3.060;

View File

@@ -6,14 +6,15 @@
#include <cmath>
#include <vector>
#include <gtest/gtest.h>
#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
#include "wpi/sysid/analysis/AnalysisManager.hpp"
#include "wpi/sysid/analysis/FeedforwardAnalysis.hpp"
#include "wpi/sysid/analysis/FilteringUtils.hpp"
#include "wpi/sysid/analysis/Storage.hpp"
TEST(FilterTest, MedianFilter) {
TEST_CASE("FilterTest MedianFilter", "[sysid]") {
std::vector<sysid::PreparedData> testData{
sysid::PreparedData{0_s, 0, 0, 0}, sysid::PreparedData{0_s, 0, 0, 1},
sysid::PreparedData{0_s, 0, 0, 10}, sysid::PreparedData{0_s, 0, 0, 5},
@@ -28,10 +29,10 @@ TEST(FilterTest, MedianFilter) {
sysid::PreparedData{0_s, 0, 0, 6}, sysid::PreparedData{0_s, 0, 0, 5}};
sysid::ApplyMedianFilter(&testData, 3);
EXPECT_EQ(expectedData, testData);
CHECK(expectedData == testData);
}
TEST(FilterTest, NoiseFloor) {
TEST_CASE("FilterTest NoiseFloor", "[sysid]") {
std::vector<sysid::PreparedData> testData = {
{0_s, 1, 2, 3, 5_ms, 0, 0}, {1_s, 1, 2, 3, 5_ms, 1, 0},
{2_s, 1, 2, 3, 5_ms, 2, 0}, {3_s, 1, 2, 3, 5_ms, 5, 0},
@@ -40,7 +41,7 @@ TEST(FilterTest, NoiseFloor) {
{8_s, 1, 2, 3, 5_ms, 0.01, 0}, {9_s, 1, 2, 3, 5_ms, 0, 0}};
double noiseFloor =
GetNoiseFloor(testData, 2, [](auto&& pt) { return pt.acceleration; });
EXPECT_NEAR(0.953, noiseFloor, 0.001);
CHECK(0.953 == Catch::Approx(noiseFloor).margin(0.001));
}
void FillStepVoltageData(std::vector<sysid::PreparedData>& data) {
@@ -59,7 +60,7 @@ void FillStepVoltageData(std::vector<sysid::PreparedData>& data) {
}
}
TEST(FilterTest, StepTrim) {
TEST_CASE("FilterTest StepTrim", "[sysid]") {
{
std::vector<sysid::PreparedData> forwardTestData = {
{0_s, 1, 0, 0, 1_s, 0}, {0_s, 1, 0, 0, 1_s, 0.25},
@@ -80,8 +81,8 @@ TEST(FilterTest, StepTrim) {
maxTime);
minTime = tempMinTime;
EXPECT_EQ(3, settings.stepTestDuration.value());
EXPECT_EQ(2, minTime.value());
CHECK(3 == settings.stepTestDuration.value());
CHECK(2 == minTime.value());
}
{
@@ -104,8 +105,8 @@ TEST(FilterTest, StepTrim) {
maxTime);
minTime = tempMinTime;
EXPECT_EQ(3, settings.stepTestDuration.value());
EXPECT_EQ(2, minTime.value());
CHECK(3 == settings.stepTestDuration.value());
CHECK(2 == minTime.value());
}
{
@@ -130,8 +131,8 @@ TEST(FilterTest, StepTrim) {
// Expect trimming to reject the erroneous peak negative accel,
// correctly picking up the max positive accel instead.
EXPECT_EQ(4, settings.stepTestDuration.value());
EXPECT_EQ(2, minTime.value());
CHECK(4 == settings.stepTestDuration.value());
CHECK(2 == minTime.value());
}
}
@@ -153,16 +154,16 @@ void AssertCentralResults(F&& f, DfDx&& dfdx, wpi::units::second_t h,
// half the window size in the past.
// The order of accuracy is O(h^(N - d)) where N is number of stencil
// points and d is order of derivative
EXPECT_NEAR(dfdx((i - static_cast<int>((Samples - 1) / 2)) * h.value()),
filter.Calculate(f(i * h.value())),
std::pow(h.value(), Samples - Derivative));
CHECK(dfdx((i - static_cast<int>((Samples - 1) / 2)) * h.value()) ==
Catch::Approx(filter.Calculate(f(i * h.value())))
.margin(std::pow(h.value(), Samples - Derivative)));
}
}
/**
* Test central finite difference.
*/
TEST(LinearFilterOutputTest, CentralFiniteDifference) {
TEST_CASE("LinearFilterOutputTest CentralFiniteDifference", "[sysid]") {
constexpr auto h = 5_ms;
AssertCentralResults<1, 3>(

View File

@@ -4,39 +4,75 @@
#include "wpi/sysid/analysis/OLS.hpp"
#include <gtest/gtest.h>
#ifndef NDEBUG
#ifndef _WIN32
#include <sys/wait.h>
#include <unistd.h>
TEST(OLSTest, TwoVariablesTwoPoints) {
#include <csignal>
#include <cstdio>
#endif
#endif
#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
#ifndef NDEBUG
#ifndef _WIN32
namespace {
template <typename F>
bool Dies(F&& f) {
pid_t pid = fork();
REQUIRE(pid >= 0);
if (pid == 0) {
std::signal(SIGABRT, SIG_DFL);
std::freopen("/dev/null", "w", stderr);
f();
_exit(0);
}
int status = 0;
REQUIRE(waitpid(pid, &status, 0) == pid);
return WIFSIGNALED(status) || (WIFEXITED(status) && WEXITSTATUS(status) != 0);
}
} // namespace
#endif
#endif
TEST_CASE("OLSTest TwoVariablesTwoPoints", "[sysid]") {
// (1, 3) and (2, 5). Should produce y = 2x + 1.
Eigen::MatrixXd X{{1.0, 1.0}, {1.0, 2.0}};
Eigen::VectorXd y{{3.0}, {5.0}};
auto [coeffs, rSquared, rmse] = sysid::OLS(X, y);
EXPECT_EQ(coeffs.size(), 2u);
CHECK(coeffs.size() == 2u);
EXPECT_NEAR(coeffs[0], 1.0, 1e-12);
EXPECT_NEAR(coeffs[1], 2.0, 1e-12);
EXPECT_DOUBLE_EQ(rSquared, 1.0);
CHECK(coeffs[0] == Catch::Approx(1.0).margin(1e-12));
CHECK(coeffs[1] == Catch::Approx(2.0).margin(1e-12));
CHECK(rSquared == Catch::Approx(1.0).margin(1e-12));
}
TEST(OLSTest, TwoVariablesFivePoints) {
TEST_CASE("OLSTest TwoVariablesFivePoints", "[sysid]") {
// (2, 4), (3, 5), (5, 7), (7, 10), (9, 15)
// Should produce 1.518x + 0.305.
Eigen::MatrixXd X{{1, 2}, {1, 3}, {1, 5}, {1, 7}, {1, 9}};
Eigen::VectorXd y{{4}, {5}, {7}, {10}, {15}};
auto [coeffs, rSquared, rmse] = sysid::OLS(X, y);
EXPECT_EQ(coeffs.size(), 2u);
CHECK(coeffs.size() == 2u);
EXPECT_NEAR(coeffs[0], 0.30487804878048774, 1e-12);
EXPECT_NEAR(coeffs[1], 1.5182926829268293, 1e-12);
EXPECT_DOUBLE_EQ(rSquared, 0.91906029466386019);
CHECK(coeffs[0] == Catch::Approx(0.30487804878048774).margin(1e-12));
CHECK(coeffs[1] == Catch::Approx(1.5182926829268293).margin(1e-12));
CHECK(rSquared == Catch::Approx(0.91906029466386019).margin(1e-12));
}
#ifndef NDEBUG
TEST(OLSTest, MalformedData) {
#if !defined(NDEBUG) && !defined(_WIN32)
TEST_CASE("OLSTest MalformedData", "[sysid]") {
Eigen::MatrixXd X{{1, 2}, {1, 3}, {1, 4}};
Eigen::VectorXd y{{4}, {5}};
EXPECT_DEATH(sysid::OLS(X, y), "");
CHECK(Dies([&] { sysid::OLS(X, y); }));
}
#endif

View File

@@ -4,9 +4,10 @@
#include "wpi/sysid/analysis/TrackwidthAnalysis.hpp"
#include <gtest/gtest.h>
#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
TEST(TrackwidthAnalysisTest, Calculate) {
TEST_CASE("TrackwidthAnalysisTest Calculate", "[sysid]") {
double result = sysid::CalculateTrackwidth(-0.5386, 0.5386, 90_deg);
EXPECT_NEAR(result, 0.6858, 1E-4);
CHECK(result == Catch::Approx(0.6858).margin(1E-4));
}