Flutter Engine
The Flutter Engine
RectTest.cpp
Go to the documentation of this file.
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
9#include "tests/Test.h"
10
11namespace skgpu::graphite {
12
13#define CHECK(A) REPORTER_ASSERT(reporter, A)
14
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;
77 rnan.vals()[i] = std::numeric_limits<float>::quiet_NaN();
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::graphite
reporter
Definition: FontMgrTest.cpp:39
skvx::float2 float2
AI void setBot(float bot)
Definition: Rect.h:87
static AI Rect XYWH(float x, float y, float w, float h)
Definition: Rect.h:40
AI bool contains(Rect rect) const
Definition: Rect.h:127
AI void setTop(float top)
Definition: Rect.h:85
AI float bot() const
Definition: Rect.h:79
AI Rect makeIntersect(Rect rect) const
Definition: Rect.h:140
AI Rect makeRoundIn() const
Definition: Rect.h:132
AI float top() const
Definition: Rect.h:77
AI bool isEmptyNegativeOrNaN() const
Definition: Rect.h:102
AI float left() const
Definition: Rect.h:76
AI Rect makeOutset(float outset) const
Definition: Rect.h:136
static AI Rect WH(float w, float h)
Definition: Rect.h:46
AI float y() const
Definition: Rect.h:75
AI void setTopLeft(float2 topLeft)
Definition: Rect.h:88
AI const float4 & vals() const
Definition: Rect.h:71
AI float area() const
Definition: Rect.h:114
AI SkRect asSkRect() const
Definition: Rect.h:91
AI float2 topLeft() const
Definition: Rect.h:80
AI float2 center() const
Definition: Rect.h:109
AI Rect makeInset(float inset) const
Definition: Rect.h:134
AI float2 size() const
Definition: Rect.h:107
static AI Rect FromVals(float4 vals)
Definition: Rect.h:55
AI bool intersects(ComplementRect comp) const
Definition: Rect.h:126
AI void setLeft(float left)
Definition: Rect.h:84
AI Rect makeJoin(Rect rect) const
Definition: Rect.h:139
AI float2 botRight() const
Definition: Rect.h:81
AI Rect makeOffset(float2 offset) const
Definition: Rect.h:138
AI Rect makeRoundOut() const
Definition: Rect.h:133
AI void setRight(float right)
Definition: Rect.h:86
AI float4 ltrb() const
Definition: Rect.h:82
AI float right() const
Definition: Rect.h:78
AI void setBotRight(float2 botRight)
Definition: Rect.h:89
AI float x() const
Definition: Rect.h:74
static bool b
#define CHECK(A)
Definition: RectTest.cpp:13
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
TRect< Scalar > Rect
Definition: rect.h:769
DEF_GRAPHITE_TEST(skgpu_IntersectionTree, reporter, CtsEnforcement::kNextRelease)
Vec< 4, float > float4
Definition: SkVx.h:1146
SIT bool all(const Vec< 1, T > &x)
Definition: SkVx.h:582
Vec< 2, float > float2
Definition: SkVx.h:1145
static SkRect Make(const SkISize &size)
Definition: SkRect.h:669
bool intersect(const SkRect &r)
Definition: SkRect.cpp:114
bool intersects(const SkRect &r) const
Definition: SkRect.h:1121
bool contains(SkScalar x, SkScalar y) const
Definition: extension.cpp:19
void join(const SkRect &r)
Definition: SkRect.cpp:126
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646
Definition: SkVx.h:83