blob: 2a135a157cd1758a9ca4a17345aec1b452192971 [file] [log] [blame]
kwiberg23c881a2016-11-01 19:04:261/*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <limits>
12
kjellander19796962017-06-30 17:45:2113#include "webrtc/rtc_base/safe_compare.h"
kwiberg23c881a2016-11-01 19:04:2614#include "webrtc/test/gtest.h"
15
16namespace rtc {
17
18namespace {
19
20constexpr std::uintmax_t umax = std::numeric_limits<std::uintmax_t>::max();
21constexpr std::intmax_t imin = std::numeric_limits<std::intmax_t>::min();
22constexpr std::intmax_t m1 = -1;
23
24// m1 and umax have the same representation because we use 2's complement
25// arithmetic, so naive casting will confuse them.
26static_assert(static_cast<std::uintmax_t>(m1) == umax, "");
27static_assert(m1 == static_cast<std::intmax_t>(umax), "");
28
kwiberga539ff02017-04-11 05:44:0729static const std::pair<int, int> p1(1, 1);
30static const std::pair<int, int> p2(1, 2);
kwiberg23c881a2016-11-01 19:04:2631
32} // namespace
33
34// clang-format off
35
36// These functions aren't used in the tests, but it's useful to look at the
37// compiler output for them, and verify that (1) the same-signedness *Safe
38// functions result in exactly the same code as their *Ref counterparts, and
39// that (2) the mixed-signedness *Safe functions have just a few extra
40// arithmetic and logic instructions (but no extra control flow instructions).
41bool TestLessThanRef( int a, int b) { return a < b; }
42bool TestLessThanRef( unsigned a, unsigned b) { return a < b; }
kwiberg1b0baa42017-06-26 08:31:3143bool TestLessThanSafe( int a, int b) { return SafeLt(a, b); }
44bool TestLessThanSafe(unsigned a, unsigned b) { return SafeLt(a, b); }
45bool TestLessThanSafe(unsigned a, int b) { return SafeLt(a, b); }
46bool TestLessThanSafe( int a, unsigned b) { return SafeLt(a, b); }
kwiberg23c881a2016-11-01 19:04:2647
48// For these, we expect the *Ref and *Safe functions to result in identical
49// code, except for the ones that compare a signed variable with an unsigned
50// constant; in that case, the *Ref function does an unsigned comparison (fast
51// but incorrect) and the *Safe function spends a few extra instructions on
52// doing it right.
53bool TestLessThan17Ref( int a) { return a < 17; }
54bool TestLessThan17Ref( unsigned a) { return a < 17; }
55bool TestLessThan17uRef( int a) { return static_cast<unsigned>(a) < 17u; }
56bool TestLessThan17uRef( unsigned a) { return a < 17u; }
kwiberg1b0baa42017-06-26 08:31:3157bool TestLessThan17Safe( int a) { return SafeLt(a, 17); }
58bool TestLessThan17Safe( unsigned a) { return SafeLt(a, 17); }
59bool TestLessThan17uSafe( int a) { return SafeLt(a, 17u); }
60bool TestLessThan17uSafe(unsigned a) { return SafeLt(a, 17u); }
kwiberg23c881a2016-11-01 19:04:2661
62// Cases where we can't convert to a larger signed type.
kwiberg1b0baa42017-06-26 08:31:3163bool TestLessThanMax( intmax_t a, uintmax_t b) { return SafeLt(a, b); }
64bool TestLessThanMax(uintmax_t a, intmax_t b) { return SafeLt(a, b); }
65bool TestLessThanMax17u( intmax_t a) { return SafeLt(a, uintmax_t{17}); }
66bool TestLessThanMax17( uintmax_t a) { return SafeLt(a, intmax_t{17}); }
kwiberg23c881a2016-11-01 19:04:2667
68// Cases where the compiler should be able to compute the result at compile
69// time.
kwiberg1b0baa42017-06-26 08:31:3170bool TestLessThanConst1() { return SafeLt( -1, 1); }
71bool TestLessThanConst2() { return SafeLt( m1, umax); }
72bool TestLessThanConst3() { return SafeLt(umax, imin); }
73bool TestLessThanConst4(unsigned a) { return SafeLt( a, -1); }
74bool TestLessThanConst5(unsigned a) { return SafeLt(-1, a); }
75bool TestLessThanConst6(unsigned a) { return SafeLt( a, a); }
kwiberg23c881a2016-11-01 19:04:2676
77// clang-format on
78
79TEST(SafeCmpTest, Eq) {
kwiberg1b0baa42017-06-26 08:31:3180 static_assert(!SafeEq(-1, 2), "");
81 static_assert(!SafeEq(-1, 2u), "");
82 static_assert(!SafeEq(2, -1), "");
83 static_assert(!SafeEq(2u, -1), "");
kwiberg23c881a2016-11-01 19:04:2684
kwiberg1b0baa42017-06-26 08:31:3185 static_assert(!SafeEq(1, 2), "");
86 static_assert(!SafeEq(1, 2u), "");
87 static_assert(!SafeEq(1u, 2), "");
88 static_assert(!SafeEq(1u, 2u), "");
89 static_assert(!SafeEq(2, 1), "");
90 static_assert(!SafeEq(2, 1u), "");
91 static_assert(!SafeEq(2u, 1), "");
92 static_assert(!SafeEq(2u, 1u), "");
kwiberg23c881a2016-11-01 19:04:2693
kwiberg1b0baa42017-06-26 08:31:3194 static_assert(SafeEq(2, 2), "");
95 static_assert(SafeEq(2, 2u), "");
96 static_assert(SafeEq(2u, 2), "");
97 static_assert(SafeEq(2u, 2u), "");
kwiberg23c881a2016-11-01 19:04:2698
kwiberg1b0baa42017-06-26 08:31:3199 static_assert(SafeEq(imin, imin), "");
100 static_assert(!SafeEq(imin, umax), "");
101 static_assert(!SafeEq(umax, imin), "");
102 static_assert(SafeEq(umax, umax), "");
kwiberg23c881a2016-11-01 19:04:26103
kwiberg1b0baa42017-06-26 08:31:31104 static_assert(SafeEq(m1, m1), "");
105 static_assert(!SafeEq(m1, umax), "");
106 static_assert(!SafeEq(umax, m1), "");
107 static_assert(SafeEq(umax, umax), "");
kwiberg23c881a2016-11-01 19:04:26108
kwiberg1b0baa42017-06-26 08:31:31109 static_assert(!SafeEq(1, 2), "");
110 static_assert(!SafeEq(1, 2.0), "");
111 static_assert(!SafeEq(1.0, 2), "");
112 static_assert(!SafeEq(1.0, 2.0), "");
113 static_assert(!SafeEq(2, 1), "");
114 static_assert(!SafeEq(2, 1.0), "");
115 static_assert(!SafeEq(2.0, 1), "");
116 static_assert(!SafeEq(2.0, 1.0), "");
kwiberg23c881a2016-11-01 19:04:26117
kwiberg1b0baa42017-06-26 08:31:31118 static_assert(SafeEq(2, 2), "");
119 static_assert(SafeEq(2, 2.0), "");
120 static_assert(SafeEq(2.0, 2), "");
121 static_assert(SafeEq(2.0, 2.0), "");
kwiberg23c881a2016-11-01 19:04:26122
kwiberg1b0baa42017-06-26 08:31:31123 EXPECT_TRUE(SafeEq(p1, p1));
124 EXPECT_FALSE(SafeEq(p1, p2));
125 EXPECT_FALSE(SafeEq(p2, p1));
126 EXPECT_TRUE(SafeEq(p2, p2));
kwiberg23c881a2016-11-01 19:04:26127}
128
129TEST(SafeCmpTest, Ne) {
kwiberg1b0baa42017-06-26 08:31:31130 static_assert(SafeNe(-1, 2), "");
131 static_assert(SafeNe(-1, 2u), "");
132 static_assert(SafeNe(2, -1), "");
133 static_assert(SafeNe(2u, -1), "");
kwiberg23c881a2016-11-01 19:04:26134
kwiberg1b0baa42017-06-26 08:31:31135 static_assert(SafeNe(1, 2), "");
136 static_assert(SafeNe(1, 2u), "");
137 static_assert(SafeNe(1u, 2), "");
138 static_assert(SafeNe(1u, 2u), "");
139 static_assert(SafeNe(2, 1), "");
140 static_assert(SafeNe(2, 1u), "");
141 static_assert(SafeNe(2u, 1), "");
142 static_assert(SafeNe(2u, 1u), "");
kwiberg23c881a2016-11-01 19:04:26143
kwiberg1b0baa42017-06-26 08:31:31144 static_assert(!SafeNe(2, 2), "");
145 static_assert(!SafeNe(2, 2u), "");
146 static_assert(!SafeNe(2u, 2), "");
147 static_assert(!SafeNe(2u, 2u), "");
kwiberg23c881a2016-11-01 19:04:26148
kwiberg1b0baa42017-06-26 08:31:31149 static_assert(!SafeNe(imin, imin), "");
150 static_assert(SafeNe(imin, umax), "");
151 static_assert(SafeNe(umax, imin), "");
152 static_assert(!SafeNe(umax, umax), "");
kwiberg23c881a2016-11-01 19:04:26153
kwiberg1b0baa42017-06-26 08:31:31154 static_assert(!SafeNe(m1, m1), "");
155 static_assert(SafeNe(m1, umax), "");
156 static_assert(SafeNe(umax, m1), "");
157 static_assert(!SafeNe(umax, umax), "");
kwiberg23c881a2016-11-01 19:04:26158
kwiberg1b0baa42017-06-26 08:31:31159 static_assert(SafeNe(1, 2), "");
160 static_assert(SafeNe(1, 2.0), "");
161 static_assert(SafeNe(1.0, 2), "");
162 static_assert(SafeNe(1.0, 2.0), "");
163 static_assert(SafeNe(2, 1), "");
164 static_assert(SafeNe(2, 1.0), "");
165 static_assert(SafeNe(2.0, 1), "");
166 static_assert(SafeNe(2.0, 1.0), "");
kwiberg23c881a2016-11-01 19:04:26167
kwiberg1b0baa42017-06-26 08:31:31168 static_assert(!SafeNe(2, 2), "");
169 static_assert(!SafeNe(2, 2.0), "");
170 static_assert(!SafeNe(2.0, 2), "");
171 static_assert(!SafeNe(2.0, 2.0), "");
kwiberg23c881a2016-11-01 19:04:26172
kwiberg1b0baa42017-06-26 08:31:31173 EXPECT_FALSE(SafeNe(p1, p1));
174 EXPECT_TRUE(SafeNe(p1, p2));
175 EXPECT_TRUE(SafeNe(p2, p1));
176 EXPECT_FALSE(SafeNe(p2, p2));
kwiberg23c881a2016-11-01 19:04:26177}
178
179TEST(SafeCmpTest, Lt) {
kwiberg1b0baa42017-06-26 08:31:31180 static_assert(SafeLt(-1, 2), "");
181 static_assert(SafeLt(-1, 2u), "");
182 static_assert(!SafeLt(2, -1), "");
183 static_assert(!SafeLt(2u, -1), "");
kwiberg23c881a2016-11-01 19:04:26184
kwiberg1b0baa42017-06-26 08:31:31185 static_assert(SafeLt(1, 2), "");
186 static_assert(SafeLt(1, 2u), "");
187 static_assert(SafeLt(1u, 2), "");
188 static_assert(SafeLt(1u, 2u), "");
189 static_assert(!SafeLt(2, 1), "");
190 static_assert(!SafeLt(2, 1u), "");
191 static_assert(!SafeLt(2u, 1), "");
192 static_assert(!SafeLt(2u, 1u), "");
kwiberg23c881a2016-11-01 19:04:26193
kwiberg1b0baa42017-06-26 08:31:31194 static_assert(!SafeLt(2, 2), "");
195 static_assert(!SafeLt(2, 2u), "");
196 static_assert(!SafeLt(2u, 2), "");
197 static_assert(!SafeLt(2u, 2u), "");
kwiberg23c881a2016-11-01 19:04:26198
kwiberg1b0baa42017-06-26 08:31:31199 static_assert(!SafeLt(imin, imin), "");
200 static_assert(SafeLt(imin, umax), "");
201 static_assert(!SafeLt(umax, imin), "");
202 static_assert(!SafeLt(umax, umax), "");
kwiberg23c881a2016-11-01 19:04:26203
kwiberg1b0baa42017-06-26 08:31:31204 static_assert(!SafeLt(m1, m1), "");
205 static_assert(SafeLt(m1, umax), "");
206 static_assert(!SafeLt(umax, m1), "");
207 static_assert(!SafeLt(umax, umax), "");
kwiberg23c881a2016-11-01 19:04:26208
kwiberg1b0baa42017-06-26 08:31:31209 static_assert(SafeLt(1, 2), "");
210 static_assert(SafeLt(1, 2.0), "");
211 static_assert(SafeLt(1.0, 2), "");
212 static_assert(SafeLt(1.0, 2.0), "");
213 static_assert(!SafeLt(2, 1), "");
214 static_assert(!SafeLt(2, 1.0), "");
215 static_assert(!SafeLt(2.0, 1), "");
216 static_assert(!SafeLt(2.0, 1.0), "");
kwiberg23c881a2016-11-01 19:04:26217
kwiberg1b0baa42017-06-26 08:31:31218 static_assert(!SafeLt(2, 2), "");
219 static_assert(!SafeLt(2, 2.0), "");
220 static_assert(!SafeLt(2.0, 2), "");
221 static_assert(!SafeLt(2.0, 2.0), "");
kwiberg23c881a2016-11-01 19:04:26222
kwiberg1b0baa42017-06-26 08:31:31223 EXPECT_FALSE(SafeLt(p1, p1));
224 EXPECT_TRUE(SafeLt(p1, p2));
225 EXPECT_FALSE(SafeLt(p2, p1));
226 EXPECT_FALSE(SafeLt(p2, p2));
kwiberg23c881a2016-11-01 19:04:26227}
228
229TEST(SafeCmpTest, Le) {
kwiberg1b0baa42017-06-26 08:31:31230 static_assert(SafeLe(-1, 2), "");
231 static_assert(SafeLe(-1, 2u), "");
232 static_assert(!SafeLe(2, -1), "");
233 static_assert(!SafeLe(2u, -1), "");
kwiberg23c881a2016-11-01 19:04:26234
kwiberg1b0baa42017-06-26 08:31:31235 static_assert(SafeLe(1, 2), "");
236 static_assert(SafeLe(1, 2u), "");
237 static_assert(SafeLe(1u, 2), "");
238 static_assert(SafeLe(1u, 2u), "");
239 static_assert(!SafeLe(2, 1), "");
240 static_assert(!SafeLe(2, 1u), "");
241 static_assert(!SafeLe(2u, 1), "");
242 static_assert(!SafeLe(2u, 1u), "");
kwiberg23c881a2016-11-01 19:04:26243
kwiberg1b0baa42017-06-26 08:31:31244 static_assert(SafeLe(2, 2), "");
245 static_assert(SafeLe(2, 2u), "");
246 static_assert(SafeLe(2u, 2), "");
247 static_assert(SafeLe(2u, 2u), "");
kwiberg23c881a2016-11-01 19:04:26248
kwiberg1b0baa42017-06-26 08:31:31249 static_assert(SafeLe(imin, imin), "");
250 static_assert(SafeLe(imin, umax), "");
251 static_assert(!SafeLe(umax, imin), "");
252 static_assert(SafeLe(umax, umax), "");
kwiberg23c881a2016-11-01 19:04:26253
kwiberg1b0baa42017-06-26 08:31:31254 static_assert(SafeLe(m1, m1), "");
255 static_assert(SafeLe(m1, umax), "");
256 static_assert(!SafeLe(umax, m1), "");
257 static_assert(SafeLe(umax, umax), "");
kwiberg23c881a2016-11-01 19:04:26258
kwiberg1b0baa42017-06-26 08:31:31259 static_assert(SafeLe(1, 2), "");
260 static_assert(SafeLe(1, 2.0), "");
261 static_assert(SafeLe(1.0, 2), "");
262 static_assert(SafeLe(1.0, 2.0), "");
263 static_assert(!SafeLe(2, 1), "");
264 static_assert(!SafeLe(2, 1.0), "");
265 static_assert(!SafeLe(2.0, 1), "");
266 static_assert(!SafeLe(2.0, 1.0), "");
kwiberg23c881a2016-11-01 19:04:26267
kwiberg1b0baa42017-06-26 08:31:31268 static_assert(SafeLe(2, 2), "");
269 static_assert(SafeLe(2, 2.0), "");
270 static_assert(SafeLe(2.0, 2), "");
271 static_assert(SafeLe(2.0, 2.0), "");
kwiberg23c881a2016-11-01 19:04:26272
kwiberg1b0baa42017-06-26 08:31:31273 EXPECT_TRUE(SafeLe(p1, p1));
274 EXPECT_TRUE(SafeLe(p1, p2));
275 EXPECT_FALSE(SafeLe(p2, p1));
276 EXPECT_TRUE(SafeLe(p2, p2));
kwiberg23c881a2016-11-01 19:04:26277}
278
279TEST(SafeCmpTest, Gt) {
kwiberg1b0baa42017-06-26 08:31:31280 static_assert(!SafeGt(-1, 2), "");
281 static_assert(!SafeGt(-1, 2u), "");
282 static_assert(SafeGt(2, -1), "");
283 static_assert(SafeGt(2u, -1), "");
kwiberg23c881a2016-11-01 19:04:26284
kwiberg1b0baa42017-06-26 08:31:31285 static_assert(!SafeGt(1, 2), "");
286 static_assert(!SafeGt(1, 2u), "");
287 static_assert(!SafeGt(1u, 2), "");
288 static_assert(!SafeGt(1u, 2u), "");
289 static_assert(SafeGt(2, 1), "");
290 static_assert(SafeGt(2, 1u), "");
291 static_assert(SafeGt(2u, 1), "");
292 static_assert(SafeGt(2u, 1u), "");
kwiberg23c881a2016-11-01 19:04:26293
kwiberg1b0baa42017-06-26 08:31:31294 static_assert(!SafeGt(2, 2), "");
295 static_assert(!SafeGt(2, 2u), "");
296 static_assert(!SafeGt(2u, 2), "");
297 static_assert(!SafeGt(2u, 2u), "");
kwiberg23c881a2016-11-01 19:04:26298
kwiberg1b0baa42017-06-26 08:31:31299 static_assert(!SafeGt(imin, imin), "");
300 static_assert(!SafeGt(imin, umax), "");
301 static_assert(SafeGt(umax, imin), "");
302 static_assert(!SafeGt(umax, umax), "");
kwiberg23c881a2016-11-01 19:04:26303
kwiberg1b0baa42017-06-26 08:31:31304 static_assert(!SafeGt(m1, m1), "");
305 static_assert(!SafeGt(m1, umax), "");
306 static_assert(SafeGt(umax, m1), "");
307 static_assert(!SafeGt(umax, umax), "");
kwiberg23c881a2016-11-01 19:04:26308
kwiberg1b0baa42017-06-26 08:31:31309 static_assert(!SafeGt(1, 2), "");
310 static_assert(!SafeGt(1, 2.0), "");
311 static_assert(!SafeGt(1.0, 2), "");
312 static_assert(!SafeGt(1.0, 2.0), "");
313 static_assert(SafeGt(2, 1), "");
314 static_assert(SafeGt(2, 1.0), "");
315 static_assert(SafeGt(2.0, 1), "");
316 static_assert(SafeGt(2.0, 1.0), "");
kwiberg23c881a2016-11-01 19:04:26317
kwiberg1b0baa42017-06-26 08:31:31318 static_assert(!SafeGt(2, 2), "");
319 static_assert(!SafeGt(2, 2.0), "");
320 static_assert(!SafeGt(2.0, 2), "");
321 static_assert(!SafeGt(2.0, 2.0), "");
kwiberg23c881a2016-11-01 19:04:26322
kwiberg1b0baa42017-06-26 08:31:31323 EXPECT_FALSE(SafeGt(p1, p1));
324 EXPECT_FALSE(SafeGt(p1, p2));
325 EXPECT_TRUE(SafeGt(p2, p1));
326 EXPECT_FALSE(SafeGt(p2, p2));
kwiberg23c881a2016-11-01 19:04:26327}
328
329TEST(SafeCmpTest, Ge) {
kwiberg1b0baa42017-06-26 08:31:31330 static_assert(!SafeGe(-1, 2), "");
331 static_assert(!SafeGe(-1, 2u), "");
332 static_assert(SafeGe(2, -1), "");
333 static_assert(SafeGe(2u, -1), "");
kwiberg23c881a2016-11-01 19:04:26334
kwiberg1b0baa42017-06-26 08:31:31335 static_assert(!SafeGe(1, 2), "");
336 static_assert(!SafeGe(1, 2u), "");
337 static_assert(!SafeGe(1u, 2), "");
338 static_assert(!SafeGe(1u, 2u), "");
339 static_assert(SafeGe(2, 1), "");
340 static_assert(SafeGe(2, 1u), "");
341 static_assert(SafeGe(2u, 1), "");
342 static_assert(SafeGe(2u, 1u), "");
kwiberg23c881a2016-11-01 19:04:26343
kwiberg1b0baa42017-06-26 08:31:31344 static_assert(SafeGe(2, 2), "");
345 static_assert(SafeGe(2, 2u), "");
346 static_assert(SafeGe(2u, 2), "");
347 static_assert(SafeGe(2u, 2u), "");
kwiberg23c881a2016-11-01 19:04:26348
kwiberg1b0baa42017-06-26 08:31:31349 static_assert(SafeGe(imin, imin), "");
350 static_assert(!SafeGe(imin, umax), "");
351 static_assert(SafeGe(umax, imin), "");
352 static_assert(SafeGe(umax, umax), "");
kwiberg23c881a2016-11-01 19:04:26353
kwiberg1b0baa42017-06-26 08:31:31354 static_assert(SafeGe(m1, m1), "");
355 static_assert(!SafeGe(m1, umax), "");
356 static_assert(SafeGe(umax, m1), "");
357 static_assert(SafeGe(umax, umax), "");
kwiberg23c881a2016-11-01 19:04:26358
kwiberg1b0baa42017-06-26 08:31:31359 static_assert(!SafeGe(1, 2), "");
360 static_assert(!SafeGe(1, 2.0), "");
361 static_assert(!SafeGe(1.0, 2), "");
362 static_assert(!SafeGe(1.0, 2.0), "");
363 static_assert(SafeGe(2, 1), "");
364 static_assert(SafeGe(2, 1.0), "");
365 static_assert(SafeGe(2.0, 1), "");
366 static_assert(SafeGe(2.0, 1.0), "");
kwiberg23c881a2016-11-01 19:04:26367
kwiberg1b0baa42017-06-26 08:31:31368 static_assert(SafeGe(2, 2), "");
369 static_assert(SafeGe(2, 2.0), "");
370 static_assert(SafeGe(2.0, 2), "");
371 static_assert(SafeGe(2.0, 2.0), "");
kwiberg23c881a2016-11-01 19:04:26372
kwiberg1b0baa42017-06-26 08:31:31373 EXPECT_TRUE(SafeGe(p1, p1));
374 EXPECT_FALSE(SafeGe(p1, p2));
375 EXPECT_TRUE(SafeGe(p2, p1));
376 EXPECT_TRUE(SafeGe(p2, p2));
kwiberg23c881a2016-11-01 19:04:26377}
378
kwiberg13d93262016-11-28 23:58:53379TEST(SafeCmpTest, Enum) {
380 enum E1 { e1 = 13 };
381 enum { e2 = 13 };
382 enum E3 : unsigned { e3 = 13 };
383 enum : unsigned { e4 = 13 };
kwiberg1b0baa42017-06-26 08:31:31384 static_assert(SafeEq(13, e1), "");
385 static_assert(SafeEq(13u, e1), "");
386 static_assert(SafeEq(13, e2), "");
387 static_assert(SafeEq(13u, e2), "");
388 static_assert(SafeEq(13, e3), "");
389 static_assert(SafeEq(13u, e3), "");
390 static_assert(SafeEq(13, e4), "");
391 static_assert(SafeEq(13u, e4), "");
kwiberg13d93262016-11-28 23:58:53392}
393
kwiberg23c881a2016-11-01 19:04:26394} // namespace rtc