| 1 | // Copyright Neil Groves 2009. Use, modification and |
| 2 | // distribution is subject to the Boost Software License, Version |
| 3 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 4 | // http://www.boost.org/LICENSE_1_0.txt) |
| 5 | // |
| 6 | // |
| 7 | // For more information, see http://www.boost.org/libs/range/ |
| 8 | // |
| 9 | #include <boost/range/algorithm/lexicographical_compare.hpp> |
| 10 | |
| 11 | #include <boost/test/test_tools.hpp> |
| 12 | #include <boost/test/unit_test.hpp> |
| 13 | |
| 14 | #include <boost/assign.hpp> |
| 15 | #include <boost/range/value_type.hpp> |
| 16 | #include <algorithm> |
| 17 | #include <functional> |
| 18 | #include <list> |
| 19 | #include <numeric> |
| 20 | #include <deque> |
| 21 | #include <vector> |
| 22 | |
| 23 | namespace boost |
| 24 | { |
| 25 | namespace |
| 26 | { |
| 27 | template<class ForwardRange1, class ForwardRange2> |
| 28 | void test_lexicographical_compare_impl_nopred(ForwardRange1& rng1, |
| 29 | ForwardRange2& rng2) |
| 30 | { |
| 31 | const bool reference = std::lexicographical_compare( |
| 32 | boost::begin(rng1), boost::end(rng1), |
| 33 | boost::begin(rng2), boost::end(rng2)); |
| 34 | |
| 35 | const bool test = boost::lexicographical_compare(rng1, rng2); |
| 36 | |
| 37 | BOOST_CHECK( reference == test ); |
| 38 | BOOST_CHECK( test == boost::lexicographical_compare(boost::make_iterator_range(rng1), rng2) ); |
| 39 | BOOST_CHECK( test == boost::lexicographical_compare(rng1, boost::make_iterator_range(rng2)) ); |
| 40 | BOOST_CHECK( test == boost::lexicographical_compare(boost::make_iterator_range(rng1), boost::make_iterator_range(rng2)) ); |
| 41 | } |
| 42 | |
| 43 | template<class ForwardRange1, class ForwardRange2, |
| 44 | class BinaryPredicate> |
| 45 | void test_lexicographical_compare_impl_pred(ForwardRange1& rng1, |
| 46 | ForwardRange2& rng2, |
| 47 | BinaryPredicate pred) |
| 48 | { |
| 49 | const bool reference = std::lexicographical_compare( |
| 50 | boost::begin(rng1), boost::end(rng1), |
| 51 | boost::begin(rng2), boost::end(rng2), |
| 52 | pred); |
| 53 | |
| 54 | const bool test = boost::lexicographical_compare(rng1, rng2, pred); |
| 55 | |
| 56 | BOOST_CHECK( reference == test ); |
| 57 | BOOST_CHECK( test == boost::lexicographical_compare(boost::make_iterator_range(rng1), rng2, pred) ); |
| 58 | BOOST_CHECK( test == boost::lexicographical_compare(rng1, boost::make_iterator_range(rng2), pred) ); |
| 59 | BOOST_CHECK( test == boost::lexicographical_compare(boost::make_iterator_range(rng1), boost::make_iterator_range(rng2), pred) ); |
| 60 | } |
| 61 | |
| 62 | template<class Container1, class Container2> |
| 63 | void test_lexicographical_compare_impl(Container1& cont1, |
| 64 | Container2& cont2) |
| 65 | { |
| 66 | typedef BOOST_DEDUCED_TYPENAME boost::range_value<Container1>::type value_t; |
| 67 | |
| 68 | test_lexicographical_compare_impl_nopred(cont1, cont2); |
| 69 | test_lexicographical_compare_impl_pred(cont1, cont2, std::less<value_t>()); |
| 70 | test_lexicographical_compare_impl_pred(cont1, cont2, std::greater<value_t>()); |
| 71 | } |
| 72 | |
| 73 | template<class Container1, class Container2> |
| 74 | void test_lexicographical_compare_impl() |
| 75 | { |
| 76 | using namespace boost::assign; |
| 77 | |
| 78 | typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container1>::type container1_t; |
| 79 | typedef BOOST_DEDUCED_TYPENAME boost::remove_const<Container2>::type container2_t; |
| 80 | |
| 81 | container1_t cont1; |
| 82 | container2_t cont2; |
| 83 | test_lexicographical_compare_impl<Container1,Container2>(cont1, cont2); |
| 84 | |
| 85 | cont1.clear(); |
| 86 | cont2.clear(); |
| 87 | cont1.push_back(1); |
| 88 | cont2.push_back(1); |
| 89 | test_lexicographical_compare_impl<Container1,Container2>(cont1, cont2); |
| 90 | |
| 91 | cont1.clear(); |
| 92 | cont2.clear(); |
| 93 | cont1 += 2; |
| 94 | cont2 += 1; |
| 95 | test_lexicographical_compare_impl<Container1,Container2>(cont1, cont2); |
| 96 | |
| 97 | cont1.clear(); |
| 98 | cont2.clear(); |
| 99 | cont1 += 1; |
| 100 | cont2 += 2; |
| 101 | test_lexicographical_compare_impl<Container1,Container2>(cont1, cont2); |
| 102 | |
| 103 | cont1.clear(); |
| 104 | cont2.clear(); |
| 105 | cont1 += 1,2,3,4,5,6,7; |
| 106 | cont2 += 1,2,3,4,5,6,7; |
| 107 | test_lexicographical_compare_impl<Container1,Container2>(cont1, cont2); |
| 108 | |
| 109 | cont1.clear(); |
| 110 | cont2.clear(); |
| 111 | cont1 += 1,2,3,4,5,6,7; |
| 112 | cont2 += 1,2,3,4,5,6; |
| 113 | test_lexicographical_compare_impl<Container1,Container2>(cont1, cont2); |
| 114 | |
| 115 | cont1.clear(); |
| 116 | cont2.clear(); |
| 117 | cont1 += 1,2,3,4,5,6; |
| 118 | cont2 += 1,2,3,4,5,6,7; |
| 119 | test_lexicographical_compare_impl<Container1,Container2>(cont1, cont2); |
| 120 | } |
| 121 | |
| 122 | template<class Container1> |
| 123 | void test_lexicographical_compare_rhs() |
| 124 | { |
| 125 | typedef BOOST_DEDUCED_TYPENAME range_value<Container1>::type value_t; |
| 126 | |
| 127 | test_lexicographical_compare_impl<Container1, const std::vector<value_t> >(); |
| 128 | test_lexicographical_compare_impl<Container1, const std::deque<value_t> >(); |
| 129 | test_lexicographical_compare_impl<Container1, const std::list<value_t> >(); |
| 130 | test_lexicographical_compare_impl<Container1, std::vector<value_t> >(); |
| 131 | test_lexicographical_compare_impl<Container1, std::deque<value_t> >(); |
| 132 | test_lexicographical_compare_impl<Container1, std::list<value_t> >(); |
| 133 | } |
| 134 | |
| 135 | void test_lexicographical_compare() |
| 136 | { |
| 137 | test_lexicographical_compare_rhs< const std::vector<int> >(); |
| 138 | test_lexicographical_compare_rhs< const std::deque<int> >(); |
| 139 | test_lexicographical_compare_rhs< const std::list<int> >(); |
| 140 | test_lexicographical_compare_rhs< std::vector<int> >(); |
| 141 | test_lexicographical_compare_rhs< std::deque<int> >(); |
| 142 | test_lexicographical_compare_rhs< std::list<int> >(); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | |
| 148 | boost::unit_test::test_suite* |
| 149 | init_unit_test_suite(int argc, char* argv[]) |
| 150 | { |
| 151 | boost::unit_test::test_suite* test |
| 152 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.lexicographical_compare" ); |
| 153 | |
| 154 | test->add( BOOST_TEST_CASE( &boost::test_lexicographical_compare ) ); |
| 155 | |
| 156 | return test; |
| 157 | } |
| 158 | |