reduce_test.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright (c) Marshall Clow 2013.
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. For more information, see http://www.boost.org
  6. */
  7. #include <vector>
  8. #include <functional>
  9. #include <boost/config.hpp>
  10. #include <boost/algorithm/cxx17/reduce.hpp>
  11. #include "iterator_test.hpp"
  12. #define BOOST_TEST_MAIN
  13. #include <boost/test/unit_test.hpp>
  14. namespace ba = boost::algorithm;
  15. template <class Iter, class T, class Op>
  16. void
  17. test_reduce(Iter first, Iter last, T init, Op op, T x)
  18. {
  19. BOOST_CHECK(ba::reduce(first, last, init, op) == x);
  20. }
  21. template <class Iter, class T, class Op>
  22. void
  23. test_reduce(Iter first, Iter last, Op op, T x)
  24. {
  25. BOOST_CHECK(ba::reduce(first, last, op) == x);
  26. }
  27. template <class Iter, class T>
  28. void
  29. test_reduce(Iter first, Iter last, T x)
  30. {
  31. BOOST_CHECK(ba::reduce(first, last) == x);
  32. }
  33. template <class Iter>
  34. void
  35. test_init_op()
  36. {
  37. int ia[] = {1, 2, 3, 4, 5, 6};
  38. unsigned sa = sizeof(ia) / sizeof(ia[0]);
  39. test_reduce(Iter(ia), Iter(ia), 0, std::plus<int>(), 0);
  40. test_reduce(Iter(ia), Iter(ia), 1, std::multiplies<int>(), 1);
  41. test_reduce(Iter(ia), Iter(ia+1), 0, std::plus<int>(), 1);
  42. test_reduce(Iter(ia), Iter(ia+1), 2, std::multiplies<int>(), 2);
  43. test_reduce(Iter(ia), Iter(ia+2), 0, std::plus<int>(), 3);
  44. test_reduce(Iter(ia), Iter(ia+2), 3, std::multiplies<int>(), 6);
  45. test_reduce(Iter(ia), Iter(ia+sa), 0, std::plus<int>(), 21);
  46. test_reduce(Iter(ia), Iter(ia+sa), 4, std::multiplies<int>(), 2880);
  47. }
  48. void test_reduce_init_op()
  49. {
  50. test_init_op<input_iterator<const int*> >();
  51. test_init_op<forward_iterator<const int*> >();
  52. test_init_op<bidirectional_iterator<const int*> >();
  53. test_init_op<random_access_iterator<const int*> >();
  54. test_init_op<const int*>();
  55. {
  56. char ia[] = {1, 2, 3, 4, 5, 6, 7, 8};
  57. unsigned sa = sizeof(ia) / sizeof(ia[0]);
  58. unsigned res = boost::algorithm::reduce(ia, ia+sa, 1U, std::multiplies<unsigned>());
  59. BOOST_CHECK(res == 40320); // 8! will not fit into a char
  60. }
  61. }
  62. template <class Iter>
  63. void
  64. test_init()
  65. {
  66. int ia[] = {1, 2, 3, 4, 5, 6};
  67. unsigned sa = sizeof(ia) / sizeof(ia[0]);
  68. test_reduce(Iter(ia), Iter(ia), 0, 0);
  69. test_reduce(Iter(ia), Iter(ia), 1, 1);
  70. test_reduce(Iter(ia), Iter(ia+1), 0, 1);
  71. test_reduce(Iter(ia), Iter(ia+1), 2, 3);
  72. test_reduce(Iter(ia), Iter(ia+2), 0, 3);
  73. test_reduce(Iter(ia), Iter(ia+2), 3, 6);
  74. test_reduce(Iter(ia), Iter(ia+sa), 0, 21);
  75. test_reduce(Iter(ia), Iter(ia+sa), 4, 25);
  76. }
  77. void test_reduce_init()
  78. {
  79. test_init<input_iterator<const int*> >();
  80. test_init<forward_iterator<const int*> >();
  81. test_init<bidirectional_iterator<const int*> >();
  82. test_init<random_access_iterator<const int*> >();
  83. test_init<const int*>();
  84. }
  85. template <class Iter>
  86. void
  87. test()
  88. {
  89. int ia[] = {1, 2, 3, 4, 5, 6};
  90. unsigned sa = sizeof(ia) / sizeof(ia[0]);
  91. test_reduce(Iter(ia), Iter(ia), 0);
  92. test_reduce(Iter(ia), Iter(ia+1), 1);
  93. test_reduce(Iter(ia), Iter(ia+2), 3);
  94. test_reduce(Iter(ia), Iter(ia+sa), 21);
  95. }
  96. void test_reduce()
  97. {
  98. test<input_iterator<const int*> >();
  99. test<forward_iterator<const int*> >();
  100. test<bidirectional_iterator<const int*> >();
  101. test<random_access_iterator<const int*> >();
  102. test<const int*>();
  103. }
  104. BOOST_AUTO_TEST_CASE( test_main )
  105. {
  106. test_reduce();
  107. test_reduce_init();
  108. test_reduce_init_op();
  109. }