]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/26_numerics/inclusive_scan/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / inclusive_scan / 1.cc
1 // { dg-do run { target c++17 } }
2
3 // Copyright (C) 2019-2023 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // C++17 29.8.8 [inclusive.scan]
21
22 #include <numeric>
23 #include <iterator>
24 #include <testsuite_hooks.h>
25 #include <testsuite_iterators.h>
26
27 int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
28
29 using __gnu_test::test_container;
30 using __gnu_test::input_iterator_wrapper;
31 using __gnu_test::output_iterator_wrapper;
32
33 /*
34 template<class InputIterator, class OutputIterator>
35 OutputIterator
36 inclusive_scan(InputIterator, InputIterator, OutputIterator);
37 */
38 void
39 test01()
40 {
41 int out[10];
42 test_container<int, output_iterator_wrapper> co(out);
43 test_container<int, input_iterator_wrapper> ca(a);
44 auto end = std::inclusive_scan(ca.begin(), ca.end(), co.begin());
45 static_assert(std::is_same_v<decltype(end), decltype(co.begin())>);
46 VERIFY( end.ptr == out+10 );
47 VERIFY( out[0] == 1 );
48 VERIFY( out[1] == (1+2) );
49 VERIFY( out[2] == (1+2+3) );
50 VERIFY( out[3] == (1+2+3+4) );
51 VERIFY( out[4] == (1+2+3+4+5) );
52 VERIFY( out[5] == (1+2+3+4+5+6) );
53 VERIFY( out[6] == (1+2+3+4+5+6+7) );
54 VERIFY( out[7] == (1+2+3+4+5+6+7+8) );
55 VERIFY( out[8] == (1+2+3+4+5+6+7+8+9) );
56 VERIFY( out[9] == (1+2+3+4+5+6+7+8+9+10) );
57 }
58
59 /*
60 template<class InputIterator, class OutputIterator, class BinaryOperation>
61 OutputIterator
62 inclusive_scan(InputIterator, InputIterator, OutputIterator,
63 BinaryOperation);
64 */
65 void
66 test02()
67 {
68 int out[10];
69 test_container<int, output_iterator_wrapper> co(out);
70 test_container<int, input_iterator_wrapper> ca(a);
71 auto end = std::inclusive_scan(ca.begin(), ca.end(), co.begin(),
72 [](int i, int j) { return 2*i + 2*j; });
73 static_assert(std::is_same_v<decltype(end), decltype(co.begin())>);
74 VERIFY( end.ptr == out+10 );
75 VERIFY( out[0] == 1 );
76 VERIFY( out[1] == (2*1+2*2) );
77 VERIFY( out[2] == (2*6+2*3) );
78 VERIFY( out[3] == (2*18+2*4) );
79 VERIFY( out[4] == (2*44+2*5) );
80 VERIFY( out[5] == (2*98+2*6));
81 VERIFY( out[6] == (2*208+2*7) );
82 VERIFY( out[7] == (2*430+2*8) );
83 VERIFY( out[8] == (2*876+2*9) );
84 VERIFY( out[9] == (2*1770+2*10) );
85 }
86
87 /*
88 template<class InputIterator, class OutputIterator, class BinaryOperation, T>
89 OutputIterator
90 inclusive_scan(InputIterator, InputIterator, OutputIterator,
91 BinaryOperation, T);
92 */
93 void
94 test03()
95 {
96 int out[10];
97 test_container<int, output_iterator_wrapper> co(out);
98 test_container<int, input_iterator_wrapper> ca(a);
99 auto end = std::inclusive_scan(ca.begin(), ca.end(), co.begin(),
100 [](int i, int j) { return 2*i + 2*j; },
101 1);
102 static_assert(std::is_same_v<decltype(end), decltype(co.begin())>);
103 VERIFY( end.ptr == out+10 );
104 VERIFY( out[0] == 4 );
105 VERIFY( out[1] == (2*4+2*2) );
106 VERIFY( out[2] == (2*12+2*3) );
107 VERIFY( out[3] == (2*30+2*4) );
108 VERIFY( out[4] == (2*68+2*5) );
109 VERIFY( out[5] == (2*146+2*6) );
110 VERIFY( out[6] == (2*304+2*7));
111 VERIFY( out[7] == (2*622+2*8) );
112 VERIFY( out[8] == (2*1260+2*9) );
113 VERIFY( out[9] == (2*2538+2*10) );
114 }
115
116 int
117 main()
118 {
119 test01();
120 test02();
121 test03();
122 }