Debian Codegraph @

libstdc++-v3/testsuite/20_util/specialized_algorithms/pstl/uninitialized_copy_move.cc

raw
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_copy_move.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_copy, uninitialized_copy_n, uninitialized_move, uninitialized_move_n
16
17 #include "pstl/pstl_test_config.h"
18
19 #ifdef PSTL_STANDALONE_TESTS
20 #include "pstl/execution"
21 #include "pstl/memory"
22 #else
23 #include <execution>
24 #include <memory>
25 #endif // PSTL_STANDALONE_TESTS
26
27 #include "pstl/test_utils.h"
28
29 using namespace TestUtils;
30
31 // function of checking correctness for uninitialized.construct.value
32 template <typename InputIterator, typename OutputIterator, typename Size>
33 bool
34 IsCheckValueCorrectness(InputIterator first1, OutputIterator first2, Size n)
35 {
36 for (Size i = 0; i < n; ++i, ++first1, ++first2)
37 {
38 if (*first1 != *first2)
39 {
40 return false;
41 }
42 }
43 return true;
44 }
45
46 struct test_uninitialized_copy_move
47 {
48 template <typename Policy, typename InputIterator, typename OutputIterator>
49 void
50 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, size_t n,
51 /*is_trivial<T>=*/std::false_type)
52 {
53 typedef typename std::iterator_traits<InputIterator>::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, out_first, n);
58
59 // reset counter of constructors
60 T::SetCount(0);
61 // run algorithm
62 std::uninitialized_copy(exec, first, last, out_first);
63 // compare counter of constructors to length of container
64 EXPECT_TRUE(T::Count() == n, "wrong uninitialized_copy");
65 // destroy objects for testing new algorithms on same memory
66 std::destroy_n(exec, out_first, n);
67
68 std::uninitialized_copy_n(exec, first, n, out_first);
69 EXPECT_TRUE(T::Count() == n, "wrong uninitialized_copy_n");
70 std::destroy_n(exec, out_first, n);
71
72 // For move
73 std::uninitialized_move(exec, first, last, out_first);
74 // compare counter of constructors to length of container
75 EXPECT_TRUE(T::MoveCount() == n, "wrong uninitialized_move");
76 // destroy objects for testing new algorithms on same memory
77 std::destroy_n(exec, out_first, n);
78
79 std::uninitialized_move_n(exec, first, n, out_first);
80 EXPECT_TRUE(T::MoveCount() == n, "wrong uninitialized_move_n");
81 std::destroy_n(exec, out_first, n);
82 }
83
84 #if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN)
85 template <typename InputIterator, typename OutputIterator>
86 void
87 operator()(__pstl::execution::unsequenced_policy, InputIterator first, InputIterator last, OutputIterator out_first,
88 size_t n, /*is_trivial<T>=*/std::true_type)
89 {
90 }
91 template <typename InputIterator, typename OutputIterator>
92 void
93 operator()(__pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last,
94 OutputIterator out_first, size_t n, /*is_trivial<T>=*/std::true_type)
95 {
96 }
97 #endif
98
99 template <typename Policy, typename InputIterator, typename OutputIterator>
100 void
101 operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, size_t n,
102 /*is_trivial<T>=*/std::true_type)
103 {
104 std::uninitialized_copy(exec, first, last, out_first);
105 EXPECT_TRUE(IsCheckValueCorrectness(first, out_first, n), "wrong uninitialized_copy");
106 std::destroy_n(exec, out_first, n);
107
108 std::uninitialized_copy_n(exec, first, n, out_first);
109 EXPECT_TRUE(IsCheckValueCorrectness(first, out_first, n), "wrong uninitialized_copy_n");
110 std::destroy_n(exec, out_first, n);
111
112 std::uninitialized_move(exec, first, last, out_first);
113 EXPECT_TRUE(IsCheckValueCorrectness(first, out_first, n), "wrong uninitialized_move");
114 std::destroy_n(exec, out_first, n);
115
116 std::uninitialized_move_n(exec, first, n, out_first);
117 EXPECT_TRUE(IsCheckValueCorrectness(first, out_first, n), "wrong uninitialized_move_n");
118 std::destroy_n(exec, out_first, n);
119 }
120 };
121
122 template <typename T>
123 void
124 test_uninitialized_copy_move_by_type()
125 {
126 std::size_t N = 100000;
127 for (size_t n = 0; n <= N; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
128 {
129 Sequence<T> in(n, [=](size_t k) -> T { return T(k); });
130 std::unique_ptr<T[]> p(new T[n]);
131 invoke_on_all_policies(test_uninitialized_copy_move(), in.begin(), in.end(), p.get(), n, std::conjunction<std::is_trivially_copyable<T>, std::is_trivially_default_constructible<T>>());
132 }
133 }
134
135 int
136 main()
137 {
138
139 // for trivial types
140 test_uninitialized_copy_move_by_type<int16_t>();
141 test_uninitialized_copy_move_by_type<float64_t>();
142
143 // for user-defined types
144 #if !defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) && !defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) && \
145 !defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN)
146 test_uninitialized_copy_move_by_type<Wrapper<int8_t>>();
147 #endif
148
149 std::cout << done() << std::endl;
150 return 0;
151 }