Skip to content

Commit

Permalink
Add GoogleTest value printers for ExpressionType and SolverExitCondition
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Dec 17, 2023
1 parent 13b4f01 commit 3bf1fbe
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 30 deletions.
27 changes: 5 additions & 22 deletions include/sleipnir/autodiff/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,9 @@
#include "sleipnir/IntrusiveSharedPtr.hpp"
#include "sleipnir/Pool.hpp"
#include "sleipnir/SymbolExports.hpp"
#include "sleipnir/autodiff/ExpressionType.hpp"

namespace sleipnir {

/**
* Expression type.
*
* Used for autodiff caching.
*/
enum class ExpressionType {
/// There is no expression.
kNone,
/// The expression is a constant.
kConstant,
/// The expression is composed of linear and lower-order operators.
kLinear,
/// The expression is composed of quadratic and lower-order operators.
kQuadratic,
/// The expression is composed of nonlinear and lower-order operators.
kNonlinear
};

namespace detail {
namespace sleipnir::detail {

struct SLEIPNIR_DLLEXPORT Expression;

Expand Down Expand Up @@ -442,7 +423,9 @@ SLEIPNIR_DLLEXPORT ExpressionPtr tan( // NOLINT
*/
SLEIPNIR_DLLEXPORT ExpressionPtr tanh(const ExpressionPtr& x);

} // namespace detail
} // namespace sleipnir::detail

namespace sleipnir {

// FIXME: Doxygen is confused:
//
Expand Down
37 changes: 37 additions & 0 deletions include/sleipnir/autodiff/ExpressionType.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Sleipnir contributors

#pragma once

#include <iosfwd>

#include "sleipnir/SymbolExports.hpp"

namespace sleipnir {

/**
* Expression type.
*
* Used for autodiff caching.
*/
enum class ExpressionType {
/// There is no expression.
kNone,
/// The expression is a constant.
kConstant,
/// The expression is composed of linear and lower-order operators.
kLinear,
/// The expression is composed of quadratic and lower-order operators.
kQuadratic,
/// The expression is composed of nonlinear and lower-order operators.
kNonlinear
};

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

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

#pragma once

#include <iosfwd>

#include "sleipnir/SymbolExports.hpp"

namespace sleipnir {

/**
Expand Down Expand Up @@ -35,4 +39,13 @@ enum class SolverExitCondition {
kMaxWallClockTimeExceeded = -7
};

/**
* GoogleTest value formatter for SolverExitCondition.
*
* @param cond SolverExitCondition to print.
* @param os Output stream to which to print.
*/
SLEIPNIR_DLLEXPORT void PrintTo(const SolverExitCondition& cond,
std::ostream* os);

} // namespace sleipnir
16 changes: 8 additions & 8 deletions src/autodiff/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
#include <cmath>
#include <numbers>

namespace sleipnir {

// Instantiate Expression pool in Expression.cpp instead to avoid ODR violation
template EXPORT_TEMPLATE_DEFINE(SLEIPNIR_DLLEXPORT)
PoolAllocator<detail::Expression> GlobalPoolAllocator<detail::Expression>();

} // namespace sleipnir

namespace sleipnir::detail {

namespace {
Expand Down Expand Up @@ -846,3 +838,11 @@ ExpressionPtr tanh(const ExpressionPtr& x) {
}

} // namespace sleipnir::detail

namespace sleipnir {

// Instantiate Expression pool in Expression.cpp instead to avoid ODR violation
template EXPORT_TEMPLATE_DEFINE(SLEIPNIR_DLLEXPORT)
PoolAllocator<detail::Expression> GlobalPoolAllocator<detail::Expression>();

} // namespace sleipnir
31 changes: 31 additions & 0 deletions src/autodiff/ExpressionType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Sleipnir contributors

#include "sleipnir/autodiff/ExpressionType.hpp"

#include <ostream>

namespace sleipnir {

void PrintTo(const ExpressionType& type, std::ostream* os) {
using enum sleipnir::ExpressionType;

switch (type) {
case kNone:
*os << "kNone";
break;
case kConstant:
*os << "kConstant";
break;
case kLinear:
*os << "kLinear";
break;
case kQuadratic:
*os << "kQuadratic";
break;
case kNonlinear:
*os << "kNonlinear";
break;
}
}

} // namespace sleipnir
46 changes: 46 additions & 0 deletions src/optimization/SolverExitCondition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Sleipnir contributors

#include "sleipnir/optimization/SolverExitCondition.hpp"

#include <ostream>

namespace sleipnir {

void PrintTo(const SolverExitCondition& cond, std::ostream* os) {
using enum sleipnir::SolverExitCondition;

switch (cond) {
case kSuccess:
*os << "kSuccess";
break;
case kSolvedToAcceptableTolerance:
*os << "kSolvedToAcceptableTolerance";
break;
case kCallbackRequestedStop:
*os << "kCallbackRequestedStop";
break;
case kTooFewDOFs:
*os << "kTooFewDOFs";
break;
case kLocallyInfeasible:
*os << "kLocallyInfeasible";
break;
case kBadSearchDirection:
*os << "kBadSearchDirection";
break;
case kMaxSearchDirectionTooSmall:
*os << "kMaxSearchDirectionTooSmall";
break;
case kDivergingIterates:
*os << "kDivergingIterates";
break;
case kMaxIterationsExceeded:
*os << "kMaxIterationsExceeded";
break;
case kMaxWallClockTimeExceeded:
*os << "kMaxWallClockTimeExceeded";
break;
}
}

} // namespace sleipnir

0 comments on commit 3bf1fbe

Please sign in to comment.