| 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 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/logging.h" |
| 12 #include "base/numerics/safe_conversions.h" | 13 #include "base/numerics/safe_conversions.h" |
| 13 #include "base/numerics/safe_math.h" | 14 #include "base/numerics/safe_math.h" |
| 14 #include "base/test/gtest_util.h" | 15 #include "base/test/gtest_util.h" |
| 15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) | 19 #if defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
| 19 #include <mmintrin.h> | 20 #include <mmintrin.h> |
| 20 #endif | 21 #endif |
| 21 | 22 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 namespace internal { | 75 namespace internal { |
| 75 template <typename U> | 76 template <typename U> |
| 76 U GetNumericValueForTest(const CheckedNumeric<U>& src) { | 77 U GetNumericValueForTest(const CheckedNumeric<U>& src) { |
| 77 return src.state_.value(); | 78 return src.state_.value(); |
| 78 } | 79 } |
| 79 } // namespace internal. | 80 } // namespace internal. |
| 80 } // namespace base. | 81 } // namespace base. |
| 81 | 82 |
| 82 using base::internal::GetNumericValueForTest; | 83 using base::internal::GetNumericValueForTest; |
| 83 | 84 |
| 85 // Logs the ValueOrDie() failure instead of crashing. |
| 86 struct LogOnFailure { |
| 87 template <typename T> |
| 88 static T HandleFailure() { |
| 89 LOG(WARNING) << "ValueOrDie() failed unexpectedly."; |
| 90 return T(); |
| 91 } |
| 92 }; |
| 93 |
| 84 // Helper macros to wrap displaying the conversion types and line numbers. | 94 // Helper macros to wrap displaying the conversion types and line numbers. |
| 85 #define TEST_EXPECTED_VALIDITY(expected, actual) \ | 95 #define TEST_EXPECTED_VALIDITY(expected, actual) \ |
| 86 EXPECT_EQ(expected, (actual).template Cast<Dst>().IsValid()) \ | 96 EXPECT_EQ(expected, (actual).template Cast<Dst>().IsValid()) \ |
| 87 << "Result test: Value " << GetNumericValueForTest(actual) << " as " \ | 97 << "Result test: Value " << GetNumericValueForTest(actual) << " as " \ |
| 88 << dst << " on line " << line | 98 << dst << " on line " << line |
| 89 | 99 |
| 90 #define TEST_EXPECTED_SUCCESS(actual) TEST_EXPECTED_VALIDITY(true, actual) | 100 #define TEST_EXPECTED_SUCCESS(actual) TEST_EXPECTED_VALIDITY(true, actual) |
| 91 #define TEST_EXPECTED_FAILURE(actual) TEST_EXPECTED_VALIDITY(false, actual) | 101 #define TEST_EXPECTED_FAILURE(actual) TEST_EXPECTED_VALIDITY(false, actual) |
| 92 | 102 |
| 93 #define TEST_EXPECTED_VALUE(expected, actual) \ | 103 #define TEST_EXPECTED_VALUE(expected, actual) \ |
| 94 EXPECT_EQ(static_cast<Dst>(expected), \ | 104 EXPECT_EQ(static_cast<Dst>(expected), \ |
| 95 (actual).template Cast<Dst>().ValueOrDie()) \ | 105 ((actual) \ |
| 106 .template Cast<Dst>() \ |
| 107 .template ValueOrDie<Dst, LogOnFailure>())) \ |
| 96 << "Result test: Value " << GetNumericValueForTest(actual) << " as " \ | 108 << "Result test: Value " << GetNumericValueForTest(actual) << " as " \ |
| 97 << dst << " on line " << line | 109 << dst << " on line " << line |
| 98 | 110 |
| 99 // Signed integer arithmetic. | 111 // Signed integer arithmetic. |
| 100 template <typename Dst> | 112 template <typename Dst> |
| 101 static void TestSpecializedArithmetic( | 113 static void TestSpecializedArithmetic( |
| 102 const char* dst, | 114 const char* dst, |
| 103 int line, | 115 int line, |
| 104 typename std::enable_if<numeric_limits<Dst>::is_integer && | 116 typename std::enable_if<numeric_limits<Dst>::is_integer && |
| 105 numeric_limits<Dst>::is_signed, | 117 numeric_limits<Dst>::is_signed, |
| (...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 710 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); | 722 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); |
| 711 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); | 723 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); |
| 712 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); | 724 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); |
| 713 EXPECT_EQ(numeric_limits<int>::min(), saturated_cast<int>(double_small_int)); | 725 EXPECT_EQ(numeric_limits<int>::min(), saturated_cast<int>(double_small_int)); |
| 714 EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int)); | 726 EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int)); |
| 715 | 727 |
| 716 float not_a_number = std::numeric_limits<float>::infinity() - | 728 float not_a_number = std::numeric_limits<float>::infinity() - |
| 717 std::numeric_limits<float>::infinity(); | 729 std::numeric_limits<float>::infinity(); |
| 718 EXPECT_TRUE(std::isnan(not_a_number)); | 730 EXPECT_TRUE(std::isnan(not_a_number)); |
| 719 EXPECT_EQ(0, saturated_cast<int>(not_a_number)); | 731 EXPECT_EQ(0, saturated_cast<int>(not_a_number)); |
| 732 |
| 733 // Test the CheckedNumeric value extractions functions. |
| 734 auto int8_min = CheckNum(numeric_limits<int8_t>::min()); |
| 735 auto int8_max = CheckNum(numeric_limits<int8_t>::max()); |
| 736 auto double_max = CheckNum(numeric_limits<double>::max()); |
| 737 static_assert( |
| 738 std::is_same<int16_t, decltype(int8_min.ValueOrDie<int16_t>())>::value, |
| 739 "ValueOrDie returning incorrect type."); |
| 740 static_assert( |
| 741 std::is_same<int16_t, |
| 742 decltype(int8_min.ValueOrDefault<int16_t>(0))>::value, |
| 743 "ValueOrDefault returning incorrect type."); |
| 744 static_assert( |
| 745 std::is_same<float, decltype(double_max.ValueFloating<float>())>::value, |
| 746 "ValueFloating returning incorrect type."); |
| 747 EXPECT_FALSE(int8_min.template IsValid<uint8_t>()); |
| 748 EXPECT_TRUE(int8_max.template IsValid<uint8_t>()); |
| 749 EXPECT_EQ(static_cast<int>(numeric_limits<int8_t>::min()), |
| 750 int8_min.template ValueOrDie<int>()); |
| 751 EXPECT_TRUE(int8_max.template IsValid<uint32_t>()); |
| 752 EXPECT_EQ(static_cast<int>(numeric_limits<int8_t>::max()), |
| 753 int8_max.template ValueOrDie<int>()); |
| 720 } | 754 } |
| 721 | 755 |
| 722 TEST(SafeNumerics, SaturatedCastChecks) { | 756 TEST(SafeNumerics, SaturatedCastChecks) { |
| 723 float not_a_number = std::numeric_limits<float>::infinity() - | 757 float not_a_number = std::numeric_limits<float>::infinity() - |
| 724 std::numeric_limits<float>::infinity(); | 758 std::numeric_limits<float>::infinity(); |
| 725 EXPECT_TRUE(std::isnan(not_a_number)); | 759 EXPECT_TRUE(std::isnan(not_a_number)); |
| 726 EXPECT_DEATH_IF_SUPPORTED( | 760 EXPECT_DEATH_IF_SUPPORTED( |
| 727 (saturated_cast<int, base::CheckOnFailure>(not_a_number)), | 761 (saturated_cast<int, base::CheckOnFailure>(not_a_number)), |
| 728 ""); | 762 ""); |
| 729 } | 763 } |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 838 EXPECT_EQ(static_cast<decltype(d)>(-.5), d); | 872 EXPECT_EQ(static_cast<decltype(d)>(-.5), d); |
| 839 auto e = CheckMod(CheckNum(20), 3).ValueOrDie(); | 873 auto e = CheckMod(CheckNum(20), 3).ValueOrDie(); |
| 840 EXPECT_EQ(static_cast<decltype(e)>(2), e); | 874 EXPECT_EQ(static_cast<decltype(e)>(2), e); |
| 841 auto f = CheckLsh(1, CheckNum(2)).ValueOrDie(); | 875 auto f = CheckLsh(1, CheckNum(2)).ValueOrDie(); |
| 842 EXPECT_EQ(static_cast<decltype(f)>(4), f); | 876 EXPECT_EQ(static_cast<decltype(f)>(4), f); |
| 843 auto g = CheckRsh(4, CheckNum(2)).ValueOrDie(); | 877 auto g = CheckRsh(4, CheckNum(2)).ValueOrDie(); |
| 844 EXPECT_EQ(static_cast<decltype(g)>(1), g); | 878 EXPECT_EQ(static_cast<decltype(g)>(1), g); |
| 845 auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie(); | 879 auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie(); |
| 846 EXPECT_EQ(static_cast<decltype(h)>(1), h); | 880 EXPECT_EQ(static_cast<decltype(h)>(1), h); |
| 847 } | 881 } |
| OLD | NEW |