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
22namespace ba = boost::algorithm;
23// namespace ba = boost;
24
25BOOST_CXX14_CONSTEXPR bool is_true ( int v ) { return true; }
26BOOST_CXX14_CONSTEXPR bool is_false ( int v ) { return false; }
27BOOST_CXX14_CONSTEXPR bool is_not_three ( int v ) { return v != 3; }
28
29BOOST_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 (first: from, last: to, p: is_false); // stops on first
37 res = (res && start == from);
38
39 const int* end = ba::find_if_not(first: from, last: to, p: is_true); // stops on the end
40 res = (res && end == to);
41
42 const int* three = ba::find_if_not(first: from, last: to, p: is_not_three); // stops on third element
43 res = (res && three == in_data + 2);
44
45 return res;
46}
47
48template <typename Container>
49typename 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
63template <typename Container, typename Predicate>
64void 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
74template <typename T>
75struct less_than {
76public:
77 less_than ( T foo ) : val ( foo ) {}
78 less_than ( const less_than &rhs ) : val ( rhs.val ) {}
79
80 bool operator () ( const T &v ) const { return v < val; }
81private:
82 less_than ();
83 less_than operator = ( const less_than &rhs );
84 T val;
85 };
86
87
88void test_sequence1 () {
89 std::vector<int> v;
90
91 v.clear ();
92 for ( int i = 5; i < 15; ++i )
93 v.push_back ( x: i );
94 test_sequence ( v, comp: less_than<int>(3), expected: 0 ); // no elements
95 test_sequence ( v, comp: less_than<int>(6), expected: 1 ); // only the first element
96 test_sequence ( v, comp: less_than<int>(10), expected: 5 );
97 test_sequence ( v, comp: less_than<int>(99), expected: -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 ( x: i );
103 test_sequence ( v&: l, comp: less_than<int>(3), expected: 0 ); // no elements
104 test_sequence ( v&: l, comp: less_than<int>(6), expected: 1 ); // only the first element
105 test_sequence ( v&: l, comp: less_than<int>(10), expected: 5 );
106 test_sequence ( v&: l, comp: less_than<int>(99), expected: -1 ); // all elements satisfy
107
108 }
109
110
111BOOST_AUTO_TEST_CASE( test_main )
112{
113 test_sequence1 ();
114}
115

source code of boost/libs/algorithm/test/find_if_not_test1.cpp