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