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

Unified Diff: ui/gfx/geometry/safe_integer_conversions_unittest.cc

Issue 2354783004: Fix overflow/underflow in gfx geometry once and for all (Closed)
Patch Set: Remove accessibility test changes Created 4 years, 2 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 | « ui/gfx/geometry/safe_integer_conversions.h ('k') | ui/gfx/geometry/size.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/geometry/safe_integer_conversions_unittest.cc
diff --git a/ui/gfx/geometry/safe_integer_conversions_unittest.cc b/ui/gfx/geometry/safe_integer_conversions_unittest.cc
index 91bdbb8d3593020fc58df008040640b44cbb0d20..f197a281d7393cf6c09075659992213c7adf2a1f 100644
--- a/ui/gfx/geometry/safe_integer_conversions_unittest.cc
+++ b/ui/gfx/geometry/safe_integer_conversions_unittest.cc
@@ -77,4 +77,72 @@ TEST(SafeIntegerConversions, ToRoundedInt) {
EXPECT_EQ(int_min, ToRoundedInt(min - 100));
}
+TEST(SafeIntegerConversions, IntegerOverflow) {
+ int int_max = std::numeric_limits<int>::max();
+ int int_min = std::numeric_limits<int>::min();
+
+ EXPECT_TRUE(AddWouldOverflow(int_max, 1));
+ EXPECT_TRUE(AddWouldOverflow(int_max, int_max));
+ EXPECT_TRUE(AddWouldOverflow(int_max - 1000, 1001));
+ EXPECT_FALSE(AddWouldOverflow(int_max, 0));
+ EXPECT_FALSE(AddWouldOverflow(1, 2));
+ EXPECT_FALSE(AddWouldOverflow(1, -1));
+ EXPECT_FALSE(AddWouldOverflow(int_min, int_max));
+ EXPECT_FALSE(AddWouldOverflow(int_max - 1000, 1000));
+
+ EXPECT_TRUE(AddWouldUnderflow(int_min, -1));
+ EXPECT_TRUE(AddWouldUnderflow(int_min, int_min));
+ EXPECT_TRUE(AddWouldUnderflow(int_min + 1000, -1001));
+ EXPECT_FALSE(AddWouldUnderflow(int_min, 0));
+ EXPECT_FALSE(AddWouldUnderflow(1, 2));
+ EXPECT_FALSE(AddWouldUnderflow(1, -1));
+ EXPECT_FALSE(AddWouldUnderflow(int_min, int_max));
+ EXPECT_FALSE(AddWouldUnderflow(int_min + 1000, -1000));
+
+ EXPECT_TRUE(SubtractWouldOverflow(int_max, -1));
+ EXPECT_TRUE(SubtractWouldOverflow(int_max, int_min));
+ EXPECT_TRUE(SubtractWouldOverflow(int_max - 1000, -1001));
+ EXPECT_TRUE(SubtractWouldOverflow(0, int_min));
+ EXPECT_FALSE(SubtractWouldOverflow(int_max, 0));
+ EXPECT_FALSE(SubtractWouldOverflow(1, 2));
+ EXPECT_FALSE(SubtractWouldOverflow(-1, 1));
+ EXPECT_FALSE(SubtractWouldOverflow(int_min, int_min));
+ EXPECT_FALSE(SubtractWouldOverflow(int_max - 1000, -1000));
+ EXPECT_FALSE(SubtractWouldOverflow(-1, int_min));
+
+ EXPECT_TRUE(SubtractWouldUnderflow(int_min, 1));
+ EXPECT_TRUE(SubtractWouldUnderflow(int_min, int_max));
+ EXPECT_TRUE(SubtractWouldUnderflow(int_min + 1000, 1001));
+ EXPECT_FALSE(SubtractWouldUnderflow(int_min, 0));
+ EXPECT_FALSE(SubtractWouldUnderflow(1, 2));
+ EXPECT_FALSE(SubtractWouldUnderflow(-1, -1));
+ EXPECT_FALSE(SubtractWouldUnderflow(int_max, int_max));
+ EXPECT_FALSE(SubtractWouldUnderflow(int_min + 1000, -1000));
+
+ EXPECT_EQ(SafeAdd(0, 0), 0);
+ EXPECT_EQ(SafeAdd(1, 2), 3);
+ EXPECT_EQ(SafeAdd(int_max, 0), int_max);
+ EXPECT_EQ(SafeAdd(int_max, 1), int_max);
+ EXPECT_EQ(SafeAdd(int_max, int_max), int_max);
+ EXPECT_EQ(SafeAdd(int_max, int_min), -1);
+ EXPECT_EQ(SafeAdd(int_min, 1), int_min + 1);
+ EXPECT_EQ(SafeAdd(int_min, -1), int_min);
+ EXPECT_EQ(SafeAdd(int_min, 0), int_min);
+ EXPECT_EQ(SafeAdd(int_min, int_min), int_min);
+
+ EXPECT_EQ(SafeSubtract(0, 0), 0);
+ EXPECT_EQ(SafeSubtract(3, 2), 1);
+ EXPECT_EQ(SafeSubtract(int_max, 0), int_max);
+ EXPECT_EQ(SafeSubtract(int_max, 1), int_max - 1);
+ EXPECT_EQ(SafeSubtract(int_max, -1), int_max);
+ EXPECT_EQ(SafeSubtract(int_max, int_min), int_max);
+ EXPECT_EQ(SafeSubtract(int_min, 0), int_min);
+ EXPECT_EQ(SafeSubtract(int_min, -1), int_min + 1);
+ EXPECT_EQ(SafeSubtract(int_min, 1), int_min);
+ EXPECT_EQ(SafeSubtract(int_min, int_min), 0);
+ EXPECT_EQ(SafeSubtract(int_max, int_max), 0);
+ EXPECT_EQ(SafeSubtract(0, int_min), int_max);
+ EXPECT_EQ(SafeSubtract(-1, int_min), int_max);
+}
+
} // namespace gfx
« no previous file with comments | « ui/gfx/geometry/safe_integer_conversions.h ('k') | ui/gfx/geometry/size.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698