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 <boost/config.hpp>
11 #include <boost/algorithm/cxx11/copy_n.hpp>
12 #include <boost/algorithm/cxx14/equal.hpp>
13 #include <boost/algorithm/cxx11/all_of.hpp>
14
15 #include "iterator_test.hpp"
16
17 #define BOOST_TEST_MAIN
18 #include <boost/test/unit_test.hpp>
19
20 #include <string>
21 #include <iostream>
22 #include <vector>
23 #include <list>
24
25 namespace ba = boost::algorithm;
26 // namespace ba = boost;
27
is_zero(int v)28 BOOST_CXX14_CONSTEXPR bool is_zero( int v ) { return v == 0; }
29
30 template <typename Container>
test_sequence(Container const & c)31 void test_sequence ( Container const &c ) {
32
33 typedef typename Container::value_type value_type;
34 std::vector<value_type> v;
35
36 // Copy zero elements
37 v.clear ();
38 ba::copy_n ( c.begin (), 0, back_inserter ( v ));
39 BOOST_CHECK ( v.size () == 0 );
40 ba::copy_n ( c.begin (), 0U, back_inserter ( v ));
41 BOOST_CHECK ( v.size () == 0 );
42
43 if ( c.size () > 0 ) {
44 // Just one element
45 v.clear ();
46 ba::copy_n ( c.begin (), 1, back_inserter ( v ));
47 BOOST_CHECK ( v.size () == 1 );
48 BOOST_CHECK ( v[0] == *c.begin ());
49
50 v.clear ();
51 ba::copy_n ( c.begin (), 1U, back_inserter ( v ));
52 BOOST_CHECK ( v.size () == 1 );
53 BOOST_CHECK ( v[0] == *c.begin ());
54
55 // Half the elements
56 v.clear ();
57 ba::copy_n ( c.begin (), c.size () / 2, back_inserter ( v ));
58 BOOST_CHECK ( v.size () == c.size () / 2);
59 BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
60
61 // Half the elements + 1
62 v.clear ();
63 ba::copy_n ( c.begin (), c.size () / 2 + 1, back_inserter ( v ));
64 BOOST_CHECK ( v.size () == c.size () / 2 + 1 );
65 BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
66
67 // All the elements
68 v.clear ();
69 ba::copy_n ( c.begin (), c.size (), back_inserter ( v ));
70 BOOST_CHECK ( v.size () == c.size ());
71 BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
72 }
73 }
74
75
test_constexpr()76 BOOST_CXX14_CONSTEXPR inline bool test_constexpr() {
77 const size_t sz = 64;
78 int in_data[sz] = {0};
79 bool res = true;
80
81 const int* from = in_data;
82 const int* to = in_data + sz;
83
84 int out_data[sz] = {0};
85 int* out = out_data;
86
87 out = ba::copy_n ( from, 0, out ); // Copy none
88 res = (res && out == out_data && ba::all_of(out, out + sz, is_zero));
89
90 out = ba::copy_n ( from, sz, out ); // Copy all
91 res = (res && out == out_data + sz
92 && ba::equal( input_iterator<const int *>(out_data), input_iterator<const int *>(out_data + sz),
93 input_iterator<const int *>(from), input_iterator<const int *>(to)));
94
95 return res;
96 }
97
98
test_sequence1()99 void test_sequence1 () {
100 std::vector<int> v;
101 for ( int i = 5; i < 15; ++i )
102 v.push_back ( i );
103 test_sequence ( v );
104
105 BOOST_CXX14_CONSTEXPR bool constexpr_res = test_constexpr();
106 BOOST_CHECK(constexpr_res);
107
108 std::list<int> l;
109 for ( int i = 25; i > 15; --i )
110 l.push_back ( i );
111 test_sequence ( l );
112 }
113
114
BOOST_AUTO_TEST_CASE(test_main)115 BOOST_AUTO_TEST_CASE( test_main )
116 {
117 test_sequence1 ();
118 }
119