Skip to content

Commit

Permalink
Move Catch enum-to-string converters to tests (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Mar 16, 2024
1 parent 403dc4a commit ce6976e
Show file tree
Hide file tree
Showing 22 changed files with 97 additions and 105 deletions.
13 changes: 0 additions & 13 deletions include/sleipnir/autodiff/ExpressionType.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

#pragma once

#include <iosfwd>

#include "sleipnir/util/SymbolExports.hpp"

namespace sleipnir {

/**
Expand All @@ -26,13 +22,4 @@ enum class ExpressionType {
kNonlinear
};

/**
* Catch2 value formatter for ExpressionType.
*
* @param os Output stream to which to print.
* @param type ExpressionType to print.
*/
SLEIPNIR_DLLEXPORT std::ostream& operator<<(std::ostream& os,
const ExpressionType& type);

} // namespace sleipnir
10 changes: 0 additions & 10 deletions include/sleipnir/optimization/SolverExitCondition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#pragma once

#include <iosfwd>
#include <string_view>

#include "sleipnir/util/SymbolExports.hpp"
Expand Down Expand Up @@ -76,13 +75,4 @@ SLEIPNIR_DLLEXPORT constexpr std::string_view ToMessage(
}
}

/**
* Catch2 value formatter for SolverExitCondition.
*
* @param os Output stream to which to print.
* @param exitCondition Solver exit condition to print.
*/
SLEIPNIR_DLLEXPORT std::ostream& operator<<(
std::ostream& os, const SolverExitCondition& exitCondition);

} // namespace sleipnir
33 changes: 0 additions & 33 deletions src/autodiff/ExpressionType.cpp

This file was deleted.

49 changes: 0 additions & 49 deletions src/optimization/SolverExitCondition.cpp

This file was deleted.

68 changes: 68 additions & 0 deletions test/include/CatchStringConverters.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Sleipnir contributors

#pragma once

#include <string>

#include <catch2/catch_tostring.hpp>
#include <sleipnir/autodiff/ExpressionType.hpp>
#include <sleipnir/optimization/SolverExitCondition.hpp>

namespace Catch {

template <>
struct StringMaker<sleipnir::ExpressionType> {
static std::string convert(const sleipnir::ExpressionType& type) {
using enum sleipnir::ExpressionType;

switch (type) {
case kNone:
return "kNone";
case kConstant:
return "kConstant";
case kLinear:
return "kLinear";
case kQuadratic:
return "kQuadratic";
case kNonlinear:
return "kNonlinear";
}

return "";
}
};

template <>
struct StringMaker<sleipnir::SolverExitCondition> {
static std::string convert(
const sleipnir::SolverExitCondition& exitCondition) {
using enum sleipnir::SolverExitCondition;

switch (exitCondition) {
case kSuccess:
return "kSuccess";
case kSolvedToAcceptableTolerance:
return "kSolvedToAcceptableTolerance";
case kCallbackRequestedStop:
return "kCallbackRequestedStop";
case kTooFewDOFs:
return "kTooFewDOFs";
case kLocallyInfeasible:
return "kLocallyInfeasible";
case kFeasibilityRestorationFailed:
return "kFeasibilityRestorationFailed";
case kNonfiniteInitialCostOrConstraints:
return "kNonfiniteInitialCostOrConstraints";
case kDivergingIterates:
return "kDivergingIterates";
case kMaxIterationsExceeded:
return "kMaxIterationsExceeded";
case kMaxWallClockTimeExceeded:
return "kMaxWallClockTimeExceeded";
}

return "";
}
};

} // namespace Catch
2 changes: 2 additions & 0 deletions test/src/autodiff/ExpressionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <catch2/catch_test_macros.hpp>
#include <sleipnir/autodiff/Expression.hpp>

#include "CatchStringConverters.hpp"

using sleipnir::detail::MakeExpressionPtr;

