1 // -*- C++ -*-
2 // { dg-options "-std=gnu++17 -ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-require-effective-target tbb-backend }
5
6 //===-- lexicographical_compare.pass.cpp ----------------------------------===//
7 //
8 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9 // See https://llvm.org/LICENSE.txt for license information.
10 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "pstl/pstl_test_config.h"
15
16 #ifdef PSTL_STANDALONE_TESTS
17 #include <iostream>
18
19 #include "pstl/execution"
20 #include "pstl/algorithm"
21 #else
22 #include <execution>
23 #include <algorithm>
24 #endif // PSTL_STANDALONE_TESTS
25
26 #include "pstl/test_utils.h"
27
28 using namespace TestUtils;
29
30 struct test_one_policy
31 {
32
33 template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Predicate>
34 void
operator ()test_one_policy35 operator()(ExecutionPolicy&& exec, Iterator1 begin1, Iterator1 end1, Iterator2 begin2, Iterator2 end2,
36 Predicate pred)
37 {
38 const bool expected = std::lexicographical_compare(begin1, end1, begin2, end2, pred);
39 const bool actual = std::lexicographical_compare(exec, begin1, end1, begin2, end2, pred);
40 EXPECT_TRUE(actual == expected, "wrong return result from lexicographical compare with predicate");
41 }
42
43 template <typename ExecutionPolicy, typename Iterator1, typename Iterator2>
44 void
operator ()test_one_policy45 operator()(ExecutionPolicy&& exec, Iterator1 begin1, Iterator1 end1, Iterator2 begin2, Iterator2 end2)
46 {
47 const bool expected = std::lexicographical_compare(begin1, end1, begin2, end2);
48 const bool actual = std::lexicographical_compare(exec, begin1, end1, begin2, end2);
49 EXPECT_TRUE(actual == expected, "wrong return result from lexicographical compare without predicate");
50 }
51 };
52
53 template <typename T1, typename T2, typename Predicate>
54 void
test(Predicate pred)55 test(Predicate pred)
56 {
57
58 const std::size_t max_n = 1000000;
59 Sequence<T1> in1(max_n, [](std::size_t k) { return T1(k); });
60 Sequence<T2> in2(2 * max_n, [](std::size_t k) { return T2(k); });
61
62 std::size_t n2;
63
64 // Test case: Call algorithm's version without predicate.
65 invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + max_n, in2.cbegin() + 3 * max_n / 10,
66 in2.cbegin() + 5 * max_n / 10);
67
68 // Test case: If one range is a prefix of another, the shorter range is lexicographically less than the other.
69 std::size_t max_n2 = max_n / 10;
70 invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + max_n, in2.cbegin(), in2.cbegin() + max_n2,
71 pred);
72 invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + max_n, in2.begin() + max_n2,
73 in2.begin() + 3 * max_n2, pred);
74
75 // Test case: If one range is a prefix of another, the shorter range is lexicographically less than the other.
76 max_n2 = 2 * max_n;
77 invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + max_n, in2.begin(), in2.begin() + max_n2,
78 pred);
79
80 for (std::size_t n1 = 0; n1 <= max_n; n1 = n1 <= 16 ? n1 + 1 : std::size_t(3.1415 * n1))
81 {
82 // Test case: If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal.
83 n2 = n1;
84 invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.begin(), in2.begin() + n2, pred);
85
86 n2 = n1;
87 // Test case: two ranges have different elements and are of the same length (second sequence less than first)
88 std::size_t ind = n1 / 2;
89 in2[ind] = T2(-1);
90 invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.begin(), in2.begin() + n2, pred);
91 in2[ind] = T2(ind);
92
93 // Test case: two ranges have different elements and are of the same length (first sequence less than second)
94 ind = n1 / 5;
95 in1[ind] = T1(-1);
96 invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.cbegin(), in2.cbegin() + n2, pred);
97 in1[ind] = T1(ind);
98 }
99 }
100
101 template <typename Predicate>
102 void
test_string(Predicate pred)103 test_string(Predicate pred)
104 {
105
106 const std::size_t max_n = 1000000;
107 std::string in1 = "";
108 std::string in2 = "";
109 for (std::size_t n1 = 0; n1 <= max_n; ++n1)
110 {
111 in1 += n1;
112 }
113
114 for (std::size_t n1 = 0; n1 <= 2 * max_n; ++n1)
115 {
116 in2 += n1;
117 }
118
119 std::size_t n2;
120
121 for (std::size_t n1 = 0; n1 < in1.size(); n1 = n1 <= 16 ? n1 + 1 : std::size_t(3.1415 * n1))
122 {
123 // Test case: If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal.
124 n2 = n1;
125 invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.begin(), in2.begin() + n2, pred);
126
127 n2 = n1;
128 // Test case: two ranges have different elements and are of the same length (second sequence less than first)
129 in2[n1 / 2] = 'a';
130 invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.begin(), in2.begin() + n2, pred);
131
132 // Test case: two ranges have different elements and are of the same length (first sequence less than second)
133 in1[n1 / 5] = 'a';
134 invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.cbegin(), in2.cbegin() + n2, pred);
135 }
136 invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + max_n, in2.cbegin() + 3 * max_n / 10,
137 in2.cbegin() + 5 * max_n / 10);
138 }
139
140 template <typename T>
141 struct LocalWrapper
142 {
LocalWrapperLocalWrapper143 explicit LocalWrapper(std::size_t k) : my_val(k) {}
144 bool
operator <LocalWrapper145 operator<(const LocalWrapper<T>& w) const
146 {
147 return my_val < w.my_val;
148 }
149
150 private:
151 T my_val;
152 };
153
154 template <typename T>
155 struct test_non_const
156 {
157 template <typename Policy, typename FirstIterator, typename SecondInterator>
158 void
operator ()test_non_const159 operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter)
160 {
161 invoke_if(exec, [&]() {
162 lexicographical_compare(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::less<T>()));
163 });
164 }
165 };
166
167 int32_t
main()168 main()
169 {
170 test<uint16_t, float64_t>(std::less<float64_t>());
171 test<float32_t, int32_t>(std::greater<float32_t>());
172 #if !__PSTL_ICC_18_TEST_EARLY_EXIT_AVX_RELEASE_BROKEN
173 test<float64_t, int32_t>([](const float64_t x, const int32_t y) { return x * x < y * y; });
174 #endif
175 test<LocalWrapper<int32_t>, LocalWrapper<int32_t>>(
176 [](const LocalWrapper<int32_t>& x, const LocalWrapper<int32_t>& y) { return x < y; });
177 test_string([](const char x, const char y) { return x < y; });
178
179 test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
180
181 std::cout << done() << std::endl;
182 return 0;
183 }
184