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