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/iota.hpp> |
12 | |
13 | #define BOOST_TEST_MAIN |
14 | #include <boost/test/unit_test.hpp> |
15 | |
16 | #include <iostream> |
17 | #include <string> |
18 | #include <vector> |
19 | #include <list> |
20 | |
21 | // Test to make sure a sequence is "correctly formed"; i.e, ascending by one |
22 | template <typename Iterator, typename T> |
23 | bool test_iota_results ( Iterator first, Iterator last, T initial_value ) { |
24 | if ( first == last ) return true; |
25 | if ( initial_value != *first ) return false; |
26 | Iterator prev = first; |
27 | while ( ++first != last ) { |
28 | if (( *first - *prev ) != 1 ) |
29 | return false; |
30 | prev = first; |
31 | } |
32 | return true; |
33 | } |
34 | |
35 | template <typename Range, typename T> |
36 | bool test_iota_results ( const Range &r, T initial_value ) { |
37 | return test_iota_results (boost::begin (r), boost::end (r), initial_value ); |
38 | } |
39 | |
40 | |
41 | void test_ints () { |
42 | std::vector<int> v; |
43 | std::list<int> l; |
44 | |
45 | v.clear (); v.resize ( new_size: 10 ); |
46 | boost::algorithm::iota ( first: v.begin (), last: v.end (), value: 23 ); |
47 | BOOST_CHECK ( test_iota_results ( v.begin (), v.end (), 23 )); |
48 | |
49 | v.clear (); v.resize ( new_size: 19 ); |
50 | boost::algorithm::iota ( r&: v, value: 18 ); |
51 | BOOST_CHECK ( test_iota_results ( v, 18 )); |
52 | |
53 | v.clear (); |
54 | boost::algorithm::iota_n ( out: std::back_inserter(x&: v), value: 99, n: 20 ); |
55 | BOOST_CHECK ( test_iota_results ( v, 99 )); |
56 | |
57 | v.clear (); |
58 | boost::algorithm::iota_n ( out: std::back_inserter(x&: v), value: 99, n: 0 ); |
59 | BOOST_CHECK ( v.size() == 0 ); |
60 | |
61 | /* |
62 | l.clear (); l.reserve ( 5 ); |
63 | boost::algorithm::iota ( l.begin (), l.end (), 123 ); |
64 | BOOST_CHECK ( test_iota_results ( l.begin (), l.end (), 123 )); |
65 | |
66 | l.clear (); l.reserve ( 9 ); |
67 | boost::algorithm::iota ( l.begin (), l.end (), 87 ); |
68 | BOOST_CHECK ( test_iota_results ( l.begin (), l.end (), 87 )); |
69 | */ |
70 | |
71 | l.clear (); |
72 | boost::algorithm::iota_n ( out: std::back_inserter(x&: l), value: 99, n: 20 ); |
73 | BOOST_CHECK ( test_iota_results ( l, 99 )); |
74 | |
75 | l.clear (); |
76 | boost::algorithm::iota_n ( out: std::front_inserter(x&: l), value: 123, n: 20 ); |
77 | BOOST_CHECK ( test_iota_results ( l.rbegin (), l.rend (), 123 )); |
78 | } |
79 | |
80 | |
81 | BOOST_AUTO_TEST_CASE( test_main ) |
82 | { |
83 | test_ints (); |
84 | } |
85 | |