1 /*
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 "rtc_base/numerics/safe_compare.h"
12 
13 #include <limits>
14 
15 #include "test/gtest.h"
16 
17 namespace rtc {
18 
19 namespace {
20 
21 constexpr std::uintmax_t umax = std::numeric_limits<std::uintmax_t>::max();
22 constexpr std::intmax_t imin = std::numeric_limits<std::intmax_t>::min();
23 constexpr std::intmax_t m1 = -1;
24 
25 // m1 and umax have the same representation because we use 2's complement
26 // arithmetic, so naive casting will confuse them.
27 static_assert(static_cast<std::uintmax_t>(m1) == umax, "");
28 static_assert(m1 == static_cast<std::intmax_t>(umax), "");
29 
30 static const std::pair<int, int> p1(1, 1);
31 static const std::pair<int, int> p2(1, 2);
32 
33 }  // namespace
34 
35 // clang-format off
36 
37 // These functions aren't used in the tests, but it's useful to look at the
38 // compiler output for them, and verify that (1) the same-signedness *Safe
39 // functions result in exactly the same code as their *Ref counterparts, and
40 // that (2) the mixed-signedness *Safe functions have just a few extra
41 // arithmetic and logic instructions (but no extra control flow instructions).
TestLessThanRef(int a,int b)42 bool TestLessThanRef(      int a,      int b) { return a < b; }
TestLessThanRef(unsigned a,unsigned b)43 bool TestLessThanRef( unsigned a, unsigned b) { return a < b; }
TestLessThanSafe(int a,int b)44 bool TestLessThanSafe(     int a,      int b) { return SafeLt(a, b); }
TestLessThanSafe(unsigned a,unsigned b)45 bool TestLessThanSafe(unsigned a, unsigned b) { return SafeLt(a, b); }
TestLessThanSafe(unsigned a,int b)46 bool TestLessThanSafe(unsigned a,      int b) { return SafeLt(a, b); }
TestLessThanSafe(int a,unsigned b)47 bool TestLessThanSafe(     int a, unsigned b) { return SafeLt(a, b); }
48 
49 // For these, we expect the *Ref and *Safe functions to result in identical
50 // code, except for the ones that compare a signed variable with an unsigned
51 // constant; in that case, the *Ref function does an unsigned comparison (fast
52 // but incorrect) and the *Safe function spends a few extra instructions on
53 // doing it right.
TestLessThan17Ref(int a)54 bool TestLessThan17Ref(       int a) { return a < 17; }
TestLessThan17Ref(unsigned a)55 bool TestLessThan17Ref(  unsigned a) { return a < 17; }
TestLessThan17uRef(int a)56 bool TestLessThan17uRef(      int a) { return static_cast<unsigned>(a) < 17u; }
TestLessThan17uRef(unsigned a)57 bool TestLessThan17uRef( unsigned a) { return a < 17u; }
TestLessThan17Safe(int a)58 bool TestLessThan17Safe(      int a) { return SafeLt(a, 17); }
TestLessThan17Safe(unsigned a)59 bool TestLessThan17Safe( unsigned a) { return SafeLt(a, 17); }
TestLessThan17uSafe(int a)60 bool TestLessThan17uSafe(     int a) { return SafeLt(a, 17u); }
TestLessThan17uSafe(unsigned a)61 bool TestLessThan17uSafe(unsigned a) { return SafeLt(a, 17u); }
62 
63 // Cases where we can't convert to a larger signed type.
TestLessThanMax(intmax_t a,uintmax_t b)64 bool TestLessThanMax( intmax_t a, uintmax_t b) { return SafeLt(a, b); }
TestLessThanMax(uintmax_t a,intmax_t b)65 bool TestLessThanMax(uintmax_t a,  intmax_t b) { return SafeLt(a, b); }
TestLessThanMax17u(intmax_t a)66 bool TestLessThanMax17u( intmax_t a) { return SafeLt(a, uintmax_t{17}); }
TestLessThanMax17(uintmax_t a)67 bool TestLessThanMax17( uintmax_t a) { return SafeLt(a,  intmax_t{17}); }
68 
69 // Cases where the compiler should be able to compute the result at compile
70 // time.
TestLessThanConst1()71 bool TestLessThanConst1() { return SafeLt(  -1,    1); }
TestLessThanConst2()72 bool TestLessThanConst2() { return SafeLt(  m1, umax); }
TestLessThanConst3()73 bool TestLessThanConst3() { return SafeLt(umax, imin); }
TestLessThanConst4(unsigned a)74 bool TestLessThanConst4(unsigned a) { return SafeLt( a, -1); }
TestLessThanConst5(unsigned a)75 bool TestLessThanConst5(unsigned a) { return SafeLt(-1,  a); }
TestLessThanConst6(unsigned a)76 bool TestLessThanConst6(unsigned a) { return SafeLt( a,  a); }
77 
78 // clang-format on
79 
TEST(SafeCmpTest,Eq)80 TEST(SafeCmpTest, Eq) {
81   static_assert(!SafeEq(-1, 2), "");
82   static_assert(!SafeEq(-1, 2u), "");
83   static_assert(!SafeEq(2, -1), "");
84   static_assert(!SafeEq(2u, -1), "");
85 
86   static_assert(!SafeEq(1, 2), "");
87   static_assert(!SafeEq(1, 2u), "");
88   static_assert(!SafeEq(1u, 2), "");
89   static_assert(!SafeEq(1u, 2u), "");
90   static_assert(!SafeEq(2, 1), "");
91   static_assert(!SafeEq(2, 1u), "");
92   static_assert(!SafeEq(2u, 1), "");
93   static_assert(!SafeEq(2u, 1u), "");
94 
95   static_assert(SafeEq(2, 2), "");
96   static_assert(SafeEq(2, 2u), "");
97   static_assert(SafeEq(2u, 2), "");
98   static_assert(SafeEq(2u, 2u), "");
99 
100   static_assert(SafeEq(imin, imin), "");
101   static_assert(!SafeEq(imin, umax), "");
102   static_assert(!SafeEq(umax, imin), "");
103   static_assert(SafeEq(umax, umax), "");
104 
105   static_assert(SafeEq(m1, m1), "");
106   static_assert(!SafeEq(m1, umax), "");
107   static_assert(!SafeEq(umax, m1), "");
108   static_assert(SafeEq(umax, umax), "");
109 
110   static_assert(!SafeEq(1, 2), "");
111   static_assert(!SafeEq(1, 2.0), "");
112   static_assert(!SafeEq(1.0, 2), "");
113   static_assert(!SafeEq(1.0, 2.0), "");
114   static_assert(!SafeEq(2, 1), "");
115   static_assert(!SafeEq(2, 1.0), "");
116   static_assert(!SafeEq(2.0, 1), "");
117   static_assert(!SafeEq(2.0, 1.0), "");
118 
119   static_assert(SafeEq(2, 2), "");
120   static_assert(SafeEq(2, 2.0), "");
121   static_assert(SafeEq(2.0, 2), "");
122   static_assert(SafeEq(2.0, 2.0), "");
123 
124   EXPECT_TRUE(SafeEq(p1, p1));
125   EXPECT_FALSE(SafeEq(p1, p2));
126   EXPECT_FALSE(SafeEq(p2, p1));
127   EXPECT_TRUE(SafeEq(p2, p2));
128 }
129 
TEST(SafeCmpTest,Ne)130 TEST(SafeCmpTest, Ne) {
131   static_assert(SafeNe(-1, 2), "");
132   static_assert(SafeNe(-1, 2u), "");
133   static_assert(SafeNe(2, -1), "");
134   static_assert(SafeNe(2u, -1), "");
135 
136   static_assert(SafeNe(1, 2), "");
137   static_assert(SafeNe(1, 2u), "");
138   static_assert(SafeNe(1u, 2), "");
139   static_assert(SafeNe(1u, 2u), "");
140   static_assert(SafeNe(2, 1), "");
141   static_assert(SafeNe(2, 1u), "");
142   static_assert(SafeNe(2u, 1), "");
143   static_assert(SafeNe(2u, 1u), "");
144 
145   static_assert(!SafeNe(2, 2), "");
146   static_assert(!SafeNe(2, 2u), "");
147   static_assert(!SafeNe(2u, 2), "");
148   static_assert(!SafeNe(2u, 2u), "");
149 
150   static_assert(!SafeNe(imin, imin), "");
151   static_assert(SafeNe(imin, umax), "");
152   static_assert(SafeNe(umax, imin), "");
153   static_assert(!SafeNe(umax, umax), "");
154 
155   static_assert(!SafeNe(m1, m1), "");
156   static_assert(SafeNe(m1, umax), "");
157   static_assert(SafeNe(umax, m1), "");
158   static_assert(!SafeNe(umax, umax), "");
159 
160   static_assert(SafeNe(1, 2), "");
161   static_assert(SafeNe(1, 2.0), "");
162   static_assert(SafeNe(1.0, 2), "");
163   static_assert(SafeNe(1.0, 2.0), "");
164   static_assert(SafeNe(2, 1), "");
165   static_assert(SafeNe(2, 1.0), "");
166   static_assert(SafeNe(2.0, 1), "");
167   static_assert(SafeNe(2.0, 1.0), "");
168 
169   static_assert(!SafeNe(2, 2), "");
170   static_assert(!SafeNe(2, 2.0), "");
171   static_assert(!SafeNe(2.0, 2), "");
172   static_assert(!SafeNe(2.0, 2.0), "");
173 
174   EXPECT_FALSE(SafeNe(p1, p1));
175   EXPECT_TRUE(SafeNe(p1, p2));
176   EXPECT_TRUE(SafeNe(p2, p1));
177   EXPECT_FALSE(SafeNe(p2, p2));
178 }
179 
TEST(SafeCmpTest,Lt)180 TEST(SafeCmpTest, Lt) {
181   static_assert(SafeLt(-1, 2), "");
182   static_assert(SafeLt(-1, 2u), "");
183   static_assert(!SafeLt(2, -1), "");
184   static_assert(!SafeLt(2u, -1), "");
185 
186   static_assert(SafeLt(1, 2), "");
187   static_assert(SafeLt(1, 2u), "");
188   static_assert(SafeLt(1u, 2), "");
189   static_assert(SafeLt(1u, 2u), "");
190   static_assert(!SafeLt(2, 1), "");
191   static_assert(!SafeLt(2, 1u), "");
192   static_assert(!SafeLt(2u, 1), "");
193   static_assert(!SafeLt(2u, 1u), "");
194 
195   static_assert(!SafeLt(2, 2), "");
196   static_assert(!SafeLt(2, 2u), "");
197   static_assert(!SafeLt(2u, 2), "");
198   static_assert(!SafeLt(2u, 2u), "");
199 
200   static_assert(!SafeLt(imin, imin), "");
201   static_assert(SafeLt(imin, umax), "");
202   static_assert(!SafeLt(umax, imin), "");
203   static_assert(!SafeLt(umax, umax), "");
204 
205   static_assert(!SafeLt(m1, m1), "");
206   static_assert(SafeLt(m1, umax), "");
207   static_assert(!SafeLt(umax, m1), "");
208   static_assert(!SafeLt(umax, umax), "");
209 
210   static_assert(SafeLt(1, 2), "");
211   static_assert(SafeLt(1, 2.0), "");
212   static_assert(SafeLt(1.0, 2), "");
213   static_assert(SafeLt(1.0, 2.0), "");
214   static_assert(!SafeLt(2, 1), "");
215   static_assert(!SafeLt(2, 1.0), "");
216   static_assert(!SafeLt(2.0, 1), "");
217   static_assert(!SafeLt(2.0, 1.0), "");
218 
219   static_assert(!SafeLt(2, 2), "");
220   static_assert(!SafeLt(2, 2.0), "");
221   static_assert(!SafeLt(2.0, 2), "");
222   static_assert(!SafeLt(2.0, 2.0), "");
223 
224   EXPECT_FALSE(SafeLt(p1, p1));
225   EXPECT_TRUE(SafeLt(p1, p2));
226   EXPECT_FALSE(SafeLt(p2, p1));
227   EXPECT_FALSE(SafeLt(p2, p2));
228 }
229 
TEST(SafeCmpTest,Le)230 TEST(SafeCmpTest, Le) {
231   static_assert(SafeLe(-1, 2), "");
232   static_assert(SafeLe(-1, 2u), "");
233   static_assert(!SafeLe(2, -1), "");
234   static_assert(!SafeLe(2u, -1), "");
235 
236   static_assert(SafeLe(1, 2), "");
237   static_assert(SafeLe(1, 2u), "");
238   static_assert(SafeLe(1u, 2), "");
239   static_assert(SafeLe(1u, 2u), "");
240   static_assert(!SafeLe(2, 1), "");
241   static_assert(!SafeLe(2, 1u), "");
242   static_assert(!SafeLe(2u, 1), "");
243   static_assert(!SafeLe(2u, 1u), "");
244 
245   static_assert(SafeLe(2, 2), "");
246   static_assert(SafeLe(2, 2u), "");
247   static_assert(SafeLe(2u, 2), "");
248   static_assert(SafeLe(2u, 2u), "");
249 
250   static_assert(SafeLe(imin, imin), "");
251   static_assert(SafeLe(imin, umax), "");
252   static_assert(!SafeLe(umax, imin), "");
253   static_assert(SafeLe(umax, umax), "");
254 
255   static_assert(SafeLe(m1, m1), "");
256   static_assert(SafeLe(m1, umax), "");
257   static_assert(!SafeLe(umax, m1), "");
258   static_assert(SafeLe(umax, umax), "");
259 
260   static_assert(SafeLe(1, 2), "");
261   static_assert(SafeLe(1, 2.0), "");
262   static_assert(SafeLe(1.0, 2), "");
263   static_assert(SafeLe(1.0, 2.0), "");
264   static_assert(!SafeLe(2, 1), "");
265   static_assert(!SafeLe(2, 1.0), "");
266   static_assert(!SafeLe(2.0, 1), "");
267   static_assert(!SafeLe(2.0, 1.0), "");
268 
269   static_assert(SafeLe(2, 2), "");
270   static_assert(SafeLe(2, 2.0), "");
271   static_assert(SafeLe(2.0, 2), "");
272   static_assert(SafeLe(2.0, 2.0), "");
273 
274   EXPECT_TRUE(SafeLe(p1, p1));
275   EXPECT_TRUE(SafeLe(p1, p2));
276   EXPECT_FALSE(SafeLe(p2, p1));
277   EXPECT_TRUE(SafeLe(p2, p2));
278 }
279 
TEST(SafeCmpTest,Gt)280 TEST(SafeCmpTest, Gt) {
281   static_assert(!SafeGt(-1, 2), "");
282   static_assert(!SafeGt(-1, 2u), "");
283   static_assert(SafeGt(2, -1), "");
284   static_assert(SafeGt(2u, -1), "");
285 
286   static_assert(!SafeGt(1, 2), "");
287   static_assert(!SafeGt(1, 2u), "");
288   static_assert(!SafeGt(1u, 2), "");
289   static_assert(!SafeGt(1u, 2u), "");
290   static_assert(SafeGt(2, 1), "");
291   static_assert(SafeGt(2, 1u), "");
292   static_assert(SafeGt(2u, 1), "");
293   static_assert(SafeGt(2u, 1u), "");
294 
295   static_assert(!SafeGt(2, 2), "");
296   static_assert(!SafeGt(2, 2u), "");
297   static_assert(!SafeGt(2u, 2), "");
298   static_assert(!SafeGt(2u, 2u), "");
299 
300   static_assert(!SafeGt(imin, imin), "");
301   static_assert(!SafeGt(imin, umax), "");
302   static_assert(SafeGt(umax, imin), "");
303   static_assert(!SafeGt(umax, umax), "");
304 
305   static_assert(!SafeGt(m1, m1), "");
306   static_assert(!SafeGt(m1, umax), "");
307   static_assert(SafeGt(umax, m1), "");
308   static_assert(!SafeGt(umax, umax), "");
309 
310   static_assert(!SafeGt(1, 2), "");
311   static_assert(!SafeGt(1, 2.0), "");
312   static_assert(!SafeGt(1.0, 2), "");
313   static_assert(!SafeGt(1.0, 2.0), "");
314   static_assert(SafeGt(2, 1), "");
315   static_assert(SafeGt(2, 1.0), "");
316   static_assert(SafeGt(2.0, 1), "");
317   static_assert(SafeGt(2.0, 1.0), "");
318 
319   static_assert(!SafeGt(2, 2), "");
320   static_assert(!SafeGt(2, 2.0), "");
321   static_assert(!SafeGt(2.0, 2), "");
322   static_assert(!SafeGt(2.0, 2.0), "");
323 
324   EXPECT_FALSE(SafeGt(p1, p1));
325   EXPECT_FALSE(SafeGt(p1, p2));
326   EXPECT_TRUE(SafeGt(p2, p1));
327   EXPECT_FALSE(SafeGt(p2, p2));
328 }
329 
TEST(SafeCmpTest,Ge)330 TEST(SafeCmpTest, Ge) {
331   static_assert(!SafeGe(-1, 2), "");
332   static_assert(!SafeGe(-1, 2u), "");
333   static_assert(SafeGe(2, -1), "");
334   static_assert(SafeGe(2u, -1), "");
335 
336   static_assert(!SafeGe(1, 2), "");
337   static_assert(!SafeGe(1, 2u), "");
338   static_assert(!SafeGe(1u, 2), "");
339   static_assert(!SafeGe(1u, 2u), "");
340   static_assert(SafeGe(2, 1), "");
341   static_assert(SafeGe(2, 1u), "");
342   static_assert(SafeGe(2u, 1), "");
343   static_assert(SafeGe(2u, 1u), "");
344 
345   static_assert(SafeGe(2, 2), "");
346   static_assert(SafeGe(2, 2u), "");
347   static_assert(SafeGe(2u, 2), "");
348   static_assert(SafeGe(2u, 2u), "");
349 
350   static_assert(SafeGe(imin, imin), "");
351   static_assert(!SafeGe(imin, umax), "");
352   static_assert(SafeGe(umax, imin), "");
353   static_assert(SafeGe(umax, umax), "");
354 
355   static_assert(SafeGe(m1, m1), "");
356   static_assert(!SafeGe(m1, umax), "");
357   static_assert(SafeGe(umax, m1), "");
358   static_assert(SafeGe(umax, umax), "");
359 
360   static_assert(!SafeGe(1, 2), "");
361   static_assert(!SafeGe(1, 2.0), "");
362   static_assert(!SafeGe(1.0, 2), "");
363   static_assert(!SafeGe(1.0, 2.0), "");
364   static_assert(SafeGe(2, 1), "");
365   static_assert(SafeGe(2, 1.0), "");
366   static_assert(SafeGe(2.0, 1), "");
367   static_assert(SafeGe(2.0, 1.0), "");
368 
369   static_assert(SafeGe(2, 2), "");
370   static_assert(SafeGe(2, 2.0), "");
371   static_assert(SafeGe(2.0, 2), "");
372   static_assert(SafeGe(2.0, 2.0), "");
373 
374   EXPECT_TRUE(SafeGe(p1, p1));
375   EXPECT_FALSE(SafeGe(p1, p2));
376   EXPECT_TRUE(SafeGe(p2, p1));
377   EXPECT_TRUE(SafeGe(p2, p2));
378 }
379 
TEST(SafeCmpTest,Enum)380 TEST(SafeCmpTest, Enum) {
381   enum E1 { e1 = 13 };
382   enum { e2 = 13 };
383   enum E3 : unsigned { e3 = 13 };
384   enum : unsigned { e4 = 13 };
385   static_assert(SafeEq(13, e1), "");
386   static_assert(SafeEq(13u, e1), "");
387   static_assert(SafeEq(13, e2), "");
388   static_assert(SafeEq(13u, e2), "");
389   static_assert(SafeEq(13, e3), "");
390   static_assert(SafeEq(13u, e3), "");
391   static_assert(SafeEq(13, e4), "");
392   static_assert(SafeEq(13u, e4), "");
393 }
394 
395 }  // namespace rtc
396