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