From d50d85613add1a52eed45ae51778d6649137865c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=81oskot?= Date: Sat, 25 Jun 2022 13:52:54 +0200 Subject: [PATCH] refactor: Make with_tolerance reusable across other tests Refinement of PR #527 --- test/extension/numeric/matrix3x2.cpp | 30 ++++++-------------- test/test_utility_with_tolerance.hpp | 41 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 test/test_utility_with_tolerance.hpp diff --git a/test/extension/numeric/matrix3x2.cpp b/test/extension/numeric/matrix3x2.cpp index 1f0ae8f67d..afc4048cc6 100644 --- a/test/extension/numeric/matrix3x2.cpp +++ b/test/extension/numeric/matrix3x2.cpp @@ -12,24 +12,12 @@ #include +#include "test_utility_with_tolerance.hpp" + #include namespace gil = boost::gil; -// Tolerance predicate for floating point comparison to use with BOOST_TEST_WITH -template -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; } @@ -134,10 +122,10 @@ void test_matrix3x2_vector_multiplication() void test_matrix3x2_get_rotate() { auto m1 = gil::matrix3x2::get_rotate(HALF_PI); - BOOST_TEST_WITH(m1.a, std::cos(HALF_PI), with_tolerance(0.03)); + BOOST_TEST_WITH(m1.a, std::cos(HALF_PI), gil::test::utility::with_tolerance(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(0.03)); + BOOST_TEST_WITH(m1.d, std::cos(HALF_PI), gil::test::utility::with_tolerance(0.03)); BOOST_TEST_EQ(m1.e, 0); BOOST_TEST_EQ(m1.f, 0); } @@ -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(1e-9)); - BOOST_TEST_WITH(p.y, p2.y, with_tolerance(1e-9)); + BOOST_TEST_WITH(p.x, p2.x, gil::test::utility::with_tolerance(1e-9)); + BOOST_TEST_WITH(p.y, p2.y, gil::test::utility::with_tolerance(1e-9)); } void test_matrix3x2_center_rotate() @@ -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(1e-9)); + BOOST_TEST_WITH(m1.a , std::cos(HALF_PI) , gil::test::utility::with_tolerance(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(1e-9)); + BOOST_TEST_WITH(m1.d , std::cos(HALF_PI) , gil::test::utility::with_tolerance(1e-9)); BOOST_TEST_EQ (m1.e , 100); - BOOST_TEST_WITH(m1.f , std::cos(HALF_PI) , with_tolerance(1e-9)); + BOOST_TEST_WITH(m1.f , std::cos(HALF_PI) , gil::test::utility::with_tolerance(1e-9)); } int main() diff --git a/test/test_utility_with_tolerance.hpp b/test/test_utility_with_tolerance.hpp new file mode 100644 index 0000000000..af5c7816c5 --- /dev/null +++ b/test/test_utility_with_tolerance.hpp @@ -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 +#include +#include + +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 +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