1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2021 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "experimental/graphite/src/geom/Rect.h"
9cb93a386Sopenharmony_ci#include "tests/Test.h"
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_cinamespace skgpu {
12cb93a386Sopenharmony_ci
13cb93a386Sopenharmony_ci#define CHECK(A) REPORTER_ASSERT(reporter, A)
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ciDEF_GRAPHITE_TEST(skgpu_Rect, reporter) {
16cb93a386Sopenharmony_ci    const SkRect skRect = SkRect::MakeLTRB(1,-3,4,0);
17cb93a386Sopenharmony_ci    const Rect rect = skRect;
18cb93a386Sopenharmony_ci    CHECK(rect == rect);
19cb93a386Sopenharmony_ci    CHECK(rect == skRect); // promotes 'skRect' to a Rect for ==
20cb93a386Sopenharmony_ci    CHECK(rect.asSkRect() == skRect); // converts 'rect' to SkRect for ==
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_ci    for (const float l : {0,1,2}) {
23cb93a386Sopenharmony_ci    for (const float t : {-4,-3,-2}) {
24cb93a386Sopenharmony_ci    for (const float r : {3,4,5}) {
25cb93a386Sopenharmony_ci    for (const float b : {-1,0,1}) {
26cb93a386Sopenharmony_ci        const Rect rect2(l,t,r,b);
27cb93a386Sopenharmony_ci        const SkRect skRect2{l,t,r,b};
28cb93a386Sopenharmony_ci
29cb93a386Sopenharmony_ci        CHECK(rect2 == rect2);
30cb93a386Sopenharmony_ci        CHECK(rect2 == Rect(float2(l,t), float2(r,b)));
31cb93a386Sopenharmony_ci        CHECK(rect2 == Rect(skRect2));
32cb93a386Sopenharmony_ci        CHECK(rect2.asSkRect() == skRect2);
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ci        CHECK((rect2 == rect) == (rect == rect2));
35cb93a386Sopenharmony_ci        CHECK((rect2 != rect) == (rect != rect2));
36cb93a386Sopenharmony_ci        CHECK((rect != rect2) == !(rect == rect2));
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ci        CHECK(rect2 == Rect::XYWH(l, t, r - l, b - t));
39cb93a386Sopenharmony_ci        CHECK(rect2 == Rect::XYWH(float2(l, t), float2(r - l, b - t)));
40cb93a386Sopenharmony_ci        if (l == 0 && t == 0) {
41cb93a386Sopenharmony_ci            CHECK(rect2 == Rect::WH(r - l, b - t));
42cb93a386Sopenharmony_ci            CHECK(rect2 == Rect::WH(float2(r - l, b - t)));
43cb93a386Sopenharmony_ci        }
44cb93a386Sopenharmony_ci        CHECK(rect2 == Rect::FromVals(rect2.vals()));
45cb93a386Sopenharmony_ci
46cb93a386Sopenharmony_ci        CHECK(rect2.x() == l);
47cb93a386Sopenharmony_ci        CHECK(rect2.y() == t);
48cb93a386Sopenharmony_ci        CHECK(rect2.left() == l);
49cb93a386Sopenharmony_ci        CHECK(rect2.top() == t);
50cb93a386Sopenharmony_ci        CHECK(rect2.right() == r);
51cb93a386Sopenharmony_ci        CHECK(rect2.bot() == b);
52cb93a386Sopenharmony_ci        CHECK(all(rect2.topLeft() == float2(l,t)));
53cb93a386Sopenharmony_ci        CHECK(all(rect2.botRight() == float2(r,b)));
54cb93a386Sopenharmony_ci        CHECK(all(rect2.ltrb() == float4(l,t,r,b)));
55cb93a386Sopenharmony_ci        CHECK(all(rect2.vals() == float4(l,t,-r,-b)));
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci        Rect setTest(-99,-99,99,99);
58cb93a386Sopenharmony_ci        CHECK(setTest != rect2);
59cb93a386Sopenharmony_ci        setTest.setLeft(l);
60cb93a386Sopenharmony_ci        setTest.setTop(t);
61cb93a386Sopenharmony_ci        setTest.setRight(r);
62cb93a386Sopenharmony_ci        setTest.setBot(b);
63cb93a386Sopenharmony_ci        CHECK(setTest == rect2);
64cb93a386Sopenharmony_ci
65cb93a386Sopenharmony_ci        setTest = Rect(-99,-99,99,99);
66cb93a386Sopenharmony_ci        CHECK(setTest != rect2);
67cb93a386Sopenharmony_ci        setTest.setTopLeft({l,t});
68cb93a386Sopenharmony_ci        setTest.setBotRight({r,b});
69cb93a386Sopenharmony_ci        CHECK(setTest == rect2);
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_ci        for (int i = 0; i < 4; ++i) {
72cb93a386Sopenharmony_ci            Rect rnan = rect2;
73cb93a386Sopenharmony_ci            CHECK(!rnan.isEmptyNegativeOrNaN());
74cb93a386Sopenharmony_ci            rnan.vals()[i] = std::numeric_limits<float>::quiet_NaN();
75cb93a386Sopenharmony_ci            CHECK(rnan.isEmptyNegativeOrNaN());
76cb93a386Sopenharmony_ci        }
77cb93a386Sopenharmony_ci
78cb93a386Sopenharmony_ci        CHECK(all(rect2.size() == float2(skRect2.width(), skRect2.height())));
79cb93a386Sopenharmony_ci        CHECK(all(rect2.center() == float2(skRect2.centerX(), skRect2.centerY())));
80cb93a386Sopenharmony_ci        CHECK(rect2.area() == skRect2.height() * skRect2.width());
81cb93a386Sopenharmony_ci
82cb93a386Sopenharmony_ci        CHECK(rect.intersects(rect2) == rect2.intersects(rect));
83cb93a386Sopenharmony_ci        CHECK(rect.intersects(rect2) == skRect.intersects(skRect2));
84cb93a386Sopenharmony_ci        CHECK(rect.contains(rect2) == skRect.contains(skRect2));
85cb93a386Sopenharmony_ci        CHECK(rect2.contains(rect) == skRect2.contains(skRect));
86cb93a386Sopenharmony_ci
87cb93a386Sopenharmony_ci        CHECK(rect2.makeRoundIn() == SkRect::Make(skRect2.roundIn()));
88cb93a386Sopenharmony_ci        CHECK(rect2.makeRoundOut() == SkRect::Make(skRect2.roundOut()));
89cb93a386Sopenharmony_ci        CHECK(rect2.makeInset(.5f) == skRect2.makeInset(.5f, .5f));
90cb93a386Sopenharmony_ci        CHECK(rect2.makeInset({.5f, -.25f}) == skRect2.makeInset(.5f, -.25f));
91cb93a386Sopenharmony_ci        CHECK(rect2.makeOutset(.5f) == skRect2.makeOutset(.5f, .5f));
92cb93a386Sopenharmony_ci        CHECK(rect2.makeOutset({.5f, -.25f}) == skRect2.makeOutset(.5f, -.25f));
93cb93a386Sopenharmony_ci        CHECK(rect2.makeOffset({.5f, -.25f}) == skRect2.makeOffset(.5f, -.25f));
94cb93a386Sopenharmony_ci
95cb93a386Sopenharmony_ci        SkRect skJoin = skRect;
96cb93a386Sopenharmony_ci        skJoin.join(skRect2);
97cb93a386Sopenharmony_ci        CHECK(rect.makeJoin(rect2) == skJoin);
98cb93a386Sopenharmony_ci        CHECK(rect.makeJoin(rect2) == rect2.makeJoin(rect));
99cb93a386Sopenharmony_ci
100cb93a386Sopenharmony_ci        CHECK(rect.intersects(rect2) == !rect.makeIntersect(rect2).isEmptyNegativeOrNaN());
101cb93a386Sopenharmony_ci        CHECK(rect.makeIntersect(rect2) == rect2.makeIntersect(rect));
102cb93a386Sopenharmony_ci        if (rect.intersects(rect2)) {
103cb93a386Sopenharmony_ci            CHECK(skRect.intersects(skRect2));
104cb93a386Sopenharmony_ci            SkRect skIsect;
105cb93a386Sopenharmony_ci            CHECK(skIsect.intersect(skRect, skRect2));
106cb93a386Sopenharmony_ci            CHECK(rect.makeIntersect(rect2) == Rect(skIsect));
107cb93a386Sopenharmony_ci        }
108cb93a386Sopenharmony_ci
109cb93a386Sopenharmony_ci        const Rect rect3{r,b,l,t}; // intentionally out of order
110cb93a386Sopenharmony_ci        const SkRect skRect3{r,b,l,t};
111cb93a386Sopenharmony_ci        CHECK(rect3.isEmptyNegativeOrNaN());
112cb93a386Sopenharmony_ci        CHECK(skRect3.isEmpty());
113cb93a386Sopenharmony_ci        CHECK(rect3.makeSorted() == skRect3.makeSorted());
114cb93a386Sopenharmony_ci        CHECK(rect3.makeSorted() == rect2);
115cb93a386Sopenharmony_ci    }}}}
116cb93a386Sopenharmony_ci}
117cb93a386Sopenharmony_ci
118cb93a386Sopenharmony_ci}  // namespace skgpu
119