| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <type_traits> | 9 #include <type_traits> |
| 10 | 10 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 if (SrcLimits::digits <= DstLimits::digits && | 55 if (SrcLimits::digits <= DstLimits::digits && |
| 56 MaxExponent<Src>::value <= MaxExponent<Dst>::value) | 56 MaxExponent<Src>::value <= MaxExponent<Dst>::value) |
| 57 return SrcLimits::max(); | 57 return SrcLimits::max(); |
| 58 Src max = SrcLimits::max() / 2 + (SrcLimits::is_integer ? 1 : 0); | 58 Src max = SrcLimits::max() / 2 + (SrcLimits::is_integer ? 1 : 0); |
| 59 while (max != static_cast<Src>(static_cast<Dst>(max))) { | 59 while (max != static_cast<Src>(static_cast<Dst>(max))) { |
| 60 max /= 2; | 60 max /= 2; |
| 61 } | 61 } |
| 62 return static_cast<Dst>(max); | 62 return static_cast<Dst>(max); |
| 63 } | 63 } |
| 64 | 64 |
| 65 namespace base { |
| 66 namespace internal { |
| 67 template <typename U> |
| 68 U GetNumericValueForTest(const CheckedNumeric<U>& src) { |
| 69 return src.state_.value(); |
| 70 } |
| 71 } // namespace internal. |
| 72 } // namespace base. |
| 73 |
| 74 using base::internal::GetNumericValueForTest; |
| 75 |
| 65 // Helper macros to wrap displaying the conversion types and line numbers. | 76 // Helper macros to wrap displaying the conversion types and line numbers. |
| 66 #define TEST_EXPECTED_VALIDITY(expected, actual) \ | 77 #define TEST_EXPECTED_VALIDITY(expected, actual) \ |
| 67 EXPECT_EQ(expected, CheckedNumeric<Dst>(actual).IsValid()) \ | 78 EXPECT_EQ(expected, CheckedNumeric<Dst>(actual).IsValid()) \ |
| 68 << "Result test: Value " << +(actual).ValueUnsafe() << " as " << dst \ | 79 << "Result test: Value " << GetNumericValueForTest(actual) << " as " \ |
| 69 << " on line " << line | 80 << dst << " on line " << line |
| 70 | 81 |
| 71 #define TEST_EXPECTED_SUCCESS(actual) TEST_EXPECTED_VALIDITY(true, actual) | 82 #define TEST_EXPECTED_SUCCESS(actual) TEST_EXPECTED_VALIDITY(true, actual) |
| 72 #define TEST_EXPECTED_FAILURE(actual) TEST_EXPECTED_VALIDITY(false, actual) | 83 #define TEST_EXPECTED_FAILURE(actual) TEST_EXPECTED_VALIDITY(false, actual) |
| 73 | 84 |
| 74 #define TEST_EXPECTED_VALUE(expected, actual) \ | 85 #define TEST_EXPECTED_VALUE(expected, actual) \ |
| 75 EXPECT_EQ(static_cast<Dst>(expected), \ | 86 EXPECT_EQ(static_cast<Dst>(expected), \ |
| 76 CheckedNumeric<Dst>(actual).ValueOrDie()) \ | 87 CheckedNumeric<Dst>(actual).ValueOrDie()) \ |
| 77 << "Result test: Value " << +((actual).ValueUnsafe()) << " as " << dst \ | 88 << "Result test: Value " << GetNumericValueForTest(actual) << " as " \ |
| 78 << " on line " << line | 89 << dst << " on line " << line |
| 79 | 90 |
| 80 // Signed integer arithmetic. | 91 // Signed integer arithmetic. |
| 81 template <typename Dst> | 92 template <typename Dst> |
| 82 static void TestSpecializedArithmetic( | 93 static void TestSpecializedArithmetic( |
| 83 const char* dst, | 94 const char* dst, |
| 84 int line, | 95 int line, |
| 85 typename std::enable_if<numeric_limits<Dst>::is_integer && | 96 typename std::enable_if<numeric_limits<Dst>::is_integer && |
| 86 numeric_limits<Dst>::is_signed, | 97 numeric_limits<Dst>::is_signed, |
| 87 int>::type = 0) { | 98 int>::type = 0) { |
| 88 typedef numeric_limits<Dst> DstLimits; | 99 typedef numeric_limits<Dst> DstLimits; |
| (...skipping 711 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 800 | 811 |
| 801 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); | 812 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); |
| 802 EXPECT_TRUE(too_large.IsValid()); | 813 EXPECT_TRUE(too_large.IsValid()); |
| 803 too_large += d; | 814 too_large += d; |
| 804 EXPECT_FALSE(too_large.IsValid()); | 815 EXPECT_FALSE(too_large.IsValid()); |
| 805 too_large -= d; | 816 too_large -= d; |
| 806 EXPECT_FALSE(too_large.IsValid()); | 817 EXPECT_FALSE(too_large.IsValid()); |
| 807 too_large /= d; | 818 too_large /= d; |
| 808 EXPECT_FALSE(too_large.IsValid()); | 819 EXPECT_FALSE(too_large.IsValid()); |
| 809 } | 820 } |
| OLD | NEW |