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> | 834 TEST(SafeNumerics, CastTests) { |
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 | |
852 TEST(SafeNumerics, ) { | |
853 // MSVC catches and warns that we're forcing saturation in these tests. | 835 // MSVC catches and warns that we're forcing saturation in these tests. |
854 // Since that's intentional, we need to shut this warning off. | 836 // Since that's intentional, we need to shut this warning off. |
855 #if defined(COMPILER_MSVC) | 837 #if defined(COMPILER_MSVC) |
856 #pragma warning(disable : 4756) | 838 #pragma warning(disable : 4756) |
857 #endif | 839 #endif |
858 | 840 |
859 int small_positive = 1; | 841 int small_positive = 1; |
860 int small_negative = -1; | 842 int small_negative = -1; |
861 double double_small = 1.0; | 843 double double_small = 1.0; |
862 double double_large = numeric_limits<double>::max(); | 844 double double_large = numeric_limits<double>::max(); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
904 static_cast<unsigned>(0)); | 886 static_cast<unsigned>(0)); |
905 EXPECT_EQ(saturated_cast<int>(double_small), | 887 EXPECT_EQ(saturated_cast<int>(double_small), |
906 static_cast<int>(double_small)); | 888 static_cast<int>(double_small)); |
907 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); | 889 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); |
908 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); | 890 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); |
909 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); | 891 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); |
910 EXPECT_EQ(numeric_limits<int>::lowest(), | 892 EXPECT_EQ(numeric_limits<int>::lowest(), |
911 saturated_cast<int>(double_small_int)); | 893 saturated_cast<int>(double_small_int)); |
912 EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int)); | 894 EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int)); |
913 | 895 |
914 // Test the saturated cast overrides. | |
915 using FloatLimits = numeric_limits<float>; | |
916 using IntLimits = numeric_limits<int>; | |
917 EXPECT_EQ(-1, (saturated_cast<int, CastTest1>(FloatLimits::quiet_NaN()))); | |
918 EXPECT_EQ(CastTest1<int>::max(), | |
919 (saturated_cast<int, CastTest1>(FloatLimits::infinity()))); | |
920 EXPECT_EQ(CastTest1<int>::max(), | |
921 (saturated_cast<int, CastTest1>(FloatLimits::max()))); | |
922 EXPECT_EQ(CastTest1<int>::max(), | |
923 (saturated_cast<int, CastTest1>(float(IntLimits::max())))); | |
924 EXPECT_EQ(CastTest1<int>::lowest(), | |
925 (saturated_cast<int, CastTest1>(-FloatLimits::infinity()))); | |
926 EXPECT_EQ(CastTest1<int>::lowest(), | |
927 (saturated_cast<int, CastTest1>(FloatLimits::lowest()))); | |
928 EXPECT_EQ(0, (saturated_cast<int, CastTest1>(0.0))); | |
929 EXPECT_EQ(1, (saturated_cast<int, CastTest1>(1.0))); | |
930 EXPECT_EQ(-1, (saturated_cast<int, CastTest1>(-1.0))); | |
931 EXPECT_EQ(0, (saturated_cast<int, CastTest1>(0))); | |
932 EXPECT_EQ(1, (saturated_cast<int, CastTest1>(1))); | |
933 EXPECT_EQ(-1, (saturated_cast<int, CastTest1>(-1))); | |
934 EXPECT_EQ(CastTest1<int>::lowest(), | |
935 (saturated_cast<int, CastTest1>(float(IntLimits::lowest())))); | |
936 EXPECT_EQ(11, (saturated_cast<int, CastTest2>(FloatLimits::quiet_NaN()))); | |
937 EXPECT_EQ(10, (saturated_cast<int, CastTest2>(FloatLimits::infinity()))); | |
938 EXPECT_EQ(10, (saturated_cast<int, CastTest2>(FloatLimits::max()))); | |
939 EXPECT_EQ(1, (saturated_cast<int, CastTest2>(-FloatLimits::infinity()))); | |
940 EXPECT_EQ(1, (saturated_cast<int, CastTest2>(FloatLimits::lowest()))); | |
941 EXPECT_EQ(1, (saturated_cast<int, CastTest2>(0U))); | |
942 | |
943 float not_a_number = std::numeric_limits<float>::infinity() - | 896 float not_a_number = std::numeric_limits<float>::infinity() - |
944 std::numeric_limits<float>::infinity(); | 897 std::numeric_limits<float>::infinity(); |
945 EXPECT_TRUE(std::isnan(not_a_number)); | 898 EXPECT_TRUE(std::isnan(not_a_number)); |
946 EXPECT_EQ(0, saturated_cast<int>(not_a_number)); | 899 EXPECT_EQ(0, saturated_cast<int>(not_a_number)); |
947 | 900 |
948 // Test the CheckedNumeric value extractions functions. | 901 // Test the CheckedNumeric value extractions functions. |
949 auto int8_min = MakeCheckedNum(numeric_limits<int8_t>::lowest()); | 902 auto int8_min = MakeCheckedNum(numeric_limits<int8_t>::lowest()); |
950 auto int8_max = MakeCheckedNum(numeric_limits<int8_t>::max()); | 903 auto int8_max = MakeCheckedNum(numeric_limits<int8_t>::max()); |
951 auto double_max = MakeCheckedNum(numeric_limits<double>::max()); | 904 auto double_max = MakeCheckedNum(numeric_limits<double>::max()); |
952 static_assert( | 905 static_assert( |
(...skipping 24 matching lines...) Expand all Loading... |
977 EXPECT_EQ(static_cast<int16_t>(numeric_limits<int8_t>::lowest()), int16_dest); | 930 EXPECT_EQ(static_cast<int16_t>(numeric_limits<int8_t>::lowest()), int16_dest); |
978 EXPECT_FALSE(double_max.AssignIfValid(&uint8_dest)); | 931 EXPECT_FALSE(double_max.AssignIfValid(&uint8_dest)); |
979 EXPECT_FALSE(double_max.AssignIfValid(&int16_dest)); | 932 EXPECT_FALSE(double_max.AssignIfValid(&int16_dest)); |
980 EXPECT_TRUE(double_max.AssignIfValid(&double_dest)); | 933 EXPECT_TRUE(double_max.AssignIfValid(&double_dest)); |
981 EXPECT_EQ(numeric_limits<double>::max(), double_dest); | 934 EXPECT_EQ(numeric_limits<double>::max(), double_dest); |
982 EXPECT_EQ(1, checked_cast<int>(StrictNumeric<int>(1))); | 935 EXPECT_EQ(1, checked_cast<int>(StrictNumeric<int>(1))); |
983 EXPECT_EQ(1, saturated_cast<int>(StrictNumeric<int>(1))); | 936 EXPECT_EQ(1, saturated_cast<int>(StrictNumeric<int>(1))); |
984 EXPECT_EQ(1, strict_cast<int>(StrictNumeric<int>(1))); | 937 EXPECT_EQ(1, strict_cast<int>(StrictNumeric<int>(1))); |
985 } | 938 } |
986 | 939 |
| 940 TEST(SafeNumerics, SaturatedCastChecks) { |
| 941 float not_a_number = std::numeric_limits<float>::infinity() - |
| 942 std::numeric_limits<float>::infinity(); |
| 943 EXPECT_TRUE(std::isnan(not_a_number)); |
| 944 EXPECT_DEATH_IF_SUPPORTED( |
| 945 (saturated_cast<int, base::CheckOnFailure>(not_a_number)), |
| 946 ""); |
| 947 } |
| 948 |
987 TEST(SafeNumerics, IsValueInRangeForNumericType) { | 949 TEST(SafeNumerics, IsValueInRangeForNumericType) { |
988 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0)); | 950 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0)); |
989 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(1)); | 951 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(1)); |
990 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(2)); | 952 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(2)); |
991 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(-1)); | 953 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(-1)); |
992 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0xffffffffu)); | 954 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0xffffffffu)); |
993 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0xffffffff))); | 955 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0xffffffff))); |
994 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000000))); | 956 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000000))); |
995 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000001))); | 957 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000001))); |
996 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>( | 958 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>( |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1094 EXPECT_EQ(static_cast<decltype(d)::type>(-.5), d); | 1056 EXPECT_EQ(static_cast<decltype(d)::type>(-.5), d); |
1095 auto e = CheckMod(MakeCheckedNum(20), 3).ValueOrDie(); | 1057 auto e = CheckMod(MakeCheckedNum(20), 3).ValueOrDie(); |
1096 EXPECT_EQ(static_cast<decltype(e)::type>(2), e); | 1058 EXPECT_EQ(static_cast<decltype(e)::type>(2), e); |
1097 auto f = CheckLsh(1, MakeCheckedNum(2)).ValueOrDie(); | 1059 auto f = CheckLsh(1, MakeCheckedNum(2)).ValueOrDie(); |
1098 EXPECT_EQ(static_cast<decltype(f)::type>(4), f); | 1060 EXPECT_EQ(static_cast<decltype(f)::type>(4), f); |
1099 auto g = CheckRsh(4, MakeCheckedNum(2)).ValueOrDie(); | 1061 auto g = CheckRsh(4, MakeCheckedNum(2)).ValueOrDie(); |
1100 EXPECT_EQ(static_cast<decltype(g)::type>(1), g); | 1062 EXPECT_EQ(static_cast<decltype(g)::type>(1), g); |
1101 auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie(); | 1063 auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie(); |
1102 EXPECT_EQ(static_cast<decltype(h)::type>(1), h); | 1064 EXPECT_EQ(static_cast<decltype(h)::type>(1), h); |
1103 } | 1065 } |
OLD | NEW |