Skip to content

Commit 49668cf

Browse files
committed
More tests; removed ambiguity
[SVN r77134]
1 parent a92ae91 commit 49668cf

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

include/boost/algorithm/cxx11/ordered.hpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <boost/utility/enable_if.hpp>
2424
#include <boost/type_traits/is_same.hpp>
25+
#include <boost/mpl/identity.hpp>
2526

2627
namespace boost { namespace algorithm {
2728

@@ -124,6 +125,9 @@ using std::is_sorted; // Section 25.4.1.5
124125
return boost::algorithm::is_sorted_until ( boost::begin ( range ), boost::end ( range ));
125126
}
126127

128+
namespace detail {
129+
typedef struct { typedef bool type; } bool_;
130+
};
127131

128132
/// \fn is_sorted ( const R &range, Pred p )
129133
/// \return whether or not the entire range R is sorted
@@ -133,7 +137,8 @@ using std::is_sorted; // Section 25.4.1.5
133137
/// \param p A binary predicate that returns true if two elements are ordered.
134138
///
135139
template <typename R, typename Pred>
136-
bool is_sorted ( const R &range, Pred p )
140+
typename boost::lazy_disable_if_c< boost::is_same<R, Pred>::value, boost::mpl::identity<bool> >::type
141+
is_sorted ( const R &range, Pred p )
137142
{
138143
return boost::algorithm::is_sorted ( boost::begin ( range ), boost::end ( range ), p );
139144
}
@@ -144,7 +149,7 @@ using std::is_sorted; // Section 25.4.1.5
144149
///
145150
/// \param range The range to be tested.
146151
///
147-
template <typename R, typename Pred>
152+
template <typename R>
148153
bool is_sorted ( const R &range )
149154
{
150155
return boost::algorithm::is_sorted ( boost::begin ( range ), boost::end ( range ));

test/ordered_test.cpp

+49-22
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,61 @@ namespace ba = boost::algorithm;
2929

3030
static void
3131
test_ordered(void)
32+
{
33+
const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 };
34+
const int randomValues[] = { 3, 6, 1, 2, 7 };
35+
const int constantValues[] = { 1, 2, 2, 2, 5 };
36+
int nonConstantArray[] = { 1, 2, 2, 2, 5 };
37+
const int inOrderUntilTheEnd [] = { 0, 1, 2, 3, 4, 5, 6, 7, 6 };
38+
39+
// Begin/end checks
40+
BOOST_CHECK ( ba::is_sorted (b_e(strictlyIncreasingValues)));
41+
BOOST_CHECK ( !ba::is_sorted (b_e(randomValues)));
42+
BOOST_CHECK ( ba::is_sorted (b_e(strictlyIncreasingValues), std::less<int>()));
43+
BOOST_CHECK ( !ba::is_sorted (b_e(strictlyIncreasingValues), std::greater<int>()));
44+
45+
// Range checks
46+
BOOST_CHECK ( ba::is_sorted (a_range(strictlyIncreasingValues)));
47+
BOOST_CHECK ( !ba::is_sorted (a_range(randomValues)));
48+
BOOST_CHECK ( ba::is_sorted (a_range(strictlyIncreasingValues), std::less<int>()));
49+
BOOST_CHECK ( !ba::is_sorted (a_range(strictlyIncreasingValues), std::greater<int>()));
50+
51+
BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues)) == a_end(strictlyIncreasingValues));
52+
BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues), std::less<int>()) == a_end(strictlyIncreasingValues));
53+
BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues)) == boost::end(strictlyIncreasingValues));
54+
BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues), std::less<int>()) == boost::end(strictlyIncreasingValues));
55+
56+
// Check for const and non-const arrays
57+
BOOST_CHECK ( ba::is_sorted_until ( b_e(constantValues), std::less<int>()) != a_end(constantValues));
58+
BOOST_CHECK ( ba::is_sorted_until ( a_range(constantValues), std::less<int>()) != boost::end(constantValues));
59+
BOOST_CHECK ( ba::is_sorted_until ( b_e(nonConstantArray), std::less<int>()) != a_end(nonConstantArray));
60+
BOOST_CHECK ( ba::is_sorted_until ( a_range(nonConstantArray), std::less<int>()) != boost::end(nonConstantArray));
61+
62+
BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues), std::less<int>()) == &randomValues[2] );
63+
BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues)) == &randomValues[2] );
64+
BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues), std::less<int>()) == &randomValues[2] );
65+
BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues)) == &randomValues[2] );
66+
67+
BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd), std::less<int>()) == &inOrderUntilTheEnd[8] );
68+
BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd)) == &inOrderUntilTheEnd[8] );
69+
70+
// For zero and one element collections, the comparison predicate should never be called
71+
BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues), std::equal_to<int>()) == a_begin(randomValues));
72+
BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues)) == a_begin(randomValues));
73+
BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1, std::equal_to<int>()) == a_begin(randomValues) + 1);
74+
BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1 ) == a_begin(randomValues) + 1);
75+
}
76+
77+
78+
static void
79+
test_increasing_decreasing(void)
3280
{
3381
const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 };
3482
const int strictlyDecreasingValues[] = { 9, 8, 7, 6, 5 };
3583
const int increasingValues[] = { 1, 2, 2, 2, 5 };
3684
const int decreasingValues[] = { 9, 7, 7, 7, 5 };
3785
const int randomValues[] = { 3, 6, 1, 2, 7 };
3886
const int constantValues[] = { 7, 7, 7, 7, 7 };
39-
int nonConstantArray[] = { 7, 7, 7, 7, 7 };
40-
const int inOrderUntilTheEnd [] = { 0, 1, 2, 3, 4, 5, 6, 7, 6 };
4187

4288
// Test a strictly increasing sequence
4389
BOOST_CHECK ( ba::is_strictly_increasing (b_e(strictlyIncreasingValues)));
@@ -98,30 +144,11 @@ test_ordered(void)
98144
BOOST_CHECK ( !ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
99145
BOOST_CHECK ( !ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2));
100146

101-
// Test underlying routines
102-
BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues), std::less<int>()) == a_end(strictlyIncreasingValues));
103-
BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues), std::less<int>()) == boost::end(strictlyIncreasingValues));
104-
105-
BOOST_CHECK ( ba::is_sorted_until ( b_e(nonConstantArray), std::less<int>()) != a_end(nonConstantArray));
106-
BOOST_CHECK ( ba::is_sorted_until ( a_range(nonConstantArray), std::less<int>()) != boost::end(nonConstantArray));
107-
108-
BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues), std::less<int>()) == &randomValues[2] );
109-
BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues), std::less<int>()) == &randomValues[2] );
110-
111-
BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues), std::less<int>()) == &randomValues[2] );
112-
BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues), std::less<int>()) == &randomValues[2] );
113-
114-
BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd), std::less<int>()) == &inOrderUntilTheEnd[8] );
115-
116-
// For zero and one element collections, the comparison predicate should never be called
117-
BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues), std::equal_to<int>()) == a_begin(randomValues));
118-
BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1, std::equal_to<int>()) == a_begin(randomValues) + 1);
119-
120147
}
121148

122149
int test_main( int, char * [] )
123150
{
124151
test_ordered ();
125-
152+
test_increasing_decreasing ();
126153
return 0;
127154
}

0 commit comments

Comments
 (0)