summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/rtc_base/numerics/safe_compare_unittest.cc
blob: 92bde686baa2517d1c2a4a43a2f1e13a66279bd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 *  Copyright 2016 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "rtc_base/numerics/safe_compare.h"

#include <limits>

#include "test/gtest.h"

namespace rtc {

namespace {

constexpr std::uintmax_t umax = std::numeric_limits<std::uintmax_t>::max();
constexpr std::intmax_t imin = std::numeric_limits<std::intmax_t>::min();
constexpr std::intmax_t m1 = -1;

// m1 and umax have the same representation because we use 2's complement
// arithmetic, so naive casting will confuse them.
static_assert(static_cast<std::uintmax_t>(m1) == umax, "");
static_assert(m1 == static_cast<std::intmax_t>(umax), "");

static const std::pair<int, int> p1(1, 1);
static const std::pair<int, int> p2(1, 2);

}  // namespace

// clang-format off

// These functions aren't used in the tests, but it's useful to look at the
// compiler output for them, and verify that (1) the same-signedness *Safe
// functions result in exactly the same code as their *Ref counterparts, and
// that (2) the mixed-signedness *Safe functions have just a few extra
// arithmetic and logic instructions (but no extra control flow instructions).
bool TestLessThanRef(      int a,      int b) { return a < b; }
bool TestLessThanRef( unsigned a, unsigned b) { return a < b; }
bool TestLessThanSafe(     int a,      int b) { return SafeLt(a, b); }
bool TestLessThanSafe(unsigned a, unsigned b) { return SafeLt(a, b); }
bool TestLessThanSafe(unsigned a,      int b) { return SafeLt(a, b); }
bool TestLessThanSafe(     int a, unsigned b) { return SafeLt(a, b); }

// For these, we expect the *Ref and *Safe functions to result in identical
// code, except for the ones that compare a signed variable with an unsigned
// constant; in that case, the *Ref function does an unsigned comparison (fast
// but incorrect) and the *Safe function spends a few extra instructions on
// doing it right.
bool TestLessThan17Ref(       int a) { return a < 17; }
bool TestLessThan17Ref(  unsigned a) { return a < 17; }
bool TestLessThan17uRef(      int a) { return static_cast<unsigned>(a) < 17u; }
bool TestLessThan17uRef( unsigned a) { return a < 17u; }
bool TestLessThan17Safe(      int a) { return SafeLt(a, 17); }
bool TestLessThan17Safe( unsigned a) { return SafeLt(a, 17); }
bool TestLessThan17uSafe(     int a) { return SafeLt(a, 17u); }
bool TestLessThan17uSafe(unsigned a) { return SafeLt(a, 17u); }

// Cases where we can't convert to a larger signed type.
bool TestLessThanMax( intmax_t a, uintmax_t b) { return SafeLt(a, b); }
bool TestLessThanMax(uintmax_t a,  intmax_t b) { return SafeLt(a, b); }
bool TestLessThanMax17u( intmax_t a) { return SafeLt(a, uintmax_t{17}); }
bool TestLessThanMax17( uintmax_t a) { return SafeLt(a,  intmax_t{17}); }

// Cases where the compiler should be able to compute the result at compile
// time.
bool TestLessThanConst1() { return SafeLt(  -1,    1); }
bool TestLessThanConst2() { return SafeLt(  m1, umax); }
bool TestLessThanConst3() { return SafeLt(umax, imin); }
bool TestLessThanConst4(unsigned a) { return SafeLt( a, -1); }
bool TestLessThanConst5(unsigned a) { return SafeLt(-1,  a); }
bool TestLessThanConst6(unsigned a) { return SafeLt( a,  a); }

// clang-format on

TEST(SafeCmpTest, Eq) {
  static_assert(!SafeEq(-1, 2), "");
  static_assert(!SafeEq(-1, 2u), "");
  static_assert(!SafeEq(2, -1), "");
  static_assert(!SafeEq(2u, -1), "");

  static_assert(!SafeEq(1, 2), "");
  static_assert(!SafeEq(1, 2u), "");
  static_assert(!SafeEq(1u, 2), "");
  static_assert(!SafeEq(1u, 2u), "");
  static_assert(!SafeEq(2, 1), "");
  static_assert(!SafeEq(2, 1u), "");
  static_assert(!SafeEq(2u, 1), "");
  static_assert(!SafeEq(2u, 1u), "");

  static_assert(SafeEq(2, 2), "");
  static_assert(SafeEq(2, 2u), "");
  static_assert(SafeEq(2u, 2), "");
  static_assert(SafeEq(2u, 2u), "");

  static_assert(SafeEq(imin, imin), "");
  static_assert(!SafeEq(imin, umax), "");
  static_assert(!SafeEq(umax, imin), "");
  static_assert(SafeEq(umax, umax), "");

  static_assert(SafeEq(m1, m1), "");
  static_assert(!SafeEq(m1, umax), "");
  static_assert(!SafeEq(umax, m1), "");
  static_assert(SafeEq(umax, umax), "");

  static_assert(!SafeEq(1, 2), "");
  static_assert(!SafeEq(1, 2.0), "");
  static_assert(!SafeEq(1.0, 2), "");
  static_assert(!SafeEq(1.0, 2.0), "");
  static_assert(!SafeEq(2, 1), "");
  static_assert(!SafeEq(2, 1.0), "");
  static_assert(!SafeEq(2.0, 1), "");
  static_assert(!SafeEq(2.0, 1.0), "");

  static_assert(SafeEq(2, 2), "");
  static_assert(SafeEq(2, 2.0), "");
  static_assert(SafeEq(2.0, 2), "");
  static_assert(SafeEq(2.0, 2.0), "");

  EXPECT_TRUE(SafeEq(p1, p1));
  EXPECT_FALSE(SafeEq(p1, p2));
  EXPECT_FALSE(SafeEq(p2, p1));
  EXPECT_TRUE(SafeEq(p2, p2));
}

TEST(SafeCmpTest, Ne) {
  static_assert(SafeNe(-1, 2), "");
  static_assert(SafeNe(-1, 2u), "");
  static_assert(SafeNe(2, -1), "");
  static_assert(SafeNe(2u, -1), "");

  static_assert(SafeNe(1, 2), "");
  static_assert(SafeNe(1, 2u), "");
  static_assert(SafeNe(1u, 2), "");
  static_assert(SafeNe(1u, 2u), "");
  static_assert(SafeNe(2, 1), "");
  static_assert(SafeNe(2, 1u), "");
  static_assert(SafeNe(2u, 1), "");
  static_assert(SafeNe(2u, 1u), "");

  static_assert(!SafeNe(2, 2), "");
  static_assert(!SafeNe(2, 2u), "");
  static_assert(!SafeNe(2u, 2), "");
  static_assert(!SafeNe(2u, 2u), "");

  static_assert(!SafeNe(imin, imin), "");
  static_assert(SafeNe(imin, umax), "");
  static_assert(SafeNe(umax, imin), "");
  static_assert(!SafeNe(umax, umax), "");

  static_assert(!SafeNe(m1, m1), "");
  static_assert(SafeNe(m1, umax), "");
  static_assert(SafeNe(umax, m1), "");
  static_assert(!SafeNe(umax, umax), "");

  static_assert(SafeNe(1, 2), "");
  static_assert(SafeNe(1, 2.0), "");
  static_assert(SafeNe(1.0, 2), "");
  static_assert(SafeNe(1.0, 2.0), "");
  static_assert(SafeNe(2, 1), "");
  static_assert(SafeNe(2, 1.0), "");
  static_assert(SafeNe(2.0, 1), "");
  static_assert(SafeNe(2.0, 1.0), "");

  static_assert(!SafeNe(2, 2), "");
  static_assert(!SafeNe(2, 2.0), "");
  static_assert(!SafeNe(2.0, 2), "");
  static_assert(!SafeNe(2.0, 2.0), "");

  EXPECT_FALSE(SafeNe(p1, p1));
  EXPECT_TRUE(SafeNe(p1, p2));
  EXPECT_TRUE(SafeNe(p2, p1));
  EXPECT_FALSE(SafeNe(p2, p2));
}

TEST(SafeCmpTest, Lt) {
  static_assert(SafeLt(-1, 2), "");
  static_assert(SafeLt(-1, 2u), "");
  static_assert(!SafeLt(2, -1), "");
  static_assert(!SafeLt(2u, -1), "");

  static_assert(SafeLt(1, 2), "");
  static_assert(SafeLt(1, 2u), "");
  static_assert(SafeLt(1u, 2), "");
  static_assert(SafeLt(1u, 2u), "");
  static_assert(!SafeLt(2, 1), "");
  static_assert(!SafeLt(2, 1u), "");
  static_assert(!SafeLt(2u, 1), "");
  static_assert(!SafeLt(2u, 1u), "");

  static_assert(!SafeLt(2, 2), "");
  static_assert(!SafeLt(2, 2u), "");
  static_assert(!SafeLt(2u, 2), "");
  static_assert(!SafeLt(2u, 2u), "");

  static_assert(!SafeLt(imin, imin), "");
  static_assert(SafeLt(imin, umax), "");
  static_assert(!SafeLt(umax, imin), "");
  static_assert(!SafeLt(umax, umax), "");

  static_assert(!SafeLt(m1, m1), "");
  static_assert(SafeLt(m1, umax), "");
  static_assert(!SafeLt(umax, m1), "");
  static_assert(!SafeLt(umax, umax), "");

  static_assert(SafeLt(1, 2), "");
  static_assert(SafeLt(1, 2.0), "");
  static_assert(SafeLt(1.0, 2), "");
  static_assert(SafeLt(1.0, 2.0), "");
  static_assert(!SafeLt(2, 1), "");
  static_assert(!SafeLt(2, 1.0), "");
  static_assert(!SafeLt(2.0, 1), "");
  static_assert(!SafeLt(2.0, 1.0), "");

  static_assert(!SafeLt(2, 2), "");
  static_assert(!SafeLt(2, 2.0), "");
  static_assert(!SafeLt(2.0, 2), "");
  static_assert(!SafeLt(2.0, 2.0), "");

  EXPECT_FALSE(SafeLt(p1, p1));
  EXPECT_TRUE(SafeLt(p1, p2));
  EXPECT_FALSE(SafeLt(p2, p1));
  EXPECT_FALSE(SafeLt(p2, p2));
}

TEST(SafeCmpTest, Le) {
  static_assert(SafeLe(-1, 2), "");
  static_assert(SafeLe(-1, 2u), "");
  static_assert(!SafeLe(2, -1), "");
  static_assert(!SafeLe(2u, -1), "");

  static_assert(SafeLe(1, 2), "");
  static_assert(SafeLe(1, 2u), "");
  static_assert(SafeLe(1u, 2), "");
  static_assert(SafeLe(1u, 2u), "");
  static_assert(!SafeLe(2, 1), "");
  static_assert(!SafeLe(2, 1u), "");
  static_assert(!SafeLe(2u, 1), "");
  static_assert(!SafeLe(2u, 1u), "");

  static_assert(SafeLe(2, 2), "");
  static_assert(SafeLe(2, 2u), "");
  static_assert(SafeLe(2u, 2), "");
  static_assert(SafeLe(2u, 2u), "");

  static_assert(SafeLe(imin, imin), "");
  static_assert(SafeLe(imin, umax), "");
  static_assert(!SafeLe(umax, imin), "");
  static_assert(SafeLe(umax, umax), "");

  static_assert(SafeLe(m1, m1), "");
  static_assert(SafeLe(m1, umax), "");
  static_assert(!SafeLe(umax, m1), "");
  static_assert(SafeLe(umax, umax), "");

  static_assert(SafeLe(1, 2), "");
  static_assert(SafeLe(1, 2.0), "");
  static_assert(SafeLe(1.0, 2), "");
  static_assert(SafeLe(1.0, 2.0), "");
  static_assert(!SafeLe(2, 1), "");
  static_assert(!SafeLe(2, 1.0), "");
  static_assert(!SafeLe(2.0, 1), "");
  static_assert(!SafeLe(2.0, 1.0), "");

  static_assert(SafeLe(2, 2), "");
  static_assert(SafeLe(2, 2.0), "");
  static_assert(SafeLe(2.0, 2), "");
  static_assert(SafeLe(2.0, 2.0), "");

  EXPECT_TRUE(SafeLe(p1, p1));
  EXPECT_TRUE(SafeLe(p1, p2));
  EXPECT_FALSE(SafeLe(p2, p1));
  EXPECT_TRUE(SafeLe(p2, p2));
}

TEST(SafeCmpTest, Gt) {
  static_assert(!SafeGt(-1, 2), "");
  static_assert(!SafeGt(-1, 2u), "");
  static_assert(SafeGt(2, -1), "");
  static_assert(SafeGt(2u, -1), "");

  static_assert(!SafeGt(1, 2), "");
  static_assert(!SafeGt(1, 2u), "");
  static_assert(!SafeGt(1u, 2), "");
  static_assert(!SafeGt(1u, 2u), "");
  static_assert(SafeGt(2, 1), "");
  static_assert(SafeGt(2, 1u), "");
  static_assert(SafeGt(2u, 1), "");
  static_assert(SafeGt(2u, 1u), "");

  static_assert(!SafeGt(2, 2), "");
  static_assert(!SafeGt(2, 2u), "");
  static_assert(!SafeGt(2u, 2), "");
  static_assert(!SafeGt(2u, 2u), "");

  static_assert(!SafeGt(imin, imin), "");
  static_assert(!SafeGt(imin, umax), "");
  static_assert(SafeGt(umax, imin), "");
  static_assert(!SafeGt(umax, umax), "");

  static_assert(!SafeGt(m1, m1), "");
  static_assert(!SafeGt(m1, umax), "");
  static_assert(SafeGt(umax, m1), "");
  static_assert(!SafeGt(umax, umax), "");

  static_assert(!SafeGt(1, 2), "");
  static_assert(!SafeGt(1, 2.0), "");
  static_assert(!SafeGt(1.0, 2), "");
  static_assert(!SafeGt(1.0, 2.0), "");
  static_assert(SafeGt(2, 1), "");
  static_assert(SafeGt(2, 1.0), "");
  static_assert(SafeGt(2.0, 1), "");
  static_assert(SafeGt(2.0, 1.0), "");

  static_assert(!SafeGt(2, 2), "");
  static_assert(!SafeGt(2, 2.0), "");
  static_assert(!SafeGt(2.0, 2), "");
  static_assert(!SafeGt(2.0, 2.0), "");

  EXPECT_FALSE(SafeGt(p1, p1));
  EXPECT_FALSE(SafeGt(p1, p2));
  EXPECT_TRUE(SafeGt(p2, p1));
  EXPECT_FALSE(SafeGt(p2, p2));
}

TEST(SafeCmpTest, Ge) {
  static_assert(!SafeGe(-1, 2), "");
  static_assert(!SafeGe(-1, 2u), "");
  static_assert(SafeGe(2, -1), "");
  static_assert(SafeGe(2u, -1), "");

  static_assert(!SafeGe(1, 2), "");
  static_assert(!SafeGe(1, 2u), "");
  static_assert(!SafeGe(1u, 2), "");
  static_assert(!SafeGe(1u, 2u), "");
  static_assert(SafeGe(2, 1), "");
  static_assert(SafeGe(2, 1u), "");
  static_assert(SafeGe(2u, 1), "");
  static_assert(SafeGe(2u, 1u), "");

  static_assert(SafeGe(2, 2), "");
  static_assert(SafeGe(2, 2u), "");
  static_assert(SafeGe(2u, 2), "");
  static_assert(SafeGe(2u, 2u), "");

  static_assert(SafeGe(imin, imin), "");
  static_assert(!SafeGe(imin, umax), "");
  static_assert(SafeGe(umax, imin), "");
  static_assert(SafeGe(umax, umax), "");

  static_assert(SafeGe(m1, m1), "");
  static_assert(!SafeGe(m1, umax), "");
  static_assert(SafeGe(umax, m1), "");
  static_assert(SafeGe(umax, umax), "");

  static_assert(!SafeGe(1, 2), "");
  static_assert(!SafeGe(1, 2.0), "");
  static_assert(!SafeGe(1.0, 2), "");
  static_assert(!SafeGe(1.0, 2.0), "");
  static_assert(SafeGe(2, 1), "");
  static_assert(SafeGe(2, 1.0), "");
  static_assert(SafeGe(2.0, 1), "");
  static_assert(SafeGe(2.0, 1.0), "");

  static_assert(SafeGe(2, 2), "");
  static_assert(SafeGe(2, 2.0), "");
  static_assert(SafeGe(2.0, 2), "");
  static_assert(SafeGe(2.0, 2.0), "");

  EXPECT_TRUE(SafeGe(p1, p1));
  EXPECT_FALSE(SafeGe(p1, p2));
  EXPECT_TRUE(SafeGe(p2, p1));
  EXPECT_TRUE(SafeGe(p2, p2));
}

TEST(SafeCmpTest, Enum) {
  enum E1 { e1 = 13 };
  enum { e2 = 13 };
  enum E3 : unsigned { e3 = 13 };
  enum : unsigned { e4 = 13 };
  static_assert(SafeEq(13, e1), "");
  static_assert(SafeEq(13u, e1), "");
  static_assert(SafeEq(13, e2), "");
  static_assert(SafeEq(13u, e2), "");
  static_assert(SafeEq(13, e3), "");
  static_assert(SafeEq(13u, e3), "");
  static_assert(SafeEq(13, e4), "");
  static_assert(SafeEq(13u, e4), "");
}

}  // namespace rtc