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.5 [transform.reduce]
22 
23 #include <numeric>
24 #include <iterator>
25 #include <testsuite_hooks.h>
26 #include <testsuite_iterators.h>
27 
28 int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
29 double b[] = {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5};
30 
31 using __gnu_test::test_container;
32 using __gnu_test::input_iterator_wrapper;
33 
34 /*
35 template<class InputIterator1, class InputIterator2, class T>
36   T transform_reduce(InputIterator1, InputIterator1, InputIterator2, T);
37 */
38 void
test01()39 test01()
40 {
41   auto res = std::transform_reduce(std::begin(a), std::end(a), std::begin(b),
42 				   1.0f);
43   static_assert(std::is_same_v<decltype(res), float>);
44   VERIFY( res == (float)(1 + 0.5 + 1 + 1.5 + 2 + 2.5 + 3 + 3.5 + 4 + 4.5 + 5) );
45 
46   test_container<int, input_iterator_wrapper> ca(a);
47   test_container<double, input_iterator_wrapper> cb(b);
48 
49   auto res2 = std::transform_reduce(ca.begin(), ca.end(), cb.begin(),
50 				   1.0f);
51   static_assert(std::is_same_v<decltype(res2), float>);
52   VERIFY( res2 == res );
53 }
54 
55 /*
56 template<class InputIterator1, class InputIterator2, class T,
57 	 class BinaryOperation1, class BinaryOperation2>
58   T transform_reduce(InputIterator1, InputIterator1, InputIterator2, T,
59 		     BinaryOperation1, BinaryOperation2);
60 */
61 void
test02()62 test02()
63 {
64   auto res = std::transform_reduce(std::begin(a), std::end(a), std::begin(b),
65 				    1L, std::multiplies<>(), std::plus<int>());
66   static_assert(std::is_same_v<decltype(res), long>);
67   VERIFY( res == (1L * 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10) );
68 
69   test_container<int, input_iterator_wrapper> ca(a);
70   test_container<double, input_iterator_wrapper> cb(b);
71 
72   auto res2 = std::transform_reduce(ca.begin(), ca.end(), cb.begin(),
73 				    1L, std::multiplies<>(), std::plus<int>());
74   static_assert(std::is_same_v<decltype(res2), long>);
75   VERIFY( res2 == res );
76 }
77 
78 /*
79 template<class InputIterator, class T, class BinaryOperation,
80 	 class UnaryOperation>
81   T transform_reduce(InputIterator, InputIterator, T,
82 		     BinaryOperation, UnaryOperation);
83 */
84 void
test03()85 test03()
86 {
87   auto res = std::transform_reduce(std::begin(a), std::end(a), 10.0,
88 				   std::plus<>(),
89 				   [](int i) { return i * i; });
90   static_assert(std::is_same_v<decltype(res), double>);
91   VERIFY( res == (10.0 + 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100) );
92 
93   test_container<int, input_iterator_wrapper> ca(a);
94   test_container<double, input_iterator_wrapper> cb(b);
95 
96   auto res2 = std::transform_reduce(ca.begin(), ca.end(), 10.0,
97 				   std::plus<>(),
98 				   [](int i) { return i * i; });
99   static_assert(std::is_same_v<decltype(res2), double>);
100   VERIFY( res2 == (10.0 + 1 + 4 + 9 + 16 + 25 + 36 + 49 + 64 + 81 + 100) );
101 }
102 
103 int
main()104 main()
105 {
106   test01();
107   test02();
108   test03();
109 }
110