95f6058035b562cefe848e1edbac31037294a82f
[platform/upstream/boost.git] / libs / algorithm / test / partition_point_test1.cpp
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>
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>
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     std::cout << "Expected(1): " << std::distance ( v.begin (), exp ) 
47               <<       ", got: " << std::distance ( v.begin (), res ) << std::endl;
48     BOOST_CHECK ( exp == res );
49
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 );
57     }
58
59 template <typename T>
60 struct less_than {
61 public:
62     less_than ( T foo ) : val ( foo ) {}
63     less_than ( const less_than &rhs ) : val ( rhs.val ) {}
64
65     bool operator () ( const T &v ) const { return v < val; }
66 private:
67     less_than ();
68     less_than operator = ( const less_than &rhs );
69     T val;
70     };
71
72
73 void test_sequence1 () {
74     std::vector<int> v;
75     
76     v.clear ();
77     for ( int i = 5; i < 15; ++i )
78         v.push_back ( 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 
83
84 //  With bidirectional iterators.
85     std::list<int> l;
86     for ( int i = 5; i < 15; ++i )
87         l.push_back ( 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 
92
93     }
94
95
96 BOOST_AUTO_TEST_CASE( test_main )
97 {
98   test_sequence1 ();
99 }