2 Copyright (c) Marshall Clow 2011-2012.
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)
7 For more information, see http://www.boost.org
12 #include <boost/config.hpp>
13 #include <boost/algorithm/cxx11/partition_point.hpp>
15 #define BOOST_TEST_MAIN
16 #include <boost/test/unit_test.hpp>
22 namespace ba = boost::algorithm;
23 // namespace ba = boost;
25 template <typename Container>
26 typename Container::iterator offset_to_iter ( Container &v, int offset ) {
27 typename Container::iterator retval;
31 std::advance ( retval, offset );
35 std::advance ( retval, offset + 1 );
40 template <typename Container, typename Predicate>
41 void test_sequence ( Container &v, Predicate comp, int expected ) {
42 typename Container::iterator res, exp;
44 res = ba::partition_point ( v.begin (), v.end (), comp );
45 exp = offset_to_iter ( v, expected );
46 std::cout << "Expected(1): " << std::distance ( v.begin (), exp )
47 << ", got: " << std::distance ( v.begin (), res ) << std::endl;
48 BOOST_CHECK ( exp == res );
50 // Duplicate the last element; this checks for any even/odd problems
51 v.push_back ( * v.rbegin ());
52 res = ba::partition_point ( v.begin (), v.end (), comp );
53 exp = offset_to_iter ( v, expected );
54 std::cout << "Expected(2): " << std::distance ( v.begin (), exp )
55 << ", got: " << std::distance ( v.begin (), res ) << std::endl;
56 BOOST_CHECK ( exp == res );
62 less_than ( T foo ) : val ( foo ) {}
63 less_than ( const less_than &rhs ) : val ( rhs.val ) {}
65 bool operator () ( const T &v ) const { return v < val; }
68 less_than operator = ( const less_than &rhs );
73 void test_sequence1 () {
77 for ( int i = 5; i < 15; ++i )
79 test_sequence ( v, less_than<int>(3), 0 ); // no elements
80 test_sequence ( v, less_than<int>(6), 1 ); // only the first element
81 test_sequence ( v, less_than<int>(10), 5 );
82 test_sequence ( v, less_than<int>(99), -1 ); // all elements satisfy
84 // With bidirectional iterators.
86 for ( int i = 5; i < 15; ++i )
88 test_sequence ( l, less_than<int>(3), 0 ); // no elements
89 test_sequence ( l, less_than<int>(6), 1 ); // only the first element
90 test_sequence ( l, less_than<int>(10), 5 );
91 test_sequence ( l, less_than<int>(99), -1 ); // all elements satisfy
96 BOOST_AUTO_TEST_CASE( test_main )