• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2    Copyright (c) Marshall Clow 2011-2012.
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     For more information, see http://www.boost.org
8 */
9 
10 #include <iostream>
11 
12 #include <boost/config.hpp>
13 #include <boost/algorithm/cxx11/partition_point.hpp>
14 
15 #define BOOST_TEST_MAIN
16 #include <boost/test/unit_test.hpp>
17 
18 #include <string>
19 #include <vector>
20 #include <list>
21 
22 namespace ba = boost::algorithm;
23 // namespace ba = boost;
24 
25 template <typename Container>
offset_to_iter(Container & v,int offset)26 typename Container::iterator offset_to_iter ( Container &v, int offset ) {
27     typename Container::iterator retval;
28 
29     if ( offset >= 0 ) {
30         retval = v.begin ();
31         std::advance ( retval, offset );
32         }
33     else {
34         retval = v.end ();
35         std::advance ( retval, offset + 1 );
36         }
37     return retval;
38     }
39 
40 template <typename Container, typename Predicate>
test_sequence(Container & v,Predicate comp,int expected)41 void test_sequence ( Container &v, Predicate comp, int expected ) {
42     typename Container::iterator res, exp;
43 
44     res = ba::partition_point ( v.begin (), v.end (), comp );
45     exp = offset_to_iter ( v, expected );
46     BOOST_CHECK ( exp == res );
47 
48 //  Duplicate the last element; this checks for any even/odd problems
49     v.push_back ( * v.rbegin ());
50     res = ba::partition_point ( v.begin (), v.end (), comp );
51     exp = offset_to_iter ( v, expected );
52     BOOST_CHECK ( exp == res );
53 
54 //  Range based test
55     res = ba::partition_point ( v, comp );
56     exp = offset_to_iter ( v, expected );
57     BOOST_CHECK ( exp == res );
58     }
59 
60 template <typename T>
61 struct less_than {
62 public:
less_thanless_than63     less_than ( T foo ) : val ( foo ) {}
less_thanless_than64     less_than ( const less_than &rhs ) : val ( rhs.val ) {}
65 
operator ()less_than66     bool operator () ( const T &v ) const { return v < val; }
67 private:
68     less_than ();
69     less_than operator = ( const less_than &rhs );
70     T val;
71     };
72 
73 
test_sequence1()74 void test_sequence1 () {
75     std::vector<int> v;
76 
77     v.clear ();
78     for ( int i = 5; i < 15; ++i )
79         v.push_back ( i );
80     test_sequence ( v, less_than<int>(3),  0 ); // no elements
81     test_sequence ( v, less_than<int>(6),  1 );    // only the first element
82     test_sequence ( v, less_than<int>(10), 5 );
83     test_sequence ( v, less_than<int>(99), -1 );   // all elements satisfy
84 
85 //  With bidirectional iterators.
86     std::list<int> l;
87     for ( int i = 5; i < 15; ++i )
88         l.push_back ( i );
89     test_sequence ( l, less_than<int>(3),  0 ); // no elements
90     test_sequence ( l, less_than<int>(6),  1 );    // only the first element
91     test_sequence ( l, less_than<int>(10), 5 );
92     test_sequence ( l, less_than<int>(99), -1 );   // all elements satisfy
93 
94     }
95 
96 
BOOST_AUTO_TEST_CASE(test_main)97 BOOST_AUTO_TEST_CASE( test_main )
98 {
99   test_sequence1 ();
100 }
101