Changed ForwardIterator to InputIterator for test_all_eq_impl

This commit is contained in:
Bjorn Reese
2017-02-12 13:19:39 +01:00
parent fb09632580
commit db8efb4ce9
2 changed files with 130 additions and 39 deletions

View File

@ -181,58 +181,73 @@ inline void test_cstr_ne_impl( char const * expr1, char const * expr2,
}
}
template<class FormattedOutputFunction, class ForwardIterator1, class ForwardIterator2>
template<class FormattedOutputFunction, class InputIterator1, class InputIterator2>
void test_all_eq_impl(FormattedOutputFunction& output,
char const * file, int line, char const * function,
ForwardIterator1 first_begin, ForwardIterator1 first_end,
ForwardIterator2 second_begin, ForwardIterator2 second_end)
InputIterator1 first_begin, InputIterator1 first_end,
InputIterator2 second_begin, InputIterator2 second_end)
{
typename std::iterator_traits<ForwardIterator1>::difference_type first_distance = std::distance(first_begin, first_end);
typename std::iterator_traits<ForwardIterator2>::difference_type second_distance = std::distance(second_begin, second_end);
if (first_distance != second_distance)
InputIterator1 first_it = first_begin;
InputIterator2 second_it = second_begin;
typename std::iterator_traits<InputIterator1>::difference_type first_index = 0;
typename std::iterator_traits<InputIterator2>::difference_type second_index = 0;
std::size_t error_count = 0;
do
{
output << file << "(" << line << "): "
<< "Container sizes are different"
<< " in function '" << function << "': "
<< first_distance << " != " << second_distance
<< std::endl;
++test_errors();
}
else
{
ForwardIterator1 first_it = first_begin;
ForwardIterator2 second_it = second_begin;
std::size_t error_count = 0;
do
while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it))
{
while ((first_it != first_end) && (second_it != second_end) && (*first_it == *second_it))
{
++first_it;
++second_it;
}
if (first_it == first_end)
{
break; // do-while
}
if (error_count == 0)
{
output << file << "(" << line << "): Container contents differ in function '" << function << "': mismatching indices";
}
output << " [" << std::distance(first_begin, first_it) << "] '" << test_output_impl(*first_it) << "' != '" << test_output_impl(*second_it) << "'";
++first_it;
++second_it;
++error_count;
} while (first_it != first_end);
++first_index;
++second_index;
}
if ((first_it == first_end) || (second_it == second_end))
{
break; // do-while
}
if (error_count == 0)
{
boost::detail::report_errors_remind();
output << file << "(" << line << "): Container contents differ in function '" << function << "':";
}
output << " [" << first_index << "] '" << test_output_impl(*first_it) << "' != '" << test_output_impl(*second_it) << "'";
++first_it;
++second_it;
++first_index;
++second_index;
++error_count;
} while (first_it != first_end);
while (first_it != first_end)
{
++first_it;
++first_index;
}
while (second_it != second_end)
{
++second_it;
++second_index;
}
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 << std::endl;
++boost::detail::test_errors();
output << " [*] size(" << first_index << ") != size(" << second_index << ")";
}
++error_count;
}
if (error_count == 0)
{
boost::detail::report_errors_remind();
}
else
{
output << std::endl;
++boost::detail::test_errors();
}
}

View File

@ -9,18 +9,52 @@
//
#include <vector>
#include <set>
#include <boost/core/lightweight_test.hpp>
int main()
{
int test_cases = 0;
// Array
{
int x[] = { 1 };
int y[] = { 1, 2 };
BOOST_TEST_ALL_EQ( x, x + sizeof(x)/sizeof(x[0]), y, y + sizeof(y)/sizeof(y[0]) );
++test_cases;
}
{
int x[] = { 1, 2 };
int y[] = { 1 };
BOOST_TEST_ALL_EQ( x, x + sizeof(x)/sizeof(x[0]), y, y + sizeof(y)/sizeof(y[0]) );
++test_cases;
}
{
int x[] = { 2 };
int y[] = { 1, 2 };
BOOST_TEST_ALL_EQ( x, x + sizeof(x)/sizeof(x[0]), y, y + sizeof(y)/sizeof(y[0]) );
++test_cases;
}
{
int x[] = { 1, 2, 3, 4 };
int y[] = { 1, 3, 2, 4 };
BOOST_TEST_ALL_EQ( x, x + sizeof(x)/sizeof(x[0]), y, y + sizeof(y)/sizeof(y[0]) );
++test_cases;
}
// Vector
{
std::vector<int> x, y;
x.push_back( 1 );
BOOST_TEST_ALL_EQ( x.begin(), x.end(), y.begin(), y.end() );
++test_cases;
}
{
std::vector<int> x, y;
y.push_back( 1 );
@ -44,6 +78,48 @@ int main()
++test_cases;
}
{
std::vector<int> x, y;
x.push_back( 1 ); x.push_back( 2 ); x.push_back( 3 );
y.push_back( 1 ); y.push_back( 3 ); y.push_back( 2 ); y.push_back( 4 );
BOOST_TEST_ALL_EQ( x.begin(), x.end(), y.begin(), y.end() );
++test_cases;
}
{
std::vector<int> 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 );;
BOOST_TEST_ALL_EQ( x.begin(), x.end(), y.begin(), y.end() );
++test_cases;
}
// Set
{
std::set<int> x, y;
x.insert(1);
y.insert(1); y.insert(3);
BOOST_TEST_ALL_EQ( x.begin(), x.end(), y.begin(), y.end() );
++test_cases;
}
{
std::set<int> x, y;
x.insert(1); x.insert(2);
y.insert(1);
BOOST_TEST_ALL_EQ( x.begin(), x.end(), y.begin(), y.end() );
++test_cases;
}
{
std::set<int> x, y;
x.insert(1); x.insert(2);
y.insert(1); y.insert(3);
BOOST_TEST_ALL_EQ( x.begin(), x.end(), y.begin(), y.end() );
++test_cases;
}
boost::report_errors();
return boost::detail::test_errors() == test_cases;