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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/numerics/safe_conversions_impl.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..0e3d999b2f5270872aa203fdd01843179a4a684b 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.
@@ -893,6 +911,35 @@ TEST(SafeNumerics, CastTests) {
saturated_cast<int>(double_small_int));
EXPECT_EQ(numeric_limits<int>::max(), saturated_cast<int>(double_large_int));
+ // 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(0, (saturated_cast<int, CastTest1>(0.0)));
+ EXPECT_EQ(1, (saturated_cast<int, CastTest1>(1.0)));
+ EXPECT_EQ(-1, (saturated_cast<int, CastTest1>(-1.0)));
+ EXPECT_EQ(0, (saturated_cast<int, CastTest1>(0)));
+ EXPECT_EQ(1, (saturated_cast<int, CastTest1>(1)));
+ EXPECT_EQ(-1, (saturated_cast<int, CastTest1>(-1)));
+ 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)));
+
float not_a_number = std::numeric_limits<float>::infinity() -
std::numeric_limits<float>::infinity();
EXPECT_TRUE(std::isnan(not_a_number));
@@ -937,15 +984,6 @@ TEST(SafeNumerics, CastTests) {
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(SafeNumerics, IsValueInRangeForNumericType) {
EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(0));
EXPECT_TRUE(IsValueInRangeForNumericType<uint32_t>(1));
« 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