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