• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
test_iota_results(Iterator first,Iterator last,T initial_value)23 BOOST_CXX14_CONSTEXPR 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 
36 template <typename Range, typename T>
test_iota_results(const Range & r,T initial_value)37 BOOST_CXX14_CONSTEXPR bool test_iota_results ( const Range &r, T initial_value ) {
38     return test_iota_results (boost::begin (r), boost::end (r), initial_value );
39 }
40 
41 
test_ints()42 void test_ints () {
43     std::vector<int> v;
44     std::list<int> l;
45 
46     v.clear (); v.resize ( 10 );
47     boost::algorithm::iota ( v.begin (), v.end (), 23 );
48     BOOST_CHECK ( test_iota_results ( v.begin (), v.end (), 23 ));
49 
50     v.clear (); v.resize ( 19 );
51     boost::algorithm::iota ( v, 18 );
52     BOOST_CHECK ( test_iota_results ( v, 18 ));
53 
54     v.clear ();
55     boost::algorithm::iota_n ( std::back_inserter(v), 99, 20 );
56     BOOST_CHECK ( test_iota_results ( v, 99 ));
57 
58     v.clear ();
59     boost::algorithm::iota_n ( std::back_inserter(v), 99, 0 );
60     BOOST_CHECK ( v.size() == 0 );
61 
62 /*
63     l.clear (); l.reserve ( 5 );
64     boost::algorithm::iota ( l.begin (), l.end (), 123 );
65     BOOST_CHECK ( test_iota_results ( l.begin (), l.end (), 123 ));
66 
67     l.clear (); l.reserve ( 9 );
68     boost::algorithm::iota ( l.begin (), l.end (), 87 );
69     BOOST_CHECK ( test_iota_results ( l.begin (), l.end (), 87 ));
70 */
71 
72     l.clear ();
73     boost::algorithm::iota_n ( std::back_inserter(l), 99, 20 );
74     BOOST_CHECK ( test_iota_results ( l, 99 ));
75 
76     l.clear ();
77     boost::algorithm::iota_n ( std::front_inserter(l), 123, 20 );
78     BOOST_CHECK ( test_iota_results ( l.rbegin (), l.rend (), 123 ));
79     }
80 
test_constexpr_iota()81 BOOST_CXX14_CONSTEXPR inline bool test_constexpr_iota() {
82     bool res = true;
83     int data[] = {0, 0, 0};
84     boost::algorithm::iota(data, data, 1); // fill none
85     res = (res && data[0] == 0);
86 
87     boost::algorithm::iota(data, data + 3, 1); // fill all
88     res = (res && test_iota_results(data, data + 3, 1));
89 
90     return res;
91     }
92 
93 
test_constexpr_iota_n()94 BOOST_CXX14_CONSTEXPR inline bool test_constexpr_iota_n() {
95     bool res = true;
96     int data[] = {0, 0, 0};
97     boost::algorithm::iota_n(data, 1, 0); // fill none
98     res = (res && data[0] == 0);
99 
100     boost::algorithm::iota_n(data, 1, 3); // fill all
101     res = (res && test_iota_results(data, 1));
102 
103     return res;
104     }
105 
106 
BOOST_AUTO_TEST_CASE(test_main)107 BOOST_AUTO_TEST_CASE( test_main )
108 {
109   test_ints ();
110   BOOST_CXX14_CONSTEXPR bool constexpr_iota_res = test_constexpr_iota ();
111   BOOST_CHECK(constexpr_iota_res);
112   BOOST_CXX14_CONSTEXPR bool constexpr_iota_n_res = test_constexpr_iota_n ();
113   BOOST_CHECK(constexpr_iota_n_res);
114 }
115