Roll ANGLE with a translator gyp change.
[chromium-blink-merge.git] / base / numerics / safe_numerics_unittest.cc
blob09ad1305a148f4f0fef21ea3a95b63caa1234b62
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)
6 #include <mmintrin.h>
7 #endif
8 #include <stdint.h>
10 #include <limits>
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 // MSVS 2013 ia32 may not reset the FPU between calculations, and the test
30 // framework masks the exceptions. So we just force a manual reset after NaN.
31 inline void ResetFloatingPointUnit() {
32 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS)
33 _mm_empty();
34 #endif
37 // Helper macros to wrap displaying the conversion types and line numbers.
38 #define TEST_EXPECTED_VALIDITY(expected, actual) \
39 EXPECT_EQ(expected, CheckedNumeric<Dst>(actual).validity()) \
40 << "Result test: Value " << +(actual).ValueUnsafe() << " as " << dst \
41 << " on line " << line;
43 #define TEST_EXPECTED_VALUE(expected, actual) \
44 EXPECT_EQ(static_cast<Dst>(expected), \
45 CheckedNumeric<Dst>(actual).ValueUnsafe()) \
46 << "Result test: Value " << +((actual).ValueUnsafe()) << " as " << dst \
47 << " on line " << line;
49 // Signed integer arithmetic.
50 template <typename Dst>
51 static void TestSpecializedArithmetic(
52 const char* dst,
53 int line,
54 typename enable_if<
55 numeric_limits<Dst>::is_integer&& numeric_limits<Dst>::is_signed,
56 int>::type = 0) {
57 typedef numeric_limits<Dst> DstLimits;
58 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW,
59 -CheckedNumeric<Dst>(DstLimits::min()));
60 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW,
61 CheckedNumeric<Dst>(DstLimits::min()).Abs());
62 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(-1).Abs());
64 TEST_EXPECTED_VALIDITY(RANGE_VALID,
65 CheckedNumeric<Dst>(DstLimits::max()) + -1);
66 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW,
67 CheckedNumeric<Dst>(DstLimits::min()) + -1);
68 TEST_EXPECTED_VALIDITY(
69 RANGE_UNDERFLOW,
70 CheckedNumeric<Dst>(-DstLimits::max()) + -DstLimits::max());
72 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW,
73 CheckedNumeric<Dst>(DstLimits::min()) - 1);
74 TEST_EXPECTED_VALIDITY(RANGE_VALID,
75 CheckedNumeric<Dst>(DstLimits::min()) - -1);
76 TEST_EXPECTED_VALIDITY(
77 RANGE_OVERFLOW,
78 CheckedNumeric<Dst>(DstLimits::max()) - -DstLimits::max());
79 TEST_EXPECTED_VALIDITY(
80 RANGE_UNDERFLOW,
81 CheckedNumeric<Dst>(-DstLimits::max()) - DstLimits::max());
83 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW,
84 CheckedNumeric<Dst>(DstLimits::min()) * 2);
86 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW,
87 CheckedNumeric<Dst>(DstLimits::min()) / -1);
88 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(-1) / 2);
90 // Modulus is legal only for integers.
91 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() % 1);
92 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1);
93 TEST_EXPECTED_VALUE(-1, CheckedNumeric<Dst>(-1) % 2);
94 TEST_EXPECTED_VALIDITY(RANGE_INVALID, CheckedNumeric<Dst>(-1) % -2);
95 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) % 2);
96 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(DstLimits::max()) % 2);
97 // Test all the different modulus combinations.
98 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % CheckedNumeric<Dst>(1));
99 TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric<Dst>(1));
100 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1);
101 CheckedNumeric<Dst> checked_dst = 1;
102 TEST_EXPECTED_VALUE(0, checked_dst %= 1);
105 // Unsigned integer arithmetic.
106 template <typename Dst>
107 static void TestSpecializedArithmetic(
108 const char* dst,
109 int line,
110 typename enable_if<
111 numeric_limits<Dst>::is_integer && !numeric_limits<Dst>::is_signed,
112 int>::type = 0) {
113 typedef numeric_limits<Dst> DstLimits;
114 TEST_EXPECTED_VALIDITY(RANGE_VALID, -CheckedNumeric<Dst>(DstLimits::min()));
115 TEST_EXPECTED_VALIDITY(RANGE_VALID,
116 CheckedNumeric<Dst>(DstLimits::min()).Abs());
117 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW,
118 CheckedNumeric<Dst>(DstLimits::min()) + -1);
119 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW,
120 CheckedNumeric<Dst>(DstLimits::min()) - 1);
121 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) * 2);
122 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) / 2);
124 // Modulus is legal only for integers.
125 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() % 1);
126 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1);
127 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) % 2);
128 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) % 2);
129 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(DstLimits::max()) % 2);
130 // Test all the different modulus combinations.
131 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % CheckedNumeric<Dst>(1));
132 TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric<Dst>(1));
133 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1);
134 CheckedNumeric<Dst> checked_dst = 1;
135 TEST_EXPECTED_VALUE(0, checked_dst %= 1);
138 // Floating point arithmetic.
139 template <typename Dst>
140 void TestSpecializedArithmetic(
141 const char* dst,
142 int line,
143 typename enable_if<numeric_limits<Dst>::is_iec559, int>::type = 0) {
144 typedef numeric_limits<Dst> DstLimits;
145 TEST_EXPECTED_VALIDITY(RANGE_VALID, -CheckedNumeric<Dst>(DstLimits::min()));
147 TEST_EXPECTED_VALIDITY(RANGE_VALID,
148 CheckedNumeric<Dst>(DstLimits::min()).Abs());
149 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(-1).Abs());
151 TEST_EXPECTED_VALIDITY(RANGE_VALID,
152 CheckedNumeric<Dst>(DstLimits::min()) + -1);
153 TEST_EXPECTED_VALIDITY(RANGE_VALID,
154 CheckedNumeric<Dst>(DstLimits::max()) + 1);
155 TEST_EXPECTED_VALIDITY(
156 RANGE_UNDERFLOW,
157 CheckedNumeric<Dst>(-DstLimits::max()) + -DstLimits::max());
159 TEST_EXPECTED_VALIDITY(
160 RANGE_OVERFLOW,
161 CheckedNumeric<Dst>(DstLimits::max()) - -DstLimits::max());
162 TEST_EXPECTED_VALIDITY(
163 RANGE_UNDERFLOW,
164 CheckedNumeric<Dst>(-DstLimits::max()) - DstLimits::max());
166 TEST_EXPECTED_VALIDITY(RANGE_VALID,
167 CheckedNumeric<Dst>(DstLimits::min()) * 2);
169 TEST_EXPECTED_VALUE(-0.5, CheckedNumeric<Dst>(-1.0) / 2);
170 EXPECT_EQ(static_cast<Dst>(1.0), CheckedNumeric<Dst>(1.0).ValueFloating());
173 // Generic arithmetic tests.
174 template <typename Dst>
175 static void TestArithmetic(const char* dst, int line) {
176 typedef numeric_limits<Dst> DstLimits;
178 EXPECT_EQ(true, CheckedNumeric<Dst>().IsValid());
179 EXPECT_EQ(false,
180 CheckedNumeric<Dst>(CheckedNumeric<Dst>(DstLimits::max()) *
181 DstLimits::max()).IsValid());
182 EXPECT_EQ(static_cast<Dst>(0), CheckedNumeric<Dst>().ValueOrDie());
183 EXPECT_EQ(static_cast<Dst>(0), CheckedNumeric<Dst>().ValueOrDefault(1));
184 EXPECT_EQ(static_cast<Dst>(1),
185 CheckedNumeric<Dst>(CheckedNumeric<Dst>(DstLimits::max()) *
186 DstLimits::max()).ValueOrDefault(1));
188 // Test the operator combinations.
189 TEST_EXPECTED_VALUE(2, CheckedNumeric<Dst>(1) + CheckedNumeric<Dst>(1));
190 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) - CheckedNumeric<Dst>(1));
191 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) * CheckedNumeric<Dst>(1));
192 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / CheckedNumeric<Dst>(1));
193 TEST_EXPECTED_VALUE(2, 1 + CheckedNumeric<Dst>(1));
194 TEST_EXPECTED_VALUE(0, 1 - CheckedNumeric<Dst>(1));
195 TEST_EXPECTED_VALUE(1, 1 * CheckedNumeric<Dst>(1));
196 TEST_EXPECTED_VALUE(1, 1 / CheckedNumeric<Dst>(1));
197 TEST_EXPECTED_VALUE(2, CheckedNumeric<Dst>(1) + 1);
198 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) - 1);
199 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) * 1);
200 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / 1);
201 CheckedNumeric<Dst> checked_dst = 1;
202 TEST_EXPECTED_VALUE(2, checked_dst += 1);
203 checked_dst = 1;
204 TEST_EXPECTED_VALUE(0, checked_dst -= 1);
205 checked_dst = 1;
206 TEST_EXPECTED_VALUE(1, checked_dst *= 1);
207 checked_dst = 1;
208 TEST_EXPECTED_VALUE(1, checked_dst /= 1);
210 // Generic negation.
211 TEST_EXPECTED_VALUE(0, -CheckedNumeric<Dst>());
212 TEST_EXPECTED_VALUE(-1, -CheckedNumeric<Dst>(1));
213 TEST_EXPECTED_VALUE(1, -CheckedNumeric<Dst>(-1));
214 TEST_EXPECTED_VALUE(static_cast<Dst>(DstLimits::max() * -1),
215 -CheckedNumeric<Dst>(DstLimits::max()));
217 // Generic absolute value.
218 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>().Abs());
219 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1).Abs());
220 TEST_EXPECTED_VALUE(DstLimits::max(),
221 CheckedNumeric<Dst>(DstLimits::max()).Abs());
223 // Generic addition.
224 TEST_EXPECTED_VALUE(1, (CheckedNumeric<Dst>() + 1));
225 TEST_EXPECTED_VALUE(2, (CheckedNumeric<Dst>(1) + 1));
226 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(-1) + 1));
227 TEST_EXPECTED_VALIDITY(RANGE_VALID,
228 CheckedNumeric<Dst>(DstLimits::min()) + 1);
229 TEST_EXPECTED_VALIDITY(
230 RANGE_OVERFLOW, CheckedNumeric<Dst>(DstLimits::max()) + DstLimits::max());
232 // Generic subtraction.
233 TEST_EXPECTED_VALUE(-1, (CheckedNumeric<Dst>() - 1));
234 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(1) - 1));
235 TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) - 1));
236 TEST_EXPECTED_VALIDITY(RANGE_VALID,
237 CheckedNumeric<Dst>(DstLimits::max()) - 1);
239 // Generic multiplication.
240 TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>() * 1));
241 TEST_EXPECTED_VALUE(1, (CheckedNumeric<Dst>(1) * 1));
242 TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) * 2));
243 TEST_EXPECTED_VALIDITY(
244 RANGE_OVERFLOW, CheckedNumeric<Dst>(DstLimits::max()) * DstLimits::max());
246 // Generic division.
247 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() / 1);
248 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / 1);
249 TEST_EXPECTED_VALUE(DstLimits::min() / 2,
250 CheckedNumeric<Dst>(DstLimits::min()) / 2);
251 TEST_EXPECTED_VALUE(DstLimits::max() / 2,
252 CheckedNumeric<Dst>(DstLimits::max()) / 2);
254 TestSpecializedArithmetic<Dst>(dst, line);
257 // Helper macro to wrap displaying the conversion types and line numbers.
258 #define TEST_ARITHMETIC(Dst) TestArithmetic<Dst>(#Dst, __LINE__)
260 TEST(SafeNumerics, SignedIntegerMath) {
261 TEST_ARITHMETIC(int8_t);
262 TEST_ARITHMETIC(int);
263 TEST_ARITHMETIC(intptr_t);
264 TEST_ARITHMETIC(intmax_t);
267 TEST(SafeNumerics, UnsignedIntegerMath) {
268 TEST_ARITHMETIC(uint8_t);
269 TEST_ARITHMETIC(unsigned int);
270 TEST_ARITHMETIC(uintptr_t);
271 TEST_ARITHMETIC(uintmax_t);
274 TEST(SafeNumerics, FloatingPointMath) {
275 TEST_ARITHMETIC(float);
276 TEST_ARITHMETIC(double);
279 // Enumerates the five different conversions types we need to test.
280 enum NumericConversionType {
281 SIGN_PRESERVING_VALUE_PRESERVING,
282 SIGN_PRESERVING_NARROW,
283 SIGN_TO_UNSIGN_WIDEN_OR_EQUAL,
284 SIGN_TO_UNSIGN_NARROW,
285 UNSIGN_TO_SIGN_NARROW_OR_EQUAL,
288 // Template covering the different conversion tests.
289 template <typename Dst, typename Src, NumericConversionType conversion>
290 struct TestNumericConversion {};
292 // EXPECT_EQ wrappers providing specific detail on test failures.
293 #define TEST_EXPECTED_RANGE(expected, actual) \
294 EXPECT_EQ(expected, base::internal::DstRangeRelationToSrcRange<Dst>(actual)) \
295 << "Conversion test: " << src << " value " << actual << " to " << dst \
296 << " on line " << line;
298 template <typename Dst, typename Src>
299 struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_VALUE_PRESERVING> {
300 static void Test(const char *dst, const char *src, int line) {
301 typedef numeric_limits<Src> SrcLimits;
302 typedef numeric_limits<Dst> DstLimits;
303 // Integral to floating.
304 COMPILE_ASSERT((DstLimits::is_iec559 && SrcLimits::is_integer) ||
305 // Not floating to integral and...
306 (!(DstLimits::is_integer && SrcLimits::is_iec559) &&
307 // Same sign, same numeric, source is narrower or same.
308 ((SrcLimits::is_signed == DstLimits::is_signed &&
309 sizeof(Dst) >= sizeof(Src)) ||
310 // Or signed destination and source is smaller
311 (DstLimits::is_signed && sizeof(Dst) > sizeof(Src)))),
312 comparison_must_be_sign_preserving_and_value_preserving);
314 const CheckedNumeric<Dst> checked_dst = SrcLimits::max();
316 TEST_EXPECTED_VALIDITY(RANGE_VALID, checked_dst);
317 if (MaxExponent<Dst>::value > MaxExponent<Src>::value) {
318 if (MaxExponent<Dst>::value >= MaxExponent<Src>::value * 2 - 1) {
319 // At least twice larger type.
320 TEST_EXPECTED_VALIDITY(RANGE_VALID, SrcLimits::max() * checked_dst);
322 } else { // Larger, but not at least twice as large.
323 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, SrcLimits::max() * checked_dst);
324 TEST_EXPECTED_VALIDITY(RANGE_VALID, checked_dst + 1);
326 } else { // Same width type.
327 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, checked_dst + 1);
330 TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::max());
331 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1));
332 if (SrcLimits::is_iec559) {
333 TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::max() * static_cast<Src>(-1));
334 TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::infinity());
335 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::infinity() * -1);
336 TEST_EXPECTED_RANGE(RANGE_INVALID, SrcLimits::quiet_NaN());
337 ResetFloatingPointUnit();
338 } else if (numeric_limits<Src>::is_signed) {
339 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(-1));
340 TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::min());
345 template <typename Dst, typename Src>
346 struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_NARROW> {
347 static void Test(const char *dst, const char *src, int line) {
348 typedef numeric_limits<Src> SrcLimits;
349 typedef numeric_limits<Dst> DstLimits;
350 COMPILE_ASSERT(SrcLimits::is_signed == DstLimits::is_signed,
351 destination_and_source_sign_must_be_the_same);
352 COMPILE_ASSERT(sizeof(Dst) < sizeof(Src) ||
353 (DstLimits::is_integer && SrcLimits::is_iec559),
354 destination_must_be_narrower_than_source);
356 const CheckedNumeric<Dst> checked_dst;
357 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, checked_dst + SrcLimits::max());
358 TEST_EXPECTED_VALUE(1, checked_dst + static_cast<Src>(1));
359 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, checked_dst - SrcLimits::max());
361 TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::max());
362 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1));
363 if (SrcLimits::is_iec559) {
364 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::max() * -1);
365 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(-1));
366 TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::infinity());
367 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::infinity() * -1);
368 TEST_EXPECTED_RANGE(RANGE_INVALID, SrcLimits::quiet_NaN());
369 ResetFloatingPointUnit();
370 } else if (SrcLimits::is_signed) {
371 TEST_EXPECTED_VALUE(-1, checked_dst - static_cast<Src>(1));
372 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::min());
373 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(-1));
374 } else {
375 TEST_EXPECTED_VALIDITY(RANGE_INVALID, checked_dst - static_cast<Src>(1));
376 TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::min());
381 template <typename Dst, typename Src>
382 struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL> {
383 static void Test(const char *dst, const char *src, int line) {
384 typedef numeric_limits<Src> SrcLimits;
385 typedef numeric_limits<Dst> DstLimits;
386 COMPILE_ASSERT(sizeof(Dst) >= sizeof(Src),
387 destination_must_be_equal_or_wider_than_source);
388 COMPILE_ASSERT(SrcLimits::is_signed, source_must_be_signed);
389 COMPILE_ASSERT(!DstLimits::is_signed, destination_must_be_unsigned);
391 const CheckedNumeric<Dst> checked_dst;
392 TEST_EXPECTED_VALUE(SrcLimits::max(), checked_dst + SrcLimits::max());
393 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, checked_dst + static_cast<Src>(-1));
394 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, checked_dst + -SrcLimits::max());
396 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::min());
397 TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::max());
398 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1));
399 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, static_cast<Src>(-1));
403 template <typename Dst, typename Src>
404 struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_NARROW> {
405 static void Test(const char *dst, const char *src, int line) {
406 typedef numeric_limits<Src> SrcLimits;
407 typedef numeric_limits<Dst> DstLimits;
408 COMPILE_ASSERT((DstLimits::is_integer && SrcLimits::is_iec559) ||
409 (sizeof(Dst) < sizeof(Src)),
410 destination_must_be_narrower_than_source);
411 COMPILE_ASSERT(SrcLimits::is_signed, source_must_be_signed);
412 COMPILE_ASSERT(!DstLimits::is_signed, destination_must_be_unsigned);
414 const CheckedNumeric<Dst> checked_dst;
415 TEST_EXPECTED_VALUE(1, checked_dst + static_cast<Src>(1));
416 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, checked_dst + SrcLimits::max());
417 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, checked_dst + static_cast<Src>(-1));
418 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, checked_dst + -SrcLimits::max());
420 TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::max());
421 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1));
422 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, static_cast<Src>(-1));
423 if (SrcLimits::is_iec559) {
424 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::max() * -1);
425 TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::infinity());
426 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::infinity() * -1);
427 TEST_EXPECTED_RANGE(RANGE_INVALID, SrcLimits::quiet_NaN());
428 ResetFloatingPointUnit();
429 } else {
430 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::min());
435 template <typename Dst, typename Src>
436 struct TestNumericConversion<Dst, Src, UNSIGN_TO_SIGN_NARROW_OR_EQUAL> {
437 static void Test(const char *dst, const char *src, int line) {
438 typedef numeric_limits<Src> SrcLimits;
439 typedef numeric_limits<Dst> DstLimits;
440 COMPILE_ASSERT(sizeof(Dst) <= sizeof(Src),
441 destination_must_be_narrower_or_equal_to_source);
442 COMPILE_ASSERT(!SrcLimits::is_signed, source_must_be_unsigned);
443 COMPILE_ASSERT(DstLimits::is_signed, destination_must_be_signed);
445 const CheckedNumeric<Dst> checked_dst;
446 TEST_EXPECTED_VALUE(1, checked_dst + static_cast<Src>(1));
447 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, checked_dst + SrcLimits::max());
448 TEST_EXPECTED_VALUE(SrcLimits::min(), checked_dst + SrcLimits::min());
450 TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::min());
451 TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::max());
452 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1));
456 // Helper macro to wrap displaying the conversion types and line numbers
457 #define TEST_NUMERIC_CONVERSION(d, s, t) \
458 TestNumericConversion<d, s, t>::Test(#d, #s, __LINE__)
460 TEST(SafeNumerics, IntMinOperations) {
461 TEST_NUMERIC_CONVERSION(int8_t, int8_t, SIGN_PRESERVING_VALUE_PRESERVING);
462 TEST_NUMERIC_CONVERSION(uint8_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING);
464 TEST_NUMERIC_CONVERSION(int8_t, int, SIGN_PRESERVING_NARROW);
465 TEST_NUMERIC_CONVERSION(uint8_t, unsigned int, SIGN_PRESERVING_NARROW);
466 TEST_NUMERIC_CONVERSION(int8_t, float, SIGN_PRESERVING_NARROW);
468 TEST_NUMERIC_CONVERSION(uint8_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
470 TEST_NUMERIC_CONVERSION(uint8_t, int, SIGN_TO_UNSIGN_NARROW);
471 TEST_NUMERIC_CONVERSION(uint8_t, intmax_t, SIGN_TO_UNSIGN_NARROW);
472 TEST_NUMERIC_CONVERSION(uint8_t, float, SIGN_TO_UNSIGN_NARROW);
474 TEST_NUMERIC_CONVERSION(int8_t, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
475 TEST_NUMERIC_CONVERSION(int8_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
478 TEST(SafeNumerics, IntOperations) {
479 TEST_NUMERIC_CONVERSION(int, int, SIGN_PRESERVING_VALUE_PRESERVING);
480 TEST_NUMERIC_CONVERSION(unsigned int, unsigned int,
481 SIGN_PRESERVING_VALUE_PRESERVING);
482 TEST_NUMERIC_CONVERSION(int, int8_t, SIGN_PRESERVING_VALUE_PRESERVING);
483 TEST_NUMERIC_CONVERSION(unsigned int, uint8_t,
484 SIGN_PRESERVING_VALUE_PRESERVING);
485 TEST_NUMERIC_CONVERSION(int, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING);
487 TEST_NUMERIC_CONVERSION(int, intmax_t, SIGN_PRESERVING_NARROW);
488 TEST_NUMERIC_CONVERSION(unsigned int, uintmax_t, SIGN_PRESERVING_NARROW);
489 TEST_NUMERIC_CONVERSION(int, float, SIGN_PRESERVING_NARROW);
490 TEST_NUMERIC_CONVERSION(int, double, SIGN_PRESERVING_NARROW);
492 TEST_NUMERIC_CONVERSION(unsigned int, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
493 TEST_NUMERIC_CONVERSION(unsigned int, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
495 TEST_NUMERIC_CONVERSION(unsigned int, intmax_t, SIGN_TO_UNSIGN_NARROW);
496 TEST_NUMERIC_CONVERSION(unsigned int, float, SIGN_TO_UNSIGN_NARROW);
497 TEST_NUMERIC_CONVERSION(unsigned int, double, SIGN_TO_UNSIGN_NARROW);
499 TEST_NUMERIC_CONVERSION(int, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
500 TEST_NUMERIC_CONVERSION(int, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
503 TEST(SafeNumerics, IntMaxOperations) {
504 TEST_NUMERIC_CONVERSION(intmax_t, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING);
505 TEST_NUMERIC_CONVERSION(uintmax_t, uintmax_t,
506 SIGN_PRESERVING_VALUE_PRESERVING);
507 TEST_NUMERIC_CONVERSION(intmax_t, int, SIGN_PRESERVING_VALUE_PRESERVING);
508 TEST_NUMERIC_CONVERSION(uintmax_t, unsigned int,
509 SIGN_PRESERVING_VALUE_PRESERVING);
510 TEST_NUMERIC_CONVERSION(intmax_t, unsigned int,
511 SIGN_PRESERVING_VALUE_PRESERVING);
512 TEST_NUMERIC_CONVERSION(intmax_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING);
514 TEST_NUMERIC_CONVERSION(intmax_t, float, SIGN_PRESERVING_NARROW);
515 TEST_NUMERIC_CONVERSION(intmax_t, double, SIGN_PRESERVING_NARROW);
517 TEST_NUMERIC_CONVERSION(uintmax_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
518 TEST_NUMERIC_CONVERSION(uintmax_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
520 TEST_NUMERIC_CONVERSION(uintmax_t, float, SIGN_TO_UNSIGN_NARROW);
521 TEST_NUMERIC_CONVERSION(uintmax_t, double, SIGN_TO_UNSIGN_NARROW);
523 TEST_NUMERIC_CONVERSION(intmax_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
526 TEST(SafeNumerics, FloatOperations) {
527 TEST_NUMERIC_CONVERSION(float, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING);
528 TEST_NUMERIC_CONVERSION(float, uintmax_t,
529 SIGN_PRESERVING_VALUE_PRESERVING);
530 TEST_NUMERIC_CONVERSION(float, int, SIGN_PRESERVING_VALUE_PRESERVING);
531 TEST_NUMERIC_CONVERSION(float, unsigned int,
532 SIGN_PRESERVING_VALUE_PRESERVING);
534 TEST_NUMERIC_CONVERSION(float, double, SIGN_PRESERVING_NARROW);
537 TEST(SafeNumerics, DoubleOperations) {
538 TEST_NUMERIC_CONVERSION(double, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING);
539 TEST_NUMERIC_CONVERSION(double, uintmax_t,
540 SIGN_PRESERVING_VALUE_PRESERVING);
541 TEST_NUMERIC_CONVERSION(double, int, SIGN_PRESERVING_VALUE_PRESERVING);
542 TEST_NUMERIC_CONVERSION(double, unsigned int,
543 SIGN_PRESERVING_VALUE_PRESERVING);
546 TEST(SafeNumerics, SizeTOperations) {
547 TEST_NUMERIC_CONVERSION(size_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL);
548 TEST_NUMERIC_CONVERSION(int, size_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL);
551 TEST(SafeNumerics, CastTests) {
552 // MSVC catches and warns that we're forcing saturation in these tests.
553 // Since that's intentional, we need to shut this warning off.
554 #if defined(COMPILER_MSVC)
555 #pragma warning(disable : 4756)
556 #endif
558 int small_positive = 1;
559 int small_negative = -1;
560 double double_small = 1.0;
561 double double_large = numeric_limits<double>::max();
562 double double_infinity = numeric_limits<float>::infinity();
564 // Just test that the cast compiles, since the other tests cover logic.
565 EXPECT_EQ(0, checked_cast<int>(static_cast<size_t>(0)));
567 // Test various saturation corner cases.
568 EXPECT_EQ(saturated_cast<int>(small_negative),
569 static_cast<int>(small_negative));
570 EXPECT_EQ(saturated_cast<int>(small_positive),
571 static_cast<int>(small_positive));
572 EXPECT_EQ(saturated_cast<unsigned>(small_negative),
573 static_cast<unsigned>(0));
574 EXPECT_EQ(saturated_cast<int>(double_small),
575 static_cast<int>(double_small));
576 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max());
577 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity);
578 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity);