Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: base/numerics/safe_numerics_unittest.cc

Issue 2578613002: Support saturation overrides in saturated_cast (Closed)
Patch Set: build fix Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/numerics/safe_conversions_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 static_cast<unsigned>(0)); 904 static_cast<unsigned>(0));
887 EXPECT_EQ(saturated_cast<int>(double_small), 905 EXPECT_EQ(saturated_cast<int>(double_small),
888 static_cast<int>(double_small)); 906 static_cast<int>(double_small));
889 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max()); 907 EXPECT_EQ(saturated_cast<int>(double_large), numeric_limits<int>::max());
890 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity); 908 EXPECT_EQ(saturated_cast<float>(double_large), double_infinity);
891 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity); 909 EXPECT_EQ(saturated_cast<float>(-double_large), -double_infinity);
892 EXPECT_EQ(numeric_limits<int>::lowest(), 910 EXPECT_EQ(numeric_limits<int>::lowest(),
893 saturated_cast<int>(double_small_int)); 911 saturated_cast<int>(double_small_int));
894 EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int)); 912 EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int));
895 913
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
896 float not_a_number = std::numeric_limits<float>::infinity() - 943 float not_a_number = std::numeric_limits<float>::infinity() -
897 std::numeric_limits<float>::infinity(); 944 std::numeric_limits<float>::infinity();
898 EXPECT_TRUE(std::isnan(not_a_number)); 945 EXPECT_TRUE(std::isnan(not_a_number));
899 EXPECT_EQ(0, saturated_cast<int>(not_a_number)); 946 EXPECT_EQ(0, saturated_cast<int>(not_a_number));
900 947
901 // Test the CheckedNumeric value extractions functions. 948 // Test the CheckedNumeric value extractions functions.
902 auto int8_min = MakeCheckedNum(numeric_limits<int8_t>::lowest()); 949 auto int8_min = MakeCheckedNum(numeric_limits<int8_t>::lowest());
903 auto int8_max = MakeCheckedNum(numeric_limits<int8_t>::max()); 950 auto int8_max = MakeCheckedNum(numeric_limits<int8_t>::max());
904 auto double_max = MakeCheckedNum(numeric_limits<double>::max()); 951 auto double_max = MakeCheckedNum(numeric_limits<double>::max());
905 static_assert( 952 static_assert(
(...skipping 24 matching lines...) Expand all
930 EXPECT_EQ(static_cast<int16_t>(numeric_limits<int8_t>::lowest()), int16_dest); 977 EXPECT_EQ(static_cast<int16_t>(numeric_limits<int8_t>::lowest()), int16_dest);
931 EXPECT_FALSE(double_max.AssignIfValid(&uint8_dest)); 978 EXPECT_FALSE(double_max.AssignIfValid(&uint8_dest));
932 EXPECT_FALSE(double_max.AssignIfValid(&int16_dest)); 979 EXPECT_FALSE(double_max.AssignIfValid(&int16_dest));
933 EXPECT_TRUE(double_max.AssignIfValid(&double_dest)); 980 EXPECT_TRUE(double_max.AssignIfValid(&double_dest));
934 EXPECT_EQ(numeric_limits<double>::max(), double_dest); 981 EXPECT_EQ(numeric_limits<double>::max(), double_dest);
935 EXPECT_EQ(1, checked_cast<int>(StrictNumeric<int>(1))); 982 EXPECT_EQ(1, checked_cast<int>(StrictNumeric<int>(1)));
936 EXPECT_EQ(1, saturated_cast<int>(StrictNumeric<int>(1))); 983 EXPECT_EQ(1, saturated_cast<int>(StrictNumeric<int>(1)));
937 EXPECT_EQ(1, strict_cast<int>(StrictNumeric<int>(1))); 984 EXPECT_EQ(1, strict_cast<int>(StrictNumeric<int>(1)));
938 } 985 }
939 986
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
949 TEST(SafeNumerics, IsValueInRangeForNumericType) { 987 TEST(SafeNumerics, IsValueInRangeForNumericType) {
950 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0)); 988 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0));
951 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(1)); 989 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(1));
952 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(2)); 990 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(2));
953 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(-1)); 991 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(-1));
954 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0xffffffffu)); 992 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0xffffffffu));
955 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0xffffffff))); 993 EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0xffffffff)));
956 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000000))); 994 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000000)));
957 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000001))); 995 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(UINT64_C(0x100000001)));
958 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>( 996 EXPECT_FALSE(IsValueInRangeForNumericType<uint32_t>(
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
1056 EXPECT_EQ(static_cast<decltype(d)::type>(-.5), d); 1094 EXPECT_EQ(static_cast<decltype(d)::type>(-.5), d);
1057 auto e = CheckMod(MakeCheckedNum(20), 3).ValueOrDie(); 1095 auto e = CheckMod(MakeCheckedNum(20), 3).ValueOrDie();
1058 EXPECT_EQ(static_cast<decltype(e)::type>(2), e); 1096 EXPECT_EQ(static_cast<decltype(e)::type>(2), e);
1059 auto f = CheckLsh(1, MakeCheckedNum(2)).ValueOrDie(); 1097 auto f = CheckLsh(1, MakeCheckedNum(2)).ValueOrDie();
1060 EXPECT_EQ(static_cast<decltype(f)::type>(4), f); 1098 EXPECT_EQ(static_cast<decltype(f)::type>(4), f);
1061 auto g = CheckRsh(4, MakeCheckedNum(2)).ValueOrDie(); 1099 auto g = CheckRsh(4, MakeCheckedNum(2)).ValueOrDie();
1062 EXPECT_EQ(static_cast<decltype(g)::type>(1), g); 1100 EXPECT_EQ(static_cast<decltype(g)::type>(1), g);
1063 auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie(); 1101 auto h = CheckRsh(CheckAdd(1, 1, 1, 1), CheckSub(4, 2)).ValueOrDie();
1064 EXPECT_EQ(static_cast<decltype(h)::type>(1), h); 1102 EXPECT_EQ(static_cast<decltype(h)::type>(1), h);
1065 } 1103 }
OLDNEW
« no previous file with comments | « base/numerics/safe_conversions_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698