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