PageRenderTime 13ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/iterator/test/function_input_iterator_test.cpp

http://hadesmem.googlecode.com/
C++ | 70 lines | 50 code | 13 blank | 7 comment | 3 complexity | 45d8dcca16962f3c52896da54deaeb96 MD5 | raw file
 1// Copyright 2010 (c) Dean Michael Berris
 2// Distributed under the Boost Software License Version 1.0.
 3// (See accompanying file LICENSE_1_0.txt or copy at
 4// http://www.boost.org/LICENSE_1_0.txt)
 5
 6#include <boost/iterator/function_input_iterator.hpp>
 7#include <vector>
 8#include <iterator>
 9#include <cassert>
10#include <algorithm>
11#include <iostream>
12
13struct ones {
14    typedef int result_type;
15    result_type operator() () {
16        return 1;
17    }
18};
19
20int ones_function () {
21    return 1;
22}
23
24using namespace std;
25
26int main(int argc, char * argv[])
27{
28    // test the iterator with function objects
29    ones ones_generator;
30    vector<int> values(10);
31    generate(values.begin(), values.end(), ones());
32    
33    vector<int> generated;
34    copy(
35        boost::make_function_input_iterator(ones_generator, 0),
36        boost::make_function_input_iterator(ones_generator, 10),
37        back_inserter(generated)
38        );
39
40    assert(values.size() == generated.size());
41    assert(equal(values.begin(), values.end(), generated.begin()));
42    cout << "function iterator test with function objects successful." << endl;
43
44    // test the iterator with normal functions
45    vector<int>().swap(generated);
46    copy(
47        boost::make_function_input_iterator(&ones_function, 0),
48        boost::make_function_input_iterator(&ones_function, 10),
49        back_inserter(generated)
50        );
51
52    assert(values.size() == generated.size());
53    assert(equal(values.begin(), values.end(), generated.begin()));
54    cout << "function iterator test with pointer to function successful." << endl;
55
56    // test the iterator with a reference to a function
57    vector<int>().swap(generated);
58    copy(
59        boost::make_function_input_iterator(ones_function, 0),
60        boost::make_function_input_iterator(ones_function, 10),
61        back_inserter(generated)
62        );
63
64    assert(values.size() == generated.size());
65    assert(equal(values.begin(), values.end(), generated.begin()));
66    cout << "function iterator test with reference to function successful." << endl;
67
68    return 0;
69}
70