xref: /llvm-project/pstl/test/std/algorithms/alg.nonmodifying/equal.pass.cpp (revision 3b9a1bb1af90db9472340ef2122d3855eb9ba3fc)
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
operator ()UserType31     operator()(UserType a, UserType b)
32     {
33         return a.key < b.key;
34     }
35     bool
operator <UserType36     operator<(UserType a)
37     {
38         return a.key < key;
39     }
40     bool
operator >=UserType41     operator>=(UserType a)
42     {
43         return a.key <= key;
44     }
45     bool
operator <=UserType46     operator<=(UserType a)
47     {
48         return a.key >= key;
49     }
50     bool
operator ==UserType51     operator==(UserType a)
52     {
53         return a.key == key;
54     }
55     bool
operator ==UserType56     operator==(UserType a) const
57     {
58         return a.key == key;
59     }
60     bool
operator !=UserType61     operator!=(UserType a)
62     {
63         return a.key != key;
64     }
operator !UserType65     UserType operator!()
66     {
67         UserType tmp;
68         tmp.key = !key;
69         return tmp;
70     }
71     friend std::ostream&
operator <<(std::ostream & stream,const UserType a)72     operator<<(std::ostream& stream, const UserType a)
73     {
74         stream << a.key;
75         return stream;
76     }
77 
UserTypeUserType78     UserType() : key(-1), f(0.0f), d(0.0), i(0) {}
UserTypeUserType79     UserType(size_t Number) : key(Number), f(0.0f), d(0.0), i(0) {}
80     UserType&
operator =UserType81     operator=(const UserType& other)
82     {
83         key = other.key;
84         return *this;
85     }
UserTypeUserType86     UserType(const UserType& other) : key(other.key), f(other.f), d(other.d), i(other.i) {}
UserTypeUserType87     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
operator ()test_one_policy100     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
test(size_t bits)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(k, 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
operator ()test_non_const146     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
main()153 main()
154 {
155 
156     test<int32_t>(8 * sizeof(int32_t));
157     test<uint16_t>(8 * sizeof(uint16_t));
158     test<float64_t>(53);
159 #if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN)
160     test<bool>(1);
161 #endif
162     test<UserType>(256);
163 
164     test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
165 
166     std::cout << done() << std::endl;
167     return 0;
168 }
169