| 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_construct.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 |
// Tests for uninitialized_default_construct, uninitialized_default_construct_n, |
| 16 |
// uninitialized_value_construct, uninitialized_value_construct_n |
| 17 |
|
| 18 |
#include "pstl/pstl_test_config.h" |
| 19 |
|
| 20 |
#ifdef PSTL_STANDALONE_TESTS |
| 21 |
#include "pstl/execution" |
| 22 |
#include "pstl/memory" |
| 23 |
#else |
| 24 |
#include <execution> |
| 25 |
#include <memory> |
| 26 |
#endif // PSTL_STANDALONE_TESTS |
| 27 |
|
| 28 |
#include "pstl/test_utils.h" |
| 29 |
|
| 30 |
using namespace TestUtils; |
| 31 |
|
| 32 |
// function of checking correctness for uninitialized.construct.value |
| 33 |
template <typename T, typename Iterator> |
| 34 |
bool |
| 35 |
IsCheckValueCorrectness(Iterator begin, Iterator end) |
| 36 |
{ |
| 37 |
for (; begin != end; ++begin) |
| 38 |
{ |
| 39 |
if (*begin != T()) |
| 40 |
{ |
| 41 |
return false; |
| 42 |
} |
| 43 |
} |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
struct test_uninit_construct |
| 48 |
{ |
| 49 |
template <typename Policy, typename Iterator> |
| 50 |
void |
| 51 |
operator()(Policy&& exec, Iterator begin, Iterator end, size_t n, /*is_trivial<T>=*/std::false_type) |
| 52 |
{ |
| 53 |
typedef typename std::iterator_traits<Iterator>::value_type T; |
| 54 |
// it needs for cleaning memory that was filled by default constructors in unique_ptr<T[]> p(new T[n]) |
| 55 |
// and for cleaning memory after last calling of uninitialized_value_construct_n. |
| 56 |
// It is important for non-trivial types |
| 57 |
std::destroy_n(exec, begin, n); |
| 58 |
|
| 59 |
// reset counter of constructors |
| 60 |
T::SetCount(0); |
| 61 |
// run algorithm |
| 62 |
std::uninitialized_default_construct(exec, begin, end); |
| 63 |
// compare counter of constructors to length of container |
| 64 |
EXPECT_TRUE(T::Count() == n, "wrong uninitialized_default_construct"); |
| 65 |
// destroy objects for testing new algorithms on same memory |
| 66 |
std::destroy(exec, begin, end); |
| 67 |
|
| 68 |
std::uninitialized_default_construct_n(exec, begin, n); |
| 69 |
EXPECT_TRUE(T::Count() == n, "wrong uninitialized_default_construct_n"); |
| 70 |
std::destroy_n(exec, begin, n); |
| 71 |
|
| 72 |
std::uninitialized_value_construct(exec, begin, end); |
| 73 |
EXPECT_TRUE(T::Count() == n, "wrong uninitialized_value_construct"); |
| 74 |
std::destroy(exec, begin, end); |
| 75 |
|
| 76 |
std::uninitialized_value_construct_n(exec, begin, n); |
| 77 |
EXPECT_TRUE(T::Count() == n, "wrong uninitialized_value_construct_n"); |
| 78 |
} |
| 79 |
|
| 80 |
template <typename Policy, typename Iterator> |
| 81 |
void |
| 82 |
operator()(Policy&& exec, Iterator begin, Iterator end, size_t n, /*is_trivial<T>=*/std::true_type) |
| 83 |
{ |
| 84 |
typedef typename std::iterator_traits<Iterator>::value_type T; |
| 85 |
|
| 86 |
std::uninitialized_default_construct(exec, begin, end); |
| 87 |
std::destroy(exec, begin, end); |
| 88 |
|
| 89 |
std::uninitialized_default_construct_n(exec, begin, n); |
| 90 |
std::destroy_n(exec, begin, n); |
| 91 |
|
| 92 |
std::uninitialized_value_construct(exec, begin, end); |
| 93 |
// check correctness for uninitialized.construct.value |
| 94 |
EXPECT_TRUE(IsCheckValueCorrectness<T>(begin, end), "wrong uninitialized_value_construct"); |
| 95 |
std::destroy(exec, begin, end); |
| 96 |
|
| 97 |
std::uninitialized_value_construct_n(exec, begin, n); |
| 98 |
EXPECT_TRUE(IsCheckValueCorrectness<T>(begin, end), "wrong uninitialized_value_construct_n"); |
| 99 |
std::destroy_n(exec, begin, n); |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
template <typename T> |
| 104 |
void |
| 105 |
test_uninit_construct_by_type() |
| 106 |
{ |
| 107 |
std::size_t N = 100000; |
| 108 |
for (size_t n = 0; n <= N; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
| 109 |
{ |
| 110 |
std::unique_ptr<T[]> p(new T[n]); |
| 111 |
invoke_on_all_policies(test_uninit_construct(), p.get(), std::next(p.get(), n), n, std::conjunction<std::is_trivially_copyable<T>, std::is_trivially_default_constructible<T>>()); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
int |
| 116 |
main() |
| 117 |
{ |
| 118 |
|
| 119 |
// for user-defined types |
| 120 |
#if !defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN) |
| 121 |
test_uninit_construct_by_type<Wrapper<int32_t>>(); |
| 122 |
test_uninit_construct_by_type<Wrapper<std::vector<std::string>>>(); |
| 123 |
#endif |
| 124 |
|
| 125 |
// for trivial types |
| 126 |
test_uninit_construct_by_type<int8_t>(); |
| 127 |
test_uninit_construct_by_type<float64_t>(); |
| 128 |
|
| 129 |
std::cout << done() << std::endl; |
| 130 |
return 0; |
| 131 |
} |