| 1 | /* |
| 2 | Copyright (c) Marshall Clow 2017. |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | For more information, see http://www.boost.org |
| 8 | */ |
| 9 | |
| 10 | #include <vector> |
| 11 | #include <functional> |
| 12 | #include <numeric> |
| 13 | |
| 14 | #include <boost/config.hpp> |
| 15 | #include <boost/algorithm/cxx11/iota.hpp> |
| 16 | #include <boost/algorithm/cxx17/transform_inclusive_scan.hpp> |
| 17 | |
| 18 | #include "iterator_test.hpp" |
| 19 | |
| 20 | #define BOOST_TEST_MAIN |
| 21 | #include <boost/test/unit_test.hpp> |
| 22 | |
| 23 | namespace ba = boost::algorithm; |
| 24 | |
| 25 | int triangle(int n) { return n*(n+1)/2; } |
| 26 | |
| 27 | template <class _Tp> |
| 28 | struct identity |
| 29 | { |
| 30 | const _Tp& operator()(const _Tp& __x) const { return __x;} |
| 31 | }; |
| 32 | |
| 33 | |
| 34 | template <class Iter1, class BOp, class UOp, class Iter2> |
| 35 | void |
| 36 | transform_inclusive_scan_test(Iter1 first, Iter1 last, BOp bop, UOp uop, Iter2 rFirst, Iter2 rLast) |
| 37 | { |
| 38 | std::vector<typename std::iterator_traits<Iter1>::value_type> v; |
| 39 | // Test not in-place |
| 40 | ba::transform_inclusive_scan(first, last, std::back_inserter(v), bop, uop); |
| 41 | BOOST_CHECK(std::distance(first, last) == v.size()); |
| 42 | BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst)); |
| 43 | |
| 44 | // Test in-place |
| 45 | v.clear(); |
| 46 | v.assign(first, last); |
| 47 | ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), bop, uop); |
| 48 | BOOST_CHECK(std::distance(first, last) == v.size()); |
| 49 | BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst)); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | template <class Iter> |
| 54 | void |
| 55 | transform_inclusive_scan_test() |
| 56 | { |
| 57 | int ia[] = { 1, 3, 5, 7, 9}; |
| 58 | const int pResI0[] = { 1, 4, 9, 16, 25}; // with identity |
| 59 | const int mResI0[] = { 1, 3, 15, 105, 945}; |
| 60 | const int pResN0[] = { -1, -4, -9, -16, -25}; // with negate |
| 61 | const int mResN0[] = { -1, 3, -15, 105, -945}; |
| 62 | const unsigned sa = sizeof(ia) / sizeof(ia[0]); |
| 63 | BOOST_CHECK(sa == sizeof(pResI0) / sizeof(pResI0[0])); // just to be sure |
| 64 | BOOST_CHECK(sa == sizeof(mResI0) / sizeof(mResI0[0])); // just to be sure |
| 65 | BOOST_CHECK(sa == sizeof(pResN0) / sizeof(pResN0[0])); // just to be sure |
| 66 | BOOST_CHECK(sa == sizeof(mResN0) / sizeof(mResN0[0])); // just to be sure |
| 67 | |
| 68 | for (unsigned int i = 0; i < sa; ++i ) { |
| 69 | transform_inclusive_scan_test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), pResI0, pResI0 + i); |
| 70 | transform_inclusive_scan_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), mResI0, mResI0 + i); |
| 71 | transform_inclusive_scan_test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), pResN0, pResN0 + i); |
| 72 | transform_inclusive_scan_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), mResN0, mResN0 + i); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | // Basic sanity |
| 78 | void basic_transform_inclusive_scan_tests() |
| 79 | { |
| 80 | { |
| 81 | std::vector<int> v(10); |
| 82 | std::fill(first: v.begin(), last: v.end(), value: 3); |
| 83 | ba::transform_inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>(), uOp: identity<int>()); |
| 84 | for (size_t i = 0; i < v.size(); ++i) |
| 85 | BOOST_CHECK(v[i] == (int)(i+1) * 3); |
| 86 | } |
| 87 | |
| 88 | { |
| 89 | std::vector<int> v(10); |
| 90 | ba::iota(first: v.begin(), last: v.end(), value: 0); |
| 91 | ba::transform_inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>(), uOp: identity<int>()); |
| 92 | for (size_t i = 0; i < v.size(); ++i) |
| 93 | BOOST_CHECK(v[i] == triangle(i)); |
| 94 | } |
| 95 | |
| 96 | { |
| 97 | std::vector<int> v(10); |
| 98 | ba::iota(first: v.begin(), last: v.end(), value: 1); |
| 99 | ba::transform_inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>(), uOp: identity<int>()); |
| 100 | for (size_t i = 0; i < v.size(); ++i) |
| 101 | BOOST_CHECK(v[i] == triangle(i + 1)); |
| 102 | } |
| 103 | |
| 104 | { |
| 105 | std::vector<int> v, res; |
| 106 | ba::transform_inclusive_scan(first: v.begin(), last: v.end(), result: std::back_inserter(x&: res), bOp: std::plus<int>(), uOp: identity<int>()); |
| 107 | BOOST_CHECK(res.empty()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void test_transform_inclusive_scan() |
| 112 | { |
| 113 | basic_transform_inclusive_scan_tests(); |
| 114 | |
| 115 | // All the iterator categories |
| 116 | transform_inclusive_scan_test<input_iterator <const int*> >(); |
| 117 | transform_inclusive_scan_test<forward_iterator <const int*> >(); |
| 118 | transform_inclusive_scan_test<bidirectional_iterator<const int*> >(); |
| 119 | transform_inclusive_scan_test<random_access_iterator<const int*> >(); |
| 120 | transform_inclusive_scan_test<const int*>(); |
| 121 | transform_inclusive_scan_test< int*>(); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | template <class Iter1, class BOp, class UOp, class T, class Iter2> |
| 126 | void |
| 127 | transform_inclusive_scan_init_test(Iter1 first, Iter1 last, BOp bop, UOp uop, T init, Iter2 rFirst, Iter2 rLast) |
| 128 | { |
| 129 | std::vector<typename std::iterator_traits<Iter1>::value_type> v; |
| 130 | // Test not in-place |
| 131 | ba::transform_inclusive_scan(first, last, std::back_inserter(v), bop, uop, init); |
| 132 | BOOST_CHECK(std::distance(rFirst, rLast) == v.size()); |
| 133 | BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst)); |
| 134 | |
| 135 | // Test in-place |
| 136 | v.clear(); |
| 137 | v.assign(first, last); |
| 138 | ba::transform_inclusive_scan(v.begin(), v.end(), v.begin(), bop, uop, init); |
| 139 | BOOST_CHECK(std::distance(rFirst, rLast) == v.size()); |
| 140 | BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst)); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | template <class Iter> |
| 145 | void |
| 146 | transform_inclusive_scan_init_test() |
| 147 | { |
| 148 | int ia[] = { 1, 3, 5, 7, 9}; |
| 149 | const int pResI0[] = { 1, 4, 9, 16, 25}; // with identity |
| 150 | const int mResI0[] = { 0, 0, 0, 0, 0}; |
| 151 | const int pResN0[] = { -1, -4, -9, -16, -25}; // with negate |
| 152 | const int mResN0[] = { 0, 0, 0, 0, 0}; |
| 153 | const int pResI2[] = { 3, 6, 11, 18, 27}; // with identity |
| 154 | const int mResI2[] = { 2, 6, 30, 210, 1890}; |
| 155 | const int pResN2[] = { 1, -2, -7, -14, -23}; // with negate |
| 156 | const int mResN2[] = { -2, 6, -30, 210, -1890}; |
| 157 | const unsigned sa = sizeof(ia) / sizeof(ia[0]); |
| 158 | BOOST_CHECK(sa == sizeof(pResI0) / sizeof(pResI0[0])); // just to be sure |
| 159 | BOOST_CHECK(sa == sizeof(mResI0) / sizeof(mResI0[0])); // just to be sure |
| 160 | BOOST_CHECK(sa == sizeof(pResN0) / sizeof(pResN0[0])); // just to be sure |
| 161 | BOOST_CHECK(sa == sizeof(mResN0) / sizeof(mResN0[0])); // just to be sure |
| 162 | BOOST_CHECK(sa == sizeof(pResI2) / sizeof(pResI2[0])); // just to be sure |
| 163 | BOOST_CHECK(sa == sizeof(mResI2) / sizeof(mResI2[0])); // just to be sure |
| 164 | BOOST_CHECK(sa == sizeof(pResN2) / sizeof(pResN2[0])); // just to be sure |
| 165 | BOOST_CHECK(sa == sizeof(mResN2) / sizeof(mResN2[0])); // just to be sure |
| 166 | |
| 167 | for (unsigned int i = 0; i < sa; ++i ) { |
| 168 | transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), 0, pResI0, pResI0 + i); |
| 169 | transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), 0, mResI0, mResI0 + i); |
| 170 | transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), 0, pResN0, pResN0 + i); |
| 171 | transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 0, mResN0, mResN0 + i); |
| 172 | transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::plus<int>(), identity<int>(), 2, pResI2, pResI2 + i); |
| 173 | transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(), 2, mResI2, mResI2 + i); |
| 174 | transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::plus<int>(), std::negate<int>(), 2, pResN2, pResN2 + i); |
| 175 | transform_inclusive_scan_init_test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 2, mResN2, mResN2 + i); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | |
| 180 | // Basic sanity |
| 181 | void basic_transform_inclusive_scan_init_tests() |
| 182 | { |
| 183 | { |
| 184 | std::vector<int> v(10); |
| 185 | std::fill(first: v.begin(), last: v.end(), value: 3); |
| 186 | ba::transform_inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>(), uOp: identity<int>(), init: 50); |
| 187 | for (size_t i = 0; i < v.size(); ++i) |
| 188 | BOOST_CHECK(v[i] == 50 + (int) (i + 1) * 3); |
| 189 | } |
| 190 | |
| 191 | { |
| 192 | std::vector<int> v(10); |
| 193 | ba::iota(first: v.begin(), last: v.end(), value: 0); |
| 194 | ba::transform_inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>(), uOp: identity<int>(), init: 30); |
| 195 | for (size_t i = 0; i < v.size(); ++i) |
| 196 | BOOST_CHECK(v[i] == 30 + triangle(i)); |
| 197 | } |
| 198 | |
| 199 | { |
| 200 | std::vector<int> v(10); |
| 201 | ba::iota(first: v.begin(), last: v.end(), value: 1); |
| 202 | ba::transform_inclusive_scan(first: v.begin(), last: v.end(), result: v.begin(), bOp: std::plus<int>(), uOp: identity<int>(), init: 40); |
| 203 | for (size_t i = 0; i < v.size(); ++i) |
| 204 | BOOST_CHECK(v[i] == 40 + triangle(i + 1)); |
| 205 | } |
| 206 | |
| 207 | { |
| 208 | std::vector<int> v, res; |
| 209 | ba::transform_inclusive_scan(first: v.begin(), last: v.end(), result: std::back_inserter(x&: res), bOp: std::plus<int>(), uOp: identity<int>(), init: 1); |
| 210 | BOOST_CHECK(res.empty()); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void test_transform_inclusive_scan_init() |
| 215 | { |
| 216 | basic_transform_inclusive_scan_init_tests(); |
| 217 | |
| 218 | // All the iterator categories |
| 219 | transform_inclusive_scan_init_test<input_iterator <const int*> >(); |
| 220 | transform_inclusive_scan_init_test<forward_iterator <const int*> >(); |
| 221 | transform_inclusive_scan_init_test<bidirectional_iterator<const int*> >(); |
| 222 | transform_inclusive_scan_init_test<random_access_iterator<const int*> >(); |
| 223 | transform_inclusive_scan_init_test<const int*>(); |
| 224 | transform_inclusive_scan_init_test< int*>(); |
| 225 | |
| 226 | } |
| 227 | |
| 228 | |
| 229 | BOOST_AUTO_TEST_CASE( test_main ) |
| 230 | { |
| 231 | test_transform_inclusive_scan(); |
| 232 | test_transform_inclusive_scan_init(); |
| 233 | } |
| 234 | |