diff --git a/include/boost/algorithm/cxx11/ordered.hpp b/include/boost/algorithm/cxx11/ordered.hpp index bd8968e..ef2ec41 100644 --- a/include/boost/algorithm/cxx11/ordered.hpp +++ b/include/boost/algorithm/cxx11/ordered.hpp @@ -22,6 +22,7 @@ #include #include +#include namespace boost { namespace algorithm { @@ -124,6 +125,9 @@ using std::is_sorted; // Section 25.4.1.5 return boost::algorithm::is_sorted_until ( boost::begin ( range ), boost::end ( range )); } +namespace detail { + typedef struct { typedef bool type; } bool_; +}; /// \fn is_sorted ( const R &range, Pred p ) /// \return whether or not the entire range R is sorted @@ -133,7 +137,8 @@ using std::is_sorted; // Section 25.4.1.5 /// \param p A binary predicate that returns true if two elements are ordered. /// template - bool is_sorted ( const R &range, Pred p ) + typename boost::lazy_disable_if_c< boost::is_same::value, boost::mpl::identity >::type + is_sorted ( const R &range, Pred p ) { return boost::algorithm::is_sorted ( boost::begin ( range ), boost::end ( range ), p ); } @@ -144,7 +149,7 @@ using std::is_sorted; // Section 25.4.1.5 /// /// \param range The range to be tested. /// - template + template bool is_sorted ( const R &range ) { return boost::algorithm::is_sorted ( boost::begin ( range ), boost::end ( range )); diff --git a/test/ordered_test.cpp b/test/ordered_test.cpp index eaf4523..55fd90f 100644 --- a/test/ordered_test.cpp +++ b/test/ordered_test.cpp @@ -29,6 +29,54 @@ namespace ba = boost::algorithm; static void test_ordered(void) +{ + const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 }; + const int randomValues[] = { 3, 6, 1, 2, 7 }; + const int constantValues[] = { 1, 2, 2, 2, 5 }; + int nonConstantArray[] = { 1, 2, 2, 2, 5 }; + const int inOrderUntilTheEnd [] = { 0, 1, 2, 3, 4, 5, 6, 7, 6 }; + +// Begin/end checks + BOOST_CHECK ( ba::is_sorted (b_e(strictlyIncreasingValues))); + BOOST_CHECK ( !ba::is_sorted (b_e(randomValues))); + BOOST_CHECK ( ba::is_sorted (b_e(strictlyIncreasingValues), std::less())); + BOOST_CHECK ( !ba::is_sorted (b_e(strictlyIncreasingValues), std::greater())); + +// Range checks + BOOST_CHECK ( ba::is_sorted (a_range(strictlyIncreasingValues))); + BOOST_CHECK ( !ba::is_sorted (a_range(randomValues))); + BOOST_CHECK ( ba::is_sorted (a_range(strictlyIncreasingValues), std::less())); + BOOST_CHECK ( !ba::is_sorted (a_range(strictlyIncreasingValues), std::greater())); + + BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues)) == a_end(strictlyIncreasingValues)); + BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues), std::less()) == a_end(strictlyIncreasingValues)); + BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues)) == boost::end(strictlyIncreasingValues)); + BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues), std::less()) == boost::end(strictlyIncreasingValues)); + +// Check for const and non-const arrays + BOOST_CHECK ( ba::is_sorted_until ( b_e(constantValues), std::less()) != a_end(constantValues)); + BOOST_CHECK ( ba::is_sorted_until ( a_range(constantValues), std::less()) != boost::end(constantValues)); + BOOST_CHECK ( ba::is_sorted_until ( b_e(nonConstantArray), std::less()) != a_end(nonConstantArray)); + BOOST_CHECK ( ba::is_sorted_until ( a_range(nonConstantArray), std::less()) != boost::end(nonConstantArray)); + + BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues), std::less()) == &randomValues[2] ); + BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues)) == &randomValues[2] ); + BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues), std::less()) == &randomValues[2] ); + BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues)) == &randomValues[2] ); + + BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd), std::less()) == &inOrderUntilTheEnd[8] ); + BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd)) == &inOrderUntilTheEnd[8] ); + +// For zero and one element collections, the comparison predicate should never be called + BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues), std::equal_to()) == a_begin(randomValues)); + BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues)) == a_begin(randomValues)); + BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1, std::equal_to()) == a_begin(randomValues) + 1); + BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1 ) == a_begin(randomValues) + 1); +} + + +static void +test_increasing_decreasing(void) { const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 }; const int strictlyDecreasingValues[] = { 9, 8, 7, 6, 5 }; @@ -36,8 +84,6 @@ test_ordered(void) const int decreasingValues[] = { 9, 7, 7, 7, 5 }; const int randomValues[] = { 3, 6, 1, 2, 7 }; const int constantValues[] = { 7, 7, 7, 7, 7 }; - int nonConstantArray[] = { 7, 7, 7, 7, 7 }; - const int inOrderUntilTheEnd [] = { 0, 1, 2, 3, 4, 5, 6, 7, 6 }; // Test a strictly increasing sequence BOOST_CHECK ( ba::is_strictly_increasing (b_e(strictlyIncreasingValues))); @@ -98,30 +144,11 @@ test_ordered(void) BOOST_CHECK ( !ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2)); BOOST_CHECK ( !ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2)); - // Test underlying routines - BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues), std::less()) == a_end(strictlyIncreasingValues)); - BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues), std::less()) == boost::end(strictlyIncreasingValues)); - - BOOST_CHECK ( ba::is_sorted_until ( b_e(nonConstantArray), std::less()) != a_end(nonConstantArray)); - BOOST_CHECK ( ba::is_sorted_until ( a_range(nonConstantArray), std::less()) != boost::end(nonConstantArray)); - - BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues), std::less()) == &randomValues[2] ); - BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues), std::less()) == &randomValues[2] ); - - BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues), std::less()) == &randomValues[2] ); - BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues), std::less()) == &randomValues[2] ); - - BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd), std::less()) == &inOrderUntilTheEnd[8] ); - -// For zero and one element collections, the comparison predicate should never be called - BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues), std::equal_to()) == a_begin(randomValues)); - BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1, std::equal_to()) == a_begin(randomValues) + 1); - } int test_main( int, char * [] ) { test_ordered (); - + test_increasing_decreasing (); return 0; }