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