Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ae669cd

Browse files
committed
unit tests for new optional<Rect> functions
1 parent be1cbf3 commit ae669cd

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

impeller/geometry/geometry_unittests.cc

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,6 +1644,58 @@ TEST(GeometryTest, RectUnion) {
16441644
}
16451645
}
16461646

1647+
TEST(GeometryTest, OptRectUnion) {
1648+
Rect a = Rect::MakeLTRB(0, 0, 100, 100);
1649+
Rect b = Rect::MakeLTRB(100, 100, 200, 200);
1650+
Rect c = Rect::MakeLTRB(100, 0, 200, 100);
1651+
1652+
// NullOpt, NullOpt
1653+
EXPECT_FALSE(Union(std::nullopt, std::nullopt).has_value());
1654+
EXPECT_EQ(Union(std::nullopt, std::nullopt), std::nullopt);
1655+
1656+
auto test1 = [](const Rect& r) {
1657+
// Rect, NullOpt
1658+
EXPECT_TRUE(Union(r, std::nullopt).has_value());
1659+
EXPECT_EQ(Union(r, std::nullopt).value(), r);
1660+
1661+
// OptRect, NullOpt
1662+
EXPECT_TRUE(Union(std::optional(r), std::nullopt).has_value());
1663+
EXPECT_EQ(Union(std::optional(r), std::nullopt).value(), r);
1664+
1665+
// NullOpt, Rect
1666+
EXPECT_TRUE(Union(std::nullopt, r).has_value());
1667+
EXPECT_EQ(Union(std::nullopt, r).value(), r);
1668+
1669+
// NullOpt, OptRect
1670+
EXPECT_TRUE(Union(std::nullopt, std::optional(r)).has_value());
1671+
EXPECT_EQ(Union(std::nullopt, std::optional(r)).value(), r);
1672+
};
1673+
1674+
test1(a);
1675+
test1(b);
1676+
test1(c);
1677+
1678+
auto test2 = [](const Rect& a, const Rect& b, const Rect& u) {
1679+
ASSERT_EQ(a.Union(b), u);
1680+
1681+
// Rect, OptRect
1682+
EXPECT_TRUE(Union(a, std::optional(b)).has_value());
1683+
EXPECT_EQ(Union(a, std::optional(b)).value(), u);
1684+
1685+
// OptRect, Rect
1686+
EXPECT_TRUE(Union(std::optional(a), b).has_value());
1687+
EXPECT_EQ(Union(std::optional(a), b).value(), u);
1688+
1689+
// OptRect, OptRect
1690+
EXPECT_TRUE(Union(std::optional(a), std::optional(b)).has_value());
1691+
EXPECT_EQ(Union(std::optional(a), std::optional(b)).value(), u);
1692+
};
1693+
1694+
test2(a, b, Rect::MakeLTRB(0, 0, 200, 200));
1695+
test2(a, c, Rect::MakeLTRB(0, 0, 200, 100));
1696+
test2(b, c, Rect::MakeLTRB(100, 0, 200, 200));
1697+
}
1698+
16471699
TEST(GeometryTest, RectIntersection) {
16481700
{
16491701
Rect a(100, 100, 100, 100);
@@ -1693,6 +1745,58 @@ TEST(GeometryTest, RectIntersection) {
16931745
}
16941746
}
16951747

1748+
TEST(GeometryTest, OptRectIntersection) {
1749+
Rect a = Rect::MakeLTRB(0, 0, 110, 110);
1750+
Rect b = Rect::MakeLTRB(100, 100, 200, 200);
1751+
Rect c = Rect::MakeLTRB(100, 0, 200, 110);
1752+
1753+
// NullOpt, NullOpt
1754+
EXPECT_FALSE(Intersection(std::nullopt, std::nullopt).has_value());
1755+
EXPECT_EQ(Intersection(std::nullopt, std::nullopt), std::nullopt);
1756+
1757+
auto test1 = [](const Rect& r) {
1758+
// Rect, NullOpt
1759+
EXPECT_TRUE(Intersection(r, std::nullopt).has_value());
1760+
EXPECT_EQ(Intersection(r, std::nullopt).value(), r);
1761+
1762+
// OptRect, NullOpt
1763+
EXPECT_TRUE(Intersection(std::optional(r), std::nullopt).has_value());
1764+
EXPECT_EQ(Intersection(std::optional(r), std::nullopt).value(), r);
1765+
1766+
// NullOpt, Rect
1767+
EXPECT_TRUE(Intersection(std::nullopt, r).has_value());
1768+
EXPECT_EQ(Intersection(std::nullopt, r).value(), r);
1769+
1770+
// NullOpt, OptRect
1771+
EXPECT_TRUE(Intersection(std::nullopt, std::optional(r)).has_value());
1772+
EXPECT_EQ(Intersection(std::nullopt, std::optional(r)).value(), r);
1773+
};
1774+
1775+
test1(a);
1776+
test1(b);
1777+
test1(c);
1778+
1779+
auto test2 = [](const Rect& a, const Rect& b, const Rect& i) {
1780+
ASSERT_EQ(a.Intersection(b), i);
1781+
1782+
// Rect, OptRect
1783+
EXPECT_TRUE(Intersection(a, std::optional(b)).has_value());
1784+
EXPECT_EQ(Intersection(a, std::optional(b)).value(), i);
1785+
1786+
// OptRect, Rect
1787+
EXPECT_TRUE(Intersection(std::optional(a), b).has_value());
1788+
EXPECT_EQ(Intersection(std::optional(a), b).value(), i);
1789+
1790+
// OptRect, OptRect
1791+
EXPECT_TRUE(Intersection(std::optional(a), std::optional(b)).has_value());
1792+
EXPECT_EQ(Intersection(std::optional(a), std::optional(b)).value(), i);
1793+
};
1794+
1795+
test2(a, b, Rect::MakeLTRB(100, 100, 110, 110));
1796+
test2(a, c, Rect::MakeLTRB(100, 0, 110, 110));
1797+
test2(b, c, Rect::MakeLTRB(100, 100, 200, 110));
1798+
}
1799+
16961800
TEST(GeometryTest, RectIntersectsWithRect) {
16971801
{
16981802
Rect a(100, 100, 100, 100);

0 commit comments

Comments
 (0)