Chromium Code Reviews| 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 813 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 824 return 0; | 824 return 0; |
| 825 } | 825 } |
| 826 | 826 |
| 827 static_assert( | 827 static_assert( |
| 828 std::is_same<decltype(TestOverload(StrictNumeric<int>())), int>::value, | 828 std::is_same<decltype(TestOverload(StrictNumeric<int>())), int>::value, |
| 829 ""); | 829 ""); |
| 830 static_assert(std::is_same<decltype(TestOverload(StrictNumeric<size_t>())), | 830 static_assert(std::is_same<decltype(TestOverload(StrictNumeric<size_t>())), |
| 831 size_t>::value, | 831 size_t>::value, |
| 832 ""); | 832 ""); |
| 833 | 833 |
| 834 template <typename T> | |
| 835 struct CastTest1 { | |
| 836 static constexpr T HandleNaN() { return -1; } | |
| 837 static constexpr T max() { return numeric_limits<T>::max() - 1; } | |
| 838 static constexpr T HandleOverflow() { return max(); } | |
| 839 static constexpr T lowest() { return numeric_limits<T>::lowest() + 1; } | |
| 840 static constexpr T HandleUnderflow() { return lowest(); } | |
| 841 }; | |
| 842 | |
| 843 template <typename T> | |
| 844 struct CastTest2 { | |
| 845 static constexpr T HandleNaN() { return 11; } | |
| 846 static constexpr T max() { return 10; } | |
| 847 static constexpr T HandleOverflow() { return max(); } | |
| 848 static constexpr T lowest() { return 1; } | |
| 849 static constexpr T HandleUnderflow() { return lowest(); } | |
| 850 }; | |
| 851 | |
| 834 TEST(SafeNumerics, CastTests) { | 852 TEST(SafeNumerics, CastTests) { |
| 835 // MSVC catches and warns that we're forcing saturation in these tests. | 853 // MSVC catches and warns that we're forcing saturation in these tests. |
| 836 // Since that's intentional, we need to shut this warning off. | 854 // Since that's intentional, we need to shut this warning off. |
| 837 #if defined(COMPILER_MSVC) | 855 #if defined(COMPILER_MSVC) |
| 838 #pragma warning(disable : 4756) | 856 #pragma warning(disable : 4756) |
| 839 #endif | 857 #endif |
| 840 | 858 |
| 841 int small_positive = 1; | 859 int small_positive = 1; |
| 842 int small_negative = -1; | 860 int small_negative = -1; |
| 843 double double_small = 1.0; | 861 double double_small = 1.0; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 928 EXPECT_EQ(static_cast<int16_t>(numeric_limits<int8_t>::max()), int16_dest); | 946 EXPECT_EQ(static_cast<int16_t>(numeric_limits<int8_t>::max()), int16_dest); |
| 929 EXPECT_TRUE(int8_min.AssignIfValid(&int16_dest)); | 947 EXPECT_TRUE(int8_min.AssignIfValid(&int16_dest)); |
| 930 EXPECT_EQ(static_cast<int16_t>(numeric_limits<int8_t>::lowest()), int16_dest); | 948 EXPECT_EQ(static_cast<int16_t>(numeric_limits<int8_t>::lowest()), int16_dest); |
| 931 EXPECT_FALSE(double_max.AssignIfValid(&uint8_dest)); | 949 EXPECT_FALSE(double_max.AssignIfValid(&uint8_dest)); |
| 932 EXPECT_FALSE(double_max.AssignIfValid(&int16_dest)); | 950 EXPECT_FALSE(double_max.AssignIfValid(&int16_dest)); |
| 933 EXPECT_TRUE(double_max.AssignIfValid(&double_dest)); | 951 EXPECT_TRUE(double_max.AssignIfValid(&double_dest)); |
| 934 EXPECT_EQ(numeric_limits<double>::max(), double_dest); | 952 EXPECT_EQ(numeric_limits<double>::max(), double_dest); |
| 935 EXPECT_EQ(1, checked_cast<int>(StrictNumeric<int>(1))); | 953 EXPECT_EQ(1, checked_cast<int>(StrictNumeric<int>(1))); |
| 936 EXPECT_EQ(1, saturated_cast<int>(StrictNumeric<int>(1))); | 954 EXPECT_EQ(1, saturated_cast<int>(StrictNumeric<int>(1))); |
| 937 EXPECT_EQ(1, strict_cast<int>(StrictNumeric<int>(1))); | 955 EXPECT_EQ(1, strict_cast<int>(StrictNumeric<int>(1))); |
| 938 } | |
| 939 | 956 |
| 940 TEST(SafeNumerics, SaturatedCastChecks) { | 957 // Test the saturated cast overrides. |
| 941 float not_a_number = std::numeric_limits<float>::infinity() - | 958 using FloatLimits = numeric_limits<float>; |
| 942 std::numeric_limits<float>::infinity(); | 959 using IntLimits = numeric_limits<int>; |
| 943 EXPECT_TRUE(std::isnan(not_a_number)); | 960 EXPECT_EQ(-1, (saturated_cast<int, CastTest1>(FloatLimits::quiet_NaN()))); |
| 944 EXPECT_DEATH_IF_SUPPORTED( | 961 EXPECT_EQ(CastTest1<int>::max(), |
| 945 (saturated_cast<int, base::CheckOnFailure>(not_a_number)), | 962 (saturated_cast<int, CastTest1>(FloatLimits::infinity()))); |
| 946 ""); | 963 EXPECT_EQ(CastTest1<int>::max(), |
| 964 (saturated_cast<int, CastTest1>(FloatLimits::max()))); | |
| 965 EXPECT_EQ(CastTest1<int>::max(), | |
| 966 (saturated_cast<int, CastTest1>(float(IntLimits::max())))); | |
| 967 EXPECT_EQ(CastTest1<int>::lowest(), | |
| 968 (saturated_cast<int, CastTest1>(-FloatLimits::infinity()))); | |
| 969 EXPECT_EQ(CastTest1<int>::lowest(), | |
| 970 (saturated_cast<int, CastTest1>(FloatLimits::lowest()))); | |
| 971 EXPECT_EQ(CastTest1<int>::lowest(), | |
| 972 (saturated_cast<int, CastTest1>(float(IntLimits::lowest())))); | |
| 973 EXPECT_EQ(11, (saturated_cast<int, CastTest2>(FloatLimits::quiet_NaN()))); | |
| 974 EXPECT_EQ(10, (saturated_cast<int, CastTest2>(FloatLimits::infinity()))); | |
| 975 EXPECT_EQ(10, (saturated_cast<int, CastTest2>(FloatLimits::max()))); | |
| 976 EXPECT_EQ(1, (saturated_cast<int, CastTest2>(-FloatLimits::infinity()))); | |
| 977 EXPECT_EQ(1, (saturated_cast<int, CastTest2>(FloatLimits::lowest()))); | |
| 978 EXPECT_EQ(1, (saturated_cast<int, CastTest2>(0U))); | |
|
eae
2016/12/15 23:07:46
This is great in that you test all the edge values
jschuh
2016/12/16 00:31:00
I've added it to clarify that it works with the cu
| |
| 947 } | 979 } |
| 948 | 980 |
| 949 TEST(SafeNumerics, IsValueInRangeForNumericType) { | 981 TEST(SafeNumerics, IsValueInRangeForNumericType) { |
| 950 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0)); | 982 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0)); |
| 951 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(1)); | 983 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(1)); |
| 952 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(2)); | 984 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(2)); |
| 953 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(-1)); | 985 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(-1)); |
| 954 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0xffffffffu)); | 986 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0xffffffffu)); |
| 955 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0xffffffff))); | 987 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0xffffffff))); |
| 956 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000000))); | 988 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000000))); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1056 EXPECT_EQ(static_cast<decltype(d)::type>(-.5), d); | 1088 EXPECT_EQ(static_cast<decltype(d)::type>(-.5), d); |
| 1057 auto e = CheckMod(MakeCheckedNum(20), 3).ValueOrDie(); | 1089 auto e = CheckMod(MakeCheckedNum(20), 3).ValueOrDie(); |
| 1058 EXPECT_EQ(static_cast<decltype(e)::type>(2), e); | 1090 EXPECT_EQ(static_cast<decltype(e)::type>(2), e); |
| 1059 auto f = CheckLsh(1, MakeCheckedNum(2)).ValueOrDie(); | 1091 auto f = CheckLsh(1, MakeCheckedNum(2)).ValueOrDie(); |
| 1060 EXPECT_EQ(static_cast<decltype(f)::type>(4), f); | 1092 EXPECT_EQ(static_cast<decltype(f)::type>(4), f); |
| 1061 auto g = CheckRsh(4, MakeCheckedNum(2)).ValueOrDie(); | 1093 auto g = CheckRsh(4, MakeCheckedNum(2)).ValueOrDie(); |
| 1062 EXPECT_EQ(static_cast<decltype(g)::type>(1), g); | 1094 EXPECT_EQ(static_cast<decltype(g)::type>(1), g); |
| 1063 auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie(); | 1095 auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie(); |
| 1064 EXPECT_EQ(static_cast<decltype(h)::type>(1), h); | 1096 EXPECT_EQ(static_cast<decltype(h)::type>(1), h); |
| 1065 } | 1097 } |
| OLD | NEW |