Added BOOST_TEST_WITH macro to verify test results with custom predicate

This commit is contained in:
Bjorn Reese
2020-06-06 18:52:43 +02:00
parent b67cce85c8
commit 6e36923915
3 changed files with 101 additions and 14 deletions

View File

@ -173,42 +173,84 @@ template<class T> 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 <typename BinaryPredicate>
struct lw_name_traits
{
static const char* op() { return "<+>"; }
};
struct lw_test_eq
{
template <typename T, typename U>
bool operator()(const T& t, const U& u) const { return t == u; }
};
template <>
struct lw_name_traits<lw_test_eq>
{
static const char* op() { return "=="; }
};
struct lw_test_ne {
struct lw_test_ne
{
template <typename T, typename U>
bool operator()(const T& t, const U& u) const { return t != u; }
};
template <>
struct lw_name_traits<lw_test_ne>
{
static const char* op() { return "!="; }
};
struct lw_test_lt {
struct lw_test_lt
{
template <typename T, typename U>
bool operator()(const T& t, const U& u) const { return t < u; }
};
template <>
struct lw_name_traits<lw_test_lt>
{
static const char* op() { return "<"; }
};
struct lw_test_le {
struct lw_test_le
{
template <typename T, typename U>
bool operator()(const T& t, const U& u) const { return t <= u; }
};
template <>
struct lw_name_traits<lw_test_le>
{
static const char* op() { return "<="; }
};
struct lw_test_gt {
struct lw_test_gt
{
template <typename T, typename U>
bool operator()(const T& t, const U& u) const { return t > u; }
};
template <>
struct lw_name_traits<lw_test_gt>
{
static const char* op() { return ">"; }
};
struct lw_test_ge {
struct lw_test_ge
{
template <typename T, typename U>
bool operator()(const T& t, const U& u) const { return t >= u; }
static const char* op() { return ">="; }
};
template <>
struct lw_name_traits<lw_test_ge>
{
static const char* op() { return ">="; }
};
template<class BinaryPredicate, class T, class U>
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<decltype(pred)>::op() << " " << expr2
<< "' ('" << test_output_impl(t) << "' " << lw_name_traits<decltype(pred)>::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) )

View File

@ -79,6 +79,8 @@ run lightweight_test_test.cpp : : :
<exception-handling>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

View File

@ -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 <cmath>
#include <functional>
#include <vector>
#include <boost/core/lightweight_test.hpp>
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;
};
void test_tolerance_predicate()
{
BOOST_TEST_WITH( 1.0, 1.0, with_tolerance<double>(1e-5) );
BOOST_TEST_WITH( 1.0, 1.0 - 1e-6, with_tolerance<double>(1e-5) );
BOOST_TEST_WITH( 1.0, 1.0 + 1e-6, with_tolerance<double>(1e-5) );
}
int main()
{
test_tolerance_predicate();
return boost::report_errors();
}