1 // -*- C++ -*- 2 //===-- rotate_copy.pass.cpp ----------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "support/pstl_test_config.h" 11 12 #include <iterator> 13 #include <execution> 14 #include <algorithm> 15 16 #include "support/utils.h" 17 18 using namespace TestUtils; 19 20 template <typename T> 21 struct wrapper; 22 23 template <typename T> 24 bool 25 compare(const wrapper<T>& a, const wrapper<T>& b) 26 { 27 return a.t == b.t; 28 } 29 30 template <typename T> 31 bool 32 compare(const T& a, const T& b) 33 { 34 return a == b; 35 } 36 37 template <typename T> 38 struct wrapper 39 { 40 explicit wrapper(T t_) : t(t_) {} 41 wrapper& 42 operator=(const T& t_) 43 { 44 t = t_; 45 return *this; 46 } 47 friend bool 48 compare<T>(const wrapper<T>& a, const wrapper<T>& b); 49 50 private: 51 T t; 52 }; 53 54 template <typename T, typename It1, typename It2> 55 struct comparator 56 { 57 using T1 = typename std::iterator_traits<It1>::value_type; 58 using T2 = typename std::iterator_traits<It2>::value_type; 59 bool 60 operator()(T1 a, T2 b) 61 { 62 T temp = a; 63 return compare(temp, b); 64 } 65 }; 66 67 struct test_one_policy 68 { 69 70 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 71 _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN // dummy specialization by policy type, in case of broken configuration 72 template <typename Iterator1, typename Iterator2> 73 typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type 74 operator()(pstl::execution::unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, 75 Iterator2 actual_e, std::size_t shift) 76 { 77 } 78 template <typename Iterator1, typename Iterator2> 79 typename std::enable_if<is_same_iterator_category<Iterator1, std::random_access_iterator_tag>::value, void>::type 80 operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, 81 Iterator2 actual_e, std::size_t shift) 82 { 83 } 84 #endif 85 86 template <typename ExecutionPolicy, typename Iterator1, typename Iterator2> 87 void 88 operator()(ExecutionPolicy&& exec, Iterator1 data_b, Iterator1 data_e, Iterator2 actual_b, Iterator2 actual_e, 89 std::size_t shift) 90 { 91 using namespace std; 92 using T = typename iterator_traits<Iterator2>::value_type; 93 Iterator1 data_m = std::next(data_b, shift); 94 95 fill(actual_b, actual_e, T(-123)); 96 Iterator2 actual_return = rotate_copy(exec, data_b, data_m, data_e, actual_b); 97 98 EXPECT_TRUE(actual_return == actual_e, "wrong result of rotate_copy"); 99 auto comparer = comparator<T, Iterator1, Iterator2>(); 100 bool check = std::equal(data_m, data_e, actual_b, comparer); 101 check = check && std::equal(data_b, data_m, std::next(actual_b, std::distance(data_m, data_e)), comparer); 102 103 EXPECT_TRUE(check, "wrong effect of rotate_copy"); 104 } 105 }; 106 107 template <typename T1, typename T2> 108 void 109 test() 110 { 111 112 const std::size_t max_len = 100000; 113 114 Sequence<T2> actual(max_len, [](std::size_t i) { return T1(i); }); 115 116 Sequence<T1> data(max_len, [](std::size_t i) { return T1(i); }); 117 118 for (std::size_t len = 0; len < max_len; len = len <= 16 ? len + 1 : std::size_t(3.1415 * len)) 119 { 120 std::size_t shifts[] = {0, 1, 2, len / 3, (2 * len) / 3, len - 1}; 121 for (std::size_t shift : shifts) 122 { 123 if (shift > 0 && shift < len) 124 { 125 invoke_on_all_policies(test_one_policy(), data.begin(), data.begin() + len, actual.begin(), 126 actual.begin() + len, shift); 127 invoke_on_all_policies(test_one_policy(), data.cbegin(), data.cbegin() + len, actual.begin(), 128 actual.begin() + len, shift); 129 } 130 } 131 } 132 } 133 134 int32_t 135 main() 136 { 137 test<int32_t, int8_t>(); 138 test<uint16_t, float32_t>(); 139 test<float64_t, int64_t>(); 140 test<wrapper<float64_t>, wrapper<float64_t>>(); 141 142 std::cout << done() << std::endl; 143 return 0; 144 } 145