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
13#define BOOST_TEST_MAIN
14#include <boost/test/unit_test.hpp>
15
16#include <string>
17#include <iostream>
18#include <vector>
19#include <list>
20
21namespace ba = boost::algorithm;
22// namespace ba = boost;
23
24template <typename Container>
25void test_sequence ( Container const &c ) {
26
27 typedef typename Container::value_type value_type;
28 std::vector<value_type> v;
29
30// Copy zero elements
31 v.clear ();
32 ba::copy_n ( c.begin (), 0, back_inserter ( v ));
33 BOOST_CHECK ( v.size () == 0 );
34 ba::copy_n ( c.begin (), 0U, back_inserter ( v ));
35 BOOST_CHECK ( v.size () == 0 );
36
37 if ( c.size () > 0 ) {
38 // Just one element
39 v.clear ();
40 ba::copy_n ( c.begin (), 1, back_inserter ( v ));
41 BOOST_CHECK ( v.size () == 1 );
42 BOOST_CHECK ( v[0] == *c.begin ());
43
44 v.clear ();
45 ba::copy_n ( c.begin (), 1U, back_inserter ( v ));
46 BOOST_CHECK ( v.size () == 1 );
47 BOOST_CHECK ( v[0] == *c.begin ());
48
49 // Half the elements
50 v.clear ();
51 ba::copy_n ( c.begin (), c.size () / 2, back_inserter ( v ));
52 BOOST_CHECK ( v.size () == c.size () / 2);
53 BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
54
55 // Half the elements + 1
56 v.clear ();
57 ba::copy_n ( c.begin (), c.size () / 2 + 1, back_inserter ( v ));
58 BOOST_CHECK ( v.size () == c.size () / 2 + 1 );
59 BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
60
61 // All the elements
62 v.clear ();
63 ba::copy_n ( c.begin (), c.size (), back_inserter ( v ));
64 BOOST_CHECK ( v.size () == c.size ());
65 BOOST_CHECK ( std::equal ( v.begin (), v.end (), c.begin ()));
66 }
67 }
68
69
70void test_sequence1 () {
71 std::vector<int> v;
72 for ( int i = 5; i < 15; ++i )
73 v.push_back ( i );
74 test_sequence ( v );
75
76 std::list<int> l;
77 for ( int i = 25; i > 15; --i )
78 l.push_back ( i );
79 test_sequence ( l );
80 }
81
82
83BOOST_AUTO_TEST_CASE( test_main )
84{
85 test_sequence1 ();
86}
87