summaryrefslogtreecommitdiff
path: root/libs/algorithm/test/iota_test1.cpp
blob: 747691f0370814f76f0055f68e155ed11f5bff08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* 
   Copyright (c) Marshall Clow 2011-2012.

   Distributed under the Boost Software License, Version 1.0. (See accompanying
   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

    For more information, see http://www.boost.org
*/

#include <boost/config.hpp>
#include <boost/algorithm/cxx11/iota.hpp>

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

#include <iostream>
#include <string>
#include <vector>
#include <list>

//  Test to make sure a sequence is "correctly formed"; i.e, ascending by one
template <typename Iterator, typename T>
bool test_iota_results ( Iterator first, Iterator last, T initial_value ) {
    if ( first == last ) return true;
    if ( initial_value != *first ) return false;
    Iterator prev = first;
    while ( ++first != last ) {
        if (( *first - *prev ) != 1 )
            return false;
        prev = first;
        }
    return true;
    }

template <typename Range, typename T>
bool test_iota_results ( const Range &r, T initial_value ) {
    return test_iota_results (boost::begin (r), boost::end (r), initial_value );
}


void test_ints () {
    std::vector<int> v;
    std::list<int> l;

    v.clear (); v.resize ( 10 );
    boost::algorithm::iota ( v.begin (), v.end (), 23 );
    BOOST_CHECK ( test_iota_results ( v.begin (), v.end (), 23 ));
    
    v.clear (); v.resize ( 19 );
    boost::algorithm::iota ( v, 18 );
    BOOST_CHECK ( test_iota_results ( v, 18 ));
    
    v.clear ();
    boost::algorithm::iota_n ( std::back_inserter(v), 99, 20 );
    BOOST_CHECK ( test_iota_results ( v, 99 ));
    
    v.clear ();
    boost::algorithm::iota_n ( std::back_inserter(v), 99, 0 );
    BOOST_CHECK ( v.size() == 0 );

/*
    l.clear (); l.reserve ( 5 );
    boost::algorithm::iota ( l.begin (), l.end (), 123 );
    BOOST_CHECK ( test_iota_results ( l.begin (), l.end (), 123 ));
    
    l.clear (); l.reserve ( 9 );
    boost::algorithm::iota ( l.begin (), l.end (), 87 );
    BOOST_CHECK ( test_iota_results ( l.begin (), l.end (), 87 ));
*/

    l.clear ();
    boost::algorithm::iota_n ( std::back_inserter(l), 99, 20 );
    BOOST_CHECK ( test_iota_results ( l, 99 ));
    
    l.clear ();
    boost::algorithm::iota_n ( std::front_inserter(l), 123, 20 );
    BOOST_CHECK ( test_iota_results ( l.rbegin (), l.rend (), 123 ));
    }


BOOST_AUTO_TEST_CASE( test_main )
{
  test_ints ();
}