[email protected] | 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 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 5 | #include <stddef.h> |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 6 | #include <stdint.h> |
| 7 | |
| 8 | #include <limits> |
vmpstr | 98a2fad | 2015-11-30 20:15:17 | [diff] [blame] | 9 | #include <type_traits> |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 10 | |
| 11 | #include "base/compiler_specific.h" |
jschuh | 224f1d7 | 2016-11-25 20:08:48 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 13 | #include "base/numerics/safe_conversions.h" |
| 14 | #include "base/numerics/safe_math.h" |
gab | 190f754 | 2016-08-01 20:03:41 | [diff] [blame] | 15 | #include "base/test/gtest_util.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 16 | #include "build/build_config.h" |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 19 | #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
| 20 | #include <mmintrin.h> |
| 21 | #endif |
| 22 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 23 | using std::numeric_limits; |
| 24 | using base::CheckedNumeric; |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 25 | using base::IsValidForType; |
| 26 | using base::ValueOrDieForType; |
| 27 | using base::ValueOrDefaultForType; |
jschuh | 4bc919cb5 | 2016-12-04 15:08:27 | [diff] [blame] | 28 | using base::MakeCheckedNum; |
jschuh | 711ac6a | 2016-12-04 07:17:48 | [diff] [blame] | 29 | using base::CheckMax; |
| 30 | using base::CheckMin; |
jschuh | 4fcd6fa | 2016-11-24 11:58:39 | [diff] [blame] | 31 | using base::CheckAdd; |
| 32 | using base::CheckSub; |
| 33 | using base::CheckMul; |
| 34 | using base::CheckDiv; |
| 35 | using base::CheckMod; |
| 36 | using base::CheckLsh; |
| 37 | using base::CheckRsh; |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 38 | using base::checked_cast; |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 39 | using base::IsValueInRangeForNumericType; |
jschuh | 07345e6 | 2015-09-22 22:13:36 | [diff] [blame] | 40 | using base::IsValueNegative; |
jschuh | 4bf22c6d | 2015-05-28 02:29:25 | [diff] [blame] | 41 | using base::SizeT; |
| 42 | using base::StrictNumeric; |
jschuh | 711ac6a | 2016-12-04 07:17:48 | [diff] [blame] | 43 | using base::MakeStrictNum; |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 44 | using base::saturated_cast; |
jschuh | 4bf22c6d | 2015-05-28 02:29:25 | [diff] [blame] | 45 | using base::strict_cast; |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 46 | using base::internal::MaxExponent; |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 47 | using base::internal::IntegerBitsPlusSign; |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 48 | using base::internal::RANGE_VALID; |
| 49 | using base::internal::RANGE_INVALID; |
| 50 | using base::internal::RANGE_OVERFLOW; |
| 51 | using base::internal::RANGE_UNDERFLOW; |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 52 | |
jschuh | 3ab5404 | 2016-11-17 06:58:44 | [diff] [blame] | 53 | // These tests deliberately cause arithmetic boundary errors. If the compiler is |
| 54 | // aggressive enough, it can const detect these errors, so we disable warnings. |
[email protected] | b6bf5c32 | 2014-08-09 05:24:02 | [diff] [blame] | 55 | #if defined(OS_WIN) |
jschuh | 3ab5404 | 2016-11-17 06:58:44 | [diff] [blame] | 56 | #pragma warning(disable : 4756) // Arithmetic overflow. |
| 57 | #pragma warning(disable : 4293) // Invalid shift. |
[email protected] | b6bf5c32 | 2014-08-09 05:24:02 | [diff] [blame] | 58 | #endif |
| 59 | |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 60 | // This is a helper function for finding the maximum value in Src that can be |
| 61 | // wholy represented as the destination floating-point type. |
| 62 | template <typename Dst, typename Src> |
| 63 | Dst GetMaxConvertibleToFloat() { |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 64 | using DstLimits = numeric_limits<Dst>; |
| 65 | using SrcLimits = numeric_limits<Src>; |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 66 | static_assert(SrcLimits::is_specialized, "Source must be numeric."); |
| 67 | static_assert(DstLimits::is_specialized, "Destination must be numeric."); |
| 68 | CHECK(DstLimits::is_iec559); |
| 69 | |
| 70 | if (SrcLimits::digits <= DstLimits::digits && |
| 71 | MaxExponent<Src>::value <= MaxExponent<Dst>::value) |
| 72 | return SrcLimits::max(); |
| 73 | Src max = SrcLimits::max() / 2 + (SrcLimits::is_integer ? 1 : 0); |
| 74 | while (max != static_cast<Src>(static_cast<Dst>(max))) { |
| 75 | max /= 2; |
| 76 | } |
| 77 | return static_cast<Dst>(max); |
| 78 | } |
| 79 | |
jschuh | d1c1f2e | 2016-11-21 21:45:48 | [diff] [blame] | 80 | namespace base { |
| 81 | namespace internal { |
jschuh | 33232e0 | 2017-01-03 20:33:36 | [diff] [blame] | 82 | |
| 83 | // Test corner case promotions used |
| 84 | static_assert(IsIntegerArithmeticSafe<int32_t, int8_t, int8_t>::value, ""); |
| 85 | static_assert(IsIntegerArithmeticSafe<int32_t, int16_t, int8_t>::value, ""); |
| 86 | static_assert(IsIntegerArithmeticSafe<int32_t, int8_t, int16_t>::value, ""); |
| 87 | static_assert(!IsIntegerArithmeticSafe<int32_t, int32_t, int8_t>::value, ""); |
| 88 | static_assert(BigEnoughPromotion<int16_t, int8_t>::is_contained, ""); |
| 89 | static_assert(BigEnoughPromotion<int32_t, uint32_t>::is_contained, ""); |
| 90 | static_assert(BigEnoughPromotion<intmax_t, int8_t>::is_contained, ""); |
| 91 | static_assert(!BigEnoughPromotion<uintmax_t, int8_t>::is_contained, ""); |
| 92 | static_assert( |
| 93 | std::is_same<BigEnoughPromotion<int16_t, int8_t>::type, int16_t>::value, |
| 94 | ""); |
| 95 | static_assert( |
| 96 | std::is_same<BigEnoughPromotion<int32_t, uint32_t>::type, int64_t>::value, |
| 97 | ""); |
| 98 | static_assert( |
| 99 | std::is_same<BigEnoughPromotion<intmax_t, int8_t>::type, intmax_t>::value, |
| 100 | ""); |
| 101 | static_assert( |
| 102 | std::is_same<BigEnoughPromotion<uintmax_t, int8_t>::type, uintmax_t>::value, |
| 103 | ""); |
| 104 | static_assert(BigEnoughPromotion<int16_t, int8_t>::is_contained, ""); |
| 105 | static_assert(BigEnoughPromotion<int32_t, uint32_t>::is_contained, ""); |
| 106 | static_assert(BigEnoughPromotion<intmax_t, int8_t>::is_contained, ""); |
| 107 | static_assert(!BigEnoughPromotion<uintmax_t, int8_t>::is_contained, ""); |
| 108 | static_assert( |
| 109 | std::is_same<FastIntegerArithmeticPromotion<int16_t, int8_t>::type, |
| 110 | int32_t>::value, |
| 111 | ""); |
| 112 | static_assert( |
| 113 | std::is_same<FastIntegerArithmeticPromotion<int32_t, uint32_t>::type, |
| 114 | int64_t>::value, |
| 115 | ""); |
| 116 | static_assert( |
| 117 | std::is_same<FastIntegerArithmeticPromotion<intmax_t, int8_t>::type, |
| 118 | intmax_t>::value, |
| 119 | ""); |
| 120 | static_assert( |
| 121 | std::is_same<FastIntegerArithmeticPromotion<uintmax_t, int8_t>::type, |
| 122 | uintmax_t>::value, |
| 123 | ""); |
| 124 | static_assert(FastIntegerArithmeticPromotion<int16_t, int8_t>::is_contained, |
| 125 | ""); |
| 126 | static_assert(FastIntegerArithmeticPromotion<int32_t, uint32_t>::is_contained, |
| 127 | ""); |
| 128 | static_assert(!FastIntegerArithmeticPromotion<intmax_t, int8_t>::is_contained, |
| 129 | ""); |
| 130 | static_assert(!FastIntegerArithmeticPromotion<uintmax_t, int8_t>::is_contained, |
| 131 | ""); |
| 132 | |
jschuh | d1c1f2e | 2016-11-21 21:45:48 | [diff] [blame] | 133 | template <typename U> |
| 134 | U GetNumericValueForTest(const CheckedNumeric<U>& src) { |
| 135 | return src.state_.value(); |
| 136 | } |
| 137 | } // namespace internal. |
| 138 | } // namespace base. |
| 139 | |
| 140 | using base::internal::GetNumericValueForTest; |
| 141 | |
jschuh | 224f1d7 | 2016-11-25 20:08:48 | [diff] [blame] | 142 | // Logs the ValueOrDie() failure instead of crashing. |
| 143 | struct LogOnFailure { |
| 144 | template <typename T> |
| 145 | static T HandleFailure() { |
| 146 | LOG(WARNING) << "ValueOrDie() failed unexpectedly."; |
| 147 | return T(); |
| 148 | } |
| 149 | }; |
| 150 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 151 | // Helper macros to wrap displaying the conversion types and line numbers. |
| 152 | #define TEST_EXPECTED_VALIDITY(expected, actual) \ |
jschuh | 4fcd6fa | 2016-11-24 11:58:39 | [diff] [blame] | 153 | EXPECT_EQ(expected, (actual).template Cast<Dst>().IsValid()) \ |
jschuh | d1c1f2e | 2016-11-21 21:45:48 | [diff] [blame] | 154 | << "Result test: Value " << GetNumericValueForTest(actual) << " as " \ |
| 155 | << dst << " on line " << line |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 156 | |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 157 | #define TEST_EXPECTED_SUCCESS(actual) TEST_EXPECTED_VALIDITY(true, actual) |
| 158 | #define TEST_EXPECTED_FAILURE(actual) TEST_EXPECTED_VALIDITY(false, actual) |
| 159 | |
jschuh | ba3c4f94 | 2016-12-10 14:03:55 | [diff] [blame] | 160 | // We have to handle promotions, so infer the underlying type below from actual. |
| 161 | #define TEST_EXPECTED_VALUE(expected, actual) \ |
| 162 | EXPECT_EQ(static_cast<typename std::decay<decltype(actual)>::type::type>( \ |
| 163 | expected), \ |
| 164 | ((actual) \ |
| 165 | .template ValueOrDie< \ |
| 166 | typename std::decay<decltype(actual)>::type::type, \ |
| 167 | LogOnFailure>())) \ |
| 168 | << "Result test: Value " << GetNumericValueForTest(actual) << " as " \ |
jschuh | d1c1f2e | 2016-11-21 21:45:48 | [diff] [blame] | 169 | << dst << " on line " << line |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 170 | |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 171 | // Test the simple pointer arithmetic overrides. |
| 172 | template <typename Dst> |
| 173 | void TestStrictPointerMath() { |
| 174 | Dst dummy_value = 0; |
| 175 | Dst* dummy_ptr = &dummy_value; |
| 176 | static const Dst kDummyOffset = 2; // Don't want to go too far. |
| 177 | EXPECT_EQ(dummy_ptr + kDummyOffset, |
| 178 | dummy_ptr + StrictNumeric<Dst>(kDummyOffset)); |
| 179 | EXPECT_EQ(dummy_ptr - kDummyOffset, |
| 180 | dummy_ptr - StrictNumeric<Dst>(kDummyOffset)); |
| 181 | EXPECT_NE(dummy_ptr, dummy_ptr + StrictNumeric<Dst>(kDummyOffset)); |
| 182 | EXPECT_NE(dummy_ptr, dummy_ptr - StrictNumeric<Dst>(kDummyOffset)); |
| 183 | EXPECT_DEATH_IF_SUPPORTED( |
| 184 | dummy_ptr + StrictNumeric<size_t>(std::numeric_limits<size_t>::max()), |
| 185 | ""); |
| 186 | } |
| 187 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 188 | // Signed integer arithmetic. |
| 189 | template <typename Dst> |
| 190 | static void TestSpecializedArithmetic( |
| 191 | const char* dst, |
| 192 | int line, |
vmpstr | 98a2fad | 2015-11-30 20:15:17 | [diff] [blame] | 193 | typename std::enable_if<numeric_limits<Dst>::is_integer && |
| 194 | numeric_limits<Dst>::is_signed, |
| 195 | int>::type = 0) { |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 196 | using DstLimits = numeric_limits<Dst>; |
| 197 | TEST_EXPECTED_FAILURE(-CheckedNumeric<Dst>(DstLimits::lowest())); |
| 198 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()).Abs()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 199 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(-1).Abs()); |
jschuh | ba3c4f94 | 2016-12-10 14:03:55 | [diff] [blame] | 200 | TEST_EXPECTED_VALUE(DstLimits::max(), |
| 201 | MakeCheckedNum(-DstLimits::max()).Abs()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 202 | |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 203 | TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::max()) + -1); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 204 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()) + -1); |
| 205 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()) + |
| 206 | DstLimits::lowest()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 207 | |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 208 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()) - 1); |
| 209 | TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::lowest()) - -1); |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 210 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) - |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 211 | DstLimits::lowest()); |
| 212 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()) - |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 213 | DstLimits::max()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 214 | |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 215 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()) * 2); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 216 | |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 217 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()) / -1); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 218 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(-1) / 2); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 219 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()) * -1); |
jschuh | 33232e0 | 2017-01-03 20:33:36 | [diff] [blame] | 220 | TEST_EXPECTED_VALUE(DstLimits::max(), |
| 221 | CheckedNumeric<Dst>(DstLimits::lowest() + 1) * Dst(-1)); |
| 222 | TEST_EXPECTED_VALUE(DstLimits::max(), |
| 223 | CheckedNumeric<Dst>(-1) * Dst(DstLimits::lowest() + 1)); |
| 224 | TEST_EXPECTED_VALUE(DstLimits::lowest(), |
| 225 | CheckedNumeric<Dst>(DstLimits::lowest()) * Dst(1)); |
| 226 | TEST_EXPECTED_VALUE(DstLimits::lowest(), |
| 227 | CheckedNumeric<Dst>(1) * Dst(DstLimits::lowest())); |
jschuh | ba3c4f94 | 2016-12-10 14:03:55 | [diff] [blame] | 228 | TEST_EXPECTED_VALUE(DstLimits::lowest(), |
| 229 | MakeCheckedNum(DstLimits::lowest()).UnsignedAbs()); |
| 230 | TEST_EXPECTED_VALUE(DstLimits::max(), |
| 231 | MakeCheckedNum(DstLimits::max()).UnsignedAbs()); |
| 232 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(0).UnsignedAbs()); |
| 233 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1).UnsignedAbs()); |
| 234 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(-1).UnsignedAbs()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 235 | |
| 236 | // Modulus is legal only for integers. |
| 237 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() % 1); |
| 238 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
| 239 | TEST_EXPECTED_VALUE(-1, CheckedNumeric<Dst>(-1) % 2); |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 240 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(-1) % -2); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 241 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::lowest()) % 2); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 242 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(DstLimits::max()) % 2); |
| 243 | // Test all the different modulus combinations. |
| 244 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % CheckedNumeric<Dst>(1)); |
| 245 | TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric<Dst>(1)); |
| 246 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
| 247 | CheckedNumeric<Dst> checked_dst = 1; |
| 248 | TEST_EXPECTED_VALUE(0, checked_dst %= 1); |
tsepez | dfd77a9 | 2016-11-02 20:18:06 | [diff] [blame] | 249 | // Test that div by 0 is avoided but returns invalid result. |
| 250 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) % 0); |
jschuh | 3ab5404 | 2016-11-17 06:58:44 | [diff] [blame] | 251 | // Test bit shifts. |
| 252 | volatile Dst negative_one = -1; |
| 253 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) << negative_one); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 254 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) |
| 255 | << (IntegerBitsPlusSign<Dst>::value - 1)); |
| 256 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(0) |
| 257 | << IntegerBitsPlusSign<Dst>::value); |
jschuh | 3ab5404 | 2016-11-17 06:58:44 | [diff] [blame] | 258 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) << 1); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 259 | TEST_EXPECTED_VALUE( |
| 260 | static_cast<Dst>(1) << (IntegerBitsPlusSign<Dst>::value - 2), |
| 261 | CheckedNumeric<Dst>(1) << (IntegerBitsPlusSign<Dst>::value - 2)); |
jschuh | 3ab5404 | 2016-11-17 06:58:44 | [diff] [blame] | 262 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(0) |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 263 | << (IntegerBitsPlusSign<Dst>::value - 1)); |
jschuh | 3ab5404 | 2016-11-17 06:58:44 | [diff] [blame] | 264 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) << 0); |
| 265 | TEST_EXPECTED_VALUE(2, CheckedNumeric<Dst>(1) << 1); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 266 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) >> |
| 267 | IntegerBitsPlusSign<Dst>::value); |
| 268 | TEST_EXPECTED_VALUE( |
| 269 | 0, CheckedNumeric<Dst>(1) >> (IntegerBitsPlusSign<Dst>::value - 1)); |
jschuh | 3ab5404 | 2016-11-17 06:58:44 | [diff] [blame] | 270 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) >> negative_one); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 271 | |
| 272 | TestStrictPointerMath<Dst>(); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // Unsigned integer arithmetic. |
| 276 | template <typename Dst> |
| 277 | static void TestSpecializedArithmetic( |
| 278 | const char* dst, |
| 279 | int line, |
vmpstr | 98a2fad | 2015-11-30 20:15:17 | [diff] [blame] | 280 | typename std::enable_if<numeric_limits<Dst>::is_integer && |
| 281 | !numeric_limits<Dst>::is_signed, |
| 282 | int>::type = 0) { |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 283 | using DstLimits = numeric_limits<Dst>; |
| 284 | TEST_EXPECTED_SUCCESS(-CheckedNumeric<Dst>(DstLimits::lowest())); |
| 285 | TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::lowest()).Abs()); |
| 286 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()) + -1); |
| 287 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()) - 1); |
| 288 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::lowest()) * 2); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 289 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) / 2); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 290 | TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::lowest()).UnsignedAbs()); |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 291 | TEST_EXPECTED_SUCCESS( |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 292 | CheckedNumeric<typename std::make_signed<Dst>::type>( |
| 293 | std::numeric_limits<typename std::make_signed<Dst>::type>::lowest()) |
jschuh | eaf375f | 2015-09-17 01:04:28 | [diff] [blame] | 294 | .UnsignedAbs()); |
jschuh | ba3c4f94 | 2016-12-10 14:03:55 | [diff] [blame] | 295 | TEST_EXPECTED_VALUE(DstLimits::lowest(), |
| 296 | MakeCheckedNum(DstLimits::lowest()).UnsignedAbs()); |
| 297 | TEST_EXPECTED_VALUE(DstLimits::max(), |
| 298 | MakeCheckedNum(DstLimits::max()).UnsignedAbs()); |
| 299 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(0).UnsignedAbs()); |
| 300 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1).UnsignedAbs()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 301 | |
| 302 | // Modulus is legal only for integers. |
| 303 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() % 1); |
| 304 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
| 305 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) % 2); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 306 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::lowest()) % 2); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 307 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(DstLimits::max()) % 2); |
| 308 | // Test all the different modulus combinations. |
| 309 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % CheckedNumeric<Dst>(1)); |
| 310 | TEST_EXPECTED_VALUE(0, 1 % CheckedNumeric<Dst>(1)); |
| 311 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
| 312 | CheckedNumeric<Dst> checked_dst = 1; |
| 313 | TEST_EXPECTED_VALUE(0, checked_dst %= 1); |
tsepez | dfd77a9 | 2016-11-02 20:18:06 | [diff] [blame] | 314 | // Test that div by 0 is avoided but returns invalid result. |
| 315 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) % 0); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 316 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) |
| 317 | << IntegerBitsPlusSign<Dst>::value); |
jschuh | 3ab5404 | 2016-11-17 06:58:44 | [diff] [blame] | 318 | // Test bit shifts. |
| 319 | volatile int negative_one = -1; |
| 320 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) << negative_one); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 321 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) |
| 322 | << IntegerBitsPlusSign<Dst>::value); |
| 323 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(0) |
| 324 | << IntegerBitsPlusSign<Dst>::value); |
jschuh | 3ab5404 | 2016-11-17 06:58:44 | [diff] [blame] | 325 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) << 1); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 326 | TEST_EXPECTED_VALUE( |
| 327 | static_cast<Dst>(1) << (IntegerBitsPlusSign<Dst>::value - 1), |
| 328 | CheckedNumeric<Dst>(1) << (IntegerBitsPlusSign<Dst>::value - 1)); |
jschuh | 3ab5404 | 2016-11-17 06:58:44 | [diff] [blame] | 329 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) << 0); |
| 330 | TEST_EXPECTED_VALUE(2, CheckedNumeric<Dst>(1) << 1); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 331 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) >> |
| 332 | IntegerBitsPlusSign<Dst>::value); |
| 333 | TEST_EXPECTED_VALUE( |
| 334 | 0, CheckedNumeric<Dst>(1) >> (IntegerBitsPlusSign<Dst>::value - 1)); |
jschuh | 3ab5404 | 2016-11-17 06:58:44 | [diff] [blame] | 335 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(1) >> negative_one); |
jschuh | b6737bb | 2016-11-29 03:06:59 | [diff] [blame] | 336 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) & 1); |
| 337 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) & 0); |
| 338 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(0) & 1); |
| 339 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) & 0); |
| 340 | TEST_EXPECTED_VALUE(std::numeric_limits<Dst>::max(), |
jschuh | 4bc919cb5 | 2016-12-04 15:08:27 | [diff] [blame] | 341 | MakeCheckedNum(DstLimits::max()) & -1); |
jschuh | b6737bb | 2016-11-29 03:06:59 | [diff] [blame] | 342 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) | 1); |
| 343 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) | 0); |
| 344 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(0) | 1); |
| 345 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(0) | 0); |
| 346 | TEST_EXPECTED_VALUE(std::numeric_limits<Dst>::max(), |
| 347 | CheckedNumeric<Dst>(0) | static_cast<Dst>(-1)); |
| 348 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) ^ 1); |
| 349 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) ^ 0); |
| 350 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(0) ^ 1); |
| 351 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(0) ^ 0); |
| 352 | TEST_EXPECTED_VALUE(std::numeric_limits<Dst>::max(), |
| 353 | CheckedNumeric<Dst>(0) ^ static_cast<Dst>(-1)); |
jschuh | 970313c | 2016-11-24 21:40:58 | [diff] [blame] | 354 | TEST_EXPECTED_VALUE(DstLimits::max(), ~CheckedNumeric<Dst>(0)); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 355 | |
| 356 | TestStrictPointerMath<Dst>(); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // Floating point arithmetic. |
| 360 | template <typename Dst> |
| 361 | void TestSpecializedArithmetic( |
| 362 | const char* dst, |
| 363 | int line, |
vmpstr | 98a2fad | 2015-11-30 20:15:17 | [diff] [blame] | 364 | typename std::enable_if<numeric_limits<Dst>::is_iec559, int>::type = 0) { |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 365 | using DstLimits = numeric_limits<Dst>; |
| 366 | TEST_EXPECTED_SUCCESS(-CheckedNumeric<Dst>(DstLimits::lowest())); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 367 | |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 368 | TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::lowest()).Abs()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 369 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(-1).Abs()); |
| 370 | |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 371 | TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::lowest()) + -1); |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 372 | TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::max()) + 1); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 373 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()) + |
| 374 | DstLimits::lowest()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 375 | |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 376 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) - |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 377 | DstLimits::lowest()); |
| 378 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()) - |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 379 | DstLimits::max()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 380 | |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 381 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::lowest()) * 2); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 382 | |
| 383 | TEST_EXPECTED_VALUE(-0.5, CheckedNumeric<Dst>(-1.0) / 2); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // Generic arithmetic tests. |
| 387 | template <typename Dst> |
| 388 | static void TestArithmetic(const char* dst, int line) { |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 389 | using DstLimits = numeric_limits<Dst>; |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 390 | |
| 391 | EXPECT_EQ(true, CheckedNumeric<Dst>().IsValid()); |
| 392 | EXPECT_EQ(false, |
| 393 | CheckedNumeric<Dst>(CheckedNumeric<Dst>(DstLimits::max()) * |
| 394 | DstLimits::max()).IsValid()); |
| 395 | EXPECT_EQ(static_cast<Dst>(0), CheckedNumeric<Dst>().ValueOrDie()); |
| 396 | EXPECT_EQ(static_cast<Dst>(0), CheckedNumeric<Dst>().ValueOrDefault(1)); |
| 397 | EXPECT_EQ(static_cast<Dst>(1), |
| 398 | CheckedNumeric<Dst>(CheckedNumeric<Dst>(DstLimits::max()) * |
| 399 | DstLimits::max()).ValueOrDefault(1)); |
| 400 | |
| 401 | // Test the operator combinations. |
| 402 | TEST_EXPECTED_VALUE(2, CheckedNumeric<Dst>(1) + CheckedNumeric<Dst>(1)); |
| 403 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) - CheckedNumeric<Dst>(1)); |
| 404 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) * CheckedNumeric<Dst>(1)); |
| 405 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / CheckedNumeric<Dst>(1)); |
| 406 | TEST_EXPECTED_VALUE(2, 1 + CheckedNumeric<Dst>(1)); |
| 407 | TEST_EXPECTED_VALUE(0, 1 - CheckedNumeric<Dst>(1)); |
| 408 | TEST_EXPECTED_VALUE(1, 1 * CheckedNumeric<Dst>(1)); |
| 409 | TEST_EXPECTED_VALUE(1, 1 / CheckedNumeric<Dst>(1)); |
| 410 | TEST_EXPECTED_VALUE(2, CheckedNumeric<Dst>(1) + 1); |
| 411 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) - 1); |
| 412 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) * 1); |
| 413 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / 1); |
| 414 | CheckedNumeric<Dst> checked_dst = 1; |
| 415 | TEST_EXPECTED_VALUE(2, checked_dst += 1); |
| 416 | checked_dst = 1; |
| 417 | TEST_EXPECTED_VALUE(0, checked_dst -= 1); |
| 418 | checked_dst = 1; |
| 419 | TEST_EXPECTED_VALUE(1, checked_dst *= 1); |
| 420 | checked_dst = 1; |
| 421 | TEST_EXPECTED_VALUE(1, checked_dst /= 1); |
| 422 | |
| 423 | // Generic negation. |
jschuh | 749c7f7 | 2016-09-07 16:22:26 | [diff] [blame] | 424 | if (DstLimits::is_signed) { |
| 425 | TEST_EXPECTED_VALUE(0, -CheckedNumeric<Dst>()); |
| 426 | TEST_EXPECTED_VALUE(-1, -CheckedNumeric<Dst>(1)); |
| 427 | TEST_EXPECTED_VALUE(1, -CheckedNumeric<Dst>(-1)); |
| 428 | TEST_EXPECTED_VALUE(static_cast<Dst>(DstLimits::max() * -1), |
| 429 | -CheckedNumeric<Dst>(DstLimits::max())); |
| 430 | } |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 431 | |
| 432 | // Generic absolute value. |
| 433 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>().Abs()); |
| 434 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1).Abs()); |
| 435 | TEST_EXPECTED_VALUE(DstLimits::max(), |
| 436 | CheckedNumeric<Dst>(DstLimits::max()).Abs()); |
| 437 | |
| 438 | // Generic addition. |
| 439 | TEST_EXPECTED_VALUE(1, (CheckedNumeric<Dst>() + 1)); |
| 440 | TEST_EXPECTED_VALUE(2, (CheckedNumeric<Dst>(1) + 1)); |
jschuh | 216b62a9 | 2016-11-08 03:53:08 | [diff] [blame] | 441 | if (numeric_limits<Dst>::is_signed) |
| 442 | TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(-1) + 1)); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 443 | TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::lowest()) + 1); |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 444 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) + |
| 445 | DstLimits::max()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 446 | |
| 447 | // Generic subtraction. |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 448 | TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(1) - 1)); |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 449 | TEST_EXPECTED_SUCCESS(CheckedNumeric<Dst>(DstLimits::max()) - 1); |
jschuh | 216b62a9 | 2016-11-08 03:53:08 | [diff] [blame] | 450 | if (numeric_limits<Dst>::is_signed) { |
| 451 | TEST_EXPECTED_VALUE(-1, (CheckedNumeric<Dst>() - 1)); |
| 452 | TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) - 1)); |
jschuh | 657a3d6 | 2016-11-15 18:23:45 | [diff] [blame] | 453 | } else { |
| 454 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) - -1); |
jschuh | 216b62a9 | 2016-11-08 03:53:08 | [diff] [blame] | 455 | } |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 456 | |
| 457 | // Generic multiplication. |
| 458 | TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>() * 1)); |
| 459 | TEST_EXPECTED_VALUE(1, (CheckedNumeric<Dst>(1) * 1)); |
eroman | 0397125f | 2015-05-18 14:44:32 | [diff] [blame] | 460 | TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(0) * 0)); |
jschuh | 216b62a9 | 2016-11-08 03:53:08 | [diff] [blame] | 461 | if (numeric_limits<Dst>::is_signed) { |
| 462 | TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(-1) * 0)); |
| 463 | TEST_EXPECTED_VALUE(0, (CheckedNumeric<Dst>(0) * -1)); |
jschuh | 749c7f7 | 2016-09-07 16:22:26 | [diff] [blame] | 464 | TEST_EXPECTED_VALUE(-2, (CheckedNumeric<Dst>(-1) * 2)); |
jschuh | 657a3d6 | 2016-11-15 18:23:45 | [diff] [blame] | 465 | } else { |
| 466 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) * -2); |
| 467 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) * |
| 468 | CheckedNumeric<uintmax_t>(-2)); |
jschuh | 749c7f7 | 2016-09-07 16:22:26 | [diff] [blame] | 469 | } |
jschuh | 216b62a9 | 2016-11-08 03:53:08 | [diff] [blame] | 470 | TEST_EXPECTED_FAILURE(CheckedNumeric<Dst>(DstLimits::max()) * |
| 471 | DstLimits::max()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 472 | |
| 473 | // Generic division. |
| 474 | TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>() / 1); |
| 475 | TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(1) / 1); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 476 | TEST_EXPECTED_VALUE(DstLimits::lowest() / 2, |
| 477 | CheckedNumeric<Dst>(DstLimits::lowest()) / 2); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 478 | TEST_EXPECTED_VALUE(DstLimits::max() / 2, |
| 479 | CheckedNumeric<Dst>(DstLimits::max()) / 2); |
| 480 | |
| 481 | TestSpecializedArithmetic<Dst>(dst, line); |
| 482 | } |
| 483 | |
| 484 | // Helper macro to wrap displaying the conversion types and line numbers. |
| 485 | #define TEST_ARITHMETIC(Dst) TestArithmetic<Dst>(#Dst, __LINE__) |
| 486 | |
| 487 | TEST(SafeNumerics, SignedIntegerMath) { |
| 488 | TEST_ARITHMETIC(int8_t); |
| 489 | TEST_ARITHMETIC(int); |
| 490 | TEST_ARITHMETIC(intptr_t); |
| 491 | TEST_ARITHMETIC(intmax_t); |
| 492 | } |
| 493 | |
| 494 | TEST(SafeNumerics, UnsignedIntegerMath) { |
| 495 | TEST_ARITHMETIC(uint8_t); |
| 496 | TEST_ARITHMETIC(unsigned int); |
| 497 | TEST_ARITHMETIC(uintptr_t); |
| 498 | TEST_ARITHMETIC(uintmax_t); |
| 499 | } |
| 500 | |
| 501 | TEST(SafeNumerics, FloatingPointMath) { |
| 502 | TEST_ARITHMETIC(float); |
| 503 | TEST_ARITHMETIC(double); |
| 504 | } |
[email protected] | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 505 | |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 506 | // Enumerates the five different conversions types we need to test. |
| 507 | enum NumericConversionType { |
| 508 | SIGN_PRESERVING_VALUE_PRESERVING, |
| 509 | SIGN_PRESERVING_NARROW, |
| 510 | SIGN_TO_UNSIGN_WIDEN_OR_EQUAL, |
| 511 | SIGN_TO_UNSIGN_NARROW, |
| 512 | UNSIGN_TO_SIGN_NARROW_OR_EQUAL, |
| 513 | }; |
[email protected] | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 514 | |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 515 | // Template covering the different conversion tests. |
| 516 | template <typename Dst, typename Src, NumericConversionType conversion> |
| 517 | struct TestNumericConversion {}; |
[email protected] | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 518 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 519 | // EXPECT_EQ wrappers providing specific detail on test failures. |
| 520 | #define TEST_EXPECTED_RANGE(expected, actual) \ |
| 521 | EXPECT_EQ(expected, base::internal::DstRangeRelationToSrcRange<Dst>(actual)) \ |
| 522 | << "Conversion test: " << src << " value " << actual << " to " << dst \ |
jschuh | 657a3d6 | 2016-11-15 18:23:45 | [diff] [blame] | 523 | << " on line " << line |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 524 | |
| 525 | template <typename Dst, typename Src> |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 526 | void TestStrictComparison() { |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 527 | using DstLimits = numeric_limits<Dst>; |
| 528 | using SrcLimits = numeric_limits<Src>; |
jschuh | 711ac6a | 2016-12-04 07:17:48 | [diff] [blame] | 529 | static_assert(StrictNumeric<Src>(SrcLimits::lowest()) < DstLimits::max(), ""); |
| 530 | static_assert(StrictNumeric<Src>(SrcLimits::lowest()) < SrcLimits::max(), ""); |
| 531 | static_assert(!(StrictNumeric<Src>(SrcLimits::lowest()) >= DstLimits::max()), |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 532 | ""); |
jschuh | 711ac6a | 2016-12-04 07:17:48 | [diff] [blame] | 533 | static_assert(!(StrictNumeric<Src>(SrcLimits::lowest()) >= SrcLimits::max()), |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 534 | ""); |
jschuh | 711ac6a | 2016-12-04 07:17:48 | [diff] [blame] | 535 | static_assert(StrictNumeric<Src>(SrcLimits::lowest()) <= DstLimits::max(), |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 536 | ""); |
jschuh | 711ac6a | 2016-12-04 07:17:48 | [diff] [blame] | 537 | static_assert(StrictNumeric<Src>(SrcLimits::lowest()) <= SrcLimits::max(), |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 538 | ""); |
jschuh | 711ac6a | 2016-12-04 07:17:48 | [diff] [blame] | 539 | static_assert(!(StrictNumeric<Src>(SrcLimits::lowest()) > DstLimits::max()), |
| 540 | ""); |
| 541 | static_assert(!(StrictNumeric<Src>(SrcLimits::lowest()) > SrcLimits::max()), |
| 542 | ""); |
| 543 | static_assert(StrictNumeric<Src>(SrcLimits::max()) > DstLimits::lowest(), ""); |
| 544 | static_assert(StrictNumeric<Src>(SrcLimits::max()) > SrcLimits::lowest(), ""); |
| 545 | static_assert(!(StrictNumeric<Src>(SrcLimits::max()) <= DstLimits::lowest()), |
| 546 | ""); |
| 547 | static_assert(!(StrictNumeric<Src>(SrcLimits::max()) <= SrcLimits::lowest()), |
| 548 | ""); |
| 549 | static_assert(StrictNumeric<Src>(SrcLimits::max()) >= DstLimits::lowest(), |
| 550 | ""); |
| 551 | static_assert(StrictNumeric<Src>(SrcLimits::max()) >= SrcLimits::lowest(), |
| 552 | ""); |
| 553 | static_assert(!(StrictNumeric<Src>(SrcLimits::max()) < DstLimits::lowest()), |
| 554 | ""); |
| 555 | static_assert(!(StrictNumeric<Src>(SrcLimits::max()) < SrcLimits::lowest()), |
| 556 | ""); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 557 | static_assert(StrictNumeric<Src>(static_cast<Src>(1)) == static_cast<Dst>(1), |
| 558 | ""); |
| 559 | static_assert(StrictNumeric<Src>(static_cast<Src>(1)) != static_cast<Dst>(0), |
| 560 | ""); |
| 561 | static_assert(StrictNumeric<Src>(SrcLimits::max()) != static_cast<Dst>(0), |
| 562 | ""); |
jschuh | 711ac6a | 2016-12-04 07:17:48 | [diff] [blame] | 563 | static_assert(StrictNumeric<Src>(SrcLimits::max()) != DstLimits::lowest(), |
| 564 | ""); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 565 | static_assert( |
| 566 | !(StrictNumeric<Src>(static_cast<Src>(1)) != static_cast<Dst>(1)), ""); |
| 567 | static_assert( |
| 568 | !(StrictNumeric<Src>(static_cast<Src>(1)) == static_cast<Dst>(0)), ""); |
jschuh | 711ac6a | 2016-12-04 07:17:48 | [diff] [blame] | 569 | |
| 570 | // Due to differences in float handling between compilers, these aren't |
| 571 | // compile-time constants everywhere. So, we use run-time tests. |
jschuh | 4bc919cb5 | 2016-12-04 15:08:27 | [diff] [blame] | 572 | EXPECT_EQ( |
| 573 | SrcLimits::max(), |
| 574 | MakeCheckedNum(SrcLimits::max()).Max(DstLimits::lowest()).ValueOrDie()); |
| 575 | EXPECT_EQ( |
| 576 | DstLimits::max(), |
| 577 | MakeCheckedNum(SrcLimits::lowest()).Max(DstLimits::max()).ValueOrDie()); |
| 578 | EXPECT_EQ( |
| 579 | DstLimits::lowest(), |
| 580 | MakeCheckedNum(SrcLimits::max()).Min(DstLimits::lowest()).ValueOrDie()); |
| 581 | EXPECT_EQ( |
| 582 | SrcLimits::lowest(), |
| 583 | MakeCheckedNum(SrcLimits::lowest()).Min(DstLimits::max()).ValueOrDie()); |
| 584 | EXPECT_EQ(SrcLimits::lowest(), CheckMin(MakeStrictNum(1), MakeCheckedNum(0), |
jschuh | 711ac6a | 2016-12-04 07:17:48 | [diff] [blame] | 585 | DstLimits::max(), SrcLimits::lowest()) |
| 586 | .ValueOrDie()); |
jschuh | 4bc919cb5 | 2016-12-04 15:08:27 | [diff] [blame] | 587 | EXPECT_EQ(DstLimits::max(), CheckMax(MakeStrictNum(1), MakeCheckedNum(0), |
jschuh | 711ac6a | 2016-12-04 07:17:48 | [diff] [blame] | 588 | DstLimits::max(), SrcLimits::lowest()) |
| 589 | .ValueOrDie()); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | template <typename Dst, typename Src> |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 593 | struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_VALUE_PRESERVING> { |
| 594 | static void Test(const char *dst, const char *src, int line) { |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 595 | using SrcLimits = numeric_limits<Src>; |
| 596 | using DstLimits = numeric_limits<Dst>; |
| 597 | // Integral to floating. |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 598 | static_assert((DstLimits::is_iec559 && SrcLimits::is_integer) || |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 599 | // Not floating to integral and... |
| 600 | (!(DstLimits::is_integer && SrcLimits::is_iec559) && |
| 601 | // Same sign, same numeric, source is narrower or same. |
| 602 | ((SrcLimits::is_signed == DstLimits::is_signed && |
| 603 | MaxExponent<Dst>::value >= MaxExponent<Src>::value) || |
| 604 | // Or signed destination and source is smaller |
| 605 | (DstLimits::is_signed && |
| 606 | MaxExponent<Dst>::value >= MaxExponent<Src>::value))), |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 607 | "Comparison must be sign preserving and value preserving"); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 608 | |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 609 | TestStrictComparison<Dst, Src>(); |
| 610 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 611 | const CheckedNumeric<Dst> checked_dst = SrcLimits::max(); |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 612 | TEST_EXPECTED_SUCCESS(checked_dst); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 613 | if (MaxExponent<Dst>::value > MaxExponent<Src>::value) { |
| 614 | if (MaxExponent<Dst>::value >= MaxExponent<Src>::value * 2 - 1) { |
| 615 | // At least twice larger type. |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 616 | TEST_EXPECTED_SUCCESS(SrcLimits::max() * checked_dst); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 617 | |
| 618 | } else { // Larger, but not at least twice as large. |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 619 | TEST_EXPECTED_FAILURE(SrcLimits::max() * checked_dst); |
| 620 | TEST_EXPECTED_SUCCESS(checked_dst + 1); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 621 | } |
| 622 | } else { // Same width type. |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 623 | TEST_EXPECTED_FAILURE(checked_dst + 1); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::max()); |
| 627 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1)); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 628 | if (SrcLimits::is_iec559) { |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 629 | TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::max() * static_cast<Src>(-1)); |
| 630 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::infinity()); |
| 631 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::infinity() * -1); |
| 632 | TEST_EXPECTED_RANGE(RANGE_INVALID, SrcLimits::quiet_NaN()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 633 | } else if (numeric_limits<Src>::is_signed) { |
| 634 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(-1)); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 635 | TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::lowest()); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 636 | } |
[email protected] | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 637 | } |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 638 | }; |
| 639 | |
| 640 | template <typename Dst, typename Src> |
| 641 | struct TestNumericConversion<Dst, Src, SIGN_PRESERVING_NARROW> { |
| 642 | static void Test(const char *dst, const char *src, int line) { |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 643 | using SrcLimits = numeric_limits<Src>; |
| 644 | using DstLimits = numeric_limits<Dst>; |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 645 | static_assert(SrcLimits::is_signed == DstLimits::is_signed, |
| 646 | "Destination and source sign must be the same"); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 647 | static_assert(MaxExponent<Dst>::value <= MaxExponent<Src>::value, |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 648 | "Destination must be narrower than source"); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 649 | |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 650 | TestStrictComparison<Dst, Src>(); |
| 651 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 652 | const CheckedNumeric<Dst> checked_dst; |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 653 | TEST_EXPECTED_FAILURE(checked_dst + SrcLimits::max()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 654 | TEST_EXPECTED_VALUE(1, checked_dst + static_cast<Src>(1)); |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 655 | TEST_EXPECTED_FAILURE(checked_dst - SrcLimits::max()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 656 | |
| 657 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::max()); |
| 658 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1)); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 659 | if (SrcLimits::is_iec559) { |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 660 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::max() * -1); |
| 661 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(-1)); |
| 662 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::infinity()); |
| 663 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::infinity() * -1); |
| 664 | TEST_EXPECTED_RANGE(RANGE_INVALID, SrcLimits::quiet_NaN()); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 665 | if (DstLimits::is_integer) { |
| 666 | if (SrcLimits::digits < DstLimits::digits) { |
| 667 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, |
| 668 | static_cast<Src>(DstLimits::max())); |
| 669 | } else { |
| 670 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(DstLimits::max())); |
| 671 | } |
| 672 | TEST_EXPECTED_RANGE( |
| 673 | RANGE_VALID, |
| 674 | static_cast<Src>(GetMaxConvertibleToFloat<Src, Dst>())); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 675 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(DstLimits::lowest())); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 676 | } |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 677 | } else if (SrcLimits::is_signed) { |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 678 | TEST_EXPECTED_VALUE(-1, checked_dst - static_cast<Src>(1)); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 679 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::lowest()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 680 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(-1)); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 681 | } else { |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 682 | TEST_EXPECTED_FAILURE(checked_dst - static_cast<Src>(1)); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 683 | TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::lowest()); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | }; |
| 687 | |
| 688 | template <typename Dst, typename Src> |
| 689 | struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL> { |
| 690 | static void Test(const char *dst, const char *src, int line) { |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 691 | using SrcLimits = numeric_limits<Src>; |
| 692 | using DstLimits = numeric_limits<Dst>; |
| 693 | static_assert(MaxExponent<Dst>::value >= MaxExponent<Src>::value, |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 694 | "Destination must be equal or wider than source."); |
| 695 | static_assert(SrcLimits::is_signed, "Source must be signed"); |
| 696 | static_assert(!DstLimits::is_signed, "Destination must be unsigned"); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 697 | |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 698 | TestStrictComparison<Dst, Src>(); |
| 699 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 700 | const CheckedNumeric<Dst> checked_dst; |
| 701 | TEST_EXPECTED_VALUE(SrcLimits::max(), checked_dst + SrcLimits::max()); |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 702 | TEST_EXPECTED_FAILURE(checked_dst + static_cast<Src>(-1)); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 703 | TEST_EXPECTED_FAILURE(checked_dst + SrcLimits::lowest()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 704 | |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 705 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::lowest()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 706 | TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::max()); |
| 707 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1)); |
| 708 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, static_cast<Src>(-1)); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 709 | } |
| 710 | }; |
| 711 | |
| 712 | template <typename Dst, typename Src> |
| 713 | struct TestNumericConversion<Dst, Src, SIGN_TO_UNSIGN_NARROW> { |
| 714 | static void Test(const char *dst, const char *src, int line) { |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 715 | using SrcLimits = numeric_limits<Src>; |
| 716 | using DstLimits = numeric_limits<Dst>; |
| 717 | static_assert(MaxExponent<Dst>::value < MaxExponent<Src>::value, |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 718 | "Destination must be narrower than source."); |
| 719 | static_assert(SrcLimits::is_signed, "Source must be signed."); |
| 720 | static_assert(!DstLimits::is_signed, "Destination must be unsigned."); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 721 | |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 722 | TestStrictComparison<Dst, Src>(); |
| 723 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 724 | const CheckedNumeric<Dst> checked_dst; |
| 725 | TEST_EXPECTED_VALUE(1, checked_dst + static_cast<Src>(1)); |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 726 | TEST_EXPECTED_FAILURE(checked_dst + SrcLimits::max()); |
| 727 | TEST_EXPECTED_FAILURE(checked_dst + static_cast<Src>(-1)); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 728 | TEST_EXPECTED_FAILURE(checked_dst + SrcLimits::lowest()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 729 | |
| 730 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::max()); |
| 731 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1)); |
| 732 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, static_cast<Src>(-1)); |
jschuh | e3bd1f6 | 2016-12-20 05:11:30 | [diff] [blame] | 733 | |
| 734 | // Additional saturation tests. |
jschuh | c8f03cd | 2017-01-05 03:40:51 | [diff] [blame^] | 735 | EXPECT_EQ(DstLimits::max(), saturated_cast<Dst>(SrcLimits::max())) << src; |
jschuh | e3bd1f6 | 2016-12-20 05:11:30 | [diff] [blame] | 736 | EXPECT_EQ(DstLimits::lowest(), saturated_cast<Dst>(SrcLimits::lowest())); |
| 737 | |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 738 | if (SrcLimits::is_iec559) { |
jschuh | e3bd1f6 | 2016-12-20 05:11:30 | [diff] [blame] | 739 | EXPECT_EQ(Dst(0), saturated_cast<Dst>(SrcLimits::quiet_NaN())); |
| 740 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 741 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::max() * -1); |
| 742 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::infinity()); |
| 743 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::infinity() * -1); |
| 744 | TEST_EXPECTED_RANGE(RANGE_INVALID, SrcLimits::quiet_NaN()); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 745 | if (DstLimits::is_integer) { |
| 746 | if (SrcLimits::digits < DstLimits::digits) { |
| 747 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, |
| 748 | static_cast<Src>(DstLimits::max())); |
| 749 | } else { |
| 750 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(DstLimits::max())); |
| 751 | } |
| 752 | TEST_EXPECTED_RANGE( |
| 753 | RANGE_VALID, |
| 754 | static_cast<Src>(GetMaxConvertibleToFloat<Src, Dst>())); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 755 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(DstLimits::lowest())); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 756 | } |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 757 | } else { |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 758 | TEST_EXPECTED_RANGE(RANGE_UNDERFLOW, SrcLimits::lowest()); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | }; |
| 762 | |
| 763 | template <typename Dst, typename Src> |
| 764 | struct TestNumericConversion<Dst, Src, UNSIGN_TO_SIGN_NARROW_OR_EQUAL> { |
| 765 | static void Test(const char *dst, const char *src, int line) { |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 766 | using SrcLimits = numeric_limits<Src>; |
| 767 | using DstLimits = numeric_limits<Dst>; |
| 768 | static_assert(MaxExponent<Dst>::value <= MaxExponent<Src>::value, |
jschuh | d2d9fe0 | 2014-10-14 14:31:37 | [diff] [blame] | 769 | "Destination must be narrower or equal to source."); |
| 770 | static_assert(!SrcLimits::is_signed, "Source must be unsigned."); |
| 771 | static_assert(DstLimits::is_signed, "Destination must be signed."); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 772 | |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 773 | TestStrictComparison<Dst, Src>(); |
| 774 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 775 | const CheckedNumeric<Dst> checked_dst; |
| 776 | TEST_EXPECTED_VALUE(1, checked_dst + static_cast<Src>(1)); |
jschuh | 819c826 | 2016-05-21 01:39:03 | [diff] [blame] | 777 | TEST_EXPECTED_FAILURE(checked_dst + SrcLimits::max()); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 778 | TEST_EXPECTED_VALUE(SrcLimits::lowest(), checked_dst + SrcLimits::lowest()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 779 | |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 780 | TEST_EXPECTED_RANGE(RANGE_VALID, SrcLimits::lowest()); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 781 | TEST_EXPECTED_RANGE(RANGE_OVERFLOW, SrcLimits::max()); |
| 782 | TEST_EXPECTED_RANGE(RANGE_VALID, static_cast<Src>(1)); |
jschuh | e3bd1f6 | 2016-12-20 05:11:30 | [diff] [blame] | 783 | |
| 784 | // Additional saturation tests. |
| 785 | EXPECT_EQ(DstLimits::max(), saturated_cast<Dst>(SrcLimits::max())); |
| 786 | EXPECT_EQ(Dst(0), saturated_cast<Dst>(SrcLimits::lowest())); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 787 | } |
| 788 | }; |
| 789 | |
| 790 | // Helper macro to wrap displaying the conversion types and line numbers |
| 791 | #define TEST_NUMERIC_CONVERSION(d, s, t) \ |
| 792 | TestNumericConversion<d, s, t>::Test(#d, #s, __LINE__) |
| 793 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 794 | TEST(SafeNumerics, IntMinOperations) { |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 795 | TEST_NUMERIC_CONVERSION(int8_t, int8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 796 | TEST_NUMERIC_CONVERSION(uint8_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 797 | |
| 798 | TEST_NUMERIC_CONVERSION(int8_t, int, SIGN_PRESERVING_NARROW); |
| 799 | TEST_NUMERIC_CONVERSION(uint8_t, unsigned int, SIGN_PRESERVING_NARROW); |
| 800 | TEST_NUMERIC_CONVERSION(int8_t, float, SIGN_PRESERVING_NARROW); |
| 801 | |
| 802 | TEST_NUMERIC_CONVERSION(uint8_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
| 803 | |
| 804 | TEST_NUMERIC_CONVERSION(uint8_t, int, SIGN_TO_UNSIGN_NARROW); |
| 805 | TEST_NUMERIC_CONVERSION(uint8_t, intmax_t, SIGN_TO_UNSIGN_NARROW); |
| 806 | TEST_NUMERIC_CONVERSION(uint8_t, float, SIGN_TO_UNSIGN_NARROW); |
| 807 | |
| 808 | TEST_NUMERIC_CONVERSION(int8_t, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
| 809 | TEST_NUMERIC_CONVERSION(int8_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
[email protected] | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 810 | } |
| 811 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 812 | TEST(SafeNumerics, IntOperations) { |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 813 | TEST_NUMERIC_CONVERSION(int, int, SIGN_PRESERVING_VALUE_PRESERVING); |
| 814 | TEST_NUMERIC_CONVERSION(unsigned int, unsigned int, |
| 815 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 816 | TEST_NUMERIC_CONVERSION(int, int8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 817 | TEST_NUMERIC_CONVERSION(unsigned int, uint8_t, |
| 818 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 819 | TEST_NUMERIC_CONVERSION(int, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 820 | |
| 821 | TEST_NUMERIC_CONVERSION(int, intmax_t, SIGN_PRESERVING_NARROW); |
| 822 | TEST_NUMERIC_CONVERSION(unsigned int, uintmax_t, SIGN_PRESERVING_NARROW); |
| 823 | TEST_NUMERIC_CONVERSION(int, float, SIGN_PRESERVING_NARROW); |
| 824 | TEST_NUMERIC_CONVERSION(int, double, SIGN_PRESERVING_NARROW); |
| 825 | |
| 826 | TEST_NUMERIC_CONVERSION(unsigned int, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
| 827 | TEST_NUMERIC_CONVERSION(unsigned int, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
| 828 | |
| 829 | TEST_NUMERIC_CONVERSION(unsigned int, intmax_t, SIGN_TO_UNSIGN_NARROW); |
| 830 | TEST_NUMERIC_CONVERSION(unsigned int, float, SIGN_TO_UNSIGN_NARROW); |
| 831 | TEST_NUMERIC_CONVERSION(unsigned int, double, SIGN_TO_UNSIGN_NARROW); |
| 832 | |
| 833 | TEST_NUMERIC_CONVERSION(int, unsigned int, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
| 834 | TEST_NUMERIC_CONVERSION(int, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
[email protected] | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 835 | } |
| 836 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 837 | TEST(SafeNumerics, IntMaxOperations) { |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 838 | TEST_NUMERIC_CONVERSION(intmax_t, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 839 | TEST_NUMERIC_CONVERSION(uintmax_t, uintmax_t, |
| 840 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 841 | TEST_NUMERIC_CONVERSION(intmax_t, int, SIGN_PRESERVING_VALUE_PRESERVING); |
| 842 | TEST_NUMERIC_CONVERSION(uintmax_t, unsigned int, |
| 843 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 844 | TEST_NUMERIC_CONVERSION(intmax_t, unsigned int, |
| 845 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 846 | TEST_NUMERIC_CONVERSION(intmax_t, uint8_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 847 | |
| 848 | TEST_NUMERIC_CONVERSION(intmax_t, float, SIGN_PRESERVING_NARROW); |
| 849 | TEST_NUMERIC_CONVERSION(intmax_t, double, SIGN_PRESERVING_NARROW); |
| 850 | |
| 851 | TEST_NUMERIC_CONVERSION(uintmax_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
| 852 | TEST_NUMERIC_CONVERSION(uintmax_t, int8_t, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
| 853 | |
| 854 | TEST_NUMERIC_CONVERSION(uintmax_t, float, SIGN_TO_UNSIGN_NARROW); |
| 855 | TEST_NUMERIC_CONVERSION(uintmax_t, double, SIGN_TO_UNSIGN_NARROW); |
| 856 | |
| 857 | TEST_NUMERIC_CONVERSION(intmax_t, uintmax_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
| 858 | } |
| 859 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 860 | TEST(SafeNumerics, FloatOperations) { |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 861 | TEST_NUMERIC_CONVERSION(float, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 862 | TEST_NUMERIC_CONVERSION(float, uintmax_t, |
| 863 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 864 | TEST_NUMERIC_CONVERSION(float, int, SIGN_PRESERVING_VALUE_PRESERVING); |
| 865 | TEST_NUMERIC_CONVERSION(float, unsigned int, |
| 866 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 867 | |
| 868 | TEST_NUMERIC_CONVERSION(float, double, SIGN_PRESERVING_NARROW); |
| 869 | } |
| 870 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 871 | TEST(SafeNumerics, DoubleOperations) { |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 872 | TEST_NUMERIC_CONVERSION(double, intmax_t, SIGN_PRESERVING_VALUE_PRESERVING); |
| 873 | TEST_NUMERIC_CONVERSION(double, uintmax_t, |
| 874 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 875 | TEST_NUMERIC_CONVERSION(double, int, SIGN_PRESERVING_VALUE_PRESERVING); |
| 876 | TEST_NUMERIC_CONVERSION(double, unsigned int, |
| 877 | SIGN_PRESERVING_VALUE_PRESERVING); |
| 878 | } |
| 879 | |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 880 | TEST(SafeNumerics, SizeTOperations) { |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 881 | TEST_NUMERIC_CONVERSION(size_t, int, SIGN_TO_UNSIGN_WIDEN_OR_EQUAL); |
| 882 | TEST_NUMERIC_CONVERSION(int, size_t, UNSIGN_TO_SIGN_NARROW_OR_EQUAL); |
| 883 | } |
| 884 | |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 885 | // A one-off test to ensure StrictNumeric won't resolve to an incorrect type. |
| 886 | // If this fails we'll just get a compiler error on an ambiguous overload. |
| 887 | int TestOverload(int) { // Overload fails. |
| 888 | return 0; |
| 889 | } |
| 890 | uint8_t TestOverload(uint8_t) { // Overload fails. |
| 891 | return 0; |
| 892 | } |
| 893 | size_t TestOverload(size_t) { // Overload succeeds. |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | static_assert( |
| 898 | std::is_same<decltype(TestOverload(StrictNumeric<int>())), int>::value, |
| 899 | ""); |
| 900 | static_assert(std::is_same<decltype(TestOverload(StrictNumeric<size_t>())), |
| 901 | size_t>::value, |
| 902 | ""); |
| 903 | |
jschuh | 71b669a | 2016-12-17 01:13:31 | [diff] [blame] | 904 | template <typename T> |
| 905 | struct CastTest1 { |
jschuh | c8f03cd | 2017-01-05 03:40:51 | [diff] [blame^] | 906 | static constexpr T NaN() { return -1; } |
jschuh | 71b669a | 2016-12-17 01:13:31 | [diff] [blame] | 907 | static constexpr T max() { return numeric_limits<T>::max() - 1; } |
jschuh | c8f03cd | 2017-01-05 03:40:51 | [diff] [blame^] | 908 | static constexpr T Overflow() { return max(); } |
jschuh | 71b669a | 2016-12-17 01:13:31 | [diff] [blame] | 909 | static constexpr T lowest() { return numeric_limits<T>::lowest() + 1; } |
jschuh | c8f03cd | 2017-01-05 03:40:51 | [diff] [blame^] | 910 | static constexpr T Underflow() { return lowest(); } |
jschuh | 71b669a | 2016-12-17 01:13:31 | [diff] [blame] | 911 | }; |
| 912 | |
| 913 | template <typename T> |
| 914 | struct CastTest2 { |
jschuh | c8f03cd | 2017-01-05 03:40:51 | [diff] [blame^] | 915 | static constexpr T NaN() { return 11; } |
jschuh | 71b669a | 2016-12-17 01:13:31 | [diff] [blame] | 916 | static constexpr T max() { return 10; } |
jschuh | c8f03cd | 2017-01-05 03:40:51 | [diff] [blame^] | 917 | static constexpr T Overflow() { return max(); } |
jschuh | 71b669a | 2016-12-17 01:13:31 | [diff] [blame] | 918 | static constexpr T lowest() { return 1; } |
jschuh | c8f03cd | 2017-01-05 03:40:51 | [diff] [blame^] | 919 | static constexpr T Underflow() { return lowest(); } |
jschuh | 71b669a | 2016-12-17 01:13:31 | [diff] [blame] | 920 | }; |
| 921 | |
agrieve | 7772a050 | 2016-12-16 18:18:48 | [diff] [blame] | 922 | TEST(SafeNumerics, CastTests) { |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 923 | // MSVC catches and warns that we're forcing saturation in these tests. |
| 924 | // Since that's intentional, we need to shut this warning off. |
| 925 | #if defined(COMPILER_MSVC) |
| 926 | #pragma warning(disable : 4756) |
[email protected] | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 927 | #endif |
| 928 | |
[email protected] | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 929 | int small_positive = 1; |
| 930 | int small_negative = -1; |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 931 | double double_small = 1.0; |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 932 | double double_large = numeric_limits<double>::max(); |
| 933 | double double_infinity = numeric_limits<float>::infinity(); |
danakj | 3193742f | 2015-06-05 18:15:10 | [diff] [blame] | 934 | double double_large_int = numeric_limits<int>::max(); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 935 | double double_small_int = numeric_limits<int>::lowest(); |
[email protected] | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 936 | |
jschuh | 4bf22c6d | 2015-05-28 02:29:25 | [diff] [blame] | 937 | // Just test that the casts compile, since the other tests cover logic. |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 938 | EXPECT_EQ(0, checked_cast<int>(static_cast<size_t>(0))); |
jschuh | 4bf22c6d | 2015-05-28 02:29:25 | [diff] [blame] | 939 | EXPECT_EQ(0, strict_cast<int>(static_cast<char>(0))); |
| 940 | EXPECT_EQ(0, strict_cast<int>(static_cast<unsigned char>(0))); |
| 941 | EXPECT_EQ(0U, strict_cast<unsigned>(static_cast<unsigned char>(0))); |
| 942 | EXPECT_EQ(1ULL, static_cast<uint64_t>(StrictNumeric<size_t>(1U))); |
| 943 | EXPECT_EQ(1ULL, static_cast<uint64_t>(SizeT(1U))); |
| 944 | EXPECT_EQ(1U, static_cast<size_t>(StrictNumeric<unsigned>(1U))); |
| 945 | |
| 946 | EXPECT_TRUE(CheckedNumeric<uint64_t>(StrictNumeric<unsigned>(1U)).IsValid()); |
| 947 | EXPECT_TRUE(CheckedNumeric<int>(StrictNumeric<unsigned>(1U)).IsValid()); |
| 948 | EXPECT_FALSE(CheckedNumeric<unsigned>(StrictNumeric<int>(-1)).IsValid()); |
| 949 | |
jschuh | 07345e6 | 2015-09-22 22:13:36 | [diff] [blame] | 950 | EXPECT_TRUE(IsValueNegative(-1)); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 951 | EXPECT_TRUE(IsValueNegative(numeric_limits<int>::lowest())); |
| 952 | EXPECT_FALSE(IsValueNegative(numeric_limits<unsigned>::lowest())); |
| 953 | EXPECT_TRUE(IsValueNegative(numeric_limits<double>::lowest())); |
jschuh | 07345e6 | 2015-09-22 22:13:36 | [diff] [blame] | 954 | EXPECT_FALSE(IsValueNegative(0)); |
| 955 | EXPECT_FALSE(IsValueNegative(1)); |
| 956 | EXPECT_FALSE(IsValueNegative(0u)); |
| 957 | EXPECT_FALSE(IsValueNegative(1u)); |
| 958 | EXPECT_FALSE(IsValueNegative(numeric_limits<int>::max())); |
| 959 | EXPECT_FALSE(IsValueNegative(numeric_limits<unsigned>::max())); |
| 960 | EXPECT_FALSE(IsValueNegative(numeric_limits<double>::max())); |
| 961 | |
jschuh | 4bf22c6d | 2015-05-28 02:29:25 | [diff] [blame] | 962 | // These casts and coercions will fail to compile: |
| 963 | // EXPECT_EQ(0, strict_cast<int>(static_cast<size_t>(0))); |
| 964 | // EXPECT_EQ(0, strict_cast<size_t>(static_cast<int>(0))); |
| 965 | // EXPECT_EQ(1ULL, StrictNumeric<size_t>(1)); |
| 966 | // EXPECT_EQ(1, StrictNumeric<size_t>(1U)); |
[email protected] | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 967 | |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 968 | // Test various saturation corner cases. |
| 969 | EXPECT_EQ(saturated_cast<int>(small_negative), |
| 970 | static_cast<int>(small_negative)); |
| 971 | EXPECT_EQ(saturated_cast<int>(small_positive), |
| 972 | static_cast<int>(small_positive)); |
| 973 | EXPECT_EQ(saturated_cast<unsigned>(small_negative), |
| 974 | static_cast<unsigned>(0)); |
| 975 | EXPECT_EQ(saturated_cast<int>(double_small), |
| 976 | static_cast<int>(double_small)); |
[email protected] | 5bfecbc | 2014-02-27 13:49:04 | [diff] [blame] | 977 | EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); |
[email protected] | 4efb2c3 | 2014-01-16 06:57:25 | [diff] [blame] | 978 | EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); |
| 979 | EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 980 | EXPECT_EQ(numeric_limits<int>::lowest(), |
| 981 | saturated_cast<int>(double_small_int)); |
danakj | 3193742f | 2015-06-05 18:15:10 | [diff] [blame] | 982 | EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int)); |
vmpstr | edf1e18 | 2015-12-14 20:09:42 | [diff] [blame] | 983 | |
jschuh | 71b669a | 2016-12-17 01:13:31 | [diff] [blame] | 984 | // Test the saturated cast overrides. |
| 985 | using FloatLimits = numeric_limits<float>; |
| 986 | using IntLimits = numeric_limits<int>; |
| 987 | EXPECT_EQ(-1, (saturated_cast<int, CastTest1>(FloatLimits::quiet_NaN()))); |
| 988 | EXPECT_EQ(CastTest1<int>::max(), |
| 989 | (saturated_cast<int, CastTest1>(FloatLimits::infinity()))); |
| 990 | EXPECT_EQ(CastTest1<int>::max(), |
| 991 | (saturated_cast<int, CastTest1>(FloatLimits::max()))); |
| 992 | EXPECT_EQ(CastTest1<int>::max(), |
| 993 | (saturated_cast<int, CastTest1>(float(IntLimits::max())))); |
| 994 | EXPECT_EQ(CastTest1<int>::lowest(), |
| 995 | (saturated_cast<int, CastTest1>(-FloatLimits::infinity()))); |
| 996 | EXPECT_EQ(CastTest1<int>::lowest(), |
| 997 | (saturated_cast<int, CastTest1>(FloatLimits::lowest()))); |
| 998 | EXPECT_EQ(0, (saturated_cast<int, CastTest1>(0.0))); |
| 999 | EXPECT_EQ(1, (saturated_cast<int, CastTest1>(1.0))); |
| 1000 | EXPECT_EQ(-1, (saturated_cast<int, CastTest1>(-1.0))); |
| 1001 | EXPECT_EQ(0, (saturated_cast<int, CastTest1>(0))); |
| 1002 | EXPECT_EQ(1, (saturated_cast<int, CastTest1>(1))); |
| 1003 | EXPECT_EQ(-1, (saturated_cast<int, CastTest1>(-1))); |
| 1004 | EXPECT_EQ(CastTest1<int>::lowest(), |
| 1005 | (saturated_cast<int, CastTest1>(float(IntLimits::lowest())))); |
| 1006 | EXPECT_EQ(11, (saturated_cast<int, CastTest2>(FloatLimits::quiet_NaN()))); |
| 1007 | EXPECT_EQ(10, (saturated_cast<int, CastTest2>(FloatLimits::infinity()))); |
| 1008 | EXPECT_EQ(10, (saturated_cast<int, CastTest2>(FloatLimits::max()))); |
| 1009 | EXPECT_EQ(1, (saturated_cast<int, CastTest2>(-FloatLimits::infinity()))); |
| 1010 | EXPECT_EQ(1, (saturated_cast<int, CastTest2>(FloatLimits::lowest()))); |
| 1011 | EXPECT_EQ(1, (saturated_cast<int, CastTest2>(0U))); |
| 1012 | |
vmpstr | edf1e18 | 2015-12-14 20:09:42 | [diff] [blame] | 1013 | float not_a_number = std::numeric_limits<float>::infinity() - |
| 1014 | std::numeric_limits<float>::infinity(); |
| 1015 | EXPECT_TRUE(std::isnan(not_a_number)); |
| 1016 | EXPECT_EQ(0, saturated_cast<int>(not_a_number)); |
jschuh | 224f1d7 | 2016-11-25 20:08:48 | [diff] [blame] | 1017 | |
| 1018 | // Test the CheckedNumeric value extractions functions. |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1019 | auto int8_min = MakeCheckedNum(numeric_limits<int8_t>::lowest()); |
jschuh | 4bc919cb5 | 2016-12-04 15:08:27 | [diff] [blame] | 1020 | auto int8_max = MakeCheckedNum(numeric_limits<int8_t>::max()); |
| 1021 | auto double_max = MakeCheckedNum(numeric_limits<double>::max()); |
jschuh | 224f1d7 | 2016-11-25 20:08:48 | [diff] [blame] | 1022 | static_assert( |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1023 | std::is_same<int16_t, |
| 1024 | decltype(int8_min.ValueOrDie<int16_t>())::type>::value, |
jschuh | 224f1d7 | 2016-11-25 20:08:48 | [diff] [blame] | 1025 | "ValueOrDie returning incorrect type."); |
| 1026 | static_assert( |
| 1027 | std::is_same<int16_t, |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1028 | decltype(int8_min.ValueOrDefault<int16_t>(0))::type>::value, |
jschuh | 224f1d7 | 2016-11-25 20:08:48 | [diff] [blame] | 1029 | "ValueOrDefault returning incorrect type."); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1030 | EXPECT_FALSE(IsValidForType<uint8_t>(int8_min)); |
| 1031 | EXPECT_TRUE(IsValidForType<uint8_t>(int8_max)); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1032 | EXPECT_EQ(static_cast<int>(numeric_limits<int8_t>::lowest()), |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1033 | ValueOrDieForType<int>(int8_min)); |
| 1034 | EXPECT_TRUE(IsValidForType<uint32_t>(int8_max)); |
jschuh | 224f1d7 | 2016-11-25 20:08:48 | [diff] [blame] | 1035 | EXPECT_EQ(static_cast<int>(numeric_limits<int8_t>::max()), |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1036 | ValueOrDieForType<int>(int8_max)); |
| 1037 | EXPECT_EQ(0, ValueOrDefaultForType<int>(double_max, 0)); |
jschuh | e17e0f1 | 2016-11-26 03:00:08 | [diff] [blame] | 1038 | uint8_t uint8_dest = 0; |
| 1039 | int16_t int16_dest = 0; |
| 1040 | double double_dest = 0; |
| 1041 | EXPECT_TRUE(int8_max.AssignIfValid(&uint8_dest)); |
| 1042 | EXPECT_EQ(static_cast<uint8_t>(numeric_limits<int8_t>::max()), uint8_dest); |
| 1043 | EXPECT_FALSE(int8_min.AssignIfValid(&uint8_dest)); |
| 1044 | EXPECT_TRUE(int8_max.AssignIfValid(&int16_dest)); |
| 1045 | EXPECT_EQ(static_cast<int16_t>(numeric_limits<int8_t>::max()), int16_dest); |
| 1046 | EXPECT_TRUE(int8_min.AssignIfValid(&int16_dest)); |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1047 | EXPECT_EQ(static_cast<int16_t>(numeric_limits<int8_t>::lowest()), int16_dest); |
jschuh | e17e0f1 | 2016-11-26 03:00:08 | [diff] [blame] | 1048 | EXPECT_FALSE(double_max.AssignIfValid(&uint8_dest)); |
| 1049 | EXPECT_FALSE(double_max.AssignIfValid(&int16_dest)); |
| 1050 | EXPECT_TRUE(double_max.AssignIfValid(&double_dest)); |
| 1051 | EXPECT_EQ(numeric_limits<double>::max(), double_dest); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1052 | EXPECT_EQ(1, checked_cast<int>(StrictNumeric<int>(1))); |
| 1053 | EXPECT_EQ(1, saturated_cast<int>(StrictNumeric<int>(1))); |
| 1054 | EXPECT_EQ(1, strict_cast<int>(StrictNumeric<int>(1))); |
[email protected] | c1c090d3 | 2013-01-16 23:34:04 | [diff] [blame] | 1055 | } |
| 1056 | |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 1057 | TEST(SafeNumerics, IsValueInRangeForNumericType) { |
| 1058 | EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0)); |
| 1059 | EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(1)); |
| 1060 | EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(2)); |
| 1061 | EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(-1)); |
| 1062 | EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0xffffffffu)); |
| 1063 | EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0xffffffff))); |
| 1064 | EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000000))); |
| 1065 | EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000001))); |
| 1066 | EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>( |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1067 | std::numeric_limits<int32_t>::lowest())); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 1068 | EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>( |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1069 | std::numeric_limits<int64_t>::lowest())); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 1070 | |
| 1071 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>(0)); |
| 1072 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>(1)); |
| 1073 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>(2)); |
| 1074 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>(-1)); |
| 1075 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>(0x7fffffff)); |
| 1076 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>(0x7fffffffu)); |
| 1077 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>(0x80000000u)); |
| 1078 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>(0xffffffffu)); |
| 1079 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>(INT64_C(0x80000000))); |
| 1080 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>(INT64_C(0xffffffff))); |
| 1081 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>(INT64_C(0x100000000))); |
| 1082 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>( |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1083 | std::numeric_limits<int32_t>::lowest())); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 1084 | EXPECT_TRUE(IsValueInRangeForNumericType<int32_t>( |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1085 | static_cast<int64_t>(std::numeric_limits<int32_t>::lowest()))); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 1086 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>( |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1087 | static_cast<int64_t>(std::numeric_limits<int32_t>::lowest()) - 1)); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 1088 | EXPECT_FALSE(IsValueInRangeForNumericType<int32_t>( |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1089 | std::numeric_limits<int64_t>::lowest())); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 1090 | |
| 1091 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(0)); |
| 1092 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(1)); |
| 1093 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(2)); |
| 1094 | EXPECT_FALSE(IsValueInRangeForNumericType<uint64_t>(-1)); |
| 1095 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(0xffffffffu)); |
| 1096 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(UINT64_C(0xffffffff))); |
| 1097 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(UINT64_C(0x100000000))); |
| 1098 | EXPECT_TRUE(IsValueInRangeForNumericType<uint64_t>(UINT64_C(0x100000001))); |
| 1099 | EXPECT_FALSE(IsValueInRangeForNumericType<uint64_t>( |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1100 | std::numeric_limits<int32_t>::lowest())); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 1101 | EXPECT_FALSE(IsValueInRangeForNumericType<uint64_t>(INT64_C(-1))); |
| 1102 | EXPECT_FALSE(IsValueInRangeForNumericType<uint64_t>( |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1103 | std::numeric_limits<int64_t>::lowest())); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 1104 | |
| 1105 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(0)); |
| 1106 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(1)); |
| 1107 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(2)); |
| 1108 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(-1)); |
| 1109 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(0x7fffffff)); |
| 1110 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(0x7fffffffu)); |
| 1111 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(0x80000000u)); |
| 1112 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(0xffffffffu)); |
| 1113 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(INT64_C(0x80000000))); |
| 1114 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(INT64_C(0xffffffff))); |
| 1115 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>(INT64_C(0x100000000))); |
| 1116 | EXPECT_TRUE( |
| 1117 | IsValueInRangeForNumericType<int64_t>(INT64_C(0x7fffffffffffffff))); |
| 1118 | EXPECT_TRUE( |
| 1119 | IsValueInRangeForNumericType<int64_t>(UINT64_C(0x7fffffffffffffff))); |
| 1120 | EXPECT_FALSE( |
| 1121 | IsValueInRangeForNumericType<int64_t>(UINT64_C(0x8000000000000000))); |
| 1122 | EXPECT_FALSE( |
| 1123 | IsValueInRangeForNumericType<int64_t>(UINT64_C(0xffffffffffffffff))); |
| 1124 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>( |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1125 | std::numeric_limits<int32_t>::lowest())); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 1126 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>( |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1127 | static_cast<int64_t>(std::numeric_limits<int32_t>::lowest()))); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 1128 | EXPECT_TRUE(IsValueInRangeForNumericType<int64_t>( |
jschuh | 5030b00 | 2016-12-05 18:21:48 | [diff] [blame] | 1129 | std::numeric_limits<int64_t>::lowest())); |
jschuh | fafe071 | 2015-09-14 20:21:24 | [diff] [blame] | 1130 | } |
vmpstr | 1947749 | 2015-09-29 22:34:49 | [diff] [blame] | 1131 | |
| 1132 | TEST(SafeNumerics, CompoundNumericOperations) { |
| 1133 | CheckedNumeric<int> a = 1; |
| 1134 | CheckedNumeric<int> b = 2; |
| 1135 | CheckedNumeric<int> c = 3; |
| 1136 | CheckedNumeric<int> d = 4; |
| 1137 | a += b; |
| 1138 | EXPECT_EQ(3, a.ValueOrDie()); |
| 1139 | a -= c; |
| 1140 | EXPECT_EQ(0, a.ValueOrDie()); |
| 1141 | d /= b; |
| 1142 | EXPECT_EQ(2, d.ValueOrDie()); |
| 1143 | d *= d; |
| 1144 | EXPECT_EQ(4, d.ValueOrDie()); |
| 1145 | |
| 1146 | CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); |
| 1147 | EXPECT_TRUE(too_large.IsValid()); |
| 1148 | too_large += d; |
| 1149 | EXPECT_FALSE(too_large.IsValid()); |
| 1150 | too_large -= d; |
| 1151 | EXPECT_FALSE(too_large.IsValid()); |
| 1152 | too_large /= d; |
| 1153 | EXPECT_FALSE(too_large.IsValid()); |
| 1154 | } |
jschuh | 4fcd6fa | 2016-11-24 11:58:39 | [diff] [blame] | 1155 | |
| 1156 | TEST(SafeNumerics, VariadicNumericOperations) { |
jschuh | 4bc919cb5 | 2016-12-04 15:08:27 | [diff] [blame] | 1157 | auto a = CheckAdd(1, 2UL, MakeCheckedNum(3LL), 4).ValueOrDie(); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1158 | EXPECT_EQ(static_cast<decltype(a)::type>(10), a); |
jschuh | 4bc919cb5 | 2016-12-04 15:08:27 | [diff] [blame] | 1159 | auto b = CheckSub(MakeCheckedNum(20.0), 2UL, 4).ValueOrDie(); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1160 | EXPECT_EQ(static_cast<decltype(b)::type>(14.0), b); |
jschuh | 4bc919cb5 | 2016-12-04 15:08:27 | [diff] [blame] | 1161 | auto c = CheckMul(20.0, MakeCheckedNum(1), 5, 3UL).ValueOrDie(); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1162 | EXPECT_EQ(static_cast<decltype(c)::type>(300.0), c); |
jschuh | 4bc919cb5 | 2016-12-04 15:08:27 | [diff] [blame] | 1163 | auto d = CheckDiv(20.0, 2.0, MakeCheckedNum(5LL), -4).ValueOrDie(); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1164 | EXPECT_EQ(static_cast<decltype(d)::type>(-.5), d); |
jschuh | 4bc919cb5 | 2016-12-04 15:08:27 | [diff] [blame] | 1165 | auto e = CheckMod(MakeCheckedNum(20), 3).ValueOrDie(); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1166 | EXPECT_EQ(static_cast<decltype(e)::type>(2), e); |
jschuh | 4bc919cb5 | 2016-12-04 15:08:27 | [diff] [blame] | 1167 | auto f = CheckLsh(1, MakeCheckedNum(2)).ValueOrDie(); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1168 | EXPECT_EQ(static_cast<decltype(f)::type>(4), f); |
jschuh | 4bc919cb5 | 2016-12-04 15:08:27 | [diff] [blame] | 1169 | auto g = CheckRsh(4, MakeCheckedNum(2)).ValueOrDie(); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1170 | EXPECT_EQ(static_cast<decltype(g)::type>(1), g); |
jschuh | 4fcd6fa | 2016-11-24 11:58:39 | [diff] [blame] | 1171 | auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie(); |
jschuh | 23a4b06 | 2016-12-02 02:55:08 | [diff] [blame] | 1172 | EXPECT_EQ(static_cast<decltype(h)::type>(1), h); |
jschuh | 4fcd6fa | 2016-11-24 11:58:39 | [diff] [blame] | 1173 | } |