/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. #include <boost/iterator/function_input_iterator.hpp>
  6. #include <vector>
  7. #include <iterator>
  8. #include <cassert>
  9. #include <algorithm>
  10. #include <iostream>
  11. struct ones {
  12. typedef int result_type;
  13. result_type operator() () {
  14. return 1;
  15. }
  16. };
  17. int ones_function () {
  18. return 1;
  19. }
  20. using namespace std;
  21. int main(int argc, char * argv[])
  22. {
  23. // test the iterator with function objects
  24. ones ones_generator;
  25. vector<int> values(10);
  26. generate(values.begin(), values.end(), ones());
  27. vector<int> generated;
  28. copy(
  29. boost::make_function_input_iterator(ones_generator, 0),
  30. boost::make_function_input_iterator(ones_generator, 10),
  31. back_inserter(generated)
  32. );
  33. assert(values.size() == generated.size());
  34. assert(equal(values.begin(), values.end(), generated.begin()));
  35. cout << "function iterator test with function objects successful." << endl;
  36. // test the iterator with normal functions
  37. vector<int>().swap(generated);
  38. copy(
  39. boost::make_function_input_iterator(&ones_function, 0),
  40. boost::make_function_input_iterator(&ones_function, 10),
  41. back_inserter(generated)
  42. );
  43. assert(values.size() == generated.size());
  44. assert(equal(values.begin(), values.end(), generated.begin()));
  45. cout << "function iterator test with pointer to function successful." << endl;
  46. // test the iterator with a reference to a function
  47. vector<int>().swap(generated);
  48. copy(
  49. boost::make_function_input_iterator(ones_function, 0),
  50. boost::make_function_input_iterator(ones_function, 10),
  51. back_inserter(generated)
  52. );
  53. assert(values.size() == generated.size());
  54. assert(equal(values.begin(), values.end(), generated.begin()));
  55. cout << "function iterator test with reference to function successful." << endl;
  56. return 0;
  57. }