function_input_iterator_test.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <cstddef>
  6. #include <algorithm>
  7. #include <iterator>
  8. #include <vector>
  9. #include <boost/config.hpp>
  10. #if !defined(BOOST_NO_CXX11_DECLTYPE)
  11. // Force boost::result_of use decltype, even on compilers that don't support N3276.
  12. // This enables this test to also verify if the iterator works with lambdas
  13. // on such compilers with this config macro. Note that without the macro result_of
  14. // (and consequently the iterator) is guaranteed to _not_ work, so this case is not
  15. // worth testing anyway.
  16. #define BOOST_RESULT_OF_USE_DECLTYPE
  17. #endif
  18. #include <boost/core/lightweight_test.hpp>
  19. #include <boost/iterator/function_input_iterator.hpp>
  20. namespace {
  21. struct ones {
  22. typedef int result_type;
  23. result_type operator() () {
  24. return 1;
  25. }
  26. };
  27. int ones_function () {
  28. return 1;
  29. }
  30. struct counter {
  31. typedef int result_type;
  32. int n;
  33. explicit counter(int n_) : n(n_) { }
  34. result_type operator() () {
  35. return n++;
  36. }
  37. };
  38. } // namespace
  39. using namespace std;
  40. int main()
  41. {
  42. // test the iterator with function objects
  43. ones ones_generator;
  44. vector<int> values(10);
  45. generate(values.begin(), values.end(), ones());
  46. vector<int> generated;
  47. copy(
  48. boost::make_function_input_iterator(ones_generator, 0),
  49. boost::make_function_input_iterator(ones_generator, 10),
  50. back_inserter(generated)
  51. );
  52. BOOST_TEST_ALL_EQ(values.begin(), values.end(), generated.begin(), generated.end());
  53. // test the iterator with normal functions
  54. vector<int>().swap(generated);
  55. copy(
  56. boost::make_function_input_iterator(&ones_function, 0),
  57. boost::make_function_input_iterator(&ones_function, 10),
  58. back_inserter(generated)
  59. );
  60. BOOST_TEST_ALL_EQ(values.begin(), values.end(), generated.begin(), generated.end());
  61. // test the iterator with a reference to a function
  62. vector<int>().swap(generated);
  63. copy(
  64. boost::make_function_input_iterator(ones_function, 0),
  65. boost::make_function_input_iterator(ones_function, 10),
  66. back_inserter(generated)
  67. );
  68. BOOST_TEST_ALL_EQ(values.begin(), values.end(), generated.begin(), generated.end());
  69. // test the iterator with a stateful function object
  70. counter counter_generator(42);
  71. vector<int>().swap(generated);
  72. copy(
  73. boost::make_function_input_iterator(counter_generator, 0),
  74. boost::make_function_input_iterator(counter_generator, 10),
  75. back_inserter(generated)
  76. );
  77. BOOST_TEST_EQ(generated.size(), 10u);
  78. BOOST_TEST_EQ(counter_generator.n, 42 + 10);
  79. for(std::size_t i = 0; i != 10; ++i)
  80. BOOST_TEST_EQ(generated[i], static_cast<int>(42 + i));
  81. #if !defined(BOOST_NO_CXX11_LAMBDAS) && !defined(BOOST_NO_CXX11_AUTO_DECLARATIONS) \
  82. && defined(BOOST_RESULT_OF_USE_DECLTYPE)
  83. // test the iterator with lambda expressions
  84. int num = 42;
  85. auto lambda_generator = [&num] { return num++; };
  86. vector<int>().swap(generated);
  87. copy(
  88. boost::make_function_input_iterator(lambda_generator, 0),
  89. boost::make_function_input_iterator(lambda_generator, 10),
  90. back_inserter(generated)
  91. );
  92. BOOST_TEST_EQ(generated.size(), 10u);
  93. for(std::size_t i = 0; i != 10; ++i)
  94. BOOST_TEST_EQ(generated[i], static_cast<int>(42 + i));
  95. #endif // BOOST_NO_CXX11_LAMBDAS
  96. return boost::report_errors();
  97. }