70 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410 11// UNSUPPORTED: libcpp-has-no-incomplete-pstl12 13// <numeric>14 15// template<class ExecutionPolicy,16// class ForwardIterator, class T,17// class BinaryOperation, class UnaryOperation>18// T transform_reduce(ExecutionPolicy&& exec,19// ForwardIterator first, ForwardIterator last,20// T init, BinaryOperation binary_op, UnaryOperation unary_op);21 22#include <numeric>23#include <string>24#include <vector>25 26#include "MoveOnly.h"27#include "test_execution_policies.h"28#include "test_iterators.h"29#include "test_macros.h"30 31template <class Iter1, class ValueT>32struct Test {33 template <class Policy>34 void operator()(Policy&& policy) {35 for (const auto& pair : {std::pair{0, 34}, {1, 35}, {2, 37}, {100, 5084}, {350, 61459}}) {36 auto [size, expected] = pair;37 std::vector<int> a(size);38 for (int i = 0; i != size; ++i)39 a[i] = i;40 41 decltype(auto) ret = std::transform_reduce(42 policy,43 Iter1(std::data(a)),44 Iter1(std::data(a) + std::size(a)),45 ValueT(34),46 [check = std::string("Banane")](ValueT i, ValueT j) {47 assert(check == "Banane"); // ensure that no double-moves happen48 return i + j;49 },50 [check = std::string("Banane")](ValueT i) {51 assert(check == "Banane"); // ensure that no double-moves happen52 return i + 1;53 });54 static_assert(std::is_same_v<decltype(ret), ValueT>);55 assert(ret == expected);56 }57 }58};59 60int main(int, char**) {61 types::for_each(types::forward_iterator_list<int*>{}, types::apply_type_identity{[](auto v) {62 using Iter2 = typename decltype(v)::type;63 types::for_each(64 types::type_list<int, MoveOnly>{},65 TestIteratorWithPolicies<types::partial_instantiation<Test, Iter2>::template apply>{});66 }});67 68 return 0;69}70