From 3bc56800cdda6bcec8b0514cac1d7939f0c74472 Mon Sep 17 00:00:00 2001 From: Bjorn Reese Date: Sat, 11 Feb 2017 15:03:45 +0100 Subject: [PATCH] Added BOOST_TEST_ALL_EQ macro to compare container contents --- doc/lightweight_test.qbk | 9 ++++++ include/boost/core/lightweight_test.hpp | 41 +++++++++++++++++++++++++ test/lightweight_test_test.cpp | 10 ++++++ 3 files changed, 60 insertions(+) diff --git a/doc/lightweight_test.qbk b/doc/lightweight_test.qbk index c7ef180..c3ab7bc 100644 --- a/doc/lightweight_test.qbk +++ b/doc/lightweight_test.qbk @@ -38,6 +38,7 @@ When using `lightweight_test.hpp`, *do not forget* to #define BOOST_TEST_NE(expr1, expr2) /*unspecified*/ #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_THROWS(expr, excep) /*unspecified*/ namespace boost @@ -121,6 +122,14 @@ BOOST_TEST_CSTR_NE(expr1, expr2) Specialization of BOOST_TEST_NE which interprets expr1 and expr2 as pointers to null-terminated byte strings (C strings). If `std::strcmp(expr1, expr2) == 0`, increase the error count and output a message containing both expressions. +[section BOOST_TEST_ALL_EQ] + +`` +BOOST_TEST_ALL_EQ(begin1, end1, begin2, end2) +`` + +Compares the content of two containers. If they have different sizes, or if any pairwise element differs, increases the error count and outputs a message containing all differing elements. + [endsect] [section BOOST_TEST_THROWS] diff --git a/include/boost/core/lightweight_test.hpp b/include/boost/core/lightweight_test.hpp index d1766a4..d8a316e 100644 --- a/include/boost/core/lightweight_test.hpp +++ b/include/boost/core/lightweight_test.hpp @@ -19,6 +19,8 @@ // http://www.boost.org/LICENSE_1_0.txt // +#include +#include #include #include #include @@ -180,6 +182,43 @@ inline void test_cstr_ne_impl( char const * expr1, char const * expr2, } } +template +void test_all_eq_impl(char const * file, int line, char const * function, + FwdIt1 first_begin, FwdIt1 first_end, + FwdIt2 second_begin, FwdIt2 second_end) +{ + if (std::distance(first_begin, first_end) != std::distance(second_begin, second_end)) + { + ::boost::detail::error_impl("Container sizes are different", __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); + } + else + { + FwdIt1 first_it = first_begin; + FwdIt2 second_it = second_begin; + std::ostringstream indices; + do + { + while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it)) + { + ++first_it; + ++second_it; + } + if (first_it == first_end) + { + boost::detail::report_errors_remind(); + return; + } + indices << " [" << std::distance(first_begin, first_it) << "] '" << *first_it << "' != '" << *second_it << "'"; + ++first_it; + ++second_it; + } while (first_it != first_end); + BOOST_LIGHTWEIGHT_TEST_OSTREAM + << file << "(" << line << "): Container contents differ in function '" << function << "': mismatching indices" + << indices.str() << std::endl; + ++boost::detail::test_errors(); + } +} + #if defined(_MSC_VER) # pragma warning(pop) #elif defined(__clang__) && defined(__has_warning) @@ -225,6 +264,8 @@ inline int report_errors() #define BOOST_TEST_CSTR_EQ(expr1,expr2) ( ::boost::detail::test_cstr_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) #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(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION, begin1, end1, begin2, end2) ) + #ifndef BOOST_NO_EXCEPTIONS #define BOOST_TEST_THROWS( EXPR, EXCEP ) \ try { \ diff --git a/test/lightweight_test_test.cpp b/test/lightweight_test_test.cpp index a3b04a9..bfef152 100644 --- a/test/lightweight_test_test.cpp +++ b/test/lightweight_test_test.cpp @@ -8,6 +8,7 @@ // http://www.boost.org/LICENSE_1_0.txt // +#include #include struct X @@ -74,6 +75,15 @@ int main() BOOST_TEST_NE("xabc"+1, "yabc"+1); // equal cstrings, different addresses BOOST_TEST_CSTR_NE("x", "y"); + // BOOST_TEST_ALL_EQ + { + std::vector xarray; + xarray.push_back(1); + xarray.push_back(2); + std::vector yarray(xarray); + BOOST_TEST_ALL_EQ(xarray.begin(), xarray.end(), yarray.begin(), yarray.end()); + } + // BOOST_TEST_THROWS BOOST_TEST_THROWS( throw X(), X );