1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // <numeric> 10 // UNSUPPORTED: clang-8 11 // UNSUPPORTED: gcc-9 12 13 // Became constexpr in C++20 14 // template <InputIterator Iter1, InputIterator Iter2, MoveConstructible T> 15 // requires HasMultiply<Iter1::reference, Iter2::reference> 16 // && HasPlus<T, HasMultiply<Iter1::reference, Iter2::reference>::result_type> 17 // && HasAssign<T, 18 // HasPlus<T, 19 // HasMultiply<Iter1::reference, 20 // Iter2::reference>::result_type>::result_type> 21 // T 22 // inner_product(Iter1 first1, Iter1 last1, Iter2 first2, T init); 23 24 #include <numeric> 25 #include <cassert> 26 27 #include "test_macros.h" 28 #include "test_iterators.h" 29 30 template <class Iter1, class Iter2, class T> 31 TEST_CONSTEXPR_CXX20 void 32 test(Iter1 first1, Iter1 last1, Iter2 first2, T init, T x) 33 { 34 assert(std::inner_product(first1, last1, first2, init) == x); 35 } 36 37 template <class Iter1, class Iter2> 38 TEST_CONSTEXPR_CXX20 void 39 test() 40 { 41 int a[] = {1, 2, 3, 4, 5, 6}; 42 int b[] = {6, 5, 4, 3, 2, 1}; 43 unsigned sa = sizeof(a) / sizeof(a[0]); 44 test(Iter1(a), Iter1(a), Iter2(b), 0, 0); 45 test(Iter1(a), Iter1(a), Iter2(b), 10, 10); 46 test(Iter1(a), Iter1(a+1), Iter2(b), 0, 6); 47 test(Iter1(a), Iter1(a+1), Iter2(b), 10, 16); 48 test(Iter1(a), Iter1(a+2), Iter2(b), 0, 16); 49 test(Iter1(a), Iter1(a+2), Iter2(b), 10, 26); 50 test(Iter1(a), Iter1(a+sa), Iter2(b), 0, 56); 51 test(Iter1(a), Iter1(a+sa), Iter2(b), 10, 66); 52 } 53 54 TEST_CONSTEXPR_CXX20 bool 55 test() 56 { 57 test<cpp17_input_iterator<const int*>, cpp17_input_iterator<const int*> >(); 58 test<cpp17_input_iterator<const int*>, forward_iterator<const int*> >(); 59 test<cpp17_input_iterator<const int*>, bidirectional_iterator<const int*> >(); 60 test<cpp17_input_iterator<const int*>, random_access_iterator<const int*> >(); 61 test<cpp17_input_iterator<const int*>, const int*>(); 62 63 test<forward_iterator<const int*>, cpp17_input_iterator<const int*> >(); 64 test<forward_iterator<const int*>, forward_iterator<const int*> >(); 65 test<forward_iterator<const int*>, bidirectional_iterator<const int*> >(); 66 test<forward_iterator<const int*>, random_access_iterator<const int*> >(); 67 test<forward_iterator<const int*>, const int*>(); 68 69 test<bidirectional_iterator<const int*>, cpp17_input_iterator<const int*> >(); 70 test<bidirectional_iterator<const int*>, forward_iterator<const int*> >(); 71 test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >(); 72 test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >(); 73 test<bidirectional_iterator<const int*>, const int*>(); 74 75 test<random_access_iterator<const int*>, cpp17_input_iterator<const int*> >(); 76 test<random_access_iterator<const int*>, forward_iterator<const int*> >(); 77 test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >(); 78 test<random_access_iterator<const int*>, random_access_iterator<const int*> >(); 79 test<random_access_iterator<const int*>, const int*>(); 80 81 test<const int*, cpp17_input_iterator<const int*> >(); 82 test<const int*, forward_iterator<const int*> >(); 83 test<const int*, bidirectional_iterator<const int*> >(); 84 test<const int*, random_access_iterator<const int*> >(); 85 test<const int*, const int*>(); 86 87 return true; 88 } 89 90 int main(int, char**) 91 { 92 test(); 93 #if TEST_STD_VER > 17 94 static_assert(test()); 95 #endif 96 return 0; 97 } 98