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 | |
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::find_if_not ( 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 | |
51 | template <typename T> |
52 | struct less_than { |
53 | public: |
54 | less_than ( T foo ) : val ( foo ) {} |
55 | less_than ( const less_than &rhs ) : val ( rhs.val ) {} |
56 | |
57 | bool operator () ( const T &v ) const { return v < val; } |
58 | private: |
59 | less_than (); |
60 | less_than operator = ( const less_than &rhs ); |
61 | T val; |
62 | }; |
63 | |
64 | |
65 | void test_sequence1 () { |
66 | std::vector<int> v; |
67 | |
68 | v.clear (); |
69 | for ( int i = 5; i < 15; ++i ) |
70 | v.push_back ( x: i ); |
71 | test_sequence ( v, comp: less_than<int>(3), expected: 0 ); // no elements |
72 | test_sequence ( v, comp: less_than<int>(6), expected: 1 ); // only the first element |
73 | test_sequence ( v, comp: less_than<int>(10), expected: 5 ); |
74 | test_sequence ( v, comp: less_than<int>(99), expected: -1 ); // all elements satisfy |
75 | |
76 | // With bidirectional iterators. |
77 | std::list<int> l; |
78 | for ( int i = 5; i < 15; ++i ) |
79 | l.push_back ( x: i ); |
80 | test_sequence ( v&: l, comp: less_than<int>(3), expected: 0 ); // no elements |
81 | test_sequence ( v&: l, comp: less_than<int>(6), expected: 1 ); // only the first element |
82 | test_sequence ( v&: l, comp: less_than<int>(10), expected: 5 ); |
83 | test_sequence ( v&: l, comp: less_than<int>(99), expected: -1 ); // all elements satisfy |
84 | |
85 | } |
86 | |
87 | |
88 | BOOST_AUTO_TEST_CASE( test_main ) |
89 | { |
90 | test_sequence1 (); |
91 | } |
92 | |