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_exclusive_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 
triangle(int n)25 int triangle(int n) { return n*(n+1)/2; }
26 
27 template <class _Tp>
28 struct identity
29 {
operator ()identity30     const _Tp& operator()(const _Tp& __x) const { return __x;}
31 };
32 
33 
34 template <class Iter1, class BOp, class UOp, class T, class Iter2>
35 void
test(Iter1 first,Iter1 last,BOp bop,UOp uop,T init,Iter2 rFirst,Iter2 rLast)36 test(Iter1 first, Iter1 last, BOp bop, UOp uop, T init, Iter2 rFirst, Iter2 rLast)
37 {
38     std::vector<typename std::iterator_traits<Iter1>::value_type> v;
39 //  Test not in-place
40     ba::transform_exclusive_scan(first, last, std::back_inserter(v), init, bop, uop);
41     BOOST_CHECK(std::distance(rFirst, rLast) == 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_exclusive_scan(v.begin(), v.end(), v.begin(), init, bop, uop);
48     BOOST_CHECK(std::distance(rFirst, rLast) == v.size());
49     BOOST_CHECK(std::equal(v.begin(), v.end(), rFirst));
50 }
51 
52 
53 template <class Iter>
54 void
test()55 test()
56 {
57           int ia[]     = { 1,  3,  5,   7,   9};
58     const int pResI0[] = { 0,  1,  4,   9,  16};        // with identity
59     const int mResI0[] = { 0,  0,  0,   0,   0};
60     const int pResN0[] = { 0, -1, -4,  -9, -16};        // with negate
61     const int mResN0[] = { 0,  0,  0,   0,   0};
62     const int pResI2[] = { 2,  3,  6,  11,  18};        // with identity
63     const int mResI2[] = { 2,  2,  6,  30, 210};
64     const int pResN2[] = { 2,  1, -2,  -7, -14};        // with negate
65     const int mResN2[] = { 2, -2,  6, -30, 210};
66     const unsigned sa = sizeof(ia) / sizeof(ia[0]);
67     BOOST_CHECK(sa == sizeof(pResI0) / sizeof(pResI0[0]));       // just to be sure
68     BOOST_CHECK(sa == sizeof(mResI0) / sizeof(mResI0[0]));       // just to be sure
69     BOOST_CHECK(sa == sizeof(pResN0) / sizeof(pResN0[0]));       // just to be sure
70     BOOST_CHECK(sa == sizeof(mResN0) / sizeof(mResN0[0]));       // just to be sure
71     BOOST_CHECK(sa == sizeof(pResI2) / sizeof(pResI2[0]));       // just to be sure
72     BOOST_CHECK(sa == sizeof(mResI2) / sizeof(mResI2[0]));       // just to be sure
73     BOOST_CHECK(sa == sizeof(pResN2) / sizeof(pResN2[0]));       // just to be sure
74     BOOST_CHECK(sa == sizeof(mResN2) / sizeof(mResN2[0]));       // just to be sure
75 
76     for (unsigned int i = 0; i < sa; ++i ) {
77         test(Iter(ia), Iter(ia + i), std::plus<int>(),       identity<int>(),    0, pResI0, pResI0 + i);
78         test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(),    0, mResI0, mResI0 + i);
79         test(Iter(ia), Iter(ia + i), std::plus<int>(),       std::negate<int>(), 0, pResN0, pResN0 + i);
80         test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 0, mResN0, mResN0 + i);
81         test(Iter(ia), Iter(ia + i), std::plus<int>(),       identity<int>(),    2, pResI2, pResI2 + i);
82         test(Iter(ia), Iter(ia + i), std::multiplies<int>(), identity<int>(),    2, mResI2, mResI2 + i);
83         test(Iter(ia), Iter(ia + i), std::plus<int>(),       std::negate<int>(), 2, pResN2, pResN2 + i);
84         test(Iter(ia), Iter(ia + i), std::multiplies<int>(), std::negate<int>(), 2, mResN2, mResN2 + i);
85         }
86 }
87 
basic_tests()88 void basic_tests()
89 {
90     {
91     std::vector<int> v(10);
92     std::fill(v.begin(), v.end(), 3);
93     ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 50, std::plus<int>(), identity<int>());
94     for (size_t i = 0; i < v.size(); ++i)
95         BOOST_CHECK(v[i] == 50 + (int) i * 3);
96     }
97 
98     {
99     std::vector<int> v(10);
100     ba::iota(v.begin(), v.end(), 0);
101     ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 30, std::plus<int>(), identity<int>());
102     for (size_t i = 0; i < v.size(); ++i)
103         BOOST_CHECK(v[i] == 30 + triangle(i-1));
104     }
105 
106     {
107     std::vector<int> v(10);
108     ba::iota(v.begin(), v.end(), 1);
109     ba::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 40, std::plus<int>(), identity<int>());
110     for (size_t i = 0; i < v.size(); ++i)
111         BOOST_CHECK(v[i] == 40 + triangle(i));
112     }
113 
114     {
115     std::vector<int> v, res;
116     ba::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 40, std::plus<int>(), identity<int>());
117     BOOST_CHECK(res.empty());
118     }
119 }
120 
121 
test_transform_exclusive_scan_init()122 void test_transform_exclusive_scan_init()
123 {
124 	basic_tests();
125 //  All the iterator categories
126     test<input_iterator        <const int*> >();
127     test<forward_iterator      <const int*> >();
128     test<bidirectional_iterator<const int*> >();
129     test<random_access_iterator<const int*> >();
130     test<const int*>();
131     test<      int*>();
132 }
133 
BOOST_AUTO_TEST_CASE(test_main)134 BOOST_AUTO_TEST_CASE( test_main )
135 {
136   test_transform_exclusive_scan_init();
137 }
138