Skip to content

Commit

Permalink
refactor: Make with_tolerance reusable across other tests
Browse files Browse the repository at this point in the history
Refinement of PR #527
  • Loading branch information
mloskot committed Jun 25, 2022
1 parent 46731e6 commit d50d856
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 21 deletions.
30 changes: 9 additions & 21 deletions test/extension/numeric/matrix3x2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,12 @@

#include <boost/core/lightweight_test.hpp>

#include "test_utility_with_tolerance.hpp"

#include <cmath>

namespace gil = boost::gil;

// Tolerance predicate for floating point comparison to use with BOOST_TEST_WITH
template <typename T>
struct with_tolerance
{
with_tolerance(T tolerance) : tolerance(tolerance) {}
bool operator()(T lhs, T rhs)
{
return (std::abs(lhs - rhs) <= tolerance);
}

private:
T tolerance;
};

namespace {
constexpr double HALF_PI = 1.57079632679489661923;
}
Expand Down Expand Up @@ -134,10 +122,10 @@ void test_matrix3x2_vector_multiplication()
void test_matrix3x2_get_rotate()
{
auto m1 = gil::matrix3x2<double>::get_rotate(HALF_PI);
BOOST_TEST_WITH(m1.a, std::cos(HALF_PI), with_tolerance<double>(0.03));
BOOST_TEST_WITH(m1.a, std::cos(HALF_PI), gil::test::utility::with_tolerance<double>(0.03));
BOOST_TEST_EQ(m1.b, 1);
BOOST_TEST_EQ(m1.c, -1);
BOOST_TEST_WITH(m1.d, std::cos(HALF_PI), with_tolerance<double>(0.03));
BOOST_TEST_WITH(m1.d, std::cos(HALF_PI), gil::test::utility::with_tolerance<double>(0.03));
BOOST_TEST_EQ(m1.e, 0);
BOOST_TEST_EQ(m1.f, 0);
}
Expand Down Expand Up @@ -197,8 +185,8 @@ void test_matrix3x2_inverse()
point_t q = gil::transform(inverse(m), p);
point_t p2 = gil::transform(m, q);

BOOST_TEST_WITH(p.x, p2.x, with_tolerance<double>(1e-9));
BOOST_TEST_WITH(p.y, p2.y, with_tolerance<double>(1e-9));
BOOST_TEST_WITH(p.x, p2.x, gil::test::utility::with_tolerance<double>(1e-9));
BOOST_TEST_WITH(p.y, p2.y, gil::test::utility::with_tolerance<double>(1e-9));
}

void test_matrix3x2_center_rotate()
Expand All @@ -208,12 +196,12 @@ void test_matrix3x2_center_rotate()

m1 = gil::center_rotate(dimension, HALF_PI);

BOOST_TEST_WITH(m1.a , std::cos(HALF_PI) , with_tolerance<double>(1e-9));
BOOST_TEST_WITH(m1.a , std::cos(HALF_PI) , gil::test::utility::with_tolerance<double>(1e-9));
BOOST_TEST_EQ (m1.b , 1);
BOOST_TEST_EQ (m1.c , -1);
BOOST_TEST_WITH(m1.d , std::cos(HALF_PI) , with_tolerance<double>(1e-9));
BOOST_TEST_WITH(m1.d , std::cos(HALF_PI) , gil::test::utility::with_tolerance<double>(1e-9));
BOOST_TEST_EQ (m1.e , 100);
BOOST_TEST_WITH(m1.f , std::cos(HALF_PI) , with_tolerance<double>(1e-9));
BOOST_TEST_WITH(m1.f , std::cos(HALF_PI) , gil::test::utility::with_tolerance<double>(1e-9));
}

int main()
Expand Down
41 changes: 41 additions & 0 deletions test/test_utility_with_tolerance.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// Copyright 2020 Samuel Debionne
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
#ifndef BOOST_GIL_TEST_TEST_UTILITY_WITH_TOLERANCE_HPP
#define BOOST_GIL_TEST_TEST_UTILITY_WITH_TOLERANCE_HPP

#include <cstdint>
#include <ostream>
#include <type_traits>

namespace boost { namespace gil {

namespace test { namespace utility {

// Tolerance predicate for floating point comparison to use with BOOST_TEST_WITH.
// See https://github.com/boostorg/core/pull/77 for details.
template <typename T>
struct with_tolerance
{
with_tolerance(T tolerance) : tolerance(tolerance)
{
}

bool operator()(T lhs, T rhs)
{
return (std::abs(lhs - rhs) <= tolerance);
}

private:
T tolerance;
};

}} // namespace test::utility

}} // namespace boost::gil

#endif

0 comments on commit d50d856

Please sign in to comment.