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_copy.hpp> |
14 | |
15 | #define BOOST_TEST_MAIN |
16 | #include <boost/test/unit_test.hpp> |
17 | |
18 | #include <boost/algorithm/cxx11/all_of.hpp> |
19 | #include <boost/algorithm/cxx11/none_of.hpp> |
20 | #include <string> |
21 | #include <vector> |
22 | #include <list> |
23 | |
24 | namespace ba = boost::algorithm; |
25 | // namespace ba = boost; |
26 | |
27 | template <typename Container, typename Predicate> |
28 | void test_sequence ( const Container &c, Predicate comp ) { |
29 | std::vector<typename Container::value_type> v1, v2; |
30 | |
31 | v1.clear (); v2.clear (); |
32 | ba::partition_copy ( c.begin (), c.end (), |
33 | std::back_inserter (v1), std::back_inserter (v2), comp ); |
34 | // std::cout << "Sizes(1): " << c.size () << " -> { " << v1.size () << ", " << v2.size () << " }" << std::endl; |
35 | BOOST_CHECK ( v1.size () + v2.size () == c.size ()); |
36 | BOOST_CHECK ( ba::all_of ( v1.begin (), v1.end (), comp )); |
37 | BOOST_CHECK ( ba::none_of ( v2.begin (), v2.end (), comp )); |
38 | |
39 | v1.clear (); v2.clear (); |
40 | ba::partition_copy ( c, std::back_inserter (v1), std::back_inserter ( v2 ), comp ); |
41 | // std::cout << "Sizes(2): " << c.size () << " -> { " << v1.size () << ", " << v2.size () << " }" << std::endl; |
42 | BOOST_CHECK ( v1.size () + v2.size () == c.size ()); |
43 | BOOST_CHECK ( ba::all_of ( v1, comp )); |
44 | BOOST_CHECK ( ba::none_of ( v2, comp )); |
45 | } |
46 | |
47 | template <typename T> |
48 | struct less_than { |
49 | public: |
50 | less_than ( T foo ) : val ( foo ) {} |
51 | less_than ( const less_than &rhs ) : val ( rhs.val ) {} |
52 | |
53 | bool operator () ( const T &v ) const { return v < val; } |
54 | private: |
55 | less_than (); |
56 | less_than operator = ( const less_than &rhs ); |
57 | T val; |
58 | }; |
59 | |
60 | bool is_even ( int v ) { return v % 2 == 0; } |
61 | |
62 | void test_sequence1 () { |
63 | std::vector<int> v; |
64 | |
65 | v.clear (); |
66 | for ( int i = 5; i < 15; ++i ) |
67 | v.push_back ( x: i ); |
68 | test_sequence ( c: v, comp: less_than<int>(3)); // no elements |
69 | test_sequence ( c: v, comp: less_than<int>(6)); // only the first element |
70 | test_sequence ( c: v, comp: less_than<int>(10)); |
71 | test_sequence ( c: v, comp: less_than<int>(99)); // all elements satisfy |
72 | |
73 | // With bidirectional iterators. |
74 | std::list<int> l; |
75 | for ( int i = 5; i < 16; ++i ) |
76 | l.push_back ( x: i ); |
77 | test_sequence ( c: l, comp: less_than<int>(3)); // no elements |
78 | test_sequence ( c: l, comp: less_than<int>(6)); // only the first element |
79 | test_sequence ( c: l, comp: less_than<int>(10)); |
80 | test_sequence ( c: l, comp: less_than<int>(99)); // all elements satisfy |
81 | |
82 | } |
83 | |
84 | |
85 | BOOST_AUTO_TEST_CASE( test_main ) |
86 | { |
87 | test_sequence1 (); |
88 | } |
89 | |