1 | // Copyright (c) 2010 Nuovation System Designs, LLC |
2 | // Grant Erickson <gerickson@nuovations.com> |
3 | // |
4 | // Reworked by Marshall Clow; August 2010 |
5 | // |
6 | // Distributed under the Boost Software License, Version 1.0. (See |
7 | // accompanying file LICENSE_1_0.txt or copy at |
8 | // http://www.boost.org/LICENSE_1_0.txt) |
9 | // |
10 | // See http://www.boost.org/ for latest version. |
11 | |
12 | #include <algorithm> |
13 | #include <iostream> |
14 | |
15 | #include <boost/algorithm/cxx11/is_sorted.hpp> |
16 | |
17 | #define BOOST_TEST_MAIN |
18 | #include <boost/test/unit_test.hpp> |
19 | |
20 | using namespace boost; |
21 | |
22 | /* Preprocessor Defines */ |
23 | |
24 | #define elementsof(v) (sizeof (v) / sizeof (v[0])) |
25 | #define a_begin(v) (&v[0]) |
26 | #define a_end(v) (v + elementsof (v)) |
27 | #define a_range(v) v |
28 | #define b_e(v) a_begin(v),a_end(v) |
29 | |
30 | namespace ba = boost::algorithm; |
31 | |
32 | static void |
33 | test_ordered(void) |
34 | { |
35 | const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 }; |
36 | const int randomValues[] = { 3, 6, 1, 2, 7 }; |
37 | const int constantValues[] = { 1, 2, 2, 2, 5 }; |
38 | int nonConstantArray[] = { 1, 2, 2, 2, 5 }; |
39 | const int inOrderUntilTheEnd [] = { 0, 1, 2, 3, 4, 5, 6, 7, 6 }; |
40 | |
41 | // Begin/end checks |
42 | BOOST_CHECK ( ba::is_sorted (b_e(strictlyIncreasingValues))); |
43 | BOOST_CHECK ( !ba::is_sorted (b_e(randomValues))); |
44 | BOOST_CHECK ( ba::is_sorted (b_e(strictlyIncreasingValues), std::less<int>())); |
45 | BOOST_CHECK ( !ba::is_sorted (b_e(strictlyIncreasingValues), std::greater<int>())); |
46 | |
47 | // Range checks |
48 | BOOST_CHECK ( ba::is_sorted (a_range(strictlyIncreasingValues))); |
49 | BOOST_CHECK ( !ba::is_sorted (a_range(randomValues))); |
50 | BOOST_CHECK ( ba::is_sorted (a_range(strictlyIncreasingValues), std::less<int>())); |
51 | BOOST_CHECK ( !ba::is_sorted (a_range(strictlyIncreasingValues), std::greater<int>())); |
52 | |
53 | BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues)) == a_end(strictlyIncreasingValues)); |
54 | BOOST_CHECK ( ba::is_sorted_until ( b_e(strictlyIncreasingValues), std::less<int>()) == a_end(strictlyIncreasingValues)); |
55 | BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues)) == boost::end(strictlyIncreasingValues)); |
56 | BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues), std::less<int>()) == boost::end(strictlyIncreasingValues)); |
57 | |
58 | // Check for const and non-const arrays |
59 | BOOST_CHECK ( ba::is_sorted_until ( b_e(constantValues), std::less<int>()) == a_end(constantValues)); |
60 | BOOST_CHECK ( ba::is_sorted_until ( a_range(constantValues), std::less<int>()) == boost::end(constantValues)); |
61 | BOOST_CHECK ( ba::is_sorted_until ( b_e(nonConstantArray), std::less<int>()) == a_end(nonConstantArray)); |
62 | BOOST_CHECK ( ba::is_sorted_until ( a_range(nonConstantArray), std::less<int>()) == boost::end(nonConstantArray)); |
63 | |
64 | BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues), std::less<int>()) == &randomValues[2] ); |
65 | BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues)) == &randomValues[2] ); |
66 | BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues), std::less<int>()) == &randomValues[2] ); |
67 | BOOST_CHECK ( ba::is_sorted_until ( a_range(randomValues)) == &randomValues[2] ); |
68 | |
69 | BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd), std::less<int>()) == &inOrderUntilTheEnd[8] ); |
70 | BOOST_CHECK ( ba::is_sorted_until ( a_range(inOrderUntilTheEnd)) == &inOrderUntilTheEnd[8] ); |
71 | |
72 | // For zero and one element collections, the comparison predicate should never be called |
73 | BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues), std::equal_to<int>()) == a_begin(randomValues)); |
74 | BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues)) == a_begin(randomValues)); |
75 | BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1, std::equal_to<int>()) == a_begin(randomValues) + 1); |
76 | BOOST_CHECK ( ba::is_sorted_until ( a_begin(randomValues), a_begin(randomValues) + 1 ) == a_begin(randomValues) + 1); |
77 | } |
78 | |
79 | |
80 | static void |
81 | test_increasing_decreasing(void) |
82 | { |
83 | const int strictlyIncreasingValues[] = { 1, 2, 3, 4, 5 }; |
84 | const int strictlyDecreasingValues[] = { 9, 8, 7, 6, 5 }; |
85 | const int increasingValues[] = { 1, 2, 2, 2, 5 }; |
86 | const int decreasingValues[] = { 9, 7, 7, 7, 5 }; |
87 | const int randomValues[] = { 3, 6, 1, 2, 7 }; |
88 | const int constantValues[] = { 7, 7, 7, 7, 7 }; |
89 | |
90 | // Test a strictly increasing sequence |
91 | BOOST_CHECK ( ba::is_strictly_increasing (b_e(strictlyIncreasingValues))); |
92 | BOOST_CHECK ( ba::is_increasing (b_e(strictlyIncreasingValues))); |
93 | BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(strictlyIncreasingValues))); |
94 | BOOST_CHECK ( !ba::is_decreasing (b_e(strictlyIncreasingValues))); |
95 | |
96 | BOOST_CHECK ( ba::is_strictly_increasing (a_range(strictlyIncreasingValues))); |
97 | BOOST_CHECK ( ba::is_increasing (a_range(strictlyIncreasingValues))); |
98 | BOOST_CHECK ( !ba::is_strictly_decreasing (a_range(strictlyIncreasingValues))); |
99 | BOOST_CHECK ( !ba::is_decreasing (a_range(strictlyIncreasingValues))); |
100 | |
101 | // Test a strictly decreasing sequence |
102 | BOOST_CHECK ( !ba::is_strictly_increasing (b_e(strictlyDecreasingValues))); |
103 | BOOST_CHECK ( !ba::is_increasing (b_e(strictlyDecreasingValues))); |
104 | BOOST_CHECK ( ba::is_strictly_decreasing (b_e(strictlyDecreasingValues))); |
105 | BOOST_CHECK ( ba::is_decreasing (b_e(strictlyDecreasingValues))); |
106 | |
107 | // Test an increasing sequence |
108 | BOOST_CHECK ( !ba::is_strictly_increasing (b_e(increasingValues))); |
109 | BOOST_CHECK ( ba::is_increasing (b_e(increasingValues))); |
110 | BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(increasingValues))); |
111 | BOOST_CHECK ( !ba::is_decreasing (b_e(increasingValues))); |
112 | |
113 | // Test a decreasing sequence |
114 | BOOST_CHECK ( !ba::is_strictly_increasing (b_e(decreasingValues))); |
115 | BOOST_CHECK ( !ba::is_increasing (b_e(decreasingValues))); |
116 | BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(decreasingValues))); |
117 | BOOST_CHECK ( ba::is_decreasing (b_e(decreasingValues))); |
118 | |
119 | // Test a random sequence |
120 | BOOST_CHECK ( !ba::is_strictly_increasing (b_e(randomValues))); |
121 | BOOST_CHECK ( !ba::is_increasing (b_e(randomValues))); |
122 | BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(randomValues))); |
123 | BOOST_CHECK ( !ba::is_decreasing (b_e(randomValues))); |
124 | |
125 | // Test a constant sequence |
126 | BOOST_CHECK ( !ba::is_strictly_increasing (b_e(constantValues))); |
127 | BOOST_CHECK ( ba::is_increasing (b_e(constantValues))); |
128 | BOOST_CHECK ( !ba::is_strictly_decreasing (b_e(constantValues))); |
129 | BOOST_CHECK ( ba::is_decreasing (b_e(constantValues))); |
130 | |
131 | // Test an empty sequence |
132 | BOOST_CHECK ( ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues)); |
133 | BOOST_CHECK ( ba::is_increasing (strictlyIncreasingValues, strictlyIncreasingValues)); |
134 | BOOST_CHECK ( ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues)); |
135 | BOOST_CHECK ( ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues)); |
136 | |
137 | // Test a one-element sequence |
138 | BOOST_CHECK ( ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues+1)); |
139 | BOOST_CHECK ( ba::is_increasing (strictlyIncreasingValues, strictlyIncreasingValues+1)); |
140 | BOOST_CHECK ( ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+1)); |
141 | BOOST_CHECK ( ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+1)); |
142 | |
143 | // Test a two-element sequence |
144 | BOOST_CHECK ( ba::is_strictly_increasing (strictlyIncreasingValues, strictlyIncreasingValues+2)); |
145 | BOOST_CHECK ( ba::is_increasing (strictlyIncreasingValues, strictlyIncreasingValues+2)); |
146 | BOOST_CHECK ( !ba::is_strictly_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2)); |
147 | BOOST_CHECK ( !ba::is_decreasing (strictlyIncreasingValues, strictlyIncreasingValues+2)); |
148 | |
149 | } |
150 | |
151 | BOOST_AUTO_TEST_CASE( test_main ) |
152 | { |
153 | test_ordered (); |
154 | test_increasing_decreasing (); |
155 | } |
156 | |