1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS)
12 #include "base/compiler_specific.h"
13 #include "base/numerics/safe_conversions.h"
14 #include "base/numerics/safe_math.h"
15 #include "base/template_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using std::numeric_limits
;
19 using base::CheckedNumeric
;
20 using base::checked_cast
;
21 using base::saturated_cast
;
22 using base::internal::MaxExponent
;
23 using base::internal::RANGE_VALID
;
24 using base::internal::RANGE_INVALID
;
25 using base::internal::RANGE_OVERFLOW
;
26 using base::internal::RANGE_UNDERFLOW
;
27 using base::enable_if
;
29 // These tests deliberately cause arithmetic overflows. If the compiler is
30 // aggressive enough, it can const fold these overflows. Disable warnings about
31 // overflows for const expressions.
33 #pragma warning(disable:4756)
36 // Helper macros to wrap displaying the conversion types and line numbers.
37 #define TEST_EXPECTED_VALIDITY(expected, actual) \
38 EXPECT_EQ(expected, CheckedNumeric<Dst>(actual).validity()) \
39 << "Result test: Value " << +(actual).ValueUnsafe() << " as " << dst \
40 << " on line " << line;
42 #define TEST_EXPECTED_VALUE(expected, actual) \
43 EXPECT_EQ(static_cast<Dst>(expected), \
44 CheckedNumeric<Dst>(actual).ValueUnsafe()) \
45 << "Result test: Value " << +((actual).ValueUnsafe()) << " as " << dst \
46 << " on line " << line;
48 // Signed integer arithmetic.
49 template <typename Dst
>
50 static void TestSpecializedArithmetic(
54 numeric_limits
<Dst
>::is_integer
&& numeric_limits
<Dst
>::is_signed
,
56 typedef numeric_limits
<Dst
> DstLimits
;
57 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW
,
58 -CheckedNumeric
<Dst
>(DstLimits::min()));
59 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW
,
60 CheckedNumeric
<Dst
>(DstLimits::min()).Abs());
61 TEST_EXPECTED_VALUE(1, CheckedNumeric
<Dst
>(-1).Abs());
63 TEST_EXPECTED_VALIDITY(RANGE_VALID
,
64 CheckedNumeric
<Dst
>(DstLimits::max()) + -1);
65 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW
,
66 CheckedNumeric
<Dst
>(DstLimits::min()) + -1);
67 TEST_EXPECTED_VALIDITY(
69 CheckedNumeric
<Dst
>(-DstLimits::max()) + -DstLimits::max());
71 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW
,
72 CheckedNumeric
<Dst
>(DstLimits::min()) - 1);
73 TEST_EXPECTED_VALIDITY(RANGE_VALID
,
74 CheckedNumeric
<Dst
>(DstLimits::min()) - -1);
75 TEST_EXPECTED_VALIDITY(
77 CheckedNumeric
<Dst
>(DstLimits::max()) - -DstLimits::max());
78 TEST_EXPECTED_VALIDITY(
80 CheckedNumeric
<Dst
>(-DstLimits::max()) - DstLimits::max());
82 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW
,
83 CheckedNumeric
<Dst
>(DstLimits::min()) * 2);
85 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW
,
86 CheckedNumeric
<Dst
>(DstLimits::min()) / -1);
87 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(-1) / 2);
89 // Modulus is legal only for integers.
90 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>() % 1);
91 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(1) % 1);
92 TEST_EXPECTED_VALUE(-1, CheckedNumeric
<Dst
>(-1) % 2);
93 TEST_EXPECTED_VALIDITY(RANGE_INVALID
, CheckedNumeric
<Dst
>(-1) % -2);
94 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(DstLimits::min()) % 2);
95 TEST_EXPECTED_VALUE(1, CheckedNumeric
<Dst
>(DstLimits::max()) % 2);
96 // Test all the different modulus combinations.
97 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(1) % CheckedNumeric
<Dst
>(1));
98 TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric
<Dst
>(1));
99 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(1) % 1);
100 CheckedNumeric
<Dst
> checked_dst
= 1;
101 TEST_EXPECTED_VALUE(0, checked_dst
%= 1);
104 // Unsigned integer arithmetic.
105 template <typename Dst
>
106 static void TestSpecializedArithmetic(
110 numeric_limits
<Dst
>::is_integer
&& !numeric_limits
<Dst
>::is_signed
,
112 typedef numeric_limits
<Dst
> DstLimits
;
113 TEST_EXPECTED_VALIDITY(RANGE_VALID
, -CheckedNumeric
<Dst
>(DstLimits::min()));
114 TEST_EXPECTED_VALIDITY(RANGE_VALID
,
115 CheckedNumeric
<Dst
>(DstLimits::min()).Abs());
116 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW
,
117 CheckedNumeric
<Dst
>(DstLimits::min()) + -1);
118 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW
,
119 CheckedNumeric
<Dst
>(DstLimits::min()) - 1);
120 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(DstLimits::min()) * 2);
121 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(1) / 2);
123 // Modulus is legal only for integers.
124 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>() % 1);
125 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(1) % 1);
126 TEST_EXPECTED_VALUE(1, CheckedNumeric
<Dst
>(1) % 2);
127 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(DstLimits::min()) % 2);
128 TEST_EXPECTED_VALUE(1, CheckedNumeric
<Dst
>(DstLimits::max()) % 2);
129 // Test all the different modulus combinations.
130 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(1) % CheckedNumeric
<Dst
>(1));
131 TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric
<Dst
>(1));
132 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(1) % 1);
133 CheckedNumeric
<Dst
> checked_dst
= 1;
134 TEST_EXPECTED_VALUE(0, checked_dst
%= 1);
137 // Floating point arithmetic.
138 template <typename Dst
>
139 void TestSpecializedArithmetic(
142 typename enable_if
<numeric_limits
<Dst
>::is_iec559
, int>::type
= 0) {
143 typedef numeric_limits
<Dst
> DstLimits
;
144 TEST_EXPECTED_VALIDITY(RANGE_VALID
, -CheckedNumeric
<Dst
>(DstLimits::min()));
146 TEST_EXPECTED_VALIDITY(RANGE_VALID
,
147 CheckedNumeric
<Dst
>(DstLimits::min()).Abs());
148 TEST_EXPECTED_VALUE(1, CheckedNumeric
<Dst
>(-1).Abs());
150 TEST_EXPECTED_VALIDITY(RANGE_VALID
,
151 CheckedNumeric
<Dst
>(DstLimits::min()) + -1);
152 TEST_EXPECTED_VALIDITY(RANGE_VALID
,
153 CheckedNumeric
<Dst
>(DstLimits::max()) + 1);
154 TEST_EXPECTED_VALIDITY(
156 CheckedNumeric
<Dst
>(-DstLimits::max()) + -DstLimits::max());
158 TEST_EXPECTED_VALIDITY(
160 CheckedNumeric
<Dst
>(DstLimits::max()) - -DstLimits::max());
161 TEST_EXPECTED_VALIDITY(
163 CheckedNumeric
<Dst
>(-DstLimits::max()) - DstLimits::max());
165 TEST_EXPECTED_VALIDITY(RANGE_VALID
,
166 CheckedNumeric
<Dst
>(DstLimits::min()) * 2);
168 TEST_EXPECTED_VALUE(-0.5, CheckedNumeric
<Dst
>(-1.0) / 2);
169 EXPECT_EQ(static_cast<Dst
>(1.0), CheckedNumeric
<Dst
>(1.0).ValueFloating());
172 // Generic arithmetic tests.
173 template <typename Dst
>
174 static void TestArithmetic(const char* dst
, int line
) {
175 typedef numeric_limits
<Dst
> DstLimits
;
177 EXPECT_EQ(true, CheckedNumeric
<Dst
>().IsValid());
179 CheckedNumeric
<Dst
>(CheckedNumeric
<Dst
>(DstLimits::max()) *
180 DstLimits::max()).IsValid());
181 EXPECT_EQ(static_cast<Dst
>(0), CheckedNumeric
<Dst
>().ValueOrDie());
182 EXPECT_EQ(static_cast<Dst
>(0), CheckedNumeric
<Dst
>().ValueOrDefault(1));
183 EXPECT_EQ(static_cast<Dst
>(1),
184 CheckedNumeric
<Dst
>(CheckedNumeric
<Dst
>(DstLimits::max()) *
185 DstLimits::max()).ValueOrDefault(1));
187 // Test the operator combinations.
188 TEST_EXPECTED_VALUE(2, CheckedNumeric
<Dst
>(1) + CheckedNumeric
<Dst
>(1));
189 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(1) - CheckedNumeric
<Dst
>(1));
190 TEST_EXPECTED_VALUE(1, CheckedNumeric
<Dst
>(1) * CheckedNumeric
<Dst
>(1));
191 TEST_EXPECTED_VALUE(1, CheckedNumeric
<Dst
>(1) / CheckedNumeric
<Dst
>(1));
192 TEST_EXPECTED_VALUE(2, 1 + CheckedNumeric
<Dst
>(1));
193 TEST_EXPECTED_VALUE(0, 1 - CheckedNumeric
<Dst
>(1));
194 TEST_EXPECTED_VALUE(1, 1 * CheckedNumeric
<Dst
>(1));
195 TEST_EXPECTED_VALUE(1, 1 / CheckedNumeric
<Dst
>(1));
196 TEST_EXPECTED_VALUE(2, CheckedNumeric
<Dst
>(1) + 1);
197 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>(1) - 1);
198 TEST_EXPECTED_VALUE(1, CheckedNumeric
<Dst
>(1) * 1);
199 TEST_EXPECTED_VALUE(1, CheckedNumeric
<Dst
>(1) / 1);
200 CheckedNumeric
<Dst
> checked_dst
= 1;
201 TEST_EXPECTED_VALUE(2, checked_dst
+= 1);
203 TEST_EXPECTED_VALUE(0, checked_dst
-= 1);
205 TEST_EXPECTED_VALUE(1, checked_dst
*= 1);
207 TEST_EXPECTED_VALUE(1, checked_dst
/= 1);
210 TEST_EXPECTED_VALUE(0, -CheckedNumeric
<Dst
>());
211 TEST_EXPECTED_VALUE(-1, -CheckedNumeric
<Dst
>(1));
212 TEST_EXPECTED_VALUE(1, -CheckedNumeric
<Dst
>(-1));
213 TEST_EXPECTED_VALUE(static_cast<Dst
>(DstLimits::max() * -1),
214 -CheckedNumeric
<Dst
>(DstLimits::max()));
216 // Generic absolute value.
217 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>().Abs());
218 TEST_EXPECTED_VALUE(1, CheckedNumeric
<Dst
>(1).Abs());
219 TEST_EXPECTED_VALUE(DstLimits::max(),
220 CheckedNumeric
<Dst
>(DstLimits::max()).Abs());
223 TEST_EXPECTED_VALUE(1, (CheckedNumeric
<Dst
>() + 1));
224 TEST_EXPECTED_VALUE(2, (CheckedNumeric
<Dst
>(1) + 1));
225 TEST_EXPECTED_VALUE(0, (CheckedNumeric
<Dst
>(-1) + 1));
226 TEST_EXPECTED_VALIDITY(RANGE_VALID
,
227 CheckedNumeric
<Dst
>(DstLimits::min()) + 1);
228 TEST_EXPECTED_VALIDITY(
229 RANGE_OVERFLOW
, CheckedNumeric
<Dst
>(DstLimits::max()) + DstLimits::max());
231 // Generic subtraction.
232 TEST_EXPECTED_VALUE(-1, (CheckedNumeric
<Dst
>() - 1));
233 TEST_EXPECTED_VALUE(0, (CheckedNumeric
<Dst
>(1) - 1));
234 TEST_EXPECTED_VALUE(-2, (CheckedNumeric
<Dst
>(-1) - 1));
235 TEST_EXPECTED_VALIDITY(RANGE_VALID
,
236 CheckedNumeric
<Dst
>(DstLimits::max()) - 1);
238 // Generic multiplication.
239 TEST_EXPECTED_VALUE(0, (CheckedNumeric
<Dst
>() * 1));
240 TEST_EXPECTED_VALUE(1, (CheckedNumeric
<Dst
>(1) * 1));
241 TEST_EXPECTED_VALUE(-2, (CheckedNumeric
<Dst
>(-1) * 2));
242 TEST_EXPECTED_VALIDITY(
243 RANGE_OVERFLOW
, CheckedNumeric
<Dst
>(DstLimits::max()) * DstLimits::max());
246 TEST_EXPECTED_VALUE(0, CheckedNumeric
<Dst
>() / 1);
247 TEST_EXPECTED_VALUE(1, CheckedNumeric
<Dst
>(1) / 1);
248 TEST_EXPECTED_VALUE(DstLimits::min() / 2,
249 CheckedNumeric
<Dst
>(DstLimits::min()) / 2);
250 TEST_EXPECTED_VALUE(DstLimits::max() / 2,
251 CheckedNumeric
<Dst
>(DstLimits::max()) / 2);
253 TestSpecializedArithmetic
<Dst
>(dst
, line
);
256 // Helper macro to wrap displaying the conversion types and line numbers.
257 #define TEST_ARITHMETIC(Dst) TestArithmetic<Dst>(#Dst, __LINE__)
259 TEST(SafeNumerics
, SignedIntegerMath
) {
260 TEST_ARITHMETIC(int8_t);
261 TEST_ARITHMETIC(int);
262 TEST_ARITHMETIC(intptr_t);
263 TEST_ARITHMETIC(intmax_t);
266 TEST(SafeNumerics
, UnsignedIntegerMath
) {
267 TEST_ARITHMETIC(uint8_t);
268 TEST_ARITHMETIC(unsigned int);
269 TEST_ARITHMETIC(uintptr_t);
270 TEST_ARITHMETIC(uintmax_t);
273 TEST(SafeNumerics
, FloatingPointMath
) {
274 TEST_ARITHMETIC(float);
275 TEST_ARITHMETIC(double);
278 // Enumerates the five different conversions types we need to test.
279 enum NumericConversionType
{
280 SIGN_PRESERVING_VALUE_PRESERVING
,
281 SIGN_PRESERVING_NARROW
,
282 SIGN_TO_UNSIGN_WIDEN_OR_EQUAL
,
283 SIGN_TO_UNSIGN_NARROW
,
284 UNSIGN_TO_SIGN_NARROW_OR_EQUAL
,
287 // Template covering the different conversion tests.
288 template <typename Dst
, typename Src
, NumericConversionType conversion
>
289 struct TestNumericConversion
{};
291 // EXPECT_EQ wrappers providing specific detail on test failures.
292 #define TEST_EXPECTED_RANGE(expected, actual) \
293 EXPECT_EQ(expected, base::internal::DstRangeRelationToSrcRange<Dst>(actual)) \
294 << "Conversion test: " << src << " value " << actual << " to " << dst \
295 << " on line " << line;
297 template <typename Dst
, typename Src
>
298 struct TestNumericConversion
<Dst
, Src
, SIGN_PRESERVING_VALUE_PRESERVING
> {
299 static void Test(const char *dst
, const char *src
, int line
) {
300 typedef numeric_limits
<Src
> SrcLimits
;
301 typedef numeric_limits
<Dst
> DstLimits
;
302 // Integral to floating.
303 static_assert((DstLimits::is_iec559
&& SrcLimits::is_integer
) ||
304 // Not floating to integral and...
305 (!(DstLimits::is_integer
&& SrcLimits::is_iec559
) &&
306 // Same sign, same numeric, source is narrower or same.
307 ((SrcLimits::is_signed
== DstLimits::is_signed
&&
308 sizeof(Dst
) >= sizeof(Src
)) ||
309 // Or signed destination and source is smaller
310 (DstLimits::is_signed
&& sizeof(Dst
) > sizeof(Src
)))),
311 "Comparison must be sign preserving and value preserving");
313 const CheckedNumeric
<Dst
> checked_dst
= SrcLimits::max();
315 TEST_EXPECTED_VALIDITY(RANGE_VALID
, checked_dst
);
316 if (MaxExponent
<Dst
>::value
> MaxExponent
<Src
>::value
) {
317 if (MaxExponent
<Dst
>::value
>= MaxExponent
<Src
>::value
* 2 - 1) {
318 // At least twice larger type.
319 TEST_EXPECTED_VALIDITY(RANGE_VALID
, SrcLimits::max() * checked_dst
);
321 } else { // Larger, but not at least twice as large.
322 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW
, SrcLimits::max() * checked_dst
);
323 TEST_EXPECTED_VALIDITY(RANGE_VALID
, checked_dst
+ 1);
325 } else { // Same width type.
326 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW
, checked_dst
+ 1);
329 TEST_EXPECTED_RANGE(RANGE_VALID
, SrcLimits::max());
330 TEST_EXPECTED_RANGE(RANGE_VALID
, static_cast<Src
>(1));
331 if (SrcLimits::is_iec559
) {
332 TEST_EXPECTED_RANGE(RANGE_VALID
, SrcLimits::max() * static_cast<Src
>(-1));
333 TEST_EXPECTED_RANGE(RANGE_OVERFLOW
, SrcLimits::infinity());
334 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW
, SrcLimits::infinity() * -1);
335 TEST_EXPECTED_RANGE(RANGE_INVALID
, SrcLimits::quiet_NaN());
336 } else if (numeric_limits
<Src
>::is_signed
) {
337 TEST_EXPECTED_RANGE(RANGE_VALID
, static_cast<Src
>(-1));
338 TEST_EXPECTED_RANGE(RANGE_VALID
, SrcLimits::min());
343 template <typename Dst
, typename Src
>
344 struct TestNumericConversion
<Dst
, Src
, SIGN_PRESERVING_NARROW
> {
345 static void Test(const char *dst
, const char *src
, int line
) {
346 typedef numeric_limits
<Src
> SrcLimits
;
347 typedef numeric_limits
<Dst
> DstLimits
;
348 static_assert(SrcLimits::is_signed
== DstLimits::is_signed
,
349 "Destination and source sign must be the same");
350 static_assert(sizeof(Dst
) < sizeof(Src
) ||
351 (DstLimits::is_integer
&& SrcLimits::is_iec559
),
352 "Destination must be narrower than source");
354 const CheckedNumeric
<Dst
> checked_dst
;
355 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW
, checked_dst
+ SrcLimits::max());
356 TEST_EXPECTED_VALUE(1, checked_dst
+ static_cast<Src
>(1));
357 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW
, checked_dst
- SrcLimits::max());
359 TEST_EXPECTED_RANGE(RANGE_OVERFLOW
, SrcLimits::max());
360 TEST_EXPECTED_RANGE(RANGE_VALID
, static_cast<Src
>(1));
361 if (SrcLimits::is_iec559
) {
362 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW
, SrcLimits::max() * -1);
363 TEST_EXPECTED_RANGE(RANGE_VALID
, static_cast<Src
>(-1));
364 TEST_EXPECTED_RANGE(RANGE_OVERFLOW
, SrcLimits::infinity());
365 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW
, SrcLimits::infinity() * -1);
366 TEST_EXPECTED_RANGE(RANGE_INVALID
, SrcLimits::quiet_NaN());
367 } else if (SrcLimits::is_signed
) {
368 TEST_EXPECTED_VALUE(-1, checked_dst
- static_cast<Src
>(1));
369 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW
, SrcLimits::min());
370 TEST_EXPECTED_RANGE(RANGE_VALID
, static_cast<Src
>(-1));
372 TEST_EXPECTED_VALIDITY(RANGE_INVALID
, checked_dst
- static_cast<Src
>(1));
373 TEST_EXPECTED_RANGE(RANGE_VALID
, SrcLimits::min());
378 template <typename Dst
, typename Src
>
379 struct TestNumericConversion
<Dst
, Src
, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL
> {
380 static void Test(const char *dst
, const char *src
, int line
) {
381 typedef numeric_limits
<Src
> SrcLimits
;
382 typedef numeric_limits
<Dst
> DstLimits
;
383 static_assert(sizeof(Dst
) >= sizeof(Src
),
384 "Destination must be equal or wider than source.");
385 static_assert(SrcLimits::is_signed
, "Source must be signed");
386 static_assert(!DstLimits::is_signed
, "Destination must be unsigned");
388 const CheckedNumeric
<Dst
> checked_dst
;
389 TEST_EXPECTED_VALUE(SrcLimits::max(), checked_dst
+ SrcLimits::max());
390 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW
, checked_dst
+ static_cast<Src
>(-1));
391 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW
, checked_dst
+ -SrcLimits::max());
393 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW
, SrcLimits::min());
394 TEST_EXPECTED_RANGE(RANGE_VALID
, SrcLimits::max());
395 TEST_EXPECTED_RANGE(RANGE_VALID
, static_cast<Src
>(1));
396 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW
, static_cast<Src
>(-1));
400 template <typename Dst
, typename Src
>
401 struct TestNumericConversion
<Dst
, Src
, SIGN_TO_UNSIGN_NARROW
> {
402 static void Test(const char *dst
, const char *src
, int line
) {
403 typedef numeric_limits
<Src
> SrcLimits
;
404 typedef numeric_limits
<Dst
> DstLimits
;
405 static_assert((DstLimits::is_integer
&& SrcLimits::is_iec559
) ||
406 (sizeof(Dst
) < sizeof(Src
)),
407 "Destination must be narrower than source.");
408 static_assert(SrcLimits::is_signed
, "Source must be signed.");
409 static_assert(!DstLimits::is_signed
, "Destination must be unsigned.");
411 const CheckedNumeric
<Dst
> checked_dst
;
412 TEST_EXPECTED_VALUE(1, checked_dst
+ static_cast<Src
>(1));
413 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW
, checked_dst
+ SrcLimits::max());
414 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW
, checked_dst
+ static_cast<Src
>(-1));
415 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW
, checked_dst
+ -SrcLimits::max());
417 TEST_EXPECTED_RANGE(RANGE_OVERFLOW
, SrcLimits::max());
418 TEST_EXPECTED_RANGE(RANGE_VALID
, static_cast<Src
>(1));
419 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW
, static_cast<Src
>(-1));
420 if (SrcLimits::is_iec559
) {
421 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW
, SrcLimits::max() * -1);
422 TEST_EXPECTED_RANGE(RANGE_OVERFLOW
, SrcLimits::infinity());
423 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW
, SrcLimits::infinity() * -1);
424 TEST_EXPECTED_RANGE(RANGE_INVALID
, SrcLimits::quiet_NaN());
426 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW
, SrcLimits::min());
431 template <typename Dst
, typename Src
>
432 struct TestNumericConversion
<Dst
, Src
, UNSIGN_TO_SIGN_NARROW_OR_EQUAL
> {
433 static void Test(const char *dst
, const char *src
, int line
) {
434 typedef numeric_limits
<Src
> SrcLimits
;
435 typedef numeric_limits
<Dst
> DstLimits
;
436 static_assert(sizeof(Dst
) <= sizeof(Src
),
437 "Destination must be narrower or equal to source.");
438 static_assert(!SrcLimits::is_signed
, "Source must be unsigned.");
439 static_assert(DstLimits::is_signed
, "Destination must be signed.");
441 const CheckedNumeric
<Dst
> checked_dst
;
442 TEST_EXPECTED_VALUE(1, checked_dst
+ static_cast<Src
>(1));
443 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW
, checked_dst
+ SrcLimits::max());
444 TEST_EXPECTED_VALUE(SrcLimits::min(), checked_dst
+ SrcLimits::min());
446 TEST_EXPECTED_RANGE(RANGE_VALID
, SrcLimits::min());
447 TEST_EXPECTED_RANGE(RANGE_OVERFLOW
, SrcLimits::max());
448 TEST_EXPECTED_RANGE(RANGE_VALID
, static_cast<Src
>(1));
452 // Helper macro to wrap displaying the conversion types and line numbers
453 #define TEST_NUMERIC_CONVERSION(d, s, t) \
454 TestNumericConversion<d, s, t>::Test(#d, #s, __LINE__)
456 TEST(SafeNumerics
, IntMinOperations
) {
457 TEST_NUMERIC_CONVERSION(int8_t, int8_t, SIGN_PRESERVING_VALUE_PRESERVING
);
458 TEST_NUMERIC_CONVERSION(uint8_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING
);
460 TEST_NUMERIC_CONVERSION(int8_t, int, SIGN_PRESERVING_NARROW
);
461 TEST_NUMERIC_CONVERSION(uint8_t, unsigned int, SIGN_PRESERVING_NARROW
);
462 TEST_NUMERIC_CONVERSION(int8_t, float, SIGN_PRESERVING_NARROW
);
464 TEST_NUMERIC_CONVERSION(uint8_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL
);
466 TEST_NUMERIC_CONVERSION(uint8_t, int, SIGN_TO_UNSIGN_NARROW
);
467 TEST_NUMERIC_CONVERSION(uint8_t, intmax_t, SIGN_TO_UNSIGN_NARROW
);
468 TEST_NUMERIC_CONVERSION(uint8_t, float, SIGN_TO_UNSIGN_NARROW
);
470 TEST_NUMERIC_CONVERSION(int8_t, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL
);
471 TEST_NUMERIC_CONVERSION(int8_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL
);
474 TEST(SafeNumerics
, IntOperations
) {
475 TEST_NUMERIC_CONVERSION(int, int, SIGN_PRESERVING_VALUE_PRESERVING
);
476 TEST_NUMERIC_CONVERSION(unsigned int, unsigned int,
477 SIGN_PRESERVING_VALUE_PRESERVING
);
478 TEST_NUMERIC_CONVERSION(int, int8_t, SIGN_PRESERVING_VALUE_PRESERVING
);
479 TEST_NUMERIC_CONVERSION(unsigned int, uint8_t,
480 SIGN_PRESERVING_VALUE_PRESERVING
);
481 TEST_NUMERIC_CONVERSION(int, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING
);
483 TEST_NUMERIC_CONVERSION(int, intmax_t, SIGN_PRESERVING_NARROW
);
484 TEST_NUMERIC_CONVERSION(unsigned int, uintmax_t, SIGN_PRESERVING_NARROW
);
485 TEST_NUMERIC_CONVERSION(int, float, SIGN_PRESERVING_NARROW
);
486 TEST_NUMERIC_CONVERSION(int, double, SIGN_PRESERVING_NARROW
);
488 TEST_NUMERIC_CONVERSION(unsigned int, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL
);
489 TEST_NUMERIC_CONVERSION(unsigned int, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL
);
491 TEST_NUMERIC_CONVERSION(unsigned int, intmax_t, SIGN_TO_UNSIGN_NARROW
);
492 TEST_NUMERIC_CONVERSION(unsigned int, float, SIGN_TO_UNSIGN_NARROW
);
493 TEST_NUMERIC_CONVERSION(unsigned int, double, SIGN_TO_UNSIGN_NARROW
);
495 TEST_NUMERIC_CONVERSION(int, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL
);
496 TEST_NUMERIC_CONVERSION(int, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL
);
499 TEST(SafeNumerics
, IntMaxOperations
) {
500 TEST_NUMERIC_CONVERSION(intmax_t, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING
);
501 TEST_NUMERIC_CONVERSION(uintmax_t, uintmax_t,
502 SIGN_PRESERVING_VALUE_PRESERVING
);
503 TEST_NUMERIC_CONVERSION(intmax_t, int, SIGN_PRESERVING_VALUE_PRESERVING
);
504 TEST_NUMERIC_CONVERSION(uintmax_t, unsigned int,
505 SIGN_PRESERVING_VALUE_PRESERVING
);
506 TEST_NUMERIC_CONVERSION(intmax_t, unsigned int,
507 SIGN_PRESERVING_VALUE_PRESERVING
);
508 TEST_NUMERIC_CONVERSION(intmax_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING
);
510 TEST_NUMERIC_CONVERSION(intmax_t, float, SIGN_PRESERVING_NARROW
);
511 TEST_NUMERIC_CONVERSION(intmax_t, double, SIGN_PRESERVING_NARROW
);
513 TEST_NUMERIC_CONVERSION(uintmax_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL
);
514 TEST_NUMERIC_CONVERSION(uintmax_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL
);
516 TEST_NUMERIC_CONVERSION(uintmax_t, float, SIGN_TO_UNSIGN_NARROW
);
517 TEST_NUMERIC_CONVERSION(uintmax_t, double, SIGN_TO_UNSIGN_NARROW
);
519 TEST_NUMERIC_CONVERSION(intmax_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL
);
522 TEST(SafeNumerics
, FloatOperations
) {
523 TEST_NUMERIC_CONVERSION(float, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING
);
524 TEST_NUMERIC_CONVERSION(float, uintmax_t,
525 SIGN_PRESERVING_VALUE_PRESERVING
);
526 TEST_NUMERIC_CONVERSION(float, int, SIGN_PRESERVING_VALUE_PRESERVING
);
527 TEST_NUMERIC_CONVERSION(float, unsigned int,
528 SIGN_PRESERVING_VALUE_PRESERVING
);
530 TEST_NUMERIC_CONVERSION(float, double, SIGN_PRESERVING_NARROW
);
533 TEST(SafeNumerics
, DoubleOperations
) {
534 TEST_NUMERIC_CONVERSION(double, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING
);
535 TEST_NUMERIC_CONVERSION(double, uintmax_t,
536 SIGN_PRESERVING_VALUE_PRESERVING
);
537 TEST_NUMERIC_CONVERSION(double, int, SIGN_PRESERVING_VALUE_PRESERVING
);
538 TEST_NUMERIC_CONVERSION(double, unsigned int,
539 SIGN_PRESERVING_VALUE_PRESERVING
);
542 TEST(SafeNumerics
, SizeTOperations
) {
543 TEST_NUMERIC_CONVERSION(size_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL
);
544 TEST_NUMERIC_CONVERSION(int, size_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL
);
547 TEST(SafeNumerics
, CastTests
) {
548 // MSVC catches and warns that we're forcing saturation in these tests.
549 // Since that's intentional, we need to shut this warning off.
550 #if defined(COMPILER_MSVC)
551 #pragma warning(disable : 4756)
554 int small_positive
= 1;
555 int small_negative
= -1;
556 double double_small
= 1.0;
557 double double_large
= numeric_limits
<double>::max();
558 double double_infinity
= numeric_limits
<float>::infinity();
560 // Just test that the cast compiles, since the other tests cover logic.
561 EXPECT_EQ(0, checked_cast
<int>(static_cast<size_t>(0)));
563 // Test various saturation corner cases.
564 EXPECT_EQ(saturated_cast
<int>(small_negative
),
565 static_cast<int>(small_negative
));
566 EXPECT_EQ(saturated_cast
<int>(small_positive
),
567 static_cast<int>(small_positive
));
568 EXPECT_EQ(saturated_cast
<unsigned>(small_negative
),
569 static_cast<unsigned>(0));
570 EXPECT_EQ(saturated_cast
<int>(double_small
),
571 static_cast<int>(double_small
));
572 EXPECT_EQ(saturated_cast
<int>(double_large
), numeric_limits
<int>::max());
573 EXPECT_EQ(saturated_cast
<float>(double_large
), double_infinity
);
574 EXPECT_EQ(saturated_cast
<float>(-double_large
), -double_infinity
);