From e95f5d37661f9e903e71614f33bad71bda8377e3 Mon Sep 17 00:00:00 2001 From: Niel Lebeck Date: Mon, 10 Jun 2024 18:46:03 -0700 Subject: [PATCH] Add some unit tests for `Rectangle::ClampUL` --- Source/UnitTests/Common/MathUtilTest.cpp | 54 +++++++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/Source/UnitTests/Common/MathUtilTest.cpp b/Source/UnitTests/Common/MathUtilTest.cpp index 7758bb5d7f..f759dc5b1c 100644 --- a/Source/UnitTests/Common/MathUtilTest.cpp +++ b/Source/UnitTests/Common/MathUtilTest.cpp @@ -187,5 +187,55 @@ TEST(MathUtil, RectangleGetHeightUnsigned) EXPECT_EQ(rect_e.GetHeight(), u32{0xFFFFFFF8}); } -// TODO: Add unit test coverage for `Rectangle::ClampUL`. (And consider removing -// `Rectangle::ClampLL`, which does not have any callers.) +TEST(MathUtil, RectangleClampUL) +{ + // Clamp in one direction + MathUtil::Rectangle rect_a(2, 2, 5, 6); + rect_a.ClampUL(3, 1, 6, 7); + EXPECT_EQ(rect_a, MathUtil::Rectangle(3, 2, 5, 6)); + + MathUtil::Rectangle rect_b(2, 2, 5, 6); + rect_b.ClampUL(1, 3, 6, 7); + EXPECT_EQ(rect_b, MathUtil::Rectangle(2, 3, 5, 6)); + + MathUtil::Rectangle rect_c(2, 2, 5, 6); + rect_c.ClampUL(1, 1, 4, 7); + EXPECT_EQ(rect_c, MathUtil::Rectangle(2, 2, 4, 6)); + + MathUtil::Rectangle rect_d(2, 2, 5, 6); + rect_d.ClampUL(1, 1, 6, 5); + EXPECT_EQ(rect_d, MathUtil::Rectangle(2, 2, 5, 5)); + + // Clamp in two directions + MathUtil::Rectangle rect_e(2, 2, 5, 6); + rect_e.ClampUL(4, 3, 6, 7); + EXPECT_EQ(rect_e, MathUtil::Rectangle(4, 3, 5, 6)); + + MathUtil::Rectangle rect_f(2, 2, 5, 6); + rect_f.ClampUL(1, 1, 4, 4); + EXPECT_EQ(rect_f, MathUtil::Rectangle(2, 2, 4, 4)); + + // Given rectangle contains this rectangle + MathUtil::Rectangle rect_g(2, 2, 5, 6); + rect_g.ClampUL(1, 1, 6, 7); + EXPECT_EQ(rect_g, MathUtil::Rectangle(2, 2, 5, 6)); + + // Given rectangle lies entirely within this rectangle + MathUtil::Rectangle rect_h(2, 2, 5, 6); + rect_h.ClampUL(3, 3, 4, 4); + EXPECT_EQ(rect_h, MathUtil::Rectangle(3, 3, 4, 4)); + + // Given rectangle exactly matches this rectangle + MathUtil::Rectangle rect_i(2, 2, 5, 6); + rect_i.ClampUL(2, 2, 5, 6); + EXPECT_EQ(rect_i, MathUtil::Rectangle(2, 2, 5, 6)); + + // Given rectangle does not intersect this rectangle at all + MathUtil::Rectangle rect_j(2, 2, 5, 6); + rect_j.ClampUL(7, 8, 10, 11); + EXPECT_EQ(rect_j, MathUtil::Rectangle(7, 8, 7, 8)); + + MathUtil::Rectangle rect_k(2, 2, 5, 6); + rect_k.ClampUL(-1, -1, 1, 0); + EXPECT_EQ(rect_k, MathUtil::Rectangle(1, 0, 1, 0)); +}