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