]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/pstl/alg_modifying_operations/swap_ranges.cc
2 // { dg-options "-std=gnu++17 -ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-timeout-factor 3 }
5 // { dg-require-effective-target tbb-backend }
7 //===-- swap_ranges.pass.cpp ----------------------------------------------===//
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
13 //===----------------------------------------------------------------------===//
15 #include "pstl/pstl_test_config.h"
17 #ifdef PSTL_STANDALONE_TESTS
20 #include "pstl/execution"
21 #include "pstl/algorithm"
25 #endif // PSTL_STANDALONE_TESTS
27 #include "pstl/test_utils.h"
29 using namespace TestUtils
;
35 std::size_t number_of_swaps
= 0;
37 explicit wrapper(T t_
) : t(t_
) {}
45 operator==(const wrapper
<T
>& a
) const
53 swap(wrapper
<T
>& a
, wrapper
<T
>& b
)
71 struct check_swap
<wrapper
<T
>>
74 operator()(wrapper
<T
>& a
)
76 bool temp
= (a
.number_of_swaps
== 1);
77 a
.number_of_swaps
= 0;
82 struct test_one_policy
84 template <typename ExecutionPolicy
, typename Iterator1
, typename Iterator2
>
86 operator()(ExecutionPolicy
&& exec
, Iterator1 data_b
, Iterator1 data_e
, Iterator2 actual_b
, Iterator2 actual_e
)
89 using T_ref
= typename iterator_traits
<Iterator1
>::reference
;
90 using T
= typename iterator_traits
<Iterator1
>::value_type
;
92 iota(data_b
, data_e
, 0);
93 iota(actual_b
, actual_e
, std::distance(data_b
, data_e
));
95 Iterator2 actual_return
= swap_ranges(exec
, data_b
, data_e
, actual_b
);
96 bool check_return
= (actual_return
== actual_e
);
97 EXPECT_TRUE(check_return
, "wrong result of swap_ranges");
101 bool check
= all_of(actual_b
, actual_e
, [&i
](T_ref a
) { return a
== T(i
++); }) &&
102 all_of(data_b
, data_e
, [&i
](T_ref a
) { return a
== T(i
++); });
104 EXPECT_TRUE(check
, "wrong effect of swap_ranges");
109 all_of(data_b
, data_e
, check_swap
<T
>()) && all_of(actual_b
, actual_e
, check_swap
<T
>());
110 EXPECT_TRUE(swap_check
, "wrong effect of swap_ranges swap check");
116 template <typename T
>
120 const std::size_t max_len
= 100000;
122 Sequence
<T
> data(max_len
);
123 Sequence
<T
> actual(max_len
);
125 for (std::size_t len
= 0; len
< max_len
; len
= len
<= 16 ? len
+ 1 : std::size_t(3.1415 * len
))
127 invoke_on_all_policies(test_one_policy(), data
.begin(), data
.begin() + len
, actual
.begin(),
128 actual
.begin() + len
);
135 test
<wrapper
<uint16_t>>();
136 test
<wrapper
<float64_t
>>();
140 std::cout
<< done() << std::endl
;