| 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 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) | 5 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
| 6 #include <mmintrin.h> | 6 #include <mmintrin.h> |
| 7 #endif | 7 #endif |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| 11 #include <type_traits> |
| 11 | 12 |
| 12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 13 #include "base/numerics/safe_conversions.h" | 14 #include "base/numerics/safe_conversions.h" |
| 14 #include "base/numerics/safe_math.h" | 15 #include "base/numerics/safe_math.h" |
| 15 #include "base/template_util.h" | 16 #include "base/template_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 using std::numeric_limits; | 19 using std::numeric_limits; |
| 19 using base::CheckedNumeric; | 20 using base::CheckedNumeric; |
| 20 using base::checked_cast; | 21 using base::checked_cast; |
| 21 using base::IsValueInRangeForNumericType; | 22 using base::IsValueInRangeForNumericType; |
| 22 using base::IsValueNegative; | 23 using base::IsValueNegative; |
| 23 using base::SizeT; | 24 using base::SizeT; |
| 24 using base::StrictNumeric; | 25 using base::StrictNumeric; |
| 25 using base::saturated_cast; | 26 using base::saturated_cast; |
| 26 using base::strict_cast; | 27 using base::strict_cast; |
| 27 using base::internal::MaxExponent; | 28 using base::internal::MaxExponent; |
| 28 using base::internal::RANGE_VALID; | 29 using base::internal::RANGE_VALID; |
| 29 using base::internal::RANGE_INVALID; | 30 using base::internal::RANGE_INVALID; |
| 30 using base::internal::RANGE_OVERFLOW; | 31 using base::internal::RANGE_OVERFLOW; |
| 31 using base::internal::RANGE_UNDERFLOW; | 32 using base::internal::RANGE_UNDERFLOW; |
| 32 using base::internal::SignedIntegerForSize; | 33 using base::internal::SignedIntegerForSize; |
| 33 using base::enable_if; | |
| 34 | 34 |
| 35 // These tests deliberately cause arithmetic overflows. If the compiler is | 35 // These tests deliberately cause arithmetic overflows. If the compiler is |
| 36 // aggressive enough, it can const fold these overflows. Disable warnings about | 36 // aggressive enough, it can const fold these overflows. Disable warnings about |
| 37 // overflows for const expressions. | 37 // overflows for const expressions. |
| 38 #if defined(OS_WIN) | 38 #if defined(OS_WIN) |
| 39 #pragma warning(disable:4756) | 39 #pragma warning(disable:4756) |
| 40 #endif | 40 #endif |
| 41 | 41 |
| 42 // This is a helper function for finding the maximum value in Src that can be | 42 // This is a helper function for finding the maximum value in Src that can be |
| 43 // wholy represented as the destination floating-point type. | 43 // wholy represented as the destination floating-point type. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 69 EXPECT_EQ(static_cast<Dst>(expected), \ | 69 EXPECT_EQ(static_cast<Dst>(expected), \ |
| 70 CheckedNumeric<Dst>(actual).ValueUnsafe()) \ | 70 CheckedNumeric<Dst>(actual).ValueUnsafe()) \ |
| 71 << "Result test: Value " << +((actual).ValueUnsafe()) << " as " << dst \ | 71 << "Result test: Value " << +((actual).ValueUnsafe()) << " as " << dst \ |
| 72 << " on line " << line; | 72 << " on line " << line; |
| 73 | 73 |
| 74 // Signed integer arithmetic. | 74 // Signed integer arithmetic. |
| 75 template <typename Dst> | 75 template <typename Dst> |
| 76 static void TestSpecializedArithmetic( | 76 static void TestSpecializedArithmetic( |
| 77 const char* dst, | 77 const char* dst, |
| 78 int line, | 78 int line, |
| 79 typename enable_if< | 79 typename std::enable_if<numeric_limits<Dst>::is_integer && |
| 80 numeric_limits<Dst>::is_integer&& numeric_limits<Dst>::is_signed, | 80 numeric_limits<Dst>::is_signed, |
| 81 int>::type = 0) { | 81 int>::type = 0) { |
| 82 typedef numeric_limits<Dst> DstLimits; | 82 typedef numeric_limits<Dst> DstLimits; |
| 83 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, | 83 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, |
| 84 -CheckedNumeric<Dst>(DstLimits::min())); | 84 -CheckedNumeric<Dst>(DstLimits::min())); |
| 85 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, | 85 TEST_EXPECTED_VALIDITY(RANGE_OVERFLOW, |
| 86 CheckedNumeric<Dst>(DstLimits::min()).Abs()); | 86 CheckedNumeric<Dst>(DstLimits::min()).Abs()); |
| 87 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(-1).Abs()); | 87 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(-1).Abs()); |
| 88 | 88 |
| 89 TEST_EXPECTED_VALIDITY(RANGE_VALID, | 89 TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 90 CheckedNumeric<Dst>(DstLimits::max()) + -1); | 90 CheckedNumeric<Dst>(DstLimits::max()) + -1); |
| 91 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, | 91 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); | 125 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
| 126 CheckedNumeric<Dst> checked_dst = 1; | 126 CheckedNumeric<Dst> checked_dst = 1; |
| 127 TEST_EXPECTED_VALUE(0, checked_dst %= 1); | 127 TEST_EXPECTED_VALUE(0, checked_dst %= 1); |
| 128 } | 128 } |
| 129 | 129 |
| 130 // Unsigned integer arithmetic. | 130 // Unsigned integer arithmetic. |
| 131 template <typename Dst> | 131 template <typename Dst> |
| 132 static void TestSpecializedArithmetic( | 132 static void TestSpecializedArithmetic( |
| 133 const char* dst, | 133 const char* dst, |
| 134 int line, | 134 int line, |
| 135 typename enable_if< | 135 typename std::enable_if<numeric_limits<Dst>::is_integer && |
| 136 numeric_limits<Dst>::is_integer && !numeric_limits<Dst>::is_signed, | 136 !numeric_limits<Dst>::is_signed, |
| 137 int>::type = 0) { | 137 int>::type = 0) { |
| 138 typedef numeric_limits<Dst> DstLimits; | 138 typedef numeric_limits<Dst> DstLimits; |
| 139 TEST_EXPECTED_VALIDITY(RANGE_VALID, -CheckedNumeric<Dst>(DstLimits::min())); | 139 TEST_EXPECTED_VALIDITY(RANGE_VALID, -CheckedNumeric<Dst>(DstLimits::min())); |
| 140 TEST_EXPECTED_VALIDITY(RANGE_VALID, | 140 TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 141 CheckedNumeric<Dst>(DstLimits::min()).Abs()); | 141 CheckedNumeric<Dst>(DstLimits::min()).Abs()); |
| 142 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, | 142 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, |
| 143 CheckedNumeric<Dst>(DstLimits::min()) + -1); | 143 CheckedNumeric<Dst>(DstLimits::min()) + -1); |
| 144 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, | 144 TEST_EXPECTED_VALIDITY(RANGE_UNDERFLOW, |
| 145 CheckedNumeric<Dst>(DstLimits::min()) - 1); | 145 CheckedNumeric<Dst>(DstLimits::min()) - 1); |
| 146 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) * 2); | 146 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(DstLimits::min()) * 2); |
| 147 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) / 2); | 147 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) / 2); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 165 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); | 165 TEST_EXPECTED_VALUE(0, CheckedNumeric<Dst>(1) % 1); |
| 166 CheckedNumeric<Dst> checked_dst = 1; | 166 CheckedNumeric<Dst> checked_dst = 1; |
| 167 TEST_EXPECTED_VALUE(0, checked_dst %= 1); | 167 TEST_EXPECTED_VALUE(0, checked_dst %= 1); |
| 168 } | 168 } |
| 169 | 169 |
| 170 // Floating point arithmetic. | 170 // Floating point arithmetic. |
| 171 template <typename Dst> | 171 template <typename Dst> |
| 172 void TestSpecializedArithmetic( | 172 void TestSpecializedArithmetic( |
| 173 const char* dst, | 173 const char* dst, |
| 174 int line, | 174 int line, |
| 175 typename enable_if<numeric_limits<Dst>::is_iec559, int>::type = 0) { | 175 typename std::enable_if<numeric_limits<Dst>::is_iec559, int>::type = 0) { |
| 176 typedef numeric_limits<Dst> DstLimits; | 176 typedef numeric_limits<Dst> DstLimits; |
| 177 TEST_EXPECTED_VALIDITY(RANGE_VALID, -CheckedNumeric<Dst>(DstLimits::min())); | 177 TEST_EXPECTED_VALIDITY(RANGE_VALID, -CheckedNumeric<Dst>(DstLimits::min())); |
| 178 | 178 |
| 179 TEST_EXPECTED_VALIDITY(RANGE_VALID, | 179 TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 180 CheckedNumeric<Dst>(DstLimits::min()).Abs()); | 180 CheckedNumeric<Dst>(DstLimits::min()).Abs()); |
| 181 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(-1).Abs()); | 181 TEST_EXPECTED_VALUE(1, CheckedNumeric<Dst>(-1).Abs()); |
| 182 | 182 |
| 183 TEST_EXPECTED_VALIDITY(RANGE_VALID, | 183 TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| 184 CheckedNumeric<Dst>(DstLimits::min()) + -1); | 184 CheckedNumeric<Dst>(DstLimits::min()) + -1); |
| 185 TEST_EXPECTED_VALIDITY(RANGE_VALID, | 185 TEST_EXPECTED_VALIDITY(RANGE_VALID, |
| (...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 757 | 757 |
| 758 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); | 758 CheckedNumeric<int> too_large = std::numeric_limits<int>::max(); |
| 759 EXPECT_TRUE(too_large.IsValid()); | 759 EXPECT_TRUE(too_large.IsValid()); |
| 760 too_large += d; | 760 too_large += d; |
| 761 EXPECT_FALSE(too_large.IsValid()); | 761 EXPECT_FALSE(too_large.IsValid()); |
| 762 too_large -= d; | 762 too_large -= d; |
| 763 EXPECT_FALSE(too_large.IsValid()); | 763 EXPECT_FALSE(too_large.IsValid()); |
| 764 too_large /= d; | 764 too_large /= d; |
| 765 EXPECT_FALSE(too_large.IsValid()); | 765 EXPECT_FALSE(too_large.IsValid()); |
| 766 } | 766 } |
| OLD | NEW |