1 /*==============================================================================
2 Copyright (c) 2010-2011 Bryce Lelbach
3
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7
8 #include <boost/detail/is_sorted.hpp>
9 #include <iostream>
10 #include <boost/config.hpp>
11 #include <boost/array.hpp>
12 #include <boost/core/lightweight_test.hpp>
13
14 template<class T>
15 struct tracking_less {
16 typedef bool result_type;
17 typedef T first_argument_type;
18 typedef T second_argument_type;
19
20 #if defined(__PATHSCALE__)
tracking_lesstracking_less21 tracking_less (void) { }
~tracking_lesstracking_less22 ~tracking_less (void) { }
23 #endif
24
operator ()tracking_less25 bool operator() (T const& x, T const& y) const {
26 std::cout << x << " < " << y << " == " << (x < y) << "\n";
27 return x < y;
28 }
29 };
30
31 template<class T>
32 struct tracking_less_equal {
33 typedef bool result_type;
34 typedef T first_argument_type;
35 typedef T second_argument_type;
36
37 #if defined(__PATHSCALE__)
tracking_less_equaltracking_less_equal38 tracking_less_equal (void) { }
~tracking_less_equaltracking_less_equal39 ~tracking_less_equal (void) { }
40 #endif
41
operator ()tracking_less_equal42 bool operator() (T const& x, T const& y) const {
43 std::cout << x << " <= " << y << " == " << (x <= y) << "\n";
44 return x <= y;
45 }
46 };
47
48 template<class T>
49 struct tracking_greater {
50 typedef bool result_type;
51 typedef T first_argument_type;
52 typedef T second_argument_type;
53
54 #if defined(__PATHSCALE__)
tracking_greatertracking_greater55 tracking_greater (void) { }
~tracking_greatertracking_greater56 ~tracking_greater (void) { }
57 #endif
58
operator ()tracking_greater59 bool operator() (T const& x, T const& y) const {
60 std::cout << x << " > " << y << " == " << (x > y) << "\n";
61 return x > y;
62 }
63 };
64
65 template<class T>
66 struct tracking_greater_equal {
67 typedef bool result_type;
68 typedef T first_argument_type;
69 typedef T second_argument_type;
70
71 #if defined(__PATHSCALE__)
tracking_greater_equaltracking_greater_equal72 tracking_greater_equal (void) { }
~tracking_greater_equaltracking_greater_equal73 ~tracking_greater_equal (void) { }
74 #endif
75
operator ()tracking_greater_equal76 bool operator() (T const& x, T const& y) const {
77 std::cout << x << " >= " << y << " == " << (x >= y) << "\n";
78 return x >= y;
79 }
80 };
81
82
main(void)83 int main (void) {
84 #define IS_SORTED ::boost::detail::is_sorted
85 #define IS_SORTED_UNTIL ::boost::detail::is_sorted_until
86 using boost::array;
87 using boost::report_errors;
88
89 std::cout << std::boolalpha;
90
91 array<int, 10> a = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
92 array<int, 10> b = { { 0, 1, 1, 2, 5, 8, 13, 34, 55, 89 } };
93 array<int, 10> c = { { 0, 1, -1, 2, -3, 5, -8, 13, -21, 34 } };
94
95 tracking_less<int> lt;
96 tracking_less_equal<int> lte;
97 tracking_greater<int> gt;
98 tracking_greater_equal<int> gte;
99
100 BOOST_TEST_EQ(IS_SORTED_UNTIL(a.begin(), a.end()), a.end());
101 BOOST_TEST_EQ(IS_SORTED_UNTIL(a.begin(), a.end(), lt), a.end());
102 BOOST_TEST_EQ(IS_SORTED_UNTIL(a.begin(), a.end(), lte), a.end());
103 BOOST_TEST_EQ(IS_SORTED_UNTIL(a.rbegin(), a.rend(), gt).base(), a.rend().base());
104 BOOST_TEST_EQ(IS_SORTED_UNTIL(a.rbegin(), a.rend(), gte).base(), a.rend().base());
105
106 BOOST_TEST_EQ(IS_SORTED(a.begin(), a.end()), true);
107 BOOST_TEST_EQ(IS_SORTED(a.begin(), a.end(), lt), true);
108 BOOST_TEST_EQ(IS_SORTED(a.begin(), a.end(), lte), true);
109 BOOST_TEST_EQ(IS_SORTED(a.rbegin(), a.rend(), gt), true);
110 BOOST_TEST_EQ(IS_SORTED(a.rbegin(), a.rend(), gte), true);
111
112 BOOST_TEST_EQ(IS_SORTED_UNTIL(b.begin(), b.end()), b.end());
113 BOOST_TEST_EQ(IS_SORTED_UNTIL(b.begin(), b.end(), lt), b.end());
114 BOOST_TEST_EQ(IS_SORTED_UNTIL(b.begin(), b.end(), lte), &b[2]);
115 BOOST_TEST_EQ(IS_SORTED_UNTIL(b.rbegin(), b.rend(), gt).base(), b.rend().base());
116 BOOST_TEST_EQ(IS_SORTED_UNTIL(b.rbegin(), b.rend(), gte).base(), &b[2]);
117
118 BOOST_TEST_EQ(IS_SORTED(b.begin(), b.end()), true);
119 BOOST_TEST_EQ(IS_SORTED(b.begin(), b.end(), lt), true);
120 BOOST_TEST_EQ(IS_SORTED(b.begin(), b.end(), lte), false);
121 BOOST_TEST_EQ(IS_SORTED(b.rbegin(), b.rend(), gt), true);
122 BOOST_TEST_EQ(IS_SORTED(b.rbegin(), b.rend(), gte), false);
123
124 BOOST_TEST_EQ(IS_SORTED_UNTIL(c.begin(), c.end()), &c[2]);
125 BOOST_TEST_EQ(IS_SORTED_UNTIL(c.begin(), c.end(), lt), &c[2]);
126 BOOST_TEST_EQ(IS_SORTED_UNTIL(c.begin(), c.end(), lte), &c[2]);
127 BOOST_TEST_EQ(IS_SORTED_UNTIL(c.rbegin(), c.rend(), gt).base(), &c[8]);
128 BOOST_TEST_EQ(IS_SORTED_UNTIL(c.rbegin(), c.rend(), gte).base(), &c[8]);
129
130 BOOST_TEST_EQ(IS_SORTED(c.begin(), c.end()), false);
131 BOOST_TEST_EQ(IS_SORTED(c.begin(), c.end(), lt), false);
132 BOOST_TEST_EQ(IS_SORTED(c.begin(), c.end(), lte), false);
133 BOOST_TEST_EQ(IS_SORTED(c.rbegin(), c.rend(), gt), false);
134 BOOST_TEST_EQ(IS_SORTED(c.rbegin(), c.rend(), gte), false);
135
136 return report_errors();
137 }
138