Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 690fa613 authored by Vishnu Nair's avatar Vishnu Nair Committed by Android (Google) Code Review
Browse files

Merge "Fix rounding error when constructing Rect from FloatRect" into main

parents e29f2f5e dfee5a2c
Loading
Loading
Loading
Loading
+4 −6
Original line number Diff line number Diff line
@@ -74,12 +74,10 @@ public:
    }

    inline explicit Rect(const FloatRect& floatRect) {
        // Ideally we would use std::round, but we don't want to add an STL
        // dependency here, so we use an approximation
        left = static_cast<int32_t>(floatRect.left + 0.5f);
        top = static_cast<int32_t>(floatRect.top + 0.5f);
        right = static_cast<int32_t>(floatRect.right + 0.5f);
        bottom = static_cast<int32_t>(floatRect.bottom + 0.5f);
        left = static_cast<int32_t>(std::round(floatRect.left));
        top = static_cast<int32_t>(std::round(floatRect.top));
        right = static_cast<int32_t>(std::round(floatRect.right));
        bottom = static_cast<int32_t>(std::round(floatRect.bottom));
    }

    inline explicit Rect(const ui::Size& size) {
+10 −0
Original line number Diff line number Diff line
@@ -99,6 +99,16 @@ TEST(RectTest, constructFromFloatRect) {
        EXPECT_EQ(30, rect.right);
        EXPECT_EQ(40, rect.bottom);
    }

    EXPECT_EQ(Rect(0, 1, -1, 0), Rect(FloatRect(0.f, 1.f, -1.f, 0.f)));
    EXPECT_EQ(Rect(100000, 100000, -100000, -100000),
              Rect(FloatRect(100000.f, 100000.f, -100000.f, -100000.f)));

    // round down if < .5
    EXPECT_EQ(Rect(0, 1, -1, 0), Rect(FloatRect(0.4f, 1.1f, -1.499f, 0.1f)));

    // round up if >= .5
    EXPECT_EQ(Rect(20, 20, -20, -20), Rect(FloatRect(19.5f, 19.9f, -19.5f, -19.9f)));
}

TEST(RectTest, makeInvalid) {