• 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/find_if_not.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 
is_true(int v)25 BOOST_CXX14_CONSTEXPR bool is_true  ( int v ) { return true; }
is_false(int v)26 BOOST_CXX14_CONSTEXPR bool is_false ( int v ) { return false; }
is_not_three(int v)27 BOOST_CXX14_CONSTEXPR bool is_not_three ( int v ) { return v != 3; }
28 
check_constexpr()29 BOOST_CXX14_CONSTEXPR bool check_constexpr() {
30     int in_data[] = {1, 2, 3, 4, 5};
31     bool res = true;
32 
33     const int* from = in_data;
34     const int* to = in_data + 5;
35 
36     const int* start = ba::find_if_not (from, to, is_false); // stops on first
37     res = (res && start == from);
38 
39     const int* end = ba::find_if_not(from, to, is_true); // stops on the end
40     res = (res && end == to);
41 
42     const int* three = ba::find_if_not(from, to, is_not_three); // stops on third element
43     res = (res && three == in_data + 2);
44 
45     return res;
46 }
47 
48 template <typename Container>
offset_to_iter(Container & v,int offset)49 typename Container::iterator offset_to_iter ( Container &v, int offset ) {
50     typename Container::iterator retval;
51 
52     if ( offset >= 0 ) {
53         retval = v.begin ();
54         std::advance ( retval, offset );
55         }
56     else {
57         retval = v.end ();
58         std::advance ( retval, offset + 1 );
59         }
60     return retval;
61     }
62 
63 template <typename Container, typename Predicate>
test_sequence(Container & v,Predicate comp,int expected)64 void test_sequence ( Container &v, Predicate comp, int expected ) {
65     typename Container::iterator res, exp;
66 
67     res = ba::find_if_not ( v.begin (), v.end (), comp );
68     exp = offset_to_iter ( v, expected );
69     std::cout << "Expected(1): " << std::distance ( v.begin (), exp )
70               <<       ", got: " << std::distance ( v.begin (), res ) << std::endl;
71     BOOST_CHECK ( exp == res );
72     }
73 
74 template <typename T>
75 struct less_than {
76 public:
less_thanless_than77     less_than ( T foo ) : val ( foo ) {}
less_thanless_than78     less_than ( const less_than &rhs ) : val ( rhs.val ) {}
79 
operator ()less_than80     bool operator () ( const T &v ) const { return v < val; }
81 private:
82     less_than ();
83     less_than operator = ( const less_than &rhs );
84     T val;
85     };
86 
87 
test_sequence1()88 void test_sequence1 () {
89     std::vector<int> v;
90 
91     v.clear ();
92     for ( int i = 5; i < 15; ++i )
93         v.push_back ( i );
94     test_sequence ( v, less_than<int>(3),  0 ); // no elements
95     test_sequence ( v, less_than<int>(6),  1 );    // only the first element
96     test_sequence ( v, less_than<int>(10), 5 );
97     test_sequence ( v, less_than<int>(99), -1 );   // all elements satisfy
98 
99 //  With bidirectional iterators.
100     std::list<int> l;
101     for ( int i = 5; i < 15; ++i )
102         l.push_back ( i );
103     test_sequence ( l, less_than<int>(3),  0 ); // no elements
104     test_sequence ( l, less_than<int>(6),  1 );    // only the first element
105     test_sequence ( l, less_than<int>(10), 5 );
106     test_sequence ( l, less_than<int>(99), -1 );   // all elements satisfy
107 
108     }
109 
110 
BOOST_AUTO_TEST_CASE(test_main)111 BOOST_AUTO_TEST_CASE( test_main )
112 {
113   test_sequence1 ();
114 }
115