Changeset View
Changeset View
Standalone View
Standalone View
test/std/algorithms/alg.nonmodifying/for_each.pass.cpp
- This file was moved from test/test_for_each.cpp.
// -*- C++ -*- | // -*- C++ -*- | ||||
//===-- test_for_each.cpp -------------------------------------------------===// | //===-- for_each.pass.cpp -------------------------------------------------===// | ||||
// | // | ||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
// See https://llvm.org/LICENSE.txt for license information. | // See https://llvm.org/LICENSE.txt for license information. | ||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
#include "support/pstl_test_config.h" | |||||
#ifdef PSTL_STANDALONE_TESTS | |||||
#include "pstl/execution" | #include "pstl/execution" | ||||
#include "pstl/algorithm" | #include "pstl/algorithm" | ||||
#include "utils.h" | #else | ||||
#include <execution> | |||||
#include <algorithm> | |||||
#endif // PSTL_STANDALONE_TESTS | |||||
#include "support/parallel_utils.h" | |||||
using namespace TestUtils; | #include <iostream> | ||||
using namespace Parallel_TestUtils; | |||||
template <typename Type> | template <typename Type> | ||||
struct Gen | struct Gen | ||||
{ | { | ||||
Type | Type | ||||
operator()(std::size_t k) | operator()(std::size_t k) | ||||
{ | { | ||||
return Type(k % 5 != 1 ? 3 * k - 7 : 0); | return Type(k % 5 != 1 ? 3 * k - 7 : 0); | ||||
Show All 16 Lines | |||||
{ | { | ||||
template <typename Policy, typename Iterator, typename Size> | template <typename Policy, typename Iterator, typename Size> | ||||
void | void | ||||
operator()(Policy&& exec, Iterator first, Iterator last, Iterator expected_first, Iterator expected_last, Size n) | operator()(Policy&& exec, Iterator first, Iterator last, Iterator expected_first, Iterator expected_last, Size n) | ||||
{ | { | ||||
typedef typename std::iterator_traits<Iterator>::value_type T; | typedef typename std::iterator_traits<Iterator>::value_type T; | ||||
// Try for_each | // Try for_each | ||||
std::for_each(expected_first, expected_last, Flip<T>(1)); | std::for_each(__pstl::execution::seq, expected_first, expected_last, Flip<T>(1)); | ||||
for_each(exec, first, last, Flip<T>(1)); | for_each(exec, first, last, Flip<T>(1)); | ||||
EXPECT_EQ_N(expected_first, first, n, "wrong effect from for_each"); | EXPECT_EQ_N(expected_first, first, n, "wrong effect from for_each"); | ||||
// Try for_each_n | // Try for_each_n | ||||
std::for_each_n(pstl::execution::seq, expected_first, n, Flip<T>(1)); | std::for_each_n(__pstl::execution::seq, expected_first, n, Flip<T>(1)); | ||||
std::for_each_n(__pstl::execution::seq, expected_first, n, Flip<T>(1)); | |||||
for_each_n(exec, first, n, Flip<T>(1)); | for_each_n(exec, first, n, Flip<T>(1)); | ||||
EXPECT_EQ_N(expected_first, first, n, "wrong effect from for_each_n"); | EXPECT_EQ_N(expected_first, first, n, "wrong effect from for_each_n"); | ||||
} | } | ||||
}; | }; | ||||
template <typename T> | template <typename T> | ||||
void | void | ||||
test() | test() | ||||
{ | { | ||||
for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) | for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) | ||||
{ | { | ||||
Sequence<T> inout(n, Gen<T>()); | Sequence<T> inout(n, Gen<T>()); | ||||
Sequence<T> expected(n, Gen<T>()); | Sequence<T> expected(n, Gen<T>()); | ||||
Show All 32 Lines |