1 | // -*- C++ -*- |
2 | //===-- lexicographical_compare.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 <iostream> |
15 | #include <execution> |
16 | #include <algorithm> |
17 | |
18 | #include "support/utils.h" |
19 | |
20 | using namespace TestUtils; |
21 | |
22 | struct test_one_policy |
23 | { |
24 | |
25 | template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Predicate> |
26 | void |
27 | operator()(ExecutionPolicy&& exec, Iterator1 begin1, Iterator1 end1, Iterator2 begin2, Iterator2 end2, |
28 | Predicate pred) |
29 | { |
30 | const bool expected = std::lexicographical_compare(begin1, end1, begin2, end2, pred); |
31 | const bool actual = std::lexicographical_compare(exec, begin1, end1, begin2, end2, pred); |
32 | EXPECT_TRUE(actual == expected, "wrong return result from lexicographical compare with predicate" ); |
33 | } |
34 | |
35 | template <typename ExecutionPolicy, typename Iterator1, typename Iterator2> |
36 | void |
37 | operator()(ExecutionPolicy&& exec, Iterator1 begin1, Iterator1 end1, Iterator2 begin2, Iterator2 end2) |
38 | { |
39 | const bool expected = std::lexicographical_compare(begin1, end1, begin2, end2); |
40 | const bool actual = std::lexicographical_compare(exec, begin1, end1, begin2, end2); |
41 | EXPECT_TRUE(actual == expected, "wrong return result from lexicographical compare without predicate" ); |
42 | } |
43 | }; |
44 | |
45 | template <typename T1, typename T2, typename Predicate> |
46 | void |
47 | test(Predicate pred) |
48 | { |
49 | |
50 | const std::size_t max_n = 1000000; |
51 | Sequence<T1> in1(max_n, [](std::size_t k) { return T1(k); }); |
52 | Sequence<T2> in2(2 * max_n, [](std::size_t k) { return T2(k); }); |
53 | |
54 | std::size_t n2; |
55 | |
56 | // Test case: Call algorithm's version without predicate. |
57 | invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + max_n, in2.cbegin() + 3 * max_n / 10, |
58 | in2.cbegin() + 5 * max_n / 10); |
59 | |
60 | // Test case: If one range is a prefix of another, the shorter range is lexicographically less than the other. |
61 | std::size_t max_n2 = max_n / 10; |
62 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + max_n, in2.cbegin(), in2.cbegin() + max_n2, |
63 | pred); |
64 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + max_n, in2.begin() + max_n2, |
65 | in2.begin() + 3 * max_n2, pred); |
66 | |
67 | // Test case: If one range is a prefix of another, the shorter range is lexicographically less than the other. |
68 | max_n2 = 2 * max_n; |
69 | invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + max_n, in2.begin(), in2.begin() + max_n2, |
70 | pred); |
71 | |
72 | for (std::size_t n1 = 0; n1 <= max_n; n1 = n1 <= 16 ? n1 + 1 : std::size_t(3.1415 * n1)) |
73 | { |
74 | // Test case: If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal. |
75 | n2 = n1; |
76 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.begin(), in2.begin() + n2, pred); |
77 | |
78 | n2 = n1; |
79 | // Test case: two ranges have different elements and are of the same length (second sequence less than first) |
80 | std::size_t ind = n1 / 2; |
81 | in2[ind] = T2(-1); |
82 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.begin(), in2.begin() + n2, pred); |
83 | in2[ind] = T2(ind); |
84 | |
85 | // Test case: two ranges have different elements and are of the same length (first sequence less than second) |
86 | ind = n1 / 5; |
87 | in1[ind] = T1(-1); |
88 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.cbegin(), in2.cbegin() + n2, pred); |
89 | in1[ind] = T1(ind); |
90 | } |
91 | } |
92 | |
93 | template <typename Predicate> |
94 | void |
95 | test_string(Predicate pred) |
96 | { |
97 | |
98 | const std::size_t max_n = 1000000; |
99 | std::string in1 = "" ; |
100 | std::string in2 = "" ; |
101 | for (std::size_t n1 = 0; n1 <= max_n; ++n1) |
102 | { |
103 | in1 += n1; |
104 | } |
105 | |
106 | for (std::size_t n1 = 0; n1 <= 2 * max_n; ++n1) |
107 | { |
108 | in2 += n1; |
109 | } |
110 | |
111 | std::size_t n2; |
112 | |
113 | for (std::size_t n1 = 0; n1 < in1.size(); n1 = n1 <= 16 ? n1 + 1 : std::size_t(3.1415 * n1)) |
114 | { |
115 | // Test case: If two ranges have equivalent elements and are of the same length, then the ranges are lexicographically equal. |
116 | n2 = n1; |
117 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.begin(), in2.begin() + n2, pred); |
118 | |
119 | n2 = n1; |
120 | // Test case: two ranges have different elements and are of the same length (second sequence less than first) |
121 | in2[n1 / 2] = 'a'; |
122 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.begin(), in2.begin() + n2, pred); |
123 | |
124 | // Test case: two ranges have different elements and are of the same length (first sequence less than second) |
125 | in1[n1 / 5] = 'a'; |
126 | invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.cbegin(), in2.cbegin() + n2, pred); |
127 | } |
128 | invoke_on_all_policies(op: test_one_policy(), rest: in1.cbegin(), rest: in1.cbegin() + max_n, rest: in2.cbegin() + 3 * max_n / 10, |
129 | rest: in2.cbegin() + 5 * max_n / 10); |
130 | } |
131 | |
132 | template <typename T> |
133 | struct LocalWrapper |
134 | { |
135 | explicit LocalWrapper(std::size_t k) : my_val(k) {} |
136 | bool |
137 | operator<(const LocalWrapper<T>& w) const |
138 | { |
139 | return my_val < w.my_val; |
140 | } |
141 | |
142 | private: |
143 | T my_val; |
144 | }; |
145 | |
146 | template <typename T> |
147 | struct test_non_const |
148 | { |
149 | template <typename Policy, typename FirstIterator, typename SecondInterator> |
150 | void |
151 | operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter) |
152 | { |
153 | invoke_if(exec, [&]() { |
154 | lexicographical_compare(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::less<T>())); |
155 | }); |
156 | } |
157 | }; |
158 | |
159 | int |
160 | main() |
161 | { |
162 | test<uint16_t, float64_t>(pred: std::less<float64_t>()); |
163 | test<float32_t, int32_t>(pred: std::greater<float32_t>()); |
164 | #if !defined(_PSTL_ICC_18_TEST_EARLY_EXIT_AVX_RELEASE_BROKEN) |
165 | test<float64_t, int32_t>(pred: [](const float64_t x, const int32_t y) { return x * x < y * y; }); |
166 | #endif |
167 | test<LocalWrapper<int32_t>, LocalWrapper<int32_t>>( |
168 | pred: [](const LocalWrapper<int32_t>& x, const LocalWrapper<int32_t>& y) { return x < y; }); |
169 | test_string(pred: [](const char x, const char y) { return x < y; }); |
170 | |
171 | test_algo_basic_double<int32_t>(f: run_for_rnd_fw<test_non_const<int32_t>>()); |
172 | |
173 | std::cout << done() << std::endl; |
174 | return 0; |
175 | } |
176 | |