| 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 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 723 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); | 735 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); |
| 724 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); | 736 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); |
| 725 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); | 737 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); |
| 726 EXPECT_EQ(numeric_limits<int>::min(), saturated_cast<int>(double_small_int)); | 738 EXPECT_EQ(numeric_limits<int>::min(), saturated_cast<int>(double_small_int)); |
| 727 EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int)); | 739 EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int)); |
| 728 | 740 |
| 729 float not_a_number = std::numeric_limits<float>::infinity() - | 741 float not_a_number = std::numeric_limits<float>::infinity() - |
| 730 std::numeric_limits<float>::infinity(); | 742 std::numeric_limits<float>::infinity(); |
| 731 EXPECT_TRUE(std::isnan(not_a_number)); | 743 EXPECT_TRUE(std::isnan(not_a_number)); |
| 732 EXPECT_EQ(0, saturated_cast<int>(not_a_number)); | 744 EXPECT_EQ(0, saturated_cast<int>(not_a_number)); |
| 745 |
| 746 // Test the CheckedNumeric value extractions functions. |
| 747 auto int8_min = CheckNum(numeric_limits<int8_t>::min()); |
| 748 auto int8_max = CheckNum(numeric_limits<int8_t>::max()); |
| 749 auto double_max = CheckNum(numeric_limits<double>::max()); |
| 750 static_assert( |
| 751 std::is_same<int16_t, decltype(int8_min.ValueOrDie<int16_t>())>::value, |
| 752 "ValueOrDie returning incorrect type."); |
| 753 static_assert( |
| 754 std::is_same<int16_t, |
| 755 decltype(int8_min.ValueOrDefault<int16_t>(0))>::value, |
| 756 "ValueOrDefault returning incorrect type."); |
| 757 static_assert( |
| 758 std::is_same<float, decltype(double_max.ValueFloating<float>())>::value, |
| 759 "ValueFloating returning incorrect type."); |
| 760 EXPECT_FALSE(int8_min.template IsValid<uint8_t>()); |
| 761 EXPECT_TRUE(int8_max.template IsValid<uint8_t>()); |
| 762 EXPECT_EQ(static_cast<int>(numeric_limits<int8_t>::min()), |
| 763 int8_min.template ValueOrDie<int>()); |
| 764 EXPECT_TRUE(int8_max.template IsValid<uint32_t>()); |
| 765 EXPECT_EQ(static_cast<int>(numeric_limits<int8_t>::max()), |
| 766 int8_max.template ValueOrDie<int>()); |
| 733 } | 767 } |
| 734 | 768 |
| 735 TEST(SafeNumerics, SaturatedCastChecks) { | 769 TEST(SafeNumerics, SaturatedCastChecks) { |
| 736 float not_a_number = std::numeric_limits<float>::infinity() - | 770 float not_a_number = std::numeric_limits<float>::infinity() - |
| 737 std::numeric_limits<float>::infinity(); | 771 std::numeric_limits<float>::infinity(); |
| 738 EXPECT_TRUE(std::isnan(not_a_number)); | 772 EXPECT_TRUE(std::isnan(not_a_number)); |
| 739 EXPECT_DEATH_IF_SUPPORTED( | 773 EXPECT_DEATH_IF_SUPPORTED( |
| 740 (saturated_cast<int, base::CheckOnFailure>(not_a_number)), | 774 (saturated_cast<int, base::CheckOnFailure>(not_a_number)), |
| 741 ""); | 775 ""); |
| 742 } | 776 } |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 851 EXPECT_EQ(static_cast<decltype(d)>(-.5), d); | 885 EXPECT_EQ(static_cast<decltype(d)>(-.5), d); |
| 852 auto e = CheckMod(CheckNum(20), 3).ValueOrDie(); | 886 auto e = CheckMod(CheckNum(20), 3).ValueOrDie(); |
| 853 EXPECT_EQ(static_cast<decltype(e)>(2), e); | 887 EXPECT_EQ(static_cast<decltype(e)>(2), e); |
| 854 auto f = CheckLsh(1, CheckNum(2)).ValueOrDie(); | 888 auto f = CheckLsh(1, CheckNum(2)).ValueOrDie(); |
| 855 EXPECT_EQ(static_cast<decltype(f)>(4), f); | 889 EXPECT_EQ(static_cast<decltype(f)>(4), f); |
| 856 auto g = CheckRsh(4, CheckNum(2)).ValueOrDie(); | 890 auto g = CheckRsh(4, CheckNum(2)).ValueOrDie(); |
| 857 EXPECT_EQ(static_cast<decltype(g)>(1), g); | 891 EXPECT_EQ(static_cast<decltype(g)>(1), g); |
| 858 auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie(); | 892 auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie(); |
| 859 EXPECT_EQ(static_cast<decltype(h)>(1), h); | 893 EXPECT_EQ(static_cast<decltype(h)>(1), h); |
| 860 } | 894 } |
| OLD | NEW |