OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
| 6 #include <mmintrin.h> |
| 7 #endif |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <limits> |
| 11 |
| 12 #include "base/compiler_specific.h" |
5 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
6 | 14 #include "base/numerics/safe_math.h" |
7 #include <stdint.h> | 15 #include "base/template_util.h" |
8 | |
9 #include <limits> | |
10 | |
11 #include "base/compiler_specific.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
13 | 17 |
14 namespace base { | 18 using std::numeric_limits; |
15 namespace internal { | 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; |
| 28 |
| 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 |
| 35 } |
| 36 |
| 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; |
| 42 |
| 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; |
| 48 |
| 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()); |
| 63 |
| 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()); |
| 71 |
| 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()); |
| 82 |
| 83 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, |
| 84 CheckedNumeric<Dst>(DstLimits::min()) * 2); |
| 85 |
| 86 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, |
| 87 CheckedNumeric<Dst>(DstLimits::min()) / -1); |
| 88 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(-1) / 2); |
| 89 |
| 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); |
| 103 } |
| 104 |
| 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); |
| 123 |
| 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); |
| 136 } |
| 137 |
| 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())); |
| 146 |
| 147 TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 148 CheckedNumeric<Dst>(DstLimits::min()).Abs()); |
| 149 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(-1).Abs()); |
| 150 |
| 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()); |
| 158 |
| 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()); |
| 165 |
| 166 TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 167 CheckedNumeric<Dst>(DstLimits::min()) * 2); |
| 168 |
| 169 TEST_EXPECTED_VALUE(-0.5, CheckedNumeric<Dst>(-1.0) / 2); |
| 170 EXPECT_EQ(static_cast<Dst>(1.0), CheckedNumeric<Dst>(1.0).ValueFloating()); |
| 171 } |
| 172 |
| 173 // Generic arithmetic tests. |
| 174 template <typename Dst> |
| 175 static void TestArithmetic(const char* dst, int line) { |
| 176 typedef numeric_limits<Dst> DstLimits; |
| 177 |
| 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)); |
| 187 |
| 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); |
| 209 |
| 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())); |
| 216 |
| 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()); |
| 222 |
| 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()); |
| 231 |
| 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); |
| 238 |
| 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()); |
| 245 |
| 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); |
| 253 |
| 254 TestSpecializedArithmetic<Dst>(dst, line); |
| 255 } |
| 256 |
| 257 // Helper macro to wrap displaying the conversion types and line numbers. |
| 258 #define TEST_ARITHMETIC(Dst) TestArithmetic<Dst>(#Dst, __LINE__) |
| 259 |
| 260 TEST(SafeNumerics, SignedIntegerMath) { |
| 261 TEST_ARITHMETIC(int8_t); |
| 262 TEST_ARITHMETIC(int); |
| 263 TEST_ARITHMETIC(intptr_t); |
| 264 TEST_ARITHMETIC(intmax_t); |
| 265 } |
| 266 |
| 267 TEST(SafeNumerics, UnsignedIntegerMath) { |
| 268 TEST_ARITHMETIC(uint8_t); |
| 269 TEST_ARITHMETIC(unsigned int); |
| 270 TEST_ARITHMETIC(uintptr_t); |
| 271 TEST_ARITHMETIC(uintmax_t); |
| 272 } |
| 273 |
| 274 TEST(SafeNumerics, FloatingPointMath) { |
| 275 TEST_ARITHMETIC(float); |
| 276 TEST_ARITHMETIC(double); |
| 277 } |
16 | 278 |
17 // Enumerates the five different conversions types we need to test. | 279 // Enumerates the five different conversions types we need to test. |
18 enum NumericConversionType { | 280 enum NumericConversionType { |
19 SIGN_PRESERVING_VALUE_PRESERVING, | 281 SIGN_PRESERVING_VALUE_PRESERVING, |
20 SIGN_PRESERVING_NARROW, | 282 SIGN_PRESERVING_NARROW, |
21 SIGN_TO_UNSIGN_WIDEN_OR_EQUAL, | 283 SIGN_TO_UNSIGN_WIDEN_OR_EQUAL, |
22 SIGN_TO_UNSIGN_NARROW, | 284 SIGN_TO_UNSIGN_NARROW, |
23 UNSIGN_TO_SIGN_NARROW_OR_EQUAL, | 285 UNSIGN_TO_SIGN_NARROW_OR_EQUAL, |
24 }; | 286 }; |
25 | 287 |
26 // Template covering the different conversion tests. | 288 // Template covering the different conversion tests. |
27 template <typename Dst, typename Src, NumericConversionType conversion> | 289 template <typename Dst, typename Src, NumericConversionType conversion> |
28 struct TestNumericConversion {}; | 290 struct TestNumericConversion {}; |
29 | 291 |
30 // EXPECT_EQ wrapper providing specific detail on test failures. | 292 // EXPECT_EQ wrappers providing specific detail on test failures. |
31 #define TEST_EXPECTED_RANGE(expected, actual) \ | 293 #define TEST_EXPECTED_RANGE(expected, actual) \ |
32 EXPECT_EQ(expected, RangeCheck<Dst>(actual)) << \ | 294 EXPECT_EQ(expected, base::internal::DstRangeRelationToSrcRange<Dst>(actual)) \ |
33 "Conversion test: " << src << " value " << actual << \ | 295 << "Conversion test: " << src << " value " << actual << " to " << dst \ |
34 " to " << dst << " on line " << line; | 296 << " on line " << line; |
35 | 297 |
36 template <typename Dst, typename Src> | 298 template <typename Dst, typename Src> |
37 struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_VALUE_PRESERVING> { | 299 struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_VALUE_PRESERVING> { |
38 static void Test(const char *dst, const char *src, int line) { | 300 static void Test(const char *dst, const char *src, int line) { |
39 typedef std::numeric_limits<Src> SrcLimits; | 301 typedef numeric_limits<Src> SrcLimits; |
40 typedef std::numeric_limits<Dst> DstLimits; | 302 typedef numeric_limits<Dst> DstLimits; |
41 // Integral to floating. | 303 // Integral to floating. |
42 COMPILE_ASSERT((DstLimits::is_iec559 && SrcLimits::is_integer) || | 304 COMPILE_ASSERT((DstLimits::is_iec559 && SrcLimits::is_integer) || |
43 // Not floating to integral and... | 305 // Not floating to integral and... |
44 (!(DstLimits::is_integer && SrcLimits::is_iec559) && | 306 (!(DstLimits::is_integer && SrcLimits::is_iec559) && |
45 // Same sign, same numeric, source is narrower or same. | 307 // Same sign, same numeric, source is narrower or same. |
46 ((SrcLimits::is_signed == DstLimits::is_signed && | 308 ((SrcLimits::is_signed == DstLimits::is_signed && |
47 sizeof(Dst) >= sizeof(Src)) || | 309 sizeof(Dst) >= sizeof(Src)) || |
48 // Or signed destination and source is smaller | 310 // Or signed destination and source is smaller |
49 (DstLimits::is_signed && sizeof(Dst) > sizeof(Src)))), | 311 (DstLimits::is_signed && sizeof(Dst) > sizeof(Src)))), |
50 comparison_must_be_sign_preserving_and_value_preserving); | 312 comparison_must_be_sign_preserving_and_value_preserving); |
51 | 313 |
52 TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::max()); | 314 const CheckedNumeric<Dst> checked_dst = SrcLimits::max(); |
53 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(1)); | 315 ; |
| 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); |
| 321 |
| 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); |
| 325 } |
| 326 } else { // Same width type. |
| 327 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, checked_dst + 1); |
| 328 } |
| 329 |
| 330 TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::max()); |
| 331 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1)); |
54 if (SrcLimits::is_iec559) { | 332 if (SrcLimits::is_iec559) { |
55 TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::max() * static_cast<Src>(-1)); | 333 TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::max() * static_cast<Src>(-1)); |
56 TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::infinity()); | 334 TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::infinity()); |
57 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::infinity() * -1); | 335 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::infinity() * -1); |
58 TEST_EXPECTED_RANGE(TYPE_INVALID, SrcLimits::quiet_NaN()); | 336 TEST_EXPECTED_RANGE(RANGE_INVALID, SrcLimits::quiet_NaN()); |
59 } else if (std::numeric_limits<Src>::is_signed) { | 337 ResetFloatingPointUnit(); |
60 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(-1)); | 338 } else if (numeric_limits<Src>::is_signed) { |
61 TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::min()); | 339 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(-1)); |
| 340 TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::min()); |
62 } | 341 } |
63 } | 342 } |
64 }; | 343 }; |
65 | 344 |
66 template <typename Dst, typename Src> | 345 template <typename Dst, typename Src> |
67 struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_NARROW> { | 346 struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_NARROW> { |
68 static void Test(const char *dst, const char *src, int line) { | 347 static void Test(const char *dst, const char *src, int line) { |
69 typedef std::numeric_limits<Src> SrcLimits; | 348 typedef numeric_limits<Src> SrcLimits; |
70 typedef std::numeric_limits<Dst> DstLimits; | 349 typedef numeric_limits<Dst> DstLimits; |
71 COMPILE_ASSERT(SrcLimits::is_signed == DstLimits::is_signed, | 350 COMPILE_ASSERT(SrcLimits::is_signed == DstLimits::is_signed, |
72 destination_and_source_sign_must_be_the_same); | 351 destination_and_source_sign_must_be_the_same); |
73 COMPILE_ASSERT(sizeof(Dst) < sizeof(Src) || | 352 COMPILE_ASSERT(sizeof(Dst) < sizeof(Src) || |
74 (DstLimits::is_integer && SrcLimits::is_iec559), | 353 (DstLimits::is_integer && SrcLimits::is_iec559), |
75 destination_must_be_narrower_than_source); | 354 destination_must_be_narrower_than_source); |
76 | 355 |
77 TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::max()); | 356 const CheckedNumeric<Dst> checked_dst; |
78 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(1)); | 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()); |
| 360 |
| 361 TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::max()); |
| 362 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1)); |
79 if (SrcLimits::is_iec559) { | 363 if (SrcLimits::is_iec559) { |
80 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::max() * -1); | 364 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::max() * -1); |
81 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(-1)); | 365 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(-1)); |
82 TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::infinity()); | 366 TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::infinity()); |
83 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::infinity() * -1); | 367 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::infinity() * -1); |
84 TEST_EXPECTED_RANGE(TYPE_INVALID, SrcLimits::quiet_NaN()); | 368 TEST_EXPECTED_RANGE(RANGE_INVALID, SrcLimits::quiet_NaN()); |
| 369 ResetFloatingPointUnit(); |
85 } else if (SrcLimits::is_signed) { | 370 } else if (SrcLimits::is_signed) { |
86 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::min()); | 371 TEST_EXPECTED_VALUE(-1, checked_dst - static_cast<Src>(1)); |
87 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(-1)); | 372 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::min()); |
| 373 TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(-1)); |
88 } else { | 374 } else { |
89 TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::min()); | 375 TEST_EXPECTED_VALIDITY(RANGE_INVALID, checked_dst - static_cast<Src>(1)); |
| 376 TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::min()); |
90 } | 377 } |
91 } | 378 } |
92 }; | 379 }; |
93 | 380 |
94 template <typename Dst, typename Src> | 381 template <typename Dst, typename Src> |
95 struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL> { | 382 struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL> { |
96 static void Test(const char *dst, const char *src, int line) { | 383 static void Test(const char *dst, const char *src, int line) { |
97 typedef std::numeric_limits<Src> SrcLimits; | 384 typedef numeric_limits<Src> SrcLimits; |
98 typedef std::numeric_limits<Dst> DstLimits; | 385 typedef numeric_limits<Dst> DstLimits; |
99 COMPILE_ASSERT(sizeof(Dst) >= sizeof(Src), | 386 COMPILE_ASSERT(sizeof(Dst) >= sizeof(Src), |
100 destination_must_be_equal_or_wider_than_source); | 387 destination_must_be_equal_or_wider_than_source); |
101 COMPILE_ASSERT(SrcLimits::is_signed, source_must_be_signed); | 388 COMPILE_ASSERT(SrcLimits::is_signed, source_must_be_signed); |
102 COMPILE_ASSERT(!DstLimits::is_signed, destination_must_be_unsigned); | 389 COMPILE_ASSERT(!DstLimits::is_signed, destination_must_be_unsigned); |
103 | 390 |
104 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::min()); | 391 const CheckedNumeric<Dst> checked_dst; |
105 TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::max()); | 392 TEST_EXPECTED_VALUE(SrcLimits::max(), checked_dst + SrcLimits::max()); |
106 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(1)); | 393 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, checked_dst + static_cast<Src>(-1)); |
107 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, static_cast<Src>(-1)); | 394 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, checked_dst + -SrcLimits::max()); |
| 395 |
| 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)); |
108 } | 400 } |
109 }; | 401 }; |
110 | 402 |
111 template <typename Dst, typename Src> | 403 template <typename Dst, typename Src> |
112 struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_NARROW> { | 404 struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_NARROW> { |
113 static void Test(const char *dst, const char *src, int line) { | 405 static void Test(const char *dst, const char *src, int line) { |
114 typedef std::numeric_limits<Src> SrcLimits; | 406 typedef numeric_limits<Src> SrcLimits; |
115 typedef std::numeric_limits<Dst> DstLimits; | 407 typedef numeric_limits<Dst> DstLimits; |
116 COMPILE_ASSERT((DstLimits::is_integer && SrcLimits::is_iec559) || | 408 COMPILE_ASSERT((DstLimits::is_integer && SrcLimits::is_iec559) || |
117 (sizeof(Dst) < sizeof(Src)), | 409 (sizeof(Dst) < sizeof(Src)), |
118 destination_must_be_narrower_than_source); | 410 destination_must_be_narrower_than_source); |
119 COMPILE_ASSERT(SrcLimits::is_signed, source_must_be_signed); | 411 COMPILE_ASSERT(SrcLimits::is_signed, source_must_be_signed); |
120 COMPILE_ASSERT(!DstLimits::is_signed, destination_must_be_unsigned); | 412 COMPILE_ASSERT(!DstLimits::is_signed, destination_must_be_unsigned); |
121 | 413 |
122 TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::max()); | 414 const CheckedNumeric<Dst> checked_dst; |
123 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(1)); | 415 TEST_EXPECTED_VALUE(1, checked_dst + static_cast<Src>(1)); |
124 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, 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()); |
| 419 |
| 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)); |
125 if (SrcLimits::is_iec559) { | 423 if (SrcLimits::is_iec559) { |
126 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::max() * -1); | 424 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::max() * -1); |
127 TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::infinity()); | 425 TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::infinity()); |
128 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::infinity() * -1); | 426 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::infinity() * -1); |
129 TEST_EXPECTED_RANGE(TYPE_INVALID, SrcLimits::quiet_NaN()); | 427 TEST_EXPECTED_RANGE(RANGE_INVALID, SrcLimits::quiet_NaN()); |
| 428 ResetFloatingPointUnit(); |
130 } else { | 429 } else { |
131 TEST_EXPECTED_RANGE(TYPE_UNDERFLOW, SrcLimits::min()); | 430 TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::min()); |
132 } | 431 } |
133 } | 432 } |
134 }; | 433 }; |
135 | 434 |
136 template <typename Dst, typename Src> | 435 template <typename Dst, typename Src> |
137 struct TestNumericConversion<Dst, Src, UNSIGN_TO_SIGN_NARROW_OR_EQUAL> { | 436 struct TestNumericConversion<Dst, Src, UNSIGN_TO_SIGN_NARROW_OR_EQUAL> { |
138 static void Test(const char *dst, const char *src, int line) { | 437 static void Test(const char *dst, const char *src, int line) { |
139 typedef std::numeric_limits<Src> SrcLimits; | 438 typedef numeric_limits<Src> SrcLimits; |
140 typedef std::numeric_limits<Dst> DstLimits; | 439 typedef numeric_limits<Dst> DstLimits; |
141 COMPILE_ASSERT(sizeof(Dst) <= sizeof(Src), | 440 COMPILE_ASSERT(sizeof(Dst) <= sizeof(Src), |
142 destination_must_be_narrower_or_equal_to_source); | 441 destination_must_be_narrower_or_equal_to_source); |
143 COMPILE_ASSERT(!SrcLimits::is_signed, source_must_be_unsigned); | 442 COMPILE_ASSERT(!SrcLimits::is_signed, source_must_be_unsigned); |
144 COMPILE_ASSERT(DstLimits::is_signed, destination_must_be_signed); | 443 COMPILE_ASSERT(DstLimits::is_signed, destination_must_be_signed); |
145 | 444 |
146 TEST_EXPECTED_RANGE(TYPE_VALID, SrcLimits::min()); | 445 const CheckedNumeric<Dst> checked_dst; |
147 TEST_EXPECTED_RANGE(TYPE_OVERFLOW, SrcLimits::max()); | 446 TEST_EXPECTED_VALUE(1, checked_dst + static_cast<Src>(1)); |
148 TEST_EXPECTED_RANGE(TYPE_VALID, static_cast<Src>(1)); | 447 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, checked_dst + SrcLimits::max()); |
| 448 TEST_EXPECTED_VALUE(SrcLimits::min(), checked_dst + SrcLimits::min()); |
| 449 |
| 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)); |
149 } | 453 } |
150 }; | 454 }; |
151 | 455 |
152 // Helper macro to wrap displaying the conversion types and line numbers | 456 // Helper macro to wrap displaying the conversion types and line numbers |
153 #define TEST_NUMERIC_CONVERSION(d, s, t) \ | 457 #define TEST_NUMERIC_CONVERSION(d, s, t) \ |
154 TestNumericConversion<d, s, t>::Test(#d, #s, __LINE__) | 458 TestNumericConversion<d, s, t>::Test(#d, #s, __LINE__) |
155 | 459 |
156 TEST(SafeNumerics, IntMinConversions) { | 460 TEST(SafeNumerics, IntMinOperations) { |
157 TEST_NUMERIC_CONVERSION(int8_t, int8_t, SIGN_PRESERVING_VALUE_PRESERVING); | 461 TEST_NUMERIC_CONVERSION(int8_t, int8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
158 TEST_NUMERIC_CONVERSION(uint8_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING); | 462 TEST_NUMERIC_CONVERSION(uint8_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
159 | 463 |
160 TEST_NUMERIC_CONVERSION(int8_t, int, SIGN_PRESERVING_NARROW); | 464 TEST_NUMERIC_CONVERSION(int8_t, int, SIGN_PRESERVING_NARROW); |
161 TEST_NUMERIC_CONVERSION(uint8_t, unsigned int, SIGN_PRESERVING_NARROW); | 465 TEST_NUMERIC_CONVERSION(uint8_t, unsigned int, SIGN_PRESERVING_NARROW); |
162 TEST_NUMERIC_CONVERSION(int8_t, float, SIGN_PRESERVING_NARROW); | 466 TEST_NUMERIC_CONVERSION(int8_t, float, SIGN_PRESERVING_NARROW); |
163 | 467 |
164 TEST_NUMERIC_CONVERSION(uint8_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); | 468 TEST_NUMERIC_CONVERSION(uint8_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
165 | 469 |
166 TEST_NUMERIC_CONVERSION(uint8_t, int, SIGN_TO_UNSIGN_NARROW); | 470 TEST_NUMERIC_CONVERSION(uint8_t, int, SIGN_TO_UNSIGN_NARROW); |
167 TEST_NUMERIC_CONVERSION(uint8_t, intmax_t, SIGN_TO_UNSIGN_NARROW); | 471 TEST_NUMERIC_CONVERSION(uint8_t, intmax_t, SIGN_TO_UNSIGN_NARROW); |
168 TEST_NUMERIC_CONVERSION(uint8_t, float, SIGN_TO_UNSIGN_NARROW); | 472 TEST_NUMERIC_CONVERSION(uint8_t, float, SIGN_TO_UNSIGN_NARROW); |
169 | 473 |
170 TEST_NUMERIC_CONVERSION(int8_t, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); | 474 TEST_NUMERIC_CONVERSION(int8_t, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
171 TEST_NUMERIC_CONVERSION(int8_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); | 475 TEST_NUMERIC_CONVERSION(int8_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
172 } | 476 } |
173 | 477 |
174 TEST(SafeNumerics, IntConversions) { | 478 TEST(SafeNumerics, IntOperations) { |
175 TEST_NUMERIC_CONVERSION(int, int, SIGN_PRESERVING_VALUE_PRESERVING); | 479 TEST_NUMERIC_CONVERSION(int, int, SIGN_PRESERVING_VALUE_PRESERVING); |
176 TEST_NUMERIC_CONVERSION(unsigned int, unsigned int, | 480 TEST_NUMERIC_CONVERSION(unsigned int, unsigned int, |
177 SIGN_PRESERVING_VALUE_PRESERVING); | 481 SIGN_PRESERVING_VALUE_PRESERVING); |
178 TEST_NUMERIC_CONVERSION(int, int8_t, SIGN_PRESERVING_VALUE_PRESERVING); | 482 TEST_NUMERIC_CONVERSION(int, int8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
179 TEST_NUMERIC_CONVERSION(unsigned int, uint8_t, | 483 TEST_NUMERIC_CONVERSION(unsigned int, uint8_t, |
180 SIGN_PRESERVING_VALUE_PRESERVING); | 484 SIGN_PRESERVING_VALUE_PRESERVING); |
181 TEST_NUMERIC_CONVERSION(int, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING); | 485 TEST_NUMERIC_CONVERSION(int, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
182 | 486 |
183 TEST_NUMERIC_CONVERSION(int, intmax_t, SIGN_PRESERVING_NARROW); | 487 TEST_NUMERIC_CONVERSION(int, intmax_t, SIGN_PRESERVING_NARROW); |
184 TEST_NUMERIC_CONVERSION(unsigned int, uintmax_t, SIGN_PRESERVING_NARROW); | 488 TEST_NUMERIC_CONVERSION(unsigned int, uintmax_t, SIGN_PRESERVING_NARROW); |
185 TEST_NUMERIC_CONVERSION(int, float, SIGN_PRESERVING_NARROW); | 489 TEST_NUMERIC_CONVERSION(int, float, SIGN_PRESERVING_NARROW); |
186 TEST_NUMERIC_CONVERSION(int, double, SIGN_PRESERVING_NARROW); | 490 TEST_NUMERIC_CONVERSION(int, double, SIGN_PRESERVING_NARROW); |
187 | 491 |
188 TEST_NUMERIC_CONVERSION(unsigned int, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); | 492 TEST_NUMERIC_CONVERSION(unsigned int, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
189 TEST_NUMERIC_CONVERSION(unsigned int, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); | 493 TEST_NUMERIC_CONVERSION(unsigned int, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
190 | 494 |
191 TEST_NUMERIC_CONVERSION(unsigned int, intmax_t, SIGN_TO_UNSIGN_NARROW); | 495 TEST_NUMERIC_CONVERSION(unsigned int, intmax_t, SIGN_TO_UNSIGN_NARROW); |
192 TEST_NUMERIC_CONVERSION(unsigned int, float, SIGN_TO_UNSIGN_NARROW); | 496 TEST_NUMERIC_CONVERSION(unsigned int, float, SIGN_TO_UNSIGN_NARROW); |
193 TEST_NUMERIC_CONVERSION(unsigned int, double, SIGN_TO_UNSIGN_NARROW); | 497 TEST_NUMERIC_CONVERSION(unsigned int, double, SIGN_TO_UNSIGN_NARROW); |
194 | 498 |
195 TEST_NUMERIC_CONVERSION(int, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); | 499 TEST_NUMERIC_CONVERSION(int, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
196 TEST_NUMERIC_CONVERSION(int, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); | 500 TEST_NUMERIC_CONVERSION(int, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
197 } | 501 } |
198 | 502 |
199 TEST(SafeNumerics, IntMaxConversions) { | 503 TEST(SafeNumerics, IntMaxOperations) { |
200 TEST_NUMERIC_CONVERSION(intmax_t, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING); | 504 TEST_NUMERIC_CONVERSION(intmax_t, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING); |
201 TEST_NUMERIC_CONVERSION(uintmax_t, uintmax_t, | 505 TEST_NUMERIC_CONVERSION(uintmax_t, uintmax_t, |
202 SIGN_PRESERVING_VALUE_PRESERVING); | 506 SIGN_PRESERVING_VALUE_PRESERVING); |
203 TEST_NUMERIC_CONVERSION(intmax_t, int, SIGN_PRESERVING_VALUE_PRESERVING); | 507 TEST_NUMERIC_CONVERSION(intmax_t, int, SIGN_PRESERVING_VALUE_PRESERVING); |
204 TEST_NUMERIC_CONVERSION(uintmax_t, unsigned int, | 508 TEST_NUMERIC_CONVERSION(uintmax_t, unsigned int, |
205 SIGN_PRESERVING_VALUE_PRESERVING); | 509 SIGN_PRESERVING_VALUE_PRESERVING); |
206 TEST_NUMERIC_CONVERSION(intmax_t, unsigned int, | 510 TEST_NUMERIC_CONVERSION(intmax_t, unsigned int, |
207 SIGN_PRESERVING_VALUE_PRESERVING); | 511 SIGN_PRESERVING_VALUE_PRESERVING); |
208 TEST_NUMERIC_CONVERSION(intmax_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING); | 512 TEST_NUMERIC_CONVERSION(intmax_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
209 | 513 |
210 TEST_NUMERIC_CONVERSION(intmax_t, float, SIGN_PRESERVING_NARROW); | 514 TEST_NUMERIC_CONVERSION(intmax_t, float, SIGN_PRESERVING_NARROW); |
211 TEST_NUMERIC_CONVERSION(intmax_t, double, SIGN_PRESERVING_NARROW); | 515 TEST_NUMERIC_CONVERSION(intmax_t, double, SIGN_PRESERVING_NARROW); |
212 | 516 |
213 TEST_NUMERIC_CONVERSION(uintmax_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); | 517 TEST_NUMERIC_CONVERSION(uintmax_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
214 TEST_NUMERIC_CONVERSION(uintmax_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); | 518 TEST_NUMERIC_CONVERSION(uintmax_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
215 | 519 |
216 TEST_NUMERIC_CONVERSION(uintmax_t, float, SIGN_TO_UNSIGN_NARROW); | 520 TEST_NUMERIC_CONVERSION(uintmax_t, float, SIGN_TO_UNSIGN_NARROW); |
217 TEST_NUMERIC_CONVERSION(uintmax_t, double, SIGN_TO_UNSIGN_NARROW); | 521 TEST_NUMERIC_CONVERSION(uintmax_t, double, SIGN_TO_UNSIGN_NARROW); |
218 | 522 |
219 TEST_NUMERIC_CONVERSION(intmax_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); | 523 TEST_NUMERIC_CONVERSION(intmax_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
220 } | 524 } |
221 | 525 |
222 TEST(SafeNumerics, FloatConversions) { | 526 TEST(SafeNumerics, FloatOperations) { |
223 TEST_NUMERIC_CONVERSION(float, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING); | 527 TEST_NUMERIC_CONVERSION(float, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING); |
224 TEST_NUMERIC_CONVERSION(float, uintmax_t, | 528 TEST_NUMERIC_CONVERSION(float, uintmax_t, |
225 SIGN_PRESERVING_VALUE_PRESERVING); | 529 SIGN_PRESERVING_VALUE_PRESERVING); |
226 TEST_NUMERIC_CONVERSION(float, int, SIGN_PRESERVING_VALUE_PRESERVING); | 530 TEST_NUMERIC_CONVERSION(float, int, SIGN_PRESERVING_VALUE_PRESERVING); |
227 TEST_NUMERIC_CONVERSION(float, unsigned int, | 531 TEST_NUMERIC_CONVERSION(float, unsigned int, |
228 SIGN_PRESERVING_VALUE_PRESERVING); | 532 SIGN_PRESERVING_VALUE_PRESERVING); |
229 | 533 |
230 TEST_NUMERIC_CONVERSION(float, double, SIGN_PRESERVING_NARROW); | 534 TEST_NUMERIC_CONVERSION(float, double, SIGN_PRESERVING_NARROW); |
231 } | 535 } |
232 | 536 |
233 TEST(SafeNumerics, DoubleConversions) { | 537 TEST(SafeNumerics, DoubleOperations) { |
234 TEST_NUMERIC_CONVERSION(double, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING); | 538 TEST_NUMERIC_CONVERSION(double, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING); |
235 TEST_NUMERIC_CONVERSION(double, uintmax_t, | 539 TEST_NUMERIC_CONVERSION(double, uintmax_t, |
236 SIGN_PRESERVING_VALUE_PRESERVING); | 540 SIGN_PRESERVING_VALUE_PRESERVING); |
237 TEST_NUMERIC_CONVERSION(double, int, SIGN_PRESERVING_VALUE_PRESERVING); | 541 TEST_NUMERIC_CONVERSION(double, int, SIGN_PRESERVING_VALUE_PRESERVING); |
238 TEST_NUMERIC_CONVERSION(double, unsigned int, | 542 TEST_NUMERIC_CONVERSION(double, unsigned int, |
239 SIGN_PRESERVING_VALUE_PRESERVING); | 543 SIGN_PRESERVING_VALUE_PRESERVING); |
240 } | 544 } |
241 | 545 |
242 TEST(SafeNumerics, SizeTConversions) { | 546 TEST(SafeNumerics, SizeTOperations) { |
243 TEST_NUMERIC_CONVERSION(size_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); | 547 TEST_NUMERIC_CONVERSION(size_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
244 TEST_NUMERIC_CONVERSION(int, size_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); | 548 TEST_NUMERIC_CONVERSION(int, size_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
245 } | 549 } |
246 | 550 |
247 TEST(SafeNumerics, CastTests) { | 551 TEST(SafeNumerics, CastTests) { |
248 // MSVC catches and warns that we're forcing saturation in these tests. | 552 // MSVC catches and warns that we're forcing saturation in these tests. |
249 // Since that's intentional, we need to shut this warning off. | 553 // Since that's intentional, we need to shut this warning off. |
250 #if defined(COMPILER_MSVC) | 554 #if defined(COMPILER_MSVC) |
251 #pragma warning(disable : 4756) | 555 #pragma warning(disable : 4756) |
252 #endif | 556 #endif |
253 | 557 |
254 int small_positive = 1; | 558 int small_positive = 1; |
255 int small_negative = -1; | 559 int small_negative = -1; |
256 double double_small = 1.0; | 560 double double_small = 1.0; |
257 double double_large = std::numeric_limits<double>::max(); | 561 double double_large = numeric_limits<double>::max(); |
258 double double_infinity = std::numeric_limits<float>::infinity(); | 562 double double_infinity = numeric_limits<float>::infinity(); |
259 | 563 |
260 // Just test that the cast compiles, since the other tests cover logic. | 564 // Just test that the cast compiles, since the other tests cover logic. |
261 EXPECT_EQ(0, base::checked_cast<int>(static_cast<size_t>(0))); | 565 EXPECT_EQ(0, checked_cast<int>(static_cast<size_t>(0))); |
262 | 566 |
263 // Test various saturation corner cases. | 567 // Test various saturation corner cases. |
264 EXPECT_EQ(saturated_cast<int>(small_negative), | 568 EXPECT_EQ(saturated_cast<int>(small_negative), |
265 static_cast<int>(small_negative)); | 569 static_cast<int>(small_negative)); |
266 EXPECT_EQ(saturated_cast<int>(small_positive), | 570 EXPECT_EQ(saturated_cast<int>(small_positive), |
267 static_cast<int>(small_positive)); | 571 static_cast<int>(small_positive)); |
268 EXPECT_EQ(saturated_cast<unsigned>(small_negative), | 572 EXPECT_EQ(saturated_cast<unsigned>(small_negative), |
269 static_cast<unsigned>(0)); | 573 static_cast<unsigned>(0)); |
270 EXPECT_EQ(saturated_cast<int>(double_small), | 574 EXPECT_EQ(saturated_cast<int>(double_small), |
271 static_cast<int>(double_small)); | 575 static_cast<int>(double_small)); |
272 EXPECT_EQ(saturated_cast<int>(double_large), | 576 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); |
273 std::numeric_limits<int>::max()); | |
274 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); | 577 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); |
275 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); | 578 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); |
276 } | 579 } |
277 | 580 |
278 } // namespace internal | |
279 } // namespace base | |
280 | |
OLD | NEW |