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

Unified Diff: cc/base/math_util_unittest.cc

Issue 1266103005: cc: Handle overflow/underflow in MathUtil::RoundUp/RoundDown functions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Small nit. Created 5 years, 4 months 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 | « cc/base/math_util.h ('k') | cc/layers/picture_layer_impl.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/base/math_util_unittest.cc
diff --git a/cc/base/math_util_unittest.cc b/cc/base/math_util_unittest.cc
index 57aede76e1f6d6876b299b4412d152aa6cbfad4b..e55c721dc883db40dee6e5a7184bbe7d2411995d 100644
--- a/cc/base/math_util_unittest.cc
+++ b/cc/base/math_util_unittest.cc
@@ -227,7 +227,7 @@ TEST(MathUtilTest, RoundUp) {
for (int attempt = 5 * multiplier; attempt >= -5 * multiplier; --attempt) {
if ((attempt % multiplier) == 0)
correct = attempt;
- EXPECT_EQ(correct, MathUtil::RoundUp(attempt, multiplier))
+ EXPECT_EQ(correct, MathUtil::UncheckedRoundUp(attempt, multiplier))
<< "attempt=" << attempt << " multiplier=" << multiplier;
}
}
@@ -239,14 +239,20 @@ TEST(MathUtilTest, RoundUp) {
for (unsigned attempt = 5 * multiplier; attempt > 0; --attempt) {
if ((attempt % multiplier) == 0)
correct = attempt;
- EXPECT_EQ(correct, MathUtil::RoundUp(attempt, multiplier))
+ EXPECT_EQ(correct, MathUtil::UncheckedRoundUp(attempt, multiplier))
<< "attempt=" << attempt << " multiplier=" << multiplier;
}
- EXPECT_EQ(0u, MathUtil::RoundUp(0u, multiplier))
+ EXPECT_EQ(0u, MathUtil::UncheckedRoundUp(0u, multiplier))
<< "attempt=0 multiplier=" << multiplier;
}
}
+TEST(MathUtilTest, RoundUpOverFlow) {
+ // Rounding up 123 by 50 is 150, which overflows int8_t, but fits in uint8_t.
+ EXPECT_FALSE(MathUtil::VerifyRoundup<int8_t>(123, 50));
+ EXPECT_TRUE(MathUtil::VerifyRoundup<uint8_t>(123, 50));
+}
+
TEST(MathUtilTest, RoundDown) {
for (int multiplier = 1; multiplier <= 10; ++multiplier) {
// Try attempts in ascending order, so that we can
@@ -255,7 +261,7 @@ TEST(MathUtilTest, RoundDown) {
for (int attempt = -5 * multiplier; attempt <= 5 * multiplier; ++attempt) {
if ((attempt % multiplier) == 0)
correct = attempt;
- EXPECT_EQ(correct, MathUtil::RoundDown(attempt, multiplier))
+ EXPECT_EQ(correct, MathUtil::UncheckedRoundDown(attempt, multiplier))
<< "attempt=" << attempt << " multiplier=" << multiplier;
}
}
@@ -267,11 +273,18 @@ TEST(MathUtilTest, RoundDown) {
for (unsigned attempt = 0; attempt <= 5 * multiplier; ++attempt) {
if ((attempt % multiplier) == 0)
correct = attempt;
- EXPECT_EQ(correct, MathUtil::RoundDown(attempt, multiplier))
+ EXPECT_EQ(correct, MathUtil::UncheckedRoundDown(attempt, multiplier))
<< "attempt=" << attempt << " multiplier=" << multiplier;
}
}
}
+TEST(MathUtilTest, RoundDownUnderFlow) {
+ // Rounding down -123 by 50 is -150, which underflows int8_t, but fits in
+ // int16_t.
+ EXPECT_FALSE(MathUtil::VerifyRoundDown<int8_t>(-123, 50));
+ EXPECT_TRUE(MathUtil::VerifyRoundDown<int16_t>(-123, 50));
+}
+
} // namespace
} // namespace cc
« no previous file with comments | « cc/base/math_util.h ('k') | cc/layers/picture_layer_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698