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