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