inclusive_scan_test.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <algorithm>
  11. #include <boost/config.hpp>
  12. #include <boost/algorithm/cxx11/iota.hpp>
  13. #include <boost/algorithm/cxx17/inclusive_scan.hpp>
  14. #include "iterator_test.hpp"
  15. #define BOOST_TEST_MAIN
  16. #include <boost/test/unit_test.hpp>
  17. namespace ba = boost::algorithm;
  18. int triangle(int n) { return n*(n+1)/2; }
  19. void basic_tests_op()
  20. {
  21. {
  22. std::vector<int> v(10);
  23. std::fill(v.begin(), v.end(), 3);
  24. ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>());
  25. for (size_t i = 0; i < v.size(); ++i)
  26. assert(v[i] == (int)(i+1) * 3);
  27. }
  28. {
  29. std::vector<int> v(10);
  30. ba::iota(v.begin(), v.end(), 0);
  31. ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>());
  32. for (size_t i = 0; i < v.size(); ++i)
  33. assert(v[i] == triangle(i));
  34. }
  35. {
  36. std::vector<int> v(10);
  37. ba::iota(v.begin(), v.end(), 1);
  38. ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>());
  39. for (size_t i = 0; i < v.size(); ++i)
  40. assert(v[i] == triangle(i + 1));
  41. }
  42. {
  43. std::vector<int> v, res;
  44. ba::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>());
  45. assert(res.empty());
  46. }
  47. }
  48. void test_inclusive_scan_op()
  49. {
  50. basic_tests_op();
  51. BOOST_CHECK(true);
  52. }
  53. void basic_tests_init()
  54. {
  55. {
  56. std::vector<int> v(10);
  57. std::fill(v.begin(), v.end(), 3);
  58. ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 50);
  59. for (size_t i = 0; i < v.size(); ++i)
  60. assert(v[i] == 50 + (int)(i+1) * 3);
  61. }
  62. {
  63. std::vector<int> v(10);
  64. ba::iota(v.begin(), v.end(), 0);
  65. ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 40);
  66. for (size_t i = 0; i < v.size(); ++i)
  67. assert(v[i] == 40 + triangle(i));
  68. }
  69. {
  70. std::vector<int> v(10);
  71. ba::iota(v.begin(), v.end(), 1);
  72. ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 30);
  73. for (size_t i = 0; i < v.size(); ++i)
  74. assert(v[i] == 30 + triangle(i + 1));
  75. }
  76. {
  77. std::vector<int> v, res;
  78. ba::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>(), 40);
  79. assert(res.empty());
  80. }
  81. }
  82. void test_inclusive_scan_init()
  83. {
  84. basic_tests_init();
  85. BOOST_CHECK(true);
  86. }
  87. void basic_tests_op_init()
  88. {
  89. {
  90. std::vector<int> v(10);
  91. std::fill(v.begin(), v.end(), 3);
  92. ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 50);
  93. for (size_t i = 0; i < v.size(); ++i)
  94. BOOST_CHECK(v[i] == 50 + (int)(i+1) * 3);
  95. }
  96. {
  97. std::vector<int> v(10);
  98. ba::iota(v.begin(), v.end(), 0);
  99. ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 40);
  100. for (size_t i = 0; i < v.size(); ++i)
  101. BOOST_CHECK(v[i] == 40 + triangle(i));
  102. }
  103. {
  104. std::vector<int> v(10);
  105. ba::iota(v.begin(), v.end(), 1);
  106. ba::inclusive_scan(v.begin(), v.end(), v.begin(), std::plus<int>(), 30);
  107. for (size_t i = 0; i < v.size(); ++i)
  108. BOOST_CHECK(v[i] == 30 + triangle(i + 1));
  109. }
  110. {
  111. std::vector<int> v, res;
  112. ba::inclusive_scan(v.begin(), v.end(), std::back_inserter(res), std::plus<int>(), 40);
  113. BOOST_CHECK(res.empty());
  114. }
  115. }
  116. void test_inclusive_scan_op_init()
  117. {
  118. basic_tests_op_init();
  119. BOOST_CHECK(true);
  120. }
  121. BOOST_AUTO_TEST_CASE( test_main )
  122. {
  123. test_inclusive_scan_op();
  124. test_inclusive_scan_init();
  125. test_inclusive_scan_op_init();
  126. }