Skip to content

Commit

Permalink
tests: add custom test helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jolars committed Nov 30, 2023
1 parent f27bae6 commit 6fd9006
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 10 deletions.
55 changes: 55 additions & 0 deletions tests/helpers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_templated.hpp>

template<typename T>
std::vector<double>
asStdVec(const T& x)
{
std::vector<double> vec(x.data(), x.data() + x.size());

return vec;
}

template<typename Range>
struct VectorApproxEqualMatcher : Catch::Matchers::MatcherGenericBase
{
VectorApproxEqualMatcher(const Range& range, const double eps)
: range{ range }
, eps{ eps }
{
}

template<typename OtherRange>
bool match(OtherRange const& other) const
{

if (range.size() != other.size()) {
return false;
}

for (int i = 0; i < range.size(); ++i) {
if (std::abs(range[i] - other[i]) > eps) {
return false;
}
}

return true;
}

std::string describe() const override
{
return "Approximately equal to: " + Catch::rangeToString(asStdVec(range));
}

private:
const Range& range;
const double eps;
};

template<typename Range>
auto
VectorApproxEqual(const Range& range, const double eps = 1e-6)
-> VectorApproxEqualMatcher<Range>
{
return VectorApproxEqualMatcher<Range>{ range, eps };
}
20 changes: 10 additions & 10 deletions tests/test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "helpers.hpp"
#include <Eigen/Core>
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>
#include <catch2/matchers/catch_matchers_range_equals.hpp>
#include <catch2/matchers/catch_matchers_templated.hpp>
#include <slope/slope.h>

TEST_CASE("Simple low-dimensional design", "[gaussian, dense]")
TEST_CASE("Simple low-dimensional design", "[gaussian, dense, ols]")
{
using namespace Catch::Matchers;

Expand All @@ -27,10 +29,9 @@ TEST_CASE("Simple low-dimensional design", "[gaussian, dense]")
auto no_intercept_no_std =
slope::slope(x, y, alpha, lambda, "gaussian", false, false);

REQUIRE_THAT(no_intercept_no_std.betas.coeff(0, 0),
WithinAbsMatcher(1, 0.001));
REQUIRE_THAT(no_intercept_no_std.betas.coeff(1, 0),
WithinAbsMatcher(1, 0.001));
Eigen::VectorXd coef = no_intercept_no_std.betas.col(0);

REQUIRE_THAT(coef, VectorApproxEqual(beta, 1e-4));
}

TEST_CASE("X is identity", "[gaussian, dense]")
Expand All @@ -49,8 +50,7 @@ TEST_CASE("X is identity", "[gaussian, dense]")

Eigen::VectorXd betas = res.betas.col(0);

REQUIRE_THAT(betas(0), WithinAbsMatcher(4.0, 0.001));
REQUIRE_THAT(betas(1), WithinAbsMatcher(3.0, 0.001));
REQUIRE_THAT(betas(2), WithinAbsMatcher(2.0, 0.001));
REQUIRE_THAT(betas(3), WithinAbsMatcher(1.0, 0.001));
std::array<double, 4> expected = { 4.0, 3.0, 2.0, 1.0 };

REQUIRE_THAT(betas, VectorApproxEqual(expected));
}

0 comments on commit 6fd9006

Please sign in to comment.