transform_exclusive_scan_test.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright (c) Marshall Clow 2017.
  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 <numeric>
  10. #include <boost/config.hpp>
  11. #include <boost/algorithm/cxx11/iota.hpp>
  12. #include <boost/algorithm/cxx17/transform_exclusive_scan.hpp>
  13. #include "iterator_test.hpp"
  14. #define BOOST_TEST_MAIN
  15. #include <boost/test/unit_test.hpp>
  16. namespace ba = boost::algorithm;
  17. int triangle(int n) { return n*(n+1)/2; }
  18. template <class _Tp>
  19. struct identity
  20. {
  21. const _Tp& operator()(const _Tp& __x) const { return __x;}
  22. };
  23. template <class Iter1, class BOp, class UOp, class T, class Iter2>
  24. void
  25. test(Iter1 first, Iter1 last, BOp bop, UOp uop, T init, Iter2 rFirst, Iter2 rLast)
  26. {
  27. std::vector<typename std::iterator_traits<Iter1>::value_type> v;
  28. // Test not in-place
  29. ba::transform_exclusive_scan(first, last, std::back_inserter(v), init, bop, uop);
  30. BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
  31. BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
  32. // Test in-place
  33. v.clear();
  34. v.assign(first, last);
  35. ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), init, bop, uop);
  36. BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
  37. BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
  38. }
  39. template <class Iter>
  40. void
  41. test()
  42. {
  43. int ia[] = { 1, 3, 5, 7, 9};
  44. const int pResI0[] = { 0, 1, 4, 9, 16}; // with identity
  45. const int mResI0[] = { 0, 0, 0, 0, 0};
  46. const int pResN0[] = { 0, -1, -4, -9, -16}; // with negate
  47. const int mResN0[] = { 0, 0, 0, 0, 0};
  48. const int pResI2[] = { 2, 3, 6, 11, 18}; // with identity
  49. const int mResI2[] = { 2, 2, 6, 30, 210};
  50. const int pResN2[] = { 2, 1, -2, -7, -14}; // with negate
  51. const int mResN2[] = { 2, -2, 6, -30, 210};
  52. const unsigned sa = sizeof(ia) / sizeof(ia[0]);
  53. BOOST_CHECK(sa == sizeof(pResI0) / sizeof(pResI0[0])); // just to be sure
  54. BOOST_CHECK(sa == sizeof(mResI0) / sizeof(mResI0[0])); // just to be sure
  55. BOOST_CHECK(sa == sizeof(pResN0) / sizeof(pResN0[0])); // just to be sure
  56. BOOST_CHECK(sa == sizeof(mResN0) / sizeof(mResN0[0])); // just to be sure
  57. BOOST_CHECK(sa == sizeof(pResI2) / sizeof(pResI2[0])); // just to be sure
  58. BOOST_CHECK(sa == sizeof(mResI2) / sizeof(mResI2[0])); // just to be sure
  59. BOOST_CHECK(sa == sizeof(pResN2) / sizeof(pResN2[0])); // just to be sure
  60. BOOST_CHECK(sa == sizeof(mResN2) / sizeof(mResN2[0])); // just to be sure
  61. for (unsigned int i = 0; i < sa; ++i ) {
  62. test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), 0, pResI0, pResI0 + i);
  63. test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), 0, mResI0, mResI0 + i);
  64. test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), 0, pResN0, pResN0 + i);
  65. test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 0, mResN0, mResN0 + i);
  66. test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), 2, pResI2, pResI2 + i);
  67. test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), 2, mResI2, mResI2 + i);
  68. test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), 2, pResN2, pResN2 + i);
  69. test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 2, mResN2, mResN2 + i);
  70. }
  71. }
  72. void basic_tests()
  73. {
  74. {
  75. std::vector<int> v(10);
  76. std::fill(v.begin(), v.end(), 3);
  77. ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 50, std::plus<int>(), identity<int>());
  78. for (size_t i = 0; i < v.size(); ++i)
  79. BOOST_CHECK(v[i] == 50 + (int) i * 3);
  80. }
  81. {
  82. std::vector<int> v(10);
  83. ba::iota(v.begin(), v.end(), 0);
  84. ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 30, std::plus<int>(), identity<int>());
  85. for (size_t i = 0; i < v.size(); ++i)
  86. BOOST_CHECK(v[i] == 30 + triangle(i-1));
  87. }
  88. {
  89. std::vector<int> v(10);
  90. ba::iota(v.begin(), v.end(), 1);
  91. ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 40, std::plus<int>(), identity<int>());
  92. for (size_t i = 0; i < v.size(); ++i)
  93. BOOST_CHECK(v[i] == 40 + triangle(i));
  94. }
  95. {
  96. std::vector<int> v, res;
  97. ba::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 40, std::plus<int>(), identity<int>());
  98. BOOST_CHECK(res.empty());
  99. }
  100. }
  101. void test_transform_exclusive_scan_init()
  102. {
  103. basic_tests();
  104. // All the iterator categories
  105. test<input_iterator <const int*> >();
  106. test<forward_iterator <const int*> >();
  107. test<bidirectional_iterator<const int*> >();
  108. test<random_access_iterator<const int*> >();
  109. test<const int*>();
  110. test< int*>();
  111. }
  112. BOOST_AUTO_TEST_CASE( test_main )
  113. {
  114. test_transform_exclusive_scan_init();
  115. }