TEST_CASE("Expression - Default constructor", "[Expression]") {
Expand Down
2 changes: 2 additions & 0 deletions test/src/autodiff/VariableTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <catch2/catch_test_macros.hpp>
#include <sleipnir/autodiff/Variable.hpp>

#include "CatchStringConverters.hpp"

TEST_CASE("Variable - Default constructor", "[Variable]") {
sleipnir::Variable a;

Expand Down
1 change: 1 addition & 0 deletions test/src/control/OCPSolverTest_CartPole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <sleipnir/control/OCPSolver.hpp>

#include "CartPoleUtil.hpp"
#include "CatchStringConverters.hpp"
#include "RK4.hpp"

TEST_CASE("OCPSolver - Cart-pole", "[OCPSolver]") {
Expand Down
1 change: 1 addition & 0 deletions test/src/control/OCPSolverTest_DifferentialDrive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <fmt/core.h>
#include <sleipnir/control/OCPSolver.hpp>

#include "CatchStringConverters.hpp"
#include "DifferentialDriveUtil.hpp"
#include "RK4.hpp"

Expand Down
2 changes: 2 additions & 0 deletions test/src/control/OCPSolverTest_Flywheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <sleipnir/control/OCPSolver.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "CatchStringConverters.hpp"

using namespace std::chrono_literals;

namespace {
Expand Down
2 changes: 2 additions & 0 deletions test/src/optimization/ConstraintsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <sleipnir/autodiff/VariableMatrix.hpp>
#include <sleipnir/optimization/Constraints.hpp>

#include "CatchStringConverters.hpp"

TEST_CASE("Constraints - Equality constraint boolean comparison",
"[Constraints]") {
using sleipnir::Variable;
Expand Down
2 changes: 2 additions & 0 deletions test/src/optimization/DecisionVariableTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <catch2/catch_test_macros.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "CatchStringConverters.hpp"

TEST_CASE("DecisionVariable - Scalar init assign", "[DecisionVariable]") {
sleipnir::OptimizationProblem problem;

Expand Down
2 changes: 2 additions & 0 deletions test/src/optimization/LinearProblemTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <catch2/catch_test_macros.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "CatchStringConverters.hpp"

TEST_CASE("LinearProblem - Maximize", "[LinearProblem]") {
sleipnir::OptimizationProblem problem;

Expand Down
1 change: 1 addition & 0 deletions test/src/optimization/NonlinearProblemTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <fmt/core.h>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "CatchStringConverters.hpp"
#include "Range.hpp"

TEST_CASE("NonlinearProblem - Quartic", "[NonlinearProblem]") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <catch2/catch_test_macros.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "CatchStringConverters.hpp"

// This problem tests the case where regularization fails
TEST_CASE("OptimizationProblem - Arm on elevator", "[OptimizationProblem]") {
using namespace std::chrono_literals;
Expand Down
1 change: 1 addition & 0 deletions test/src/optimization/OptimizationProblemTest_CartPole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "CartPoleUtil.hpp"
#include "CatchStringConverters.hpp"
#include "RK4.hpp"

TEST_CASE("OptimizationProblem - Cart-pole", "[OptimizationProblem]") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <fmt/core.h>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "CatchStringConverters.hpp"
#include "DifferentialDriveUtil.hpp"
#include "RK4.hpp"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <fmt/core.h>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "CatchStringConverters.hpp"

TEST_CASE("OptimizationProblem - Double integrator", "[OptimizationProblem]") {
using namespace std::chrono_literals;

Expand Down
2 changes: 2 additions & 0 deletions test/src/optimization/OptimizationProblemTest_Flywheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <fmt/core.h>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "CatchStringConverters.hpp"

namespace {
bool Near(double expected, double actual, double tolerance) {
return std::abs(expected - actual) < tolerance;
Expand Down
2 changes: 2 additions & 0 deletions test/src/optimization/QuadraticProblemTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <catch2/catch_test_macros.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "CatchStringConverters.hpp"

TEST_CASE("QuadraticProblem - Unconstrained 1D", "[QuadraticProblem]") {
sleipnir::OptimizationProblem problem;

Expand Down
2 changes: 2 additions & 0 deletions test/src/optimization/SolverExitConditionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <catch2/catch_test_macros.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "CatchStringConverters.hpp"

// These tests ensure coverage of the off-nominal solver exit conditions

TEST_CASE("SolverExitCondition - Callback requested stop",
Expand Down
2 changes: 2 additions & 0 deletions test/src/optimization/TrivialProblemTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <catch2/catch_test_macros.hpp>
#include <sleipnir/optimization/OptimizationProblem.hpp>

#include "CatchStringConverters.hpp"

TEST_CASE("TrivialProblem - Empty", "[TrivialProblem]") {
sleipnir::OptimizationProblem problem;

Expand Down

0 comments on commit ce6976e

Please sign in to comment.