diff --git a/doc/lightweight_test.qbk b/doc/lightweight_test.qbk index 77a3e49..e0a1f77 100644 --- a/doc/lightweight_test.qbk +++ b/doc/lightweight_test.qbk @@ -39,6 +39,7 @@ When using `lightweight_test.hpp`, *do not forget* to #define BOOST_TEST_CSTR_EQ(expr1, expr2) /*unspecified*/ #define BOOST_TEST_CSTR_NE(expr1, expr2) /*unspecified*/ #define BOOST_TEST_ALL_EQ(begin1, end1, begin2, end2) /* unspecified */ +#define BOOST_TEST_ALL_WITH(begin1, end1, begin2, end2, predicate) /* unspecified */ #define BOOST_TEST_THROWS(expr, excep) /*unspecified*/ namespace boost @@ -132,6 +133,16 @@ Compares the content of two sequences. If they have different sizes, or if any p [endsect] +[section BOOST_TEST_ALL_WITH] + +`` +BOOST_TEST_ALL_WITH(begin1, end1, begin2, end2, predicate) +`` + +Compares the content of two sequences. If they have different sizes, or if any pairwise element do not fulfill the binary predicate, increases the error count and outputs a message containing at most 8 differing elements. + +[endsect] + [section BOOST_TEST_THROWS] `` diff --git a/include/boost/core/lightweight_test.hpp b/include/boost/core/lightweight_test.hpp index b4b59c7..b580dd2 100644 --- a/include/boost/core/lightweight_test.hpp +++ b/include/boost/core/lightweight_test.hpp @@ -249,6 +249,75 @@ void test_all_eq_impl(FormattedOutputFunction& output, } } +template +void test_all_with_impl(FormattedOutputFunction& output, + char const * file, int line, char const * function, + InputIterator1 first_begin, InputIterator1 first_end, + InputIterator2 second_begin, InputIterator2 second_end, + BinaryPredicate predicate) +{ + InputIterator1 first_it = first_begin; + InputIterator2 second_it = second_begin; + typename std::iterator_traits::difference_type first_index = 0; + typename std::iterator_traits::difference_type second_index = 0; + std::size_t error_count = 0; + const std::size_t max_count = 8; + do + { + while ((first_it != first_end) && (second_it != second_end) && predicate(*first_it, *second_it)) + { + ++first_it; + ++second_it; + ++first_index; + ++second_index; + } + if ((first_it == first_end) || (second_it == second_end)) + { + break; // do-while + } + if (error_count == 0) + { + output << file << "(" << line << "): Container contents differ in function '" << function << "':"; + } + else if (error_count >= max_count) + { + output << " ..."; + break; + } + output << " [" << first_index << "]"; + ++first_it; + ++second_it; + ++first_index; + ++second_index; + ++error_count; + } while (first_it != first_end); + + first_index += std::distance(first_it, first_end); + second_index += std::distance(second_it, second_end); + if (first_index != second_index) + { + if (error_count == 0) + { + output << file << "(" << line << "): Container sizes differ in function '" << function << "': size(" << first_index << ") != size(" << second_index << ")"; + } + else + { + output << " [*] size(" << first_index << ") != size(" << second_index << ")"; + } + ++error_count; + } + + if (error_count == 0) + { + report_errors_remind(); + } + else + { + output << std::endl; + ++test_errors(); + } +} + #if defined(_MSC_VER) # pragma warning(pop) #elif defined(__clang__) && defined(__has_warning) @@ -295,6 +364,7 @@ inline int report_errors() #define BOOST_TEST_CSTR_NE(expr1,expr2) ( ::boost::detail::test_cstr_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) #define BOOST_TEST_ALL_EQ(begin1, end1, begin2, end2) ( ::boost::detail::test_all_eq_impl(BOOST_LIGHTWEIGHT_TEST_OSTREAM, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, begin1, end1, begin2, end2) ) +#define BOOST_TEST_ALL_WITH(begin1, end1, begin2, end2, predicate) ( ::boost::detail::test_all_with_impl(BOOST_LIGHTWEIGHT_TEST_OSTREAM, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, begin1, end1, begin2, end2, predicate) ) #ifndef BOOST_NO_EXCEPTIONS #define BOOST_TEST_THROWS( EXPR, EXCEP ) \ diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 712de26..d675eae 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -66,6 +66,7 @@ run lightweight_test_test.cpp : : : off : lightweight_test_t run lightweight_test_test2.cpp ; run lightweight_test_all_eq_test.cpp ; +run lightweight_test_all_with_test.cpp ; run-fail lightweight_test_fail.cpp ; run-fail lightweight_test_fail2.cpp ; diff --git a/test/lightweight_test_all_with_test.cpp b/test/lightweight_test_all_with_test.cpp new file mode 100644 index 0000000..add725e --- /dev/null +++ b/test/lightweight_test_all_with_test.cpp @@ -0,0 +1,150 @@ +// +// Test for BOOST_TEST_ALL_WITH +// +// Copyright (c) 2017 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 + +int test_vector() +{ + int failed_test_cases = 0; + + // Successes + + { + std::vector x, y; + x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); x.push_back( 4 ); + y.push_back( 1 ); y.push_back( 2 ); y.push_back( 3 ); y.push_back( 4 ); + BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::equal_to() ); + } + + { + std::vector x, y; + x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); x.push_back( 4 ); + y.push_back( 2 ); y.push_back( 3 ); y.push_back( 4 ); y.push_back( 5 ); + BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::not_equal_to() ); + } + + { + std::vector x, y; + x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); x.push_back( 4 ); + y.push_back( 2 ); y.push_back( 3 ); y.push_back( 4 ); y.push_back( 5 ); + BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::less() ); + } + + // Failures + + { + std::vector x, y; + x.push_back( 1 ); + BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::equal_to() ); + ++failed_test_cases; + } + + { + std::vector x, y; + y.push_back( 1 ); + BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::equal_to() ); + ++failed_test_cases; + } + + { + std::vector x, y; + x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); x.push_back( 4 ); + y.push_back( 1 ); y.push_back( 2 ); y.push_back( 3 ); + BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::equal_to() ); + ++failed_test_cases; + } + + { + std::vector x, y; + x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); + y.push_back( 1 ); y.push_back( 2 ); y.push_back( 3 ); y.push_back( 4 ); + BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::equal_to() ); + ++failed_test_cases; + } + + { + std::vector x, y; + x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); x.push_back( 4 ); + y.push_back( 1 ); y.push_back( 3 ); y.push_back( 2 ); y.push_back( 4 ); + BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::equal_to() ); + ++failed_test_cases; + } + + { + std::vector x, y; + x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 ); x.push_back( 4 ); + y.push_back( 1 ); y.push_back( 3 ); y.push_back( 2 ); y.push_back( 4 ); + BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), std::less() ); + ++failed_test_cases; + } + + return failed_test_cases; +} + +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; +}; + +int test_tolerance_predicate() +{ + int failed_test_cases = 0; + + // Successes + + { + std::vector x, y; + x.push_back( 1.0 ); x.push_back( 2.0 ); x.push_back( 3.0 ); x.push_back( 4.0 ); + y.push_back( 1.0 ); y.push_back( 2.0 ); y.push_back( 3.0 ); y.push_back( 4.0 ); + BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), with_tolerance(1e-5) ); + } + + { + std::vector x, y; + x.push_back( 1.0 ); x.push_back( 1.0 ); + y.push_back( 1.0 - 1e-6 ); y.push_back( 1.0 + 1e-6 ); + BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), with_tolerance(1e-5) ); + } + + // Failures + + { + std::vector x, y; + x.push_back( 1.0 ); x.push_back( 1.0 ); + y.push_back( 1.0 - 1e-4 ); y.push_back( 1.0 + 1e-4 ); + BOOST_TEST_ALL_WITH( x.begin(), x.end(), y.begin(), y.end(), with_tolerance(1e-5) ); + ++failed_test_cases; + } + + return failed_test_cases; +} + +int main() +{ + int failed_test_cases = 0; + + failed_test_cases += test_vector(); + failed_test_cases += test_tolerance_predicate(); + + boost::report_errors(); + + return boost::detail::test_errors() != failed_test_cases; +}