compare_test.cc
Go to the documentation of this file.
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/types/compare.h"
16 
17 #include "gtest/gtest.h"
18 #include "absl/base/casts.h"
19 
20 namespace absl {
21 namespace {
22 
23 // This is necessary to avoid a bunch of lint warnings suggesting that we use
24 // EXPECT_EQ/etc., which doesn't work in this case because they convert the `0`
25 // to an int, which can't be converted to the unspecified zero type.
26 bool Identity(bool b) { return b; }
27 
28 TEST(Compare, WeakEquality) {
29  EXPECT_TRUE(Identity(weak_equality::equivalent == 0));
30  EXPECT_TRUE(Identity(0 == weak_equality::equivalent));
31  EXPECT_TRUE(Identity(weak_equality::nonequivalent != 0));
32  EXPECT_TRUE(Identity(0 != weak_equality::nonequivalent));
33 }
34 
35 TEST(Compare, StrongEquality) {
36  EXPECT_TRUE(Identity(strong_equality::equal == 0));
37  EXPECT_TRUE(Identity(0 == strong_equality::equal));
38  EXPECT_TRUE(Identity(strong_equality::nonequal != 0));
39  EXPECT_TRUE(Identity(0 != strong_equality::nonequal));
40  EXPECT_TRUE(Identity(strong_equality::equivalent == 0));
41  EXPECT_TRUE(Identity(0 == strong_equality::equivalent));
42  EXPECT_TRUE(Identity(strong_equality::nonequivalent != 0));
43  EXPECT_TRUE(Identity(0 != strong_equality::nonequivalent));
44 }
45 
46 TEST(Compare, PartialOrdering) {
47  EXPECT_TRUE(Identity(partial_ordering::less < 0));
48  EXPECT_TRUE(Identity(0 > partial_ordering::less));
49  EXPECT_TRUE(Identity(partial_ordering::less <= 0));
50  EXPECT_TRUE(Identity(0 >= partial_ordering::less));
51  EXPECT_TRUE(Identity(partial_ordering::equivalent == 0));
52  EXPECT_TRUE(Identity(0 == partial_ordering::equivalent));
53  EXPECT_TRUE(Identity(partial_ordering::greater > 0));
54  EXPECT_TRUE(Identity(0 < partial_ordering::greater));
55  EXPECT_TRUE(Identity(partial_ordering::greater >= 0));
56  EXPECT_TRUE(Identity(0 <= partial_ordering::greater));
57  EXPECT_TRUE(Identity(partial_ordering::unordered != 0));
58  EXPECT_TRUE(Identity(0 != partial_ordering::unordered));
59  EXPECT_FALSE(Identity(partial_ordering::unordered < 0));
60  EXPECT_FALSE(Identity(0 < partial_ordering::unordered));
61  EXPECT_FALSE(Identity(partial_ordering::unordered <= 0));
62  EXPECT_FALSE(Identity(0 <= partial_ordering::unordered));
63  EXPECT_FALSE(Identity(partial_ordering::unordered > 0));
64  EXPECT_FALSE(Identity(0 > partial_ordering::unordered));
65  EXPECT_FALSE(Identity(partial_ordering::unordered >= 0));
66  EXPECT_FALSE(Identity(0 >= partial_ordering::unordered));
67 }
68 
69 TEST(Compare, WeakOrdering) {
70  EXPECT_TRUE(Identity(weak_ordering::less < 0));
71  EXPECT_TRUE(Identity(0 > weak_ordering::less));
72  EXPECT_TRUE(Identity(weak_ordering::less <= 0));
73  EXPECT_TRUE(Identity(0 >= weak_ordering::less));
74  EXPECT_TRUE(Identity(weak_ordering::equivalent == 0));
75  EXPECT_TRUE(Identity(0 == weak_ordering::equivalent));
76  EXPECT_TRUE(Identity(weak_ordering::greater > 0));
77  EXPECT_TRUE(Identity(0 < weak_ordering::greater));
78  EXPECT_TRUE(Identity(weak_ordering::greater >= 0));
79  EXPECT_TRUE(Identity(0 <= weak_ordering::greater));
80 }
81 
82 TEST(Compare, StrongOrdering) {
83  EXPECT_TRUE(Identity(strong_ordering::less < 0));
84  EXPECT_TRUE(Identity(0 > strong_ordering::less));
85  EXPECT_TRUE(Identity(strong_ordering::less <= 0));
86  EXPECT_TRUE(Identity(0 >= strong_ordering::less));
87  EXPECT_TRUE(Identity(strong_ordering::equal == 0));
88  EXPECT_TRUE(Identity(0 == strong_ordering::equal));
89  EXPECT_TRUE(Identity(strong_ordering::equivalent == 0));
90  EXPECT_TRUE(Identity(0 == strong_ordering::equivalent));
91  EXPECT_TRUE(Identity(strong_ordering::greater > 0));
92  EXPECT_TRUE(Identity(0 < strong_ordering::greater));
93  EXPECT_TRUE(Identity(strong_ordering::greater >= 0));
94  EXPECT_TRUE(Identity(0 <= strong_ordering::greater));
95 }
96 
97 TEST(Compare, Conversions) {
98  EXPECT_TRUE(
99  Identity(implicit_cast<weak_equality>(strong_equality::equal) == 0));
100  EXPECT_TRUE(
101  Identity(implicit_cast<weak_equality>(strong_equality::nonequal) != 0));
102  EXPECT_TRUE(
103  Identity(implicit_cast<weak_equality>(strong_equality::equivalent) == 0));
104  EXPECT_TRUE(Identity(
105  implicit_cast<weak_equality>(strong_equality::nonequivalent) != 0));
106 
107  EXPECT_TRUE(
108  Identity(implicit_cast<weak_equality>(partial_ordering::less) != 0));
109  EXPECT_TRUE(Identity(
110  implicit_cast<weak_equality>(partial_ordering::equivalent) == 0));
111  EXPECT_TRUE(
112  Identity(implicit_cast<weak_equality>(partial_ordering::greater) != 0));
113  EXPECT_TRUE(
114  Identity(implicit_cast<weak_equality>(partial_ordering::unordered) != 0));
115 
116  EXPECT_TRUE(implicit_cast<weak_equality>(weak_ordering::less) != 0);
117  EXPECT_TRUE(
118  Identity(implicit_cast<weak_equality>(weak_ordering::equivalent) == 0));
119  EXPECT_TRUE(
120  Identity(implicit_cast<weak_equality>(weak_ordering::greater) != 0));
121 
122  EXPECT_TRUE(
123  Identity(implicit_cast<partial_ordering>(weak_ordering::less) != 0));
124  EXPECT_TRUE(
125  Identity(implicit_cast<partial_ordering>(weak_ordering::less) < 0));
126  EXPECT_TRUE(
127  Identity(implicit_cast<partial_ordering>(weak_ordering::less) <= 0));
128  EXPECT_TRUE(Identity(
129  implicit_cast<partial_ordering>(weak_ordering::equivalent) == 0));
130  EXPECT_TRUE(
131  Identity(implicit_cast<partial_ordering>(weak_ordering::greater) != 0));
132  EXPECT_TRUE(
133  Identity(implicit_cast<partial_ordering>(weak_ordering::greater) > 0));
134  EXPECT_TRUE(
135  Identity(implicit_cast<partial_ordering>(weak_ordering::greater) >= 0));
136 
137  EXPECT_TRUE(
138  Identity(implicit_cast<weak_equality>(strong_ordering::less) != 0));
139  EXPECT_TRUE(
140  Identity(implicit_cast<weak_equality>(strong_ordering::equal) == 0));
141  EXPECT_TRUE(
142  Identity(implicit_cast<weak_equality>(strong_ordering::equivalent) == 0));
143  EXPECT_TRUE(
144  Identity(implicit_cast<weak_equality>(strong_ordering::greater) != 0));
145 
146  EXPECT_TRUE(
147  Identity(implicit_cast<strong_equality>(strong_ordering::less) != 0));
148  EXPECT_TRUE(
149  Identity(implicit_cast<strong_equality>(strong_ordering::equal) == 0));
150  EXPECT_TRUE(Identity(
151  implicit_cast<strong_equality>(strong_ordering::equivalent) == 0));
152  EXPECT_TRUE(
153  Identity(implicit_cast<strong_equality>(strong_ordering::greater) != 0));
154 
155  EXPECT_TRUE(
156  Identity(implicit_cast<partial_ordering>(strong_ordering::less) != 0));
157  EXPECT_TRUE(
158  Identity(implicit_cast<partial_ordering>(strong_ordering::less) < 0));
159  EXPECT_TRUE(
160  Identity(implicit_cast<partial_ordering>(strong_ordering::less) <= 0));
161  EXPECT_TRUE(
162  Identity(implicit_cast<partial_ordering>(strong_ordering::equal) == 0));
163  EXPECT_TRUE(Identity(
164  implicit_cast<partial_ordering>(strong_ordering::equivalent) == 0));
165  EXPECT_TRUE(
166  Identity(implicit_cast<partial_ordering>(strong_ordering::greater) != 0));
167  EXPECT_TRUE(
168  Identity(implicit_cast<partial_ordering>(strong_ordering::greater) > 0));
169  EXPECT_TRUE(
170  Identity(implicit_cast<partial_ordering>(strong_ordering::greater) >= 0));
171 
172  EXPECT_TRUE(
173  Identity(implicit_cast<weak_ordering>(strong_ordering::less) != 0));
174  EXPECT_TRUE(
175  Identity(implicit_cast<weak_ordering>(strong_ordering::less) < 0));
176  EXPECT_TRUE(
177  Identity(implicit_cast<weak_ordering>(strong_ordering::less) <= 0));
178  EXPECT_TRUE(
179  Identity(implicit_cast<weak_ordering>(strong_ordering::equal) == 0));
180  EXPECT_TRUE(
181  Identity(implicit_cast<weak_ordering>(strong_ordering::equivalent) == 0));
182  EXPECT_TRUE(
183  Identity(implicit_cast<weak_ordering>(strong_ordering::greater) != 0));
184  EXPECT_TRUE(
185  Identity(implicit_cast<weak_ordering>(strong_ordering::greater) > 0));
186  EXPECT_TRUE(
187  Identity(implicit_cast<weak_ordering>(strong_ordering::greater) >= 0));
188 }
189 
190 struct WeakOrderingLess {
191  template <typename T>
192  absl::weak_ordering operator()(const T &a, const T &b) const {
193  return a < b ? absl::weak_ordering::less
196  }
197 };
198 
199 TEST(CompareResultAsLessThan, SanityTest) {
202 
203  EXPECT_TRUE(
209 }
210 
211 TEST(DoLessThanComparison, SanityTest) {
212  std::less<int> less;
213  WeakOrderingLess weak;
214 
215  EXPECT_TRUE(absl::compare_internal::do_less_than_comparison(less, -1, 0));
216  EXPECT_TRUE(absl::compare_internal::do_less_than_comparison(weak, -1, 0));
217 
218  EXPECT_FALSE(absl::compare_internal::do_less_than_comparison(less, 10, 10));
219  EXPECT_FALSE(absl::compare_internal::do_less_than_comparison(weak, 10, 10));
220 
221  EXPECT_FALSE(absl::compare_internal::do_less_than_comparison(less, 10, 5));
222  EXPECT_FALSE(absl::compare_internal::do_less_than_comparison(weak, 10, 5));
223 }
224 
225 TEST(CompareResultAsOrdering, SanityTest) {
226  EXPECT_TRUE(Identity(
228  EXPECT_FALSE(Identity(
230  EXPECT_FALSE(
233  weak_ordering::less) < 0));
235  weak_ordering::less) == 0));
237  weak_ordering::less) > 0));
238 
239  EXPECT_FALSE(Identity(
241  EXPECT_TRUE(Identity(
243  EXPECT_FALSE(Identity(
251 
252  EXPECT_FALSE(Identity(
254  EXPECT_FALSE(Identity(
256  EXPECT_TRUE(Identity(
259  weak_ordering::greater) < 0));
261  weak_ordering::greater) == 0));
263  weak_ordering::greater) > 0));
264 }
265 
266 TEST(DoThreeWayComparison, SanityTest) {
267  std::less<int> less;
268  WeakOrderingLess weak;
269 
270  EXPECT_TRUE(Identity(
272  EXPECT_FALSE(Identity(
274  EXPECT_FALSE(Identity(
276  EXPECT_TRUE(Identity(
278  EXPECT_FALSE(Identity(
280  EXPECT_FALSE(Identity(
282 
283  EXPECT_FALSE(Identity(
285  EXPECT_TRUE(Identity(
287  EXPECT_FALSE(Identity(
289  EXPECT_FALSE(Identity(
291  EXPECT_TRUE(Identity(
293  EXPECT_FALSE(Identity(
295 
296  EXPECT_FALSE(Identity(
298  EXPECT_FALSE(Identity(
300  EXPECT_TRUE(Identity(
302  EXPECT_FALSE(Identity(
304  EXPECT_FALSE(Identity(
306  EXPECT_TRUE(Identity(
308 }
309 
310 } // namespace
311 } // namespace absl
static ABSL_CONST_INIT const partial_ordering unordered
Definition: compare.h:116
static ABSL_CONST_INIT const partial_ordering equivalent
Definition: compare.h:114
static ABSL_CONST_INIT const partial_ordering greater
Definition: compare.h:115
static ABSL_CONST_INIT const weak_ordering equivalent
Definition: compare.h:130
constexpr bool do_less_than_comparison(const Compare &compare, const K &x, const LK &y)
Definition: compare.h:464
constexpr bool compare_result_as_less_than(const Bool r)
Definition: compare.h:458
static ABSL_CONST_INIT const T equal
Definition: compare.h:97
constexpr absl::weak_ordering compare_result_as_ordering(const Int c)
Definition: compare.h:474
TEST(NotificationTest, SanityTest)
static ABSL_CONST_INIT const weak_ordering less
Definition: compare.h:129
static ABSL_CONST_INIT const weak_ordering greater
Definition: compare.h:131
Definition: algorithm.h:29
static ABSL_CONST_INIT const weak_equality nonequivalent
Definition: compare.h:88
static ABSL_CONST_INIT const T nonequal
Definition: compare.h:98
constexpr absl::weak_ordering do_three_way_comparison(const Compare &compare, const K &x, const LK &y)
Definition: compare.h:489
static ABSL_CONST_INIT const strong_ordering equivalent
Definition: compare.h:144
static ABSL_CONST_INIT const T nonequivalent
Definition: compare.h:100
static ABSL_CONST_INIT const partial_ordering less
Definition: compare.h:113
static ABSL_CONST_INIT const strong_ordering equal
Definition: compare.h:143
static ABSL_CONST_INIT const strong_ordering less
Definition: compare.h:142
uint64_t b
Definition: layout_test.cc:50
int Compare(const BigUnsigned< N > &lhs, const BigUnsigned< M > &rhs)
static ABSL_CONST_INIT const T equivalent
Definition: compare.h:99
static ABSL_CONST_INIT const weak_equality equivalent
Definition: compare.h:87
static ABSL_CONST_INIT const strong_ordering greater
Definition: compare.h:145


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:18