1 // -*- C++ -*- 2 //===-- equal.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 <execution> 13 #include <algorithm> 14 15 #include "support/utils.h" 16 17 using namespace TestUtils; 18 19 #define CPP14_ENABLED 0 20 21 struct UserType 22 { 23 float32_t f; 24 float64_t d; 25 int32_t i; 26 size_t key; 27 28 bool 29 operator()(UserType a, UserType b) 30 { 31 return a.key < b.key; 32 } 33 bool 34 operator<(UserType a) 35 { 36 return a.key < key; 37 } 38 bool 39 operator>=(UserType a) 40 { 41 return a.key <= key; 42 } 43 bool 44 operator<=(UserType a) 45 { 46 return a.key >= key; 47 } 48 bool 49 operator==(UserType a) 50 { 51 return a.key == key; 52 } 53 bool 54 operator==(UserType a) const 55 { 56 return a.key == key; 57 } 58 bool 59 operator!=(UserType a) 60 { 61 return a.key != key; 62 } 63 UserType operator!() 64 { 65 UserType tmp; 66 tmp.key = !key; 67 return tmp; 68 } 69 friend std::ostream& 70 operator<<(std::ostream& stream, const UserType a) 71 { 72 stream << a.key; 73 return stream; 74 } 75 76 UserType() : key(-1), f(0.0f), d(0.0), i(0) {} 77 UserType(size_t Number) : key(Number), f(0.0f), d(0.0), i(0) {} 78 UserType& 79 operator=(const UserType& other) 80 { 81 key = other.key; 82 return *this; 83 } 84 UserType(const UserType& other) : key(other.key), f(other.f), d(other.d), i(other.i) {} 85 UserType(UserType&& other) : key(other.key), f(other.f), d(other.d), i(other.i) 86 { 87 other.key = -1; 88 other.f = 0.0f; 89 other.d = 0.0; 90 other.i = 0; 91 } 92 }; 93 94 struct test_one_policy 95 { 96 template <typename ExecutionPolicy, typename Iterator1, typename Iterator2> 97 void 98 operator()(ExecutionPolicy&& exec, Iterator1 first1, Iterator1 last1, Iterator2 first2, bool is_true_equal) 99 { 100 using namespace std; 101 102 auto expected = equal(first1, last1, first2); 103 auto actual = equal(exec, first1, last1, first2); 104 EXPECT_EQ(expected, actual, "result for equal for random-access iterator, checking against std::equal()"); 105 106 // testing bool 107 EXPECT_TRUE(is_true_equal == actual, "result for equal for random-access iterator, bool"); 108 109 //add C++14 equal symantics tests 110 //add more cases for inCopy size less than in 111 #if CPP14_ENABLED 112 auto actualr14 = std::equal(in.cbegin(), in.cend(), inCopy.cbegin(), inCopy.cend()); 113 EXPECT_EQ(expected, actualr14, "result for equal for random-access iterator"); 114 #endif 115 } 116 }; 117 118 template <typename T> 119 void 120 test(size_t bits) 121 { 122 for (size_t n = 1; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 123 { 124 125 // Sequence of odd values 126 Sequence<T> in(n, [bits](size_t k) { return T(2 * HashBits(k, bits - 1) ^ 1); }); 127 Sequence<T> inCopy(in); 128 129 invoke_on_all_policies(test_one_policy(), in.begin(), in.end(), inCopy.begin(), true); 130 invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cend(), inCopy.cbegin(), true); 131 132 // testing bool !equal() 133 inCopy[0] = !inCopy[0]; 134 invoke_on_all_policies(test_one_policy(), in.begin(), in.end(), inCopy.begin(), false); 135 invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cend(), inCopy.cbegin(), false); 136 } 137 } 138 139 template <typename T> 140 struct test_non_const 141 { 142 template <typename Policy, typename FirstIterator, typename SecondInterator> 143 void 144 operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter) 145 { 146 equal(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>())); 147 } 148 }; 149 150 int32_t 151 main() 152 { 153 154 test<int32_t>(8 * sizeof(int32_t)); 155 test<uint16_t>(8 * sizeof(uint16_t)); 156 test<float64_t>(53); 157 #if !_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN 158 test<bool>(1); 159 #endif 160 test<UserType>(256); 161 162 test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); 163 164 std::cout << done() << std::endl; 165 return 0; 166 } 167