scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 5 | #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
| 6 | #include <mmintrin.h> |
| 7 | #endif |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <limits> |
| 11 | |
| 12 | #include "base/compiler_specific.h" |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 13 | #include "base/numerics/safe_conversions.h" |
| 14 | #include "base/numerics/safe_math.h" |
| 15 | #include "base/template_util.h" |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 17 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 18 | using std::numeric_limits; |
| 19 | using base::CheckedNumeric; |
| 20 | using base::checked_cast; |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame^] | 21 | using base::IsValueInRangeForNumericType; |
jschuh | 4bf22c6d | 2015-05-28 02:29:25 | [diff] [blame] | 22 | using base::SizeT; |
| 23 | using base::StrictNumeric; |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 24 | using base::saturated_cast; |
jschuh | 4bf22c6d | 2015-05-28 02:29:25 | [diff] [blame] | 25 | using base::strict_cast; |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 26 | using base::internal::MaxExponent; |
| 27 | using base::internal::RANGE_VALID; |
| 28 | using base::internal::RANGE_INVALID; |
| 29 | using base::internal::RANGE_OVERFLOW; |
| 30 | using base::internal::RANGE_UNDERFLOW; |
| 31 | using base::enable_if; |
| 32 | |
ncbray@chromium.org | b6bf5c32 | 2014-08-09 05:24:02 | [diff] [blame] | 33 | // These tests deliberately cause arithmetic overflows. If the compiler is |
| 34 | // aggressive enough, it can const fold these overflows. Disable warnings about |
| 35 | // overflows for const expressions. |
| 36 | #if defined(OS_WIN) |
| 37 | #pragma warning(disable:4756) |
| 38 | #endif |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame^] | 39 | |
| 40 | // This is a helper function for finding the maximum value in Src that can be |
| 41 | // wholy represented as the destination floating-point type. |
| 42 | template <typename Dst, typename Src> |
| 43 | Dst GetMaxConvertibleToFloat() { |
| 44 | typedef numeric_limits<Dst> DstLimits; |
| 45 | typedef numeric_limits<Src> SrcLimits; |
| 46 | static_assert(SrcLimits::is_specialized, "Source must be numeric."); |
| 47 | static_assert(DstLimits::is_specialized, "Destination must be numeric."); |
| 48 | CHECK(DstLimits::is_iec559); |
| 49 | |
| 50 | if (SrcLimits::digits <= DstLimits::digits && |
| 51 | MaxExponent<Src>::value <= MaxExponent<Dst>::value) |
| 52 | return SrcLimits::max(); |
| 53 | Src max = SrcLimits::max() / 2 + (SrcLimits::is_integer ? 1 : 0); |
| 54 | while (max != static_cast<Src>(static_cast<Dst>(max))) { |
| 55 | max /= 2; |
| 56 | } |
| 57 | return static_cast<Dst>(max); |
| 58 | } |
ncbray@chromium.org | b6bf5c32 | 2014-08-09 05:24:02 | [diff] [blame] | 59 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 60 | // Helper macros to wrap displaying the conversion types and line numbers. |
| 61 | #define TEST_EXPECTED_VALIDITY(expected, actual) \ |
| 62 | EXPECT_EQ(expected, CheckedNumeric<Dst>(actual).validity()) \ |
| 63 | << "Result test: Value " << +(actual).ValueUnsafe() << " as " << dst \ |
| 64 | << " on line " << line; |
| 65 | |
| 66 | #define TEST_EXPECTED_VALUE(expected, actual) \ |
| 67 | EXPECT_EQ(static_cast<Dst>(expected), \ |
| 68 | CheckedNumeric<Dst>(actual).ValueUnsafe()) \ |
| 69 | << "Result test: Value " << +((actual).ValueUnsafe()) << " as " << dst \ |
| 70 | << " on line " << line; |
| 71 | |
| 72 | // Signed integer arithmetic. |
| 73 | template <typename Dst> |
| 74 | static void TestSpecializedArithmetic( |
| 75 | const char* dst, |
| 76 | int line, |
| 77 | typename enable_if< |
| 78 | numeric_limits<Dst>::is_integer&& numeric_limits<Dst>::is_signed, |
| 79 | int>::type = 0) { |
| 80 | typedef numeric_limits<Dst> DstLimits; |
| 81 | TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, |
| 82 | -CheckedNumeric<Dst>(DstLimits::min())); |
| 83 | TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, |
| 84 | CheckedNumeric<Dst>(DstLimits::min()).Abs()); |
| 85 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(-1).Abs()); |
| 86 | |
| 87 | TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 88 | CheckedNumeric<Dst>(DstLimits::max()) + -1); |
| 89 | TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, |
| 90 | CheckedNumeric<Dst>(DstLimits::min()) + -1); |
| 91 | TEST_EXPECTED_VALIDITY( |
| 92 | RANGE_UNDERFLOW, |
| 93 | CheckedNumeric<Dst>(-DstLimits::max()) + -DstLimits::max()); |
| 94 | |
| 95 | TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, |
| 96 | CheckedNumeric<Dst>(DstLimits::min()) - 1); |
| 97 | TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 98 | CheckedNumeric<Dst>(DstLimits::min()) - -1); |
| 99 | TEST_EXPECTED_VALIDITY( |
| 100 | RANGE_OVERFLOW, |
| 101 | CheckedNumeric<Dst>(DstLimits::max()) - -DstLimits::max()); |
| 102 | TEST_EXPECTED_VALIDITY( |
| 103 | RANGE_UNDERFLOW, |
| 104 | CheckedNumeric<Dst>(-DstLimits::max()) - DstLimits::max()); |
| 105 | |
| 106 | TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, |
| 107 | CheckedNumeric<Dst>(DstLimits::min()) * 2); |
| 108 | |
| 109 | TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, |
| 110 | CheckedNumeric<Dst>(DstLimits::min()) / -1); |
| 111 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(-1) / 2); |
| 112 | |
| 113 | // Modulus is legal only for integers. |
| 114 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() % 1); |
| 115 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
| 116 | TEST_EXPECTED_VALUE(-1, CheckedNumeric<Dst>(-1) % 2); |
| 117 | TEST_EXPECTED_VALIDITY(RANGE_INVALID, CheckedNumeric<Dst>(-1) % -2); |
| 118 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) % 2); |
| 119 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(DstLimits::max()) % 2); |
| 120 | // Test all the different modulus combinations. |
| 121 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % CheckedNumeric<Dst>(1)); |
| 122 | TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric<Dst>(1)); |
| 123 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
| 124 | CheckedNumeric<Dst> checked_dst = 1; |
| 125 | TEST_EXPECTED_VALUE(0, checked_dst %= 1); |
| 126 | } |
| 127 | |
| 128 | // Unsigned integer arithmetic. |
| 129 | template <typename Dst> |
| 130 | static void TestSpecializedArithmetic( |
| 131 | const char* dst, |
| 132 | int line, |
| 133 | typename enable_if< |
| 134 | numeric_limits<Dst>::is_integer && !numeric_limits<Dst>::is_signed, |
| 135 | int>::type = 0) { |
| 136 | typedef numeric_limits<Dst> DstLimits; |
| 137 | TEST_EXPECTED_VALIDITY(RANGE_VALID, -CheckedNumeric<Dst>(DstLimits::min())); |
| 138 | TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 139 | CheckedNumeric<Dst>(DstLimits::min()).Abs()); |
| 140 | TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, |
| 141 | CheckedNumeric<Dst>(DstLimits::min()) + -1); |
| 142 | TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, |
| 143 | CheckedNumeric<Dst>(DstLimits::min()) - 1); |
| 144 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) * 2); |
| 145 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) / 2); |
| 146 | |
| 147 | // Modulus is legal only for integers. |
| 148 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() % 1); |
| 149 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
| 150 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) % 2); |
| 151 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) % 2); |
| 152 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(DstLimits::max()) % 2); |
| 153 | // Test all the different modulus combinations. |
| 154 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % CheckedNumeric<Dst>(1)); |
| 155 | TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric<Dst>(1)); |
| 156 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
| 157 | CheckedNumeric<Dst> checked_dst = 1; |
| 158 | TEST_EXPECTED_VALUE(0, checked_dst %= 1); |
| 159 | } |
| 160 | |
| 161 | // Floating point arithmetic. |
| 162 | template <typename Dst> |
| 163 | void TestSpecializedArithmetic( |
| 164 | const char* dst, |
| 165 | int line, |
| 166 | typename enable_if<numeric_limits<Dst>::is_iec559, int>::type = 0) { |
| 167 | typedef numeric_limits<Dst> DstLimits; |
| 168 | TEST_EXPECTED_VALIDITY(RANGE_VALID, -CheckedNumeric<Dst>(DstLimits::min())); |
| 169 | |
| 170 | TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 171 | CheckedNumeric<Dst>(DstLimits::min()).Abs()); |
| 172 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(-1).Abs()); |
| 173 | |
| 174 | TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 175 | CheckedNumeric<Dst>(DstLimits::min()) + -1); |
| 176 | TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 177 | CheckedNumeric<Dst>(DstLimits::max()) + 1); |
| 178 | TEST_EXPECTED_VALIDITY( |
| 179 | RANGE_UNDERFLOW, |
| 180 | CheckedNumeric<Dst>(-DstLimits::max()) + -DstLimits::max()); |
| 181 | |
| 182 | TEST_EXPECTED_VALIDITY( |
| 183 | RANGE_OVERFLOW, |
| 184 | CheckedNumeric<Dst>(DstLimits::max()) - -DstLimits::max()); |
| 185 | TEST_EXPECTED_VALIDITY( |
| 186 | RANGE_UNDERFLOW, |
| 187 | CheckedNumeric<Dst>(-DstLimits::max()) - DstLimits::max()); |
| 188 | |
| 189 | TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 190 | CheckedNumeric<Dst>(DstLimits::min()) * 2); |
| 191 | |
| 192 | TEST_EXPECTED_VALUE(-0.5, CheckedNumeric<Dst>(-1.0) / 2); |
| 193 | EXPECT_EQ(static_cast<Dst>(1.0), CheckedNumeric<Dst>(1.0).ValueFloating()); |
| 194 | } |
| 195 | |
| 196 | // Generic arithmetic tests. |
| 197 | template <typename Dst> |
| 198 | static void TestArithmetic(const char* dst, int line) { |
| 199 | typedef numeric_limits<Dst> DstLimits; |
| 200 | |
| 201 | EXPECT_EQ(true, CheckedNumeric<Dst>().IsValid()); |
| 202 | EXPECT_EQ(false, |
| 203 | CheckedNumeric<Dst>(CheckedNumeric<Dst>(DstLimits::max()) * |
| 204 | DstLimits::max()).IsValid()); |
| 205 | EXPECT_EQ(static_cast<Dst>(0), CheckedNumeric<Dst>().ValueOrDie()); |
| 206 | EXPECT_EQ(static_cast<Dst>(0), CheckedNumeric<Dst>().ValueOrDefault(1)); |
| 207 | EXPECT_EQ(static_cast<Dst>(1), |
| 208 | CheckedNumeric<Dst>(CheckedNumeric<Dst>(DstLimits::max()) * |
| 209 | DstLimits::max()).ValueOrDefault(1)); |
| 210 | |
| 211 | // Test the operator combinations. |
| 212 | TEST_EXPECTED_VALUE(2, CheckedNumeric<Dst>(1) + CheckedNumeric<Dst>(1)); |
| 213 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) - CheckedNumeric<Dst>(1)); |
| 214 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) * CheckedNumeric<Dst>(1)); |
| 215 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / CheckedNumeric<Dst>(1)); |
| 216 | TEST_EXPECTED_VALUE(2, 1 + CheckedNumeric<Dst>(1)); |
| 217 | TEST_EXPECTED_VALUE(0, 1 - CheckedNumeric<Dst>(1)); |
| 218 | TEST_EXPECTED_VALUE(1, 1 * CheckedNumeric<Dst>(1)); |
| 219 | TEST_EXPECTED_VALUE(1, 1 / CheckedNumeric<Dst>(1)); |
| 220 | TEST_EXPECTED_VALUE(2, CheckedNumeric<Dst>(1) + 1); |
| 221 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) - 1); |
| 222 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) * 1); |
| 223 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / 1); |
| 224 | CheckedNumeric<Dst> checked_dst = 1; |
| 225 | TEST_EXPECTED_VALUE(2, checked_dst += 1); |
| 226 | checked_dst = 1; |
| 227 | TEST_EXPECTED_VALUE(0, checked_dst -= 1); |
| 228 | checked_dst = 1; |
| 229 | TEST_EXPECTED_VALUE(1, checked_dst *= 1); |
| 230 | checked_dst = 1; |
| 231 | TEST_EXPECTED_VALUE(1, checked_dst /= 1); |
| 232 | |
| 233 | // Generic negation. |
| 234 | TEST_EXPECTED_VALUE(0, -CheckedNumeric<Dst>()); |
| 235 | TEST_EXPECTED_VALUE(-1, -CheckedNumeric<Dst>(1)); |
| 236 | TEST_EXPECTED_VALUE(1, -CheckedNumeric<Dst>(-1)); |
| 237 | TEST_EXPECTED_VALUE(static_cast<Dst>(DstLimits::max() * -1), |
| 238 | -CheckedNumeric<Dst>(DstLimits::max())); |
| 239 | |
| 240 | // Generic absolute value. |
| 241 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>().Abs()); |
| 242 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1).Abs()); |
| 243 | TEST_EXPECTED_VALUE(DstLimits::max(), |
| 244 | CheckedNumeric<Dst>(DstLimits::max()).Abs()); |
| 245 | |
| 246 | // Generic addition. |
| 247 | TEST_EXPECTED_VALUE(1, (CheckedNumeric<Dst>() + 1)); |
| 248 | TEST_EXPECTED_VALUE(2, (CheckedNumeric<Dst>(1) + 1)); |
| 249 | TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(-1) + 1)); |
| 250 | TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 251 | CheckedNumeric<Dst>(DstLimits::min()) + 1); |
| 252 | TEST_EXPECTED_VALIDITY( |
| 253 | RANGE_OVERFLOW, CheckedNumeric<Dst>(DstLimits::max()) + DstLimits::max()); |
| 254 | |
| 255 | // Generic subtraction. |
| 256 | TEST_EXPECTED_VALUE(-1, (CheckedNumeric<Dst>() - 1)); |
| 257 | TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(1) - 1)); |
| 258 | TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) - 1)); |
| 259 | TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 260 | CheckedNumeric<Dst>(DstLimits::max()) - 1); |
| 261 | |
| 262 | // Generic multiplication. |
| 263 | TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>() * 1)); |
| 264 | TEST_EXPECTED_VALUE(1, (CheckedNumeric<Dst>(1) * 1)); |
| 265 | TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) * 2)); |
eroman | 0397125f | 2015-05-18 14:44:32 | [diff] [blame] | 266 | TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(0) * 0)); |
| 267 | TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(-1) * 0)); |
| 268 | TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(0) * -1)); |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 269 | TEST_EXPECTED_VALIDITY( |
| 270 | RANGE_OVERFLOW, CheckedNumeric<Dst>(DstLimits::max()) * DstLimits::max()); |
| 271 | |
| 272 | // Generic division. |
| 273 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() / 1); |
| 274 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / 1); |
| 275 | TEST_EXPECTED_VALUE(DstLimits::min() / 2, |
| 276 | CheckedNumeric<Dst>(DstLimits::min()) / 2); |
| 277 | TEST_EXPECTED_VALUE(DstLimits::max() / 2, |
| 278 | CheckedNumeric<Dst>(DstLimits::max()) / 2); |
| 279 | |
| 280 | TestSpecializedArithmetic<Dst>(dst, line); |
| 281 | } |
| 282 | |
| 283 | // Helper macro to wrap displaying the conversion types and line numbers. |
| 284 | #define TEST_ARITHMETIC(Dst) TestArithmetic<Dst>(#Dst, __LINE__) |
| 285 | |
| 286 | TEST(SafeNumerics, SignedIntegerMath) { |
| 287 | TEST_ARITHMETIC(int8_t); |
| 288 | TEST_ARITHMETIC(int); |
| 289 | TEST_ARITHMETIC(intptr_t); |
| 290 | TEST_ARITHMETIC(intmax_t); |
| 291 | } |
| 292 | |
| 293 | TEST(SafeNumerics, UnsignedIntegerMath) { |
| 294 | TEST_ARITHMETIC(uint8_t); |
| 295 | TEST_ARITHMETIC(unsigned int); |
| 296 | TEST_ARITHMETIC(uintptr_t); |
| 297 | TEST_ARITHMETIC(uintmax_t); |
| 298 | } |
| 299 | |
| 300 | TEST(SafeNumerics, FloatingPointMath) { |
| 301 | TEST_ARITHMETIC(float); |
| 302 | TEST_ARITHMETIC(double); |
| 303 | } |
scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 304 | |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 305 | // Enumerates the five different conversions types we need to test. |
| 306 | enum NumericConversionType { |
| 307 | SIGN_PRESERVING_VALUE_PRESERVING, |
| 308 | SIGN_PRESERVING_NARROW, |
| 309 | SIGN_TO_UNSIGN_WIDEN_OR_EQUAL, |
| 310 | SIGN_TO_UNSIGN_NARROW, |
| 311 | UNSIGN_TO_SIGN_NARROW_OR_EQUAL, |
| 312 | }; |
scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 313 | |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 314 | // Template covering the different conversion tests. |
| 315 | template <typename Dst, typename Src, NumericConversionType conversion> |
| 316 | struct TestNumericConversion {}; |
scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 317 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 318 | // EXPECT_EQ wrappers providing specific detail on test failures. |
| 319 | #define TEST_EXPECTED_RANGE(expected, actual) \ |
| 320 | EXPECT_EQ(expected, base::internal::DstRangeRelationToSrcRange<Dst>(actual)) \ |
| 321 | << "Conversion test: " << src << " value " << actual << " to " << dst \ |
| 322 | << " on line " << line; |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 323 | |
| 324 | template <typename Dst, typename Src> |
| 325 | struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_VALUE_PRESERVING> { |
| 326 | static void Test(const char *dst, const char *src, int line) { |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 327 | typedef numeric_limits<Src> SrcLimits; |
| 328 | typedef numeric_limits<Dst> DstLimits; |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 329 | // Integral to floating. |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 330 | static_assert((DstLimits::is_iec559 && SrcLimits::is_integer) || |
| 331 | // Not floating to integral and... |
| 332 | (!(DstLimits::is_integer && SrcLimits::is_iec559) && |
| 333 | // Same sign, same numeric, source is narrower or same. |
| 334 | ((SrcLimits::is_signed == DstLimits::is_signed && |
| 335 | sizeof(Dst) >= sizeof(Src)) || |
| 336 | // Or signed destination and source is smaller |
| 337 | (DstLimits::is_signed && sizeof(Dst) > sizeof(Src)))), |
| 338 | "Comparison must be sign preserving and value preserving"); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 339 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 340 | const CheckedNumeric<Dst> checked_dst = SrcLimits::max(); |
| 341 | ; |
| 342 | TEST_EXPECTED_VALIDITY(RANGE_VALID, checked_dst); |
| 343 | if (MaxExponent<Dst>::value > MaxExponent<Src>::value) { |
| 344 | if (MaxExponent<Dst>::value >= MaxExponent<Src>::value * 2 - 1) { |
| 345 | // At least twice larger type. |
| 346 | TEST_EXPECTED_VALIDITY(RANGE_VALID, SrcLimits::max() * checked_dst); |
| 347 | |
| 348 | } else { // Larger, but not at least twice as large. |
| 349 | TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, SrcLimits::max() * checked_dst); |
| 350 | TEST_EXPECTED_VALIDITY(RANGE_VALID, checked_dst + 1); |
| 351 | } |
| 352 | } else { // Same width type. |
| 353 | TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, checked_dst + 1); |
| 354 | } |
| 355 | |
| 356 | TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::max()); |
| 357 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1)); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 358 | if (SrcLimits::is_iec559) { |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 359 | TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::max() * static_cast<Src>(-1)); |
| 360 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::infinity()); |
| 361 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::infinity() * -1); |
| 362 | TEST_EXPECTED_RANGE(RANGE_INVALID, SrcLimits::quiet_NaN()); |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 363 | } else if (numeric_limits<Src>::is_signed) { |
| 364 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(-1)); |
| 365 | TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::min()); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 366 | } |
scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 367 | } |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | template <typename Dst, typename Src> |
| 371 | struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_NARROW> { |
| 372 | static void Test(const char *dst, const char *src, int line) { |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 373 | typedef numeric_limits<Src> SrcLimits; |
| 374 | typedef numeric_limits<Dst> DstLimits; |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 375 | static_assert(SrcLimits::is_signed == DstLimits::is_signed, |
| 376 | "Destination and source sign must be the same"); |
| 377 | static_assert(sizeof(Dst) < sizeof(Src) || |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 378 | (DstLimits::is_integer && SrcLimits::is_iec559), |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 379 | "Destination must be narrower than source"); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 380 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 381 | const CheckedNumeric<Dst> checked_dst; |
| 382 | TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, checked_dst + SrcLimits::max()); |
| 383 | TEST_EXPECTED_VALUE(1, checked_dst + static_cast<Src>(1)); |
| 384 | TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, checked_dst - SrcLimits::max()); |
| 385 | |
| 386 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::max()); |
| 387 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1)); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 388 | if (SrcLimits::is_iec559) { |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 389 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::max() * -1); |
| 390 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(-1)); |
| 391 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::infinity()); |
| 392 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::infinity() * -1); |
| 393 | TEST_EXPECTED_RANGE(RANGE_INVALID, SrcLimits::quiet_NaN()); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame^] | 394 | if (DstLimits::is_integer) { |
| 395 | if (SrcLimits::digits < DstLimits::digits) { |
| 396 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, |
| 397 | static_cast<Src>(DstLimits::max())); |
| 398 | } else { |
| 399 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(DstLimits::max())); |
| 400 | } |
| 401 | TEST_EXPECTED_RANGE( |
| 402 | RANGE_VALID, |
| 403 | static_cast<Src>(GetMaxConvertibleToFloat<Src, Dst>())); |
| 404 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(DstLimits::min())); |
| 405 | } |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 406 | } else if (SrcLimits::is_signed) { |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 407 | TEST_EXPECTED_VALUE(-1, checked_dst - static_cast<Src>(1)); |
| 408 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::min()); |
| 409 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(-1)); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 410 | } else { |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 411 | TEST_EXPECTED_VALIDITY(RANGE_INVALID, checked_dst - static_cast<Src>(1)); |
| 412 | TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::min()); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | }; |
| 416 | |
| 417 | template <typename Dst, typename Src> |
| 418 | struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL> { |
| 419 | static void Test(const char *dst, const char *src, int line) { |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 420 | typedef numeric_limits<Src> SrcLimits; |
| 421 | typedef numeric_limits<Dst> DstLimits; |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 422 | static_assert(sizeof(Dst) >= sizeof(Src), |
| 423 | "Destination must be equal or wider than source."); |
| 424 | static_assert(SrcLimits::is_signed, "Source must be signed"); |
| 425 | static_assert(!DstLimits::is_signed, "Destination must be unsigned"); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 426 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 427 | const CheckedNumeric<Dst> checked_dst; |
| 428 | TEST_EXPECTED_VALUE(SrcLimits::max(), checked_dst + SrcLimits::max()); |
| 429 | TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, checked_dst + static_cast<Src>(-1)); |
| 430 | TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, checked_dst + -SrcLimits::max()); |
| 431 | |
| 432 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::min()); |
| 433 | TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::max()); |
| 434 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1)); |
| 435 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, static_cast<Src>(-1)); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 436 | } |
| 437 | }; |
| 438 | |
| 439 | template <typename Dst, typename Src> |
| 440 | struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_NARROW> { |
| 441 | static void Test(const char *dst, const char *src, int line) { |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 442 | typedef numeric_limits<Src> SrcLimits; |
| 443 | typedef numeric_limits<Dst> DstLimits; |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 444 | static_assert((DstLimits::is_integer && SrcLimits::is_iec559) || |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 445 | (sizeof(Dst) < sizeof(Src)), |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 446 | "Destination must be narrower than source."); |
| 447 | static_assert(SrcLimits::is_signed, "Source must be signed."); |
| 448 | static_assert(!DstLimits::is_signed, "Destination must be unsigned."); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 449 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 450 | const CheckedNumeric<Dst> checked_dst; |
| 451 | TEST_EXPECTED_VALUE(1, checked_dst + static_cast<Src>(1)); |
| 452 | TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, checked_dst + SrcLimits::max()); |
| 453 | TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, checked_dst + static_cast<Src>(-1)); |
| 454 | TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, checked_dst + -SrcLimits::max()); |
| 455 | |
| 456 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::max()); |
| 457 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1)); |
| 458 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, static_cast<Src>(-1)); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 459 | if (SrcLimits::is_iec559) { |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 460 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::max() * -1); |
| 461 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::infinity()); |
| 462 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::infinity() * -1); |
| 463 | TEST_EXPECTED_RANGE(RANGE_INVALID, SrcLimits::quiet_NaN()); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame^] | 464 | if (DstLimits::is_integer) { |
| 465 | if (SrcLimits::digits < DstLimits::digits) { |
| 466 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, |
| 467 | static_cast<Src>(DstLimits::max())); |
| 468 | } else { |
| 469 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(DstLimits::max())); |
| 470 | } |
| 471 | TEST_EXPECTED_RANGE( |
| 472 | RANGE_VALID, |
| 473 | static_cast<Src>(GetMaxConvertibleToFloat<Src, Dst>())); |
| 474 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(DstLimits::min())); |
| 475 | } |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 476 | } else { |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 477 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::min()); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | }; |
| 481 | |
| 482 | template <typename Dst, typename Src> |
| 483 | struct TestNumericConversion<Dst, Src, UNSIGN_TO_SIGN_NARROW_OR_EQUAL> { |
| 484 | static void Test(const char *dst, const char *src, int line) { |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 485 | typedef numeric_limits<Src> SrcLimits; |
| 486 | typedef numeric_limits<Dst> DstLimits; |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 487 | static_assert(sizeof(Dst) <= sizeof(Src), |
| 488 | "Destination must be narrower or equal to source."); |
| 489 | static_assert(!SrcLimits::is_signed, "Source must be unsigned."); |
| 490 | static_assert(DstLimits::is_signed, "Destination must be signed."); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 491 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 492 | const CheckedNumeric<Dst> checked_dst; |
| 493 | TEST_EXPECTED_VALUE(1, checked_dst + static_cast<Src>(1)); |
| 494 | TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, checked_dst + SrcLimits::max()); |
| 495 | TEST_EXPECTED_VALUE(SrcLimits::min(), checked_dst + SrcLimits::min()); |
| 496 | |
| 497 | TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::min()); |
| 498 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::max()); |
| 499 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1)); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 500 | } |
| 501 | }; |
| 502 | |
| 503 | // Helper macro to wrap displaying the conversion types and line numbers |
| 504 | #define TEST_NUMERIC_CONVERSION(d, s, t) \ |
| 505 | TestNumericConversion<d, s, t>::Test(#d, #s, __LINE__) |
| 506 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 507 | TEST(SafeNumerics, IntMinOperations) { |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 508 | TEST_NUMERIC_CONVERSION(int8_t, int8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 509 | TEST_NUMERIC_CONVERSION(uint8_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 510 | |
| 511 | TEST_NUMERIC_CONVERSION(int8_t, int, SIGN_PRESERVING_NARROW); |
| 512 | TEST_NUMERIC_CONVERSION(uint8_t, unsigned int, SIGN_PRESERVING_NARROW); |
| 513 | TEST_NUMERIC_CONVERSION(int8_t, float, SIGN_PRESERVING_NARROW); |
| 514 | |
| 515 | TEST_NUMERIC_CONVERSION(uint8_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
| 516 | |
| 517 | TEST_NUMERIC_CONVERSION(uint8_t, int, SIGN_TO_UNSIGN_NARROW); |
| 518 | TEST_NUMERIC_CONVERSION(uint8_t, intmax_t, SIGN_TO_UNSIGN_NARROW); |
| 519 | TEST_NUMERIC_CONVERSION(uint8_t, float, SIGN_TO_UNSIGN_NARROW); |
| 520 | |
| 521 | TEST_NUMERIC_CONVERSION(int8_t, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
| 522 | TEST_NUMERIC_CONVERSION(int8_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 523 | } |
| 524 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 525 | TEST(SafeNumerics, IntOperations) { |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 526 | TEST_NUMERIC_CONVERSION(int, int, SIGN_PRESERVING_VALUE_PRESERVING); |
| 527 | TEST_NUMERIC_CONVERSION(unsigned int, unsigned int, |
| 528 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 529 | TEST_NUMERIC_CONVERSION(int, int8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 530 | TEST_NUMERIC_CONVERSION(unsigned int, uint8_t, |
| 531 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 532 | TEST_NUMERIC_CONVERSION(int, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 533 | |
| 534 | TEST_NUMERIC_CONVERSION(int, intmax_t, SIGN_PRESERVING_NARROW); |
| 535 | TEST_NUMERIC_CONVERSION(unsigned int, uintmax_t, SIGN_PRESERVING_NARROW); |
| 536 | TEST_NUMERIC_CONVERSION(int, float, SIGN_PRESERVING_NARROW); |
| 537 | TEST_NUMERIC_CONVERSION(int, double, SIGN_PRESERVING_NARROW); |
| 538 | |
| 539 | TEST_NUMERIC_CONVERSION(unsigned int, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
| 540 | TEST_NUMERIC_CONVERSION(unsigned int, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
| 541 | |
| 542 | TEST_NUMERIC_CONVERSION(unsigned int, intmax_t, SIGN_TO_UNSIGN_NARROW); |
| 543 | TEST_NUMERIC_CONVERSION(unsigned int, float, SIGN_TO_UNSIGN_NARROW); |
| 544 | TEST_NUMERIC_CONVERSION(unsigned int, double, SIGN_TO_UNSIGN_NARROW); |
| 545 | |
| 546 | TEST_NUMERIC_CONVERSION(int, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
| 547 | TEST_NUMERIC_CONVERSION(int, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 548 | } |
| 549 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 550 | TEST(SafeNumerics, IntMaxOperations) { |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 551 | TEST_NUMERIC_CONVERSION(intmax_t, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 552 | TEST_NUMERIC_CONVERSION(uintmax_t, uintmax_t, |
| 553 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 554 | TEST_NUMERIC_CONVERSION(intmax_t, int, SIGN_PRESERVING_VALUE_PRESERVING); |
| 555 | TEST_NUMERIC_CONVERSION(uintmax_t, unsigned int, |
| 556 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 557 | TEST_NUMERIC_CONVERSION(intmax_t, unsigned int, |
| 558 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 559 | TEST_NUMERIC_CONVERSION(intmax_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 560 | |
| 561 | TEST_NUMERIC_CONVERSION(intmax_t, float, SIGN_PRESERVING_NARROW); |
| 562 | TEST_NUMERIC_CONVERSION(intmax_t, double, SIGN_PRESERVING_NARROW); |
| 563 | |
| 564 | TEST_NUMERIC_CONVERSION(uintmax_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
| 565 | TEST_NUMERIC_CONVERSION(uintmax_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
| 566 | |
| 567 | TEST_NUMERIC_CONVERSION(uintmax_t, float, SIGN_TO_UNSIGN_NARROW); |
| 568 | TEST_NUMERIC_CONVERSION(uintmax_t, double, SIGN_TO_UNSIGN_NARROW); |
| 569 | |
| 570 | TEST_NUMERIC_CONVERSION(intmax_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
| 571 | } |
| 572 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 573 | TEST(SafeNumerics, FloatOperations) { |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 574 | TEST_NUMERIC_CONVERSION(float, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 575 | TEST_NUMERIC_CONVERSION(float, uintmax_t, |
| 576 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 577 | TEST_NUMERIC_CONVERSION(float, int, SIGN_PRESERVING_VALUE_PRESERVING); |
| 578 | TEST_NUMERIC_CONVERSION(float, unsigned int, |
| 579 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 580 | |
| 581 | TEST_NUMERIC_CONVERSION(float, double, SIGN_PRESERVING_NARROW); |
| 582 | } |
| 583 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 584 | TEST(SafeNumerics, DoubleOperations) { |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 585 | TEST_NUMERIC_CONVERSION(double, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 586 | TEST_NUMERIC_CONVERSION(double, uintmax_t, |
| 587 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 588 | TEST_NUMERIC_CONVERSION(double, int, SIGN_PRESERVING_VALUE_PRESERVING); |
| 589 | TEST_NUMERIC_CONVERSION(double, unsigned int, |
| 590 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 591 | } |
| 592 | |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 593 | TEST(SafeNumerics, SizeTOperations) { |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 594 | TEST_NUMERIC_CONVERSION(size_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
| 595 | TEST_NUMERIC_CONVERSION(int, size_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
| 596 | } |
| 597 | |
| 598 | TEST(SafeNumerics, CastTests) { |
| 599 | // MSVC catches and warns that we're forcing saturation in these tests. |
| 600 | // Since that's intentional, we need to shut this warning off. |
| 601 | #if defined(COMPILER_MSVC) |
| 602 | #pragma warning(disable : 4756) |
scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 603 | #endif |
| 604 | |
scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 605 | int small_positive = 1; |
| 606 | int small_negative = -1; |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 607 | double double_small = 1.0; |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 608 | double double_large = numeric_limits<double>::max(); |
| 609 | double double_infinity = numeric_limits<float>::infinity(); |
danakj | 3193742f | 2015-06-05 18:15:10 | [diff] [blame] | 610 | double double_large_int = numeric_limits<int>::max(); |
| 611 | double double_small_int = numeric_limits<int>::min(); |
scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 612 | |
jschuh | 4bf22c6d | 2015-05-28 02:29:25 | [diff] [blame] | 613 | // Just test that the casts compile, since the other tests cover logic. |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 614 | EXPECT_EQ(0, checked_cast<int>(static_cast<size_t>(0))); |
jschuh | 4bf22c6d | 2015-05-28 02:29:25 | [diff] [blame] | 615 | EXPECT_EQ(0, strict_cast<int>(static_cast<char>(0))); |
| 616 | EXPECT_EQ(0, strict_cast<int>(static_cast<unsigned char>(0))); |
| 617 | EXPECT_EQ(0U, strict_cast<unsigned>(static_cast<unsigned char>(0))); |
| 618 | EXPECT_EQ(1ULL, static_cast<uint64_t>(StrictNumeric<size_t>(1U))); |
| 619 | EXPECT_EQ(1ULL, static_cast<uint64_t>(SizeT(1U))); |
| 620 | EXPECT_EQ(1U, static_cast<size_t>(StrictNumeric<unsigned>(1U))); |
| 621 | |
| 622 | EXPECT_TRUE(CheckedNumeric<uint64_t>(StrictNumeric<unsigned>(1U)).IsValid()); |
| 623 | EXPECT_TRUE(CheckedNumeric<int>(StrictNumeric<unsigned>(1U)).IsValid()); |
| 624 | EXPECT_FALSE(CheckedNumeric<unsigned>(StrictNumeric<int>(-1)).IsValid()); |
| 625 | |
| 626 | // These casts and coercions will fail to compile: |
| 627 | // EXPECT_EQ(0, strict_cast<int>(static_cast<size_t>(0))); |
| 628 | // EXPECT_EQ(0, strict_cast<size_t>(static_cast<int>(0))); |
| 629 | // EXPECT_EQ(1ULL, StrictNumeric<size_t>(1)); |
| 630 | // EXPECT_EQ(1, StrictNumeric<size_t>(1U)); |
scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 631 | |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 632 | // Test various saturation corner cases. |
| 633 | EXPECT_EQ(saturated_cast<int>(small_negative), |
| 634 | static_cast<int>(small_negative)); |
| 635 | EXPECT_EQ(saturated_cast<int>(small_positive), |
| 636 | static_cast<int>(small_positive)); |
| 637 | EXPECT_EQ(saturated_cast<unsigned>(small_negative), |
| 638 | static_cast<unsigned>(0)); |
| 639 | EXPECT_EQ(saturated_cast<int>(double_small), |
| 640 | static_cast<int>(double_small)); |
jschuh@chromium.org | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 641 | EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); |
jschuh@chromium.org | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 642 | EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); |
| 643 | EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); |
danakj | 3193742f | 2015-06-05 18:15:10 | [diff] [blame] | 644 | EXPECT_EQ(numeric_limits<int>::min(), saturated_cast<int>(double_small_int)); |
| 645 | EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int)); |
scottmg@chromium.org | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 646 | } |
jschuh@chromium.org | cb15406 | 2014-01-17 03:32:40 | [diff] [blame] | 647 | |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame^] | 648 | TEST(SafeNumerics, IsValueInRangeForNumericType) { |
| 649 | EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0)); |
| 650 | EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(1)); |
| 651 | EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(2)); |
| 652 | EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(-1)); |
| 653 | EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0xffffffffu)); |
| 654 | EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0xffffffff))); |
| 655 | EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000000))); |
| 656 | EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000001))); |
| 657 | EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>( |
| 658 | std::numeric_limits<int32_t>::min())); |
| 659 | EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>( |
| 660 | std::numeric_limits<int64_t>::min())); |
| 661 | |
| 662 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>(0)); |
| 663 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>(1)); |
| 664 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>(2)); |
| 665 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>(-1)); |
| 666 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>(0x7fffffff)); |
| 667 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>(0x7fffffffu)); |
| 668 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>(0x80000000u)); |
| 669 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>(0xffffffffu)); |
| 670 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>(INT64_C(0x80000000))); |
| 671 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>(INT64_C(0xffffffff))); |
| 672 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>(INT64_C(0x100000000))); |
| 673 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>( |
| 674 | std::numeric_limits<int32_t>::min())); |
| 675 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>( |
| 676 | implicit_cast<int64_t>(std::numeric_limits<int32_t>::min()))); |
| 677 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>( |
| 678 | implicit_cast<int64_t>(std::numeric_limits<int32_t>::min()) - 1)); |
| 679 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>( |
| 680 | std::numeric_limits<int64_t>::min())); |
| 681 | |
| 682 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(0)); |
| 683 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(1)); |
| 684 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(2)); |
| 685 | EXPECT_FALSE(IsValueInRangeForNumericType<uint64_t>(-1)); |
| 686 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(0xffffffffu)); |
| 687 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(UINT64_C(0xffffffff))); |
| 688 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(UINT64_C(0x100000000))); |
| 689 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(UINT64_C(0x100000001))); |
| 690 | EXPECT_FALSE(IsValueInRangeForNumericType<uint64_t>( |
| 691 | std::numeric_limits<int32_t>::min())); |
| 692 | EXPECT_FALSE(IsValueInRangeForNumericType<uint64_t>(INT64_C(-1))); |
| 693 | EXPECT_FALSE(IsValueInRangeForNumericType<uint64_t>( |
| 694 | std::numeric_limits<int64_t>::min())); |
| 695 | |
| 696 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(0)); |
| 697 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(1)); |
| 698 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(2)); |
| 699 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(-1)); |
| 700 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(0x7fffffff)); |
| 701 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(0x7fffffffu)); |
| 702 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(0x80000000u)); |
| 703 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(0xffffffffu)); |
| 704 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(INT64_C(0x80000000))); |
| 705 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(INT64_C(0xffffffff))); |
| 706 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(INT64_C(0x100000000))); |
| 707 | EXPECT_TRUE( |
| 708 | IsValueInRangeForNumericType<int64_t>(INT64_C(0x7fffffffffffffff))); |
| 709 | EXPECT_TRUE( |
| 710 | IsValueInRangeForNumericType<int64_t>(UINT64_C(0x7fffffffffffffff))); |
| 711 | EXPECT_FALSE( |
| 712 | IsValueInRangeForNumericType<int64_t>(UINT64_C(0x8000000000000000))); |
| 713 | EXPECT_FALSE( |
| 714 | IsValueInRangeForNumericType<int64_t>(UINT64_C(0xffffffffffffffff))); |
| 715 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>( |
| 716 | std::numeric_limits<int32_t>::min())); |
| 717 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>( |
| 718 | implicit_cast<int64_t>(std::numeric_limits<int32_t>::min()))); |
| 719 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>( |
| 720 | std::numeric_limits<int64_t>::min())); |
| 721 | } |