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