]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/26_numerics/reduce/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / reduce / 1.cc
1 // { dg-options "-std=gnu++17" }
2 // { dg-do run { target c++17 } }
3
4 // Copyright (C) 2019-2020 Free Software Foundation, Inc.
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.3 [reduce]
22
23 #include <numeric>
24 #include <iterator>
25 #include <testsuite_hooks.h>
26 #include <testsuite_iterators.h>
27
28 /*
29 template<class InputIterator>
30 iterator_traits<InputIterator>::value_type
31 reduce(InputIterator, InputIterator);
32 */
33 void
34 test01()
35 {
36 using __gnu_test::test_container;
37 using __gnu_test::input_iterator_wrapper;
38 int array[5] = { 1, 2, 3, 4, 5 };
39 test_container<int, input_iterator_wrapper> con(array);
40 int res = std::reduce(con.begin(), con.end());
41 VERIFY( res == 15 );
42 }
43
44 /*
45 template<class InputIterator, class T>
46 T reduce(InputIterator, InputIterator, T);
47 */
48 void
49 test02()
50 {
51 bool b[] = {true, false, true, true, false, true, false, true, true, false};
52 int res = std::reduce(std::begin(b), std::end(b), 100);
53 VERIFY( res == 106 );
54 }
55
56 /*
57 template<class InputIterator, class T>
58 T reduce(InputIterator, InputIterator, T);
59 template<class InputIterator, class T, class BinaryOperation>
60 T reduce(InputIterator, InputIterator, T, BinaryOperation);
61 */
62 void
63 test03()
64 {
65 int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
66
67 auto res = std::reduce(std::begin(a), std::end(a), (short)11);
68 static_assert(std::is_same_v<decltype(res), short>);
69 VERIFY( res == 66 );
70
71 auto res2 = std::reduce(std::begin(a), std::end(a), -1l, std::multiplies<>());
72 static_assert(std::is_same_v<decltype(res2), long>);
73 VERIFY( res2 == -3628800 );
74 }
75
76 int
77 main()
78 {
79 test01();
80 test02();
81 test03();
82 }