From 6e3692391560ce130ba3997db7158d3d64c12a43 Mon Sep 17 00:00:00 2001 From: Bjorn Reese Date: Sat, 6 Jun 2020 18:52:43 +0200 Subject: [PATCH] Added BOOST_TEST_WITH macro to verify test results with custom predicate --- include/boost/core/lightweight_test.hpp | 72 ++++++++++++++++++++----- test/Jamfile.v2 | 2 + test/lightweight_test_with_test.cpp | 41 ++++++++++++++ 3 files changed, 101 insertions(+), 14 deletions(-) create mode 100644 test/lightweight_test_with_test.cpp diff --git a/include/boost/core/lightweight_test.hpp b/include/boost/core/lightweight_test.hpp index 43507ad..405682c 100644 --- a/include/boost/core/lightweight_test.hpp +++ b/include/boost/core/lightweight_test.hpp @@ -173,42 +173,84 @@ template inline const void* test_output_impl(T volatile* v) { return co inline const void* test_output_impl(std::nullptr_t) { return nullptr; } #endif -struct lw_test_eq { +template +struct lw_name_traits +{ + static const char* op() { return "<+>"; } +}; + +struct lw_test_eq +{ template bool operator()(const T& t, const U& u) const { return t == u; } +}; + +template <> +struct lw_name_traits +{ static const char* op() { return "=="; } }; - -struct lw_test_ne { + +struct lw_test_ne +{ template bool operator()(const T& t, const U& u) const { return t != u; } +}; + +template <> +struct lw_name_traits +{ static const char* op() { return "!="; } }; - -struct lw_test_lt { + +struct lw_test_lt +{ template bool operator()(const T& t, const U& u) const { return t < u; } +}; + +template <> +struct lw_name_traits +{ static const char* op() { return "<"; } }; - -struct lw_test_le { + +struct lw_test_le +{ template bool operator()(const T& t, const U& u) const { return t <= u; } +}; + +template <> +struct lw_name_traits +{ static const char* op() { return "<="; } }; - -struct lw_test_gt { + +struct lw_test_gt +{ template bool operator()(const T& t, const U& u) const { return t > u; } +}; + +template <> +struct lw_name_traits +{ static const char* op() { return ">"; } }; - -struct lw_test_ge { + +struct lw_test_ge +{ template bool operator()(const T& t, const U& u) const { return t >= u; } - static const char* op() { return ">="; } }; +template <> +struct lw_name_traits +{ + static const char* op() { return ">="; } +}; + template inline bool test_with_impl(BinaryPredicate pred, char const * expr1, char const * expr2, char const * file, int line, char const * function, @@ -222,8 +264,8 @@ inline bool test_with_impl(BinaryPredicate pred, char const * expr1, char const else { BOOST_LIGHTWEIGHT_TEST_OSTREAM - << file << "(" << line << "): test '" << expr1 << " " << pred.op() << " " << expr2 - << "' ('" << test_output_impl(t) << "' " << pred.op() << " '" << test_output_impl(u) + << file << "(" << line << "): test '" << expr1 << " " << lw_name_traits::op() << " " << expr2 + << "' ('" << test_output_impl(t) << "' " << lw_name_traits::op() << " '" << test_output_impl(u) << "') failed in function '" << function << "'" << std::endl; ++test_results().errors(); return false; @@ -448,6 +490,8 @@ inline int report_errors() #define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) ) +#define BOOST_TEST_WITH(expr1,expr2,predicate) ( ::boost::detail::test_with_impl(predicate, #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) + #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_eq(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) #define BOOST_TEST_NE(expr1,expr2) ( ::boost::detail::test_with_impl(::boost::detail::lw_test_ne(), #expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index d565ceb..f39218a 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -79,6 +79,8 @@ run lightweight_test_test.cpp : : : off $(pedantic-errors) : lightweight_test_test_no_except ; run lightweight_test_test2.cpp : : : $(pedantic-errors) ; +run lightweight_test_with_test.cpp + : : : $(pedantic-errors) ; run lightweight_test_all_with_test.cpp : : : $(pedantic-errors) ; run lightweight_test_lt_le_test.cpp diff --git a/test/lightweight_test_with_test.cpp b/test/lightweight_test_with_test.cpp new file mode 100644 index 0000000..9f3c42b --- /dev/null +++ b/test/lightweight_test_with_test.cpp @@ -0,0 +1,41 @@ +// +// Test for BOOST_TEST_WITH +// +// Copyright (c) 2020 Bjorn Reese +// +// 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 +// + +#include +#include +#include +#include + +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; +}; + +void test_tolerance_predicate() +{ + BOOST_TEST_WITH( 1.0, 1.0, with_tolerance(1e-5) ); + BOOST_TEST_WITH( 1.0, 1.0 - 1e-6, with_tolerance(1e-5) ); + BOOST_TEST_WITH( 1.0, 1.0 + 1e-6, with_tolerance(1e-5) ); +} + +int main() +{ + test_tolerance_predicate(); + + return boost::report_errors(); +}