]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/std/numerics/numeric.ops/transform.exclusive.scan/transform_exclusive_scan_init_bop_uop.pass.cpp
Vendor import of libc++ trunk r305145:
[FreeBSD/FreeBSD.git] / test / std / numerics / numeric.ops / transform.exclusive.scan / transform_exclusive_scan_init_bop_uop.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <numeric>
11 // UNSUPPORTED: c++98, c++03, c++11, c++14
12
13 // template<class InputIterator, class OutputIterator, class T, 
14 //          class BinaryOperation, class UnaryOperation>
15 //   OutputIterator transform_exclusive_scan(InputIterator first, InputIterator last,
16 //                                           OutputIterator result, T init,
17 //                                           BinaryOperation binary_op,
18 //                                           UnaryOperation unary_op);
19
20
21 #include <numeric>
22 #include <vector>
23 #include <cassert>
24 #include <iostream>
25
26 #include "test_iterators.h"
27
28 template <class _Tp = void>
29 struct identity : std::unary_function<_Tp, _Tp>
30 {
31     constexpr const _Tp& operator()(const _Tp& __x) const { return __x;}
32 };
33
34 template <>
35 struct identity<void>
36 {
37     template <class _Tp>
38     constexpr auto operator()(_Tp&& __x) const
39     _NOEXCEPT_(noexcept(_VSTD::forward<_Tp>(__x)))
40     -> decltype        (_VSTD::forward<_Tp>(__x))
41         { return        _VSTD::forward<_Tp>(__x); }
42 };
43
44 template <class Iter1, class BOp, class UOp, class T, class Iter2>
45 void
46 test(Iter1 first, Iter1 last, BOp bop, UOp uop, T init, Iter2 rFirst, Iter2 rLast)
47 {
48     std::vector<typename std::iterator_traits<Iter1>::value_type> v;
49 //  Test not in-place
50     std::transform_exclusive_scan(first, last, std::back_inserter(v), init, bop, uop);
51     assert(std::equal(v.begin(), v.end(), rFirst, rLast));
52
53 //  Test in-place
54     v.clear();
55     v.assign(first, last);
56     std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), init, bop, uop);
57     assert(std::equal(v.begin(), v.end(), rFirst, rLast));
58 }
59
60
61 template <class Iter>
62 void
63 test()
64 {
65           int ia[]     = { 1,  3,  5,   7,   9};
66     const int pResI0[] = { 0,  1,  4,   9,  16};        // with identity
67     const int mResI0[] = { 0,  0,  0,   0,   0};        
68     const int pResN0[] = { 0, -1, -4,  -9, -16};        // with negate
69     const int mResN0[] = { 0,  0,  0,   0,   0};
70     const int pResI2[] = { 2,  3,  6,  11,  18};        // with identity
71     const int mResI2[] = { 2,  2,  6,  30, 210};        
72     const int pResN2[] = { 2,  1, -2,  -7, -14};        // with negate
73     const int mResN2[] = { 2, -2,  6, -30, 210};
74     const unsigned sa = sizeof(ia) / sizeof(ia[0]);
75     static_assert(sa == sizeof(pResI0) / sizeof(pResI0[0]));       // just to be sure
76     static_assert(sa == sizeof(mResI0) / sizeof(mResI0[0]));       // just to be sure
77     static_assert(sa == sizeof(pResN0) / sizeof(pResN0[0]));       // just to be sure
78     static_assert(sa == sizeof(mResN0) / sizeof(mResN0[0]));       // just to be sure
79     static_assert(sa == sizeof(pResI2) / sizeof(pResI2[0]));       // just to be sure
80     static_assert(sa == sizeof(mResI2) / sizeof(mResI2[0]));       // just to be sure
81     static_assert(sa == sizeof(pResN2) / sizeof(pResN2[0]));       // just to be sure
82     static_assert(sa == sizeof(mResN2) / sizeof(mResN2[0]));       // just to be sure
83
84     for (unsigned int i = 0; i < sa; ++i ) {
85         test(Iter(ia), Iter(ia + i), std::plus<>(),       identity<>(),    0, pResI0, pResI0 + i);
86         test(Iter(ia), Iter(ia + i), std::multiplies<>(), identity<>(),    0, mResI0, mResI0 + i);
87         test(Iter(ia), Iter(ia + i), std::plus<>(),       std::negate<>(), 0, pResN0, pResN0 + i);
88         test(Iter(ia), Iter(ia + i), std::multiplies<>(), std::negate<>(), 0, mResN0, mResN0 + i);
89         test(Iter(ia), Iter(ia + i), std::plus<>(),       identity<>(),    2, pResI2, pResI2 + i);
90         test(Iter(ia), Iter(ia + i), std::multiplies<>(), identity<>(),    2, mResI2, mResI2 + i);
91         test(Iter(ia), Iter(ia + i), std::plus<>(),       std::negate<>(), 2, pResN2, pResN2 + i);
92         test(Iter(ia), Iter(ia + i), std::multiplies<>(), std::negate<>(), 2, mResN2, mResN2 + i);
93         }
94 }
95
96 int triangle(int n) { return n*(n+1)/2; }
97
98 //  Basic sanity
99 void basic_tests()
100 {
101     {
102     std::vector<int> v(10);
103     std::fill(v.begin(), v.end(), 3);
104     std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 50, std::plus<>(), identity<>());
105     for (size_t i = 0; i < v.size(); ++i)
106         assert(v[i] == 50 + (int) i * 3);
107     }
108
109     {
110     std::vector<int> v(10);
111     std::iota(v.begin(), v.end(), 0);
112     std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 30, std::plus<>(), identity<>());
113     for (size_t i = 0; i < v.size(); ++i)
114         assert(v[i] == 30 + triangle(i-1));
115     }
116
117     {
118     std::vector<int> v(10);
119     std::iota(v.begin(), v.end(), 1);
120     std::transform_exclusive_scan(v.begin(), v.end(), v.begin(), 40, std::plus<>(), identity<>());
121     for (size_t i = 0; i < v.size(); ++i)
122         assert(v[i] == 40 + triangle(i));
123     }
124
125     {
126     std::vector<int> v, res;
127     std::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 40, std::plus<>(), identity<>());
128     assert(res.empty());
129     }
130
131 //  Make sure that the calculations are done using the init typedef
132     {
133     std::vector<unsigned char> v(10);
134     std::iota(v.begin(), v.end(), 1);
135     std::vector<int> res;
136     std::transform_exclusive_scan(v.begin(), v.end(), std::back_inserter(res), 1, std::multiplies<>(), identity<>());
137
138     assert(res.size() == 10);
139     int j = 1;
140     assert(res[0] == 1);
141     for (size_t i = 1; i < res.size(); ++i)
142     {
143         j *= i;
144         assert(res[i] == j);
145     }
146     }
147 }
148
149 int main()
150 {
151     basic_tests();
152     
153 //  All the iterator categories
154     test<input_iterator        <const int*> >();
155     test<forward_iterator      <const int*> >();
156     test<bidirectional_iterator<const int*> >();
157     test<random_access_iterator<const int*> >();
158     test<const int*>();
159     test<      int*>();
160 }