[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,9 +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);
return RUN_ALL_TESTS();
return Catch::Session().run(argc, argv);
}

View File

@@ -2,21 +2,26 @@
// 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 <chrono>
#include <cstdlib>
#include <optional>
#include <string>
#include <system_error>
#include <thread>
#include <gtest/gtest.h>
#include <catch2/catch_test_macros.hpp>
#include "cameracalibration.hpp"
#include "fieldcalibration.hpp"
#include "path_lookup.hpp"
#include "wpi/apriltag/AprilTagFieldLayout.hpp"
#include "wpi/apriltag/AprilTagFields.hpp"
#include "wpi/util/MemoryBuffer.hpp"
#include "wpi/util/fs.hpp"
#include "wpi/util/json.hpp"
#include "wpi/util/raw_ostream.hpp"
namespace {
const std::string projectRootPath = PROJECT_ROOT_PATH;
const char* const tmpdir_c_str = std::getenv("TEST_TMPDIR");
@@ -36,21 +41,34 @@ const std::string fileSuffix = ".mp4";
const std::string videoLocation = "/fieldvideo";
#endif
TEST(CameraCalibrationTest, Typical) {
wpical::CameraModel GetCameraModel() {
static std::optional<wpical::CameraModel> cameraModel;
if (cameraModel) {
return *cameraModel;
}
auto path = LookupPath(projectRootPath + "/testcalibration" + fileSuffix);
auto calibrator = wpical::CameraCalibrator(4, 0.709, 0.551, 12, 8, path);
while (!calibrator.IsFinished()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
auto ret = calibrator.GetCameraModel();
EXPECT_NE(ret, std::nullopt);
REQUIRE(ret != std::nullopt);
cameraModel = *ret;
return *cameraModel;
}
} // namespace
TEST_CASE("CameraCalibrationTest Typical", "[wpical]") {
auto ret = GetCameraModel();
std::error_code ec;
wpi::util::raw_fd_ostream output_file(calSavePath + "/cameracalibration.json",
ec, fs::OF_Text);
wpi::util::json(ret.value()).marshal(output_file, true, 4);
wpi::util::json(ret).marshal(output_file, true, 4);
}
TEST(CameraCalibrationTest, Atypical) {
TEST_CASE("CameraCalibrationTest Atypical", "[wpical]") {
auto path =
LookupPath(projectRootPath + videoLocation + "/short" + fileSuffix);
auto calibrator = wpical::CameraCalibrator(4, 0.709, 0.551, 12, 8, path);
@@ -58,83 +76,63 @@ TEST(CameraCalibrationTest, Atypical) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
auto ret = calibrator.GetCameraModel();
EXPECT_EQ(ret, std::nullopt);
CHECK(ret == std::nullopt);
}
TEST(FieldCalibrationTest, Typical) {
auto buffer =
wpi::util::MemoryBuffer::GetFile(calSavePath + "/cameracalibration.json");
auto buf = buffer.value()->GetCharBuffer();
auto model = wpi::util::json::parse_or_throw({buf.data(), buf.size()})
.get<wpical::CameraModel>();
TEST_CASE("FieldCalibrationTest Typical", "[wpical]") {
auto model = GetCameraModel();
auto ret =
wpical::calibrate(LookupPath(projectRootPath + videoLocation), model,
wpi::apriltag::AprilTagFieldLayout::LoadField(
wpi::apriltag::AprilTagField::k2024Crescendo),
3, false);
EXPECT_NE(ret, std::nullopt);
REQUIRE(ret != std::nullopt);
}
TEST(FieldCalibrationTest, Atypical_Bad_Camera_Model) {
TEST_CASE("FieldCalibrationTest Atypical_Bad_Camera_Model", "[wpical]") {
wpical::CameraModel model{};
auto ret =
wpical::calibrate(LookupPath(projectRootPath + videoLocation), model,
wpi::apriltag::AprilTagFieldLayout::LoadField(
wpi::apriltag::AprilTagField::k2024Crescendo),
3, false);
EXPECT_EQ(ret, std::nullopt);
CHECK(ret == std::nullopt);
}
TEST(FieldCalibrationTest, Atypical_Bad_Field_Layout) {
auto buffer =
wpi::util::MemoryBuffer::GetFile(calSavePath + "/cameracalibration.json");
auto buf = buffer.value()->GetCharBuffer();
auto model = wpi::util::json::parse_or_throw({buf.data(), buf.size()})
.get<wpical::CameraModel>();
TEST_CASE("FieldCalibrationTest Atypical_Bad_Field_Layout", "[wpical]") {
auto model = GetCameraModel();
auto ret =
wpical::calibrate(LookupPath(projectRootPath + videoLocation), model,
wpi::apriltag::AprilTagFieldLayout{}, 3, false);
EXPECT_EQ(ret, std::nullopt);
CHECK(ret == std::nullopt);
}
TEST(FieldCalibrationTest, Atypical_Bad_Input_Directory) {
auto buffer =
wpi::util::MemoryBuffer::GetFile(calSavePath + "/cameracalibration.json");
auto buf = buffer.value()->GetCharBuffer();
auto model = wpi::util::json::parse_or_throw({buf.data(), buf.size()})
.get<wpical::CameraModel>();
TEST_CASE("FieldCalibrationTest Atypical_Bad_Input_Directory", "[wpical]") {
auto model = GetCameraModel();
auto ret =
wpical::calibrate(LookupPath(projectRootPath), model,
wpi::apriltag::AprilTagFieldLayout::LoadField(
wpi::apriltag::AprilTagField::k2024Crescendo),
3, false);
EXPECT_EQ(ret, std::nullopt);
CHECK(ret == std::nullopt);
}
TEST(FieldCalibrationTest, Atypical_Bad_Pinned_Tag) {
auto buffer =
wpi::util::MemoryBuffer::GetFile(calSavePath + "/cameracalibration.json");
auto buf = buffer.value()->GetCharBuffer();
auto model = wpi::util::json::parse_or_throw({buf.data(), buf.size()})
.get<wpical::CameraModel>();
TEST_CASE("FieldCalibrationTest Atypical_Bad_Pinned_Tag", "[wpical]") {
auto model = GetCameraModel();
auto ret =
wpical::calibrate(LookupPath(projectRootPath + videoLocation), model,
wpi::apriltag::AprilTagFieldLayout::LoadField(
wpi::apriltag::AprilTagField::k2024Crescendo),
42, false);
EXPECT_EQ(ret, std::nullopt);
CHECK(ret == std::nullopt);
}
TEST(FieldCalibrationTest, Atypical_Bad_Pinned_Tag_Negative) {
auto buffer =
wpi::util::MemoryBuffer::GetFile(calSavePath + "/cameracalibration.json");
auto buf = buffer.value()->GetCharBuffer();
auto model = wpi::util::json::parse_or_throw({buf.data(), buf.size()})
.get<wpical::CameraModel>();
TEST_CASE("FieldCalibrationTest Atypical_Bad_Pinned_Tag_Negative", "[wpical]") {
auto model = GetCameraModel();
auto ret =
wpical::calibrate(LookupPath(projectRootPath + videoLocation), model,
wpi::apriltag::AprilTagFieldLayout::LoadField(
wpi::apriltag::AprilTagField::k2024Crescendo),
-1, false);
EXPECT_EQ(ret, std::nullopt);
CHECK(ret == std::nullopt);
}

View File

@@ -2,15 +2,20 @@
// 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 <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <gtest/gtest.h>
#include <catch2/catch_approx.hpp>
#include <catch2/catch_message.hpp>
#include <catch2/catch_test_macros.hpp>
#include <mrcal_wrapper.h>
#include "path_lookup.hpp"
@@ -148,7 +153,7 @@ std::vector<double> calibrate(const std::string& fname, cv::Size boardSize,
const std::string projectRootPath = PROJECT_ROOT_PATH;
TEST(MrcalResultExactlyMatchesTest, lifecam_1280) {
TEST_CASE("MrcalResultExactlyMatchesTest lifecam_1280", "[wpical]") {
auto calculated_intrinsics{
calibrate(LookupPath(projectRootPath + "/lifecam_1280p_10x10.vnl"),
{10, 10}, {1280, 720})};
@@ -163,8 +168,12 @@ TEST(MrcalResultExactlyMatchesTest, lifecam_1280) {
0.1489878273, -1.348622726, 0.002839630852, 0.001135629909,
2.560627057, -0.03170208336, 0.0695788644, -0.09547554864};
for (int i = 0; i < 12; i++) {
EXPECT_NEAR(mrcal_cli_groundtruth_intrinsics[i], calculated_intrinsics[i],
1e-6);
REQUIRE(calculated_intrinsics.size() ==
mrcal_cli_groundtruth_intrinsics.size());
for (size_t i = 0; i < mrcal_cli_groundtruth_intrinsics.size(); i++) {
UNSCOPED_INFO("i = " << i);
CHECK(mrcal_cli_groundtruth_intrinsics[i] ==
Catch::Approx(calculated_intrinsics[i]).margin(1e-6));
}
}