| 1 |
// -*- C++ -*- |
| 2 |
// { dg-options "-ltbb" } |
| 3 |
// { dg-do run { target c++17 } } |
| 4 |
// { dg-timeout-factor 3 } |
| 5 |
// { dg-require-effective-target tbb_backend } |
| 6 |
|
| 7 |
//===-- uninitialized_fill_destroy.pass.cpp -------------------------------===// |
| 8 |
// |
| 9 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 10 |
// See https://llvm.org/LICENSE.txt for license information. |
| 11 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 12 |
// |
| 13 |
//===----------------------------------------------------------------------===// |
| 14 |
|
| 15 |
#include "pstl/pstl_test_config.h" |
| 16 |
|
| 17 |
#ifdef PSTL_STANDALONE_TESTS |
| 18 |
|
| 19 |
#include "pstl/execution" |
| 20 |
#include "pstl/memory" |
| 21 |
#include "pstl/algorithm" |
| 22 |
#else |
| 23 |
#include <execution> |
| 24 |
#include <algorithm> |
| 25 |
#endif // PSTL_STANDALONE_TESTS |
| 26 |
|
| 27 |
#include "pstl/test_utils.h" |
| 28 |
|
| 29 |
using namespace TestUtils; |
| 30 |
|
| 31 |
struct test_uninitialized_fill_destroy |
| 32 |
{ |
| 33 |
template <typename Policy, typename Iterator, typename T> |
| 34 |
void |
| 35 |
operator()(Policy&& exec, Iterator first, Iterator last, const T& in, std::size_t n, std::false_type) |
| 36 |
{ |
| 37 |
using namespace std; |
| 38 |
{ |
| 39 |
T::SetCount(0); |
| 40 |
uninitialized_fill(exec, first, last, in); |
| 41 |
size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; }); |
| 42 |
EXPECT_TRUE(n == count, "wrong work of uninitialized_fill"); |
| 43 |
destroy(exec, first, last); |
| 44 |
EXPECT_TRUE(T::Count() == 0, "wrong work of destroy"); |
| 45 |
} |
| 46 |
|
| 47 |
{ |
| 48 |
auto res = uninitialized_fill_n(exec, first, n, in); |
| 49 |
EXPECT_TRUE(res == last, "wrong result of uninitialized_fill_n"); |
| 50 |
size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; }); |
| 51 |
EXPECT_TRUE(n == count, "wrong work of uninitialized_fill_n"); |
| 52 |
destroy_n(exec, first, n); |
| 53 |
EXPECT_TRUE(T::Count() == 0, "wrong work of destroy_n"); |
| 54 |
} |
| 55 |
} |
| 56 |
template <typename Policy, typename Iterator, typename T> |
| 57 |
void |
| 58 |
operator()(Policy&& exec, Iterator first, Iterator last, const T& in, std::size_t n, std::true_type) |
| 59 |
{ |
| 60 |
using namespace std; |
| 61 |
{ |
| 62 |
destroy(exec, first, last); |
| 63 |
uninitialized_fill(exec, first, last, in); |
| 64 |
size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; }); |
| 65 |
EXPECT_EQ(n, count, "wrong work of uninitialized:_fill"); |
| 66 |
} |
| 67 |
{ |
| 68 |
destroy_n(exec, first, n); |
| 69 |
auto res = uninitialized_fill_n(exec, first, n, in); |
| 70 |
size_t count = count_if(first, last, [&in](T& x) -> bool { return x == in; }); |
| 71 |
EXPECT_EQ(n, count, "wrong work of uninitialized_fill_n"); |
| 72 |
EXPECT_TRUE(res == last, "wrong result of uninitialized_fill_n"); |
| 73 |
} |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
template <typename T> |
| 78 |
void |
| 79 |
test_uninitialized_fill_destroy_by_type() |
| 80 |
{ |
| 81 |
std::size_t N = 100000; |
| 82 |
for (size_t n = 0; n <= N; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
| 83 |
{ |
| 84 |
std::unique_ptr<T[]> p(new T[n]); |
| 85 |
invoke_on_all_policies(test_uninitialized_fill_destroy(), p.get(), std::next(p.get(), n), T(), n, |
| 86 |
std::conjunction<std::is_trivially_copyable<T>, std::is_trivially_default_constructible<T>>()); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
int |
| 91 |
main() |
| 92 |
{ |
| 93 |
// for trivial types |
| 94 |
test_uninitialized_fill_destroy_by_type<int32_t>(); |
| 95 |
test_uninitialized_fill_destroy_by_type<float64_t>(); |
| 96 |
|
| 97 |
// for user-defined types |
| 98 |
test_uninitialized_fill_destroy_by_type<Wrapper<std::string>>(); |
| 99 |
test_uninitialized_fill_destroy_by_type<Wrapper<int8_t*>>(); |
| 100 |
std::cout << done() << std::endl; |
| 101 |
|
| 102 |
return 0; |
| 103 |
} |