Index: base/numerics/safe_numerics_unittest.cc |
diff --git a/base/numerics/safe_numerics_unittest.cc b/base/numerics/safe_numerics_unittest.cc |
index c0cc5143ea446fec09a39ea9ce71d139149b97ee..4cddd4a7a764518fe1de7f4d510649925536a3cb 100644 |
--- a/base/numerics/safe_numerics_unittest.cc |
+++ b/base/numerics/safe_numerics_unittest.cc |
@@ -831,6 +831,24 @@ static_assert(std::is_same<decltype(TestOverload(StrictNumeric<size_t>())), |
size_t>::value, |
""); |
+template <typename T> |
+struct CastTest1 { |
+ static constexpr T HandleNaN() { return -1; } |
+ static constexpr T max() { return numeric_limits<T>::max() - 1; } |
+ static constexpr T HandleOverflow() { return max(); } |
+ static constexpr T lowest() { return numeric_limits<T>::lowest() + 1; } |
+ static constexpr T HandleUnderflow() { return lowest(); } |
+}; |
+ |
+template <typename T> |
+struct CastTest2 { |
+ static constexpr T HandleNaN() { return 11; } |
+ static constexpr T max() { return 10; } |
+ static constexpr T HandleOverflow() { return max(); } |
+ static constexpr T lowest() { return 1; } |
+ static constexpr T HandleUnderflow() { return lowest(); } |
+}; |
+ |
TEST(SafeNumerics, CastTests) { |
// MSVC catches and warns that we're forcing saturation in these tests. |
// Since that's intentional, we need to shut this warning off. |
@@ -935,15 +953,29 @@ TEST(SafeNumerics, CastTests) { |
EXPECT_EQ(1, checked_cast<int>(StrictNumeric<int>(1))); |
EXPECT_EQ(1, saturated_cast<int>(StrictNumeric<int>(1))); |
EXPECT_EQ(1, strict_cast<int>(StrictNumeric<int>(1))); |
-} |
-TEST(SafeNumerics, SaturatedCastChecks) { |
- float not_a_number = std::numeric_limits<float>::infinity() - |
- std::numeric_limits<float>::infinity(); |
- EXPECT_TRUE(std::isnan(not_a_number)); |
- EXPECT_DEATH_IF_SUPPORTED( |
- (saturated_cast<int, base::CheckOnFailure>(not_a_number)), |
- ""); |
+ // Test the saturated cast overrides. |
+ using FloatLimits = numeric_limits<float>; |
+ using IntLimits = numeric_limits<int>; |
+ EXPECT_EQ(-1, (saturated_cast<int, CastTest1>(FloatLimits::quiet_NaN()))); |
+ EXPECT_EQ(CastTest1<int>::max(), |
+ (saturated_cast<int, CastTest1>(FloatLimits::infinity()))); |
+ EXPECT_EQ(CastTest1<int>::max(), |
+ (saturated_cast<int, CastTest1>(FloatLimits::max()))); |
+ EXPECT_EQ(CastTest1<int>::max(), |
+ (saturated_cast<int, CastTest1>(float(IntLimits::max())))); |
+ EXPECT_EQ(CastTest1<int>::lowest(), |
+ (saturated_cast<int, CastTest1>(-FloatLimits::infinity()))); |
+ EXPECT_EQ(CastTest1<int>::lowest(), |
+ (saturated_cast<int, CastTest1>(FloatLimits::lowest()))); |
+ EXPECT_EQ(CastTest1<int>::lowest(), |
+ (saturated_cast<int, CastTest1>(float(IntLimits::lowest())))); |
+ EXPECT_EQ(11, (saturated_cast<int, CastTest2>(FloatLimits::quiet_NaN()))); |
+ EXPECT_EQ(10, (saturated_cast<int, CastTest2>(FloatLimits::infinity()))); |
+ EXPECT_EQ(10, (saturated_cast<int, CastTest2>(FloatLimits::max()))); |
+ EXPECT_EQ(1, (saturated_cast<int, CastTest2>(-FloatLimits::infinity()))); |
+ EXPECT_EQ(1, (saturated_cast<int, CastTest2>(FloatLimits::lowest()))); |
+ 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
|
} |
TEST(SafeNumerics, IsValueInRangeForNumericType) { |