for_each_ext.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Boost.Range library
  2. //
  3. // Copyright Neil Groves 2010. Use, modification and
  4. // distribution is subject to the Boost Software License, Version
  5. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. //
  9. // For more information, see http://www.boost.org/libs/range/
  10. //
  11. #include <boost/range/algorithm_ext/for_each.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/range/iterator.hpp>
  15. #include <algorithm>
  16. #include <list>
  17. #include <vector>
  18. namespace
  19. {
  20. struct MockBinaryFn
  21. {
  22. typedef void result_type;
  23. typedef int first_argument_type;
  24. typedef int second_argument_type;
  25. void operator()(int x, int y)
  26. {
  27. xs.push_back(x);
  28. ys.push_back(y);
  29. }
  30. std::vector<int> xs;
  31. std::vector<int> ys;
  32. };
  33. template< class Range1, class Range2 >
  34. void test_for_each_impl( Range1& rng1, Range2& rng2 )
  35. {
  36. MockBinaryFn fn = boost::range::for_each(rng1, rng2, MockBinaryFn());
  37. BOOST_CHECK_EQUAL_COLLECTIONS( ::boost::begin(rng1), ::boost::end(rng1),
  38. fn.xs.begin(), fn.xs.end() );
  39. BOOST_CHECK_EQUAL_COLLECTIONS( ::boost::begin(rng2), ::boost::end(rng2),
  40. fn.ys.begin(), fn.ys.end() );
  41. }
  42. template< class Collection1, class Collection2 >
  43. void test_for_each_impl(const int max_count)
  44. {
  45. Collection1 c1;
  46. for (int i = 0; i < max_count; ++i)
  47. c1.push_back(i);
  48. Collection2 c2;
  49. for (int i = 0; i < max_count; ++i)
  50. c2.push_back(i);
  51. test_for_each_impl(c1, c2);
  52. const Collection1& const_c1 = c1;
  53. const Collection2& const_c2 = c2;
  54. test_for_each_impl(c1, const_c2);
  55. test_for_each_impl(const_c1, c2);
  56. test_for_each_impl(const_c1, const_c2);
  57. }
  58. template< class Collection1, class Collection2 >
  59. void test_for_each_impl()
  60. {
  61. test_for_each_impl< Collection1, Collection2 >(0);
  62. test_for_each_impl< Collection1, Collection2 >(1);
  63. test_for_each_impl< Collection1, Collection2 >(10);
  64. }
  65. void test_for_each()
  66. {
  67. test_for_each_impl< std::vector<int>, std::vector<int> >();
  68. test_for_each_impl< std::list<int>, std::list<int> >();
  69. test_for_each_impl< std::vector<int>, std::list<int> >();
  70. test_for_each_impl< std::list<int>, std::vector<int> >();
  71. }
  72. }
  73. boost::unit_test::test_suite*
  74. init_unit_test_suite(int argc, char* argv[])
  75. {
  76. boost::unit_test::test_suite* test
  77. = BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.for_each" );
  78. test->add( BOOST_TEST_CASE( &test_for_each ) );
  79. return test;
  80. }