Flutter Engine
The Flutter Engine
range_unittest.cc
Go to the documentation of this file.
1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <sstream>
6
7#include "gtest/gtest.h"
8#include "range.h"
9
10namespace {
11
12template <typename T>
13class RangeTest : public testing::Test {};
14
15typedef testing::Types<gfx::Range> RangeTypes;
16TYPED_TEST_SUITE(RangeTest, RangeTypes);
17
18template <typename T>
19void TestContainsAndIntersects(const T& r1, const T& r2, const T& r3) {
20 EXPECT_TRUE(r1.Intersects(r1));
21 EXPECT_TRUE(r1.Contains(r1));
22 EXPECT_EQ(T(10, 12), r1.Intersect(r1));
23
24 EXPECT_FALSE(r1.Intersects(r2));
25 EXPECT_FALSE(r1.Contains(r2));
26 EXPECT_TRUE(r1.Intersect(r2).is_empty());
27 EXPECT_FALSE(r2.Intersects(r1));
28 EXPECT_FALSE(r2.Contains(r1));
29 EXPECT_TRUE(r2.Intersect(r1).is_empty());
30
31 EXPECT_TRUE(r1.Intersects(r3));
32 EXPECT_TRUE(r3.Intersects(r1));
33 EXPECT_TRUE(r3.Contains(r1));
34 EXPECT_FALSE(r1.Contains(r3));
35 EXPECT_EQ(T(10, 12), r1.Intersect(r3));
36 EXPECT_EQ(T(10, 12), r3.Intersect(r1));
37
38 EXPECT_TRUE(r2.Intersects(r3));
39 EXPECT_TRUE(r3.Intersects(r2));
40 EXPECT_FALSE(r3.Contains(r2));
41 EXPECT_FALSE(r2.Contains(r3));
42 EXPECT_EQ(T(5, 8), r2.Intersect(r3));
43 EXPECT_EQ(T(5, 8), r3.Intersect(r2));
44}
45
46} // namespace
47
48TYPED_TEST(RangeTest, EmptyInit) {
49 TypeParam r;
50 EXPECT_EQ(0U, r.start());
51 EXPECT_EQ(0U, r.end());
52 EXPECT_EQ(0U, r.length());
53 EXPECT_FALSE(r.is_reversed());
54 EXPECT_TRUE(r.is_empty());
55 EXPECT_TRUE(r.IsValid());
56 EXPECT_EQ(0U, r.GetMin());
57 EXPECT_EQ(0U, r.GetMax());
58}
59
60TYPED_TEST(RangeTest, StartEndInit) {
61 TypeParam r(10, 15);
62 EXPECT_EQ(10U, r.start());
63 EXPECT_EQ(15U, r.end());
64 EXPECT_EQ(5U, r.length());
65 EXPECT_FALSE(r.is_reversed());
66 EXPECT_FALSE(r.is_empty());
67 EXPECT_TRUE(r.IsValid());
68 EXPECT_EQ(10U, r.GetMin());
69 EXPECT_EQ(15U, r.GetMax());
70}
71
72TYPED_TEST(RangeTest, StartEndReversedInit) {
73 TypeParam r(10, 5);
74 EXPECT_EQ(10U, r.start());
75 EXPECT_EQ(5U, r.end());
76 EXPECT_EQ(5U, r.length());
77 EXPECT_TRUE(r.is_reversed());
78 EXPECT_FALSE(r.is_empty());
79 EXPECT_TRUE(r.IsValid());
80 EXPECT_EQ(5U, r.GetMin());
81 EXPECT_EQ(10U, r.GetMax());
82}
83
84TYPED_TEST(RangeTest, PositionInit) {
85 TypeParam r(12);
86 EXPECT_EQ(12U, r.start());
87 EXPECT_EQ(12U, r.end());
88 EXPECT_EQ(0U, r.length());
89 EXPECT_FALSE(r.is_reversed());
90 EXPECT_TRUE(r.is_empty());
91 EXPECT_TRUE(r.IsValid());
92 EXPECT_EQ(12U, r.GetMin());
93 EXPECT_EQ(12U, r.GetMax());
94}
95
96TYPED_TEST(RangeTest, InvalidRange) {
97 TypeParam r(TypeParam::InvalidRange());
98 EXPECT_EQ(0U, r.length());
99 EXPECT_EQ(r.start(), r.end());
100 EXPECT_EQ(r.GetMax(), r.GetMin());
101 EXPECT_FALSE(r.is_reversed());
102 EXPECT_TRUE(r.is_empty());
103 EXPECT_FALSE(r.IsValid());
104 EXPECT_EQ(r, TypeParam::InvalidRange());
105 EXPECT_TRUE(r.EqualsIgnoringDirection(TypeParam::InvalidRange()));
106}
107
108TYPED_TEST(RangeTest, Equality) {
109 TypeParam r1(10, 4);
110 TypeParam r2(10, 4);
111 TypeParam r3(10, 2);
112 EXPECT_EQ(r1, r2);
113 EXPECT_NE(r1, r3);
114 EXPECT_NE(r2, r3);
115
116 TypeParam r4(11, 4);
117 EXPECT_NE(r1, r4);
118 EXPECT_NE(r2, r4);
119 EXPECT_NE(r3, r4);
120
121 TypeParam r5(12, 5);
122 EXPECT_NE(r1, r5);
123 EXPECT_NE(r2, r5);
124 EXPECT_NE(r3, r5);
125}
126
127TYPED_TEST(RangeTest, EqualsIgnoringDirection) {
128 TypeParam r1(10, 5);
129 TypeParam r2(5, 10);
130 EXPECT_TRUE(r1.EqualsIgnoringDirection(r2));
131}
132
133TYPED_TEST(RangeTest, SetStart) {
134 TypeParam r(10, 20);
135 EXPECT_EQ(10U, r.start());
136 EXPECT_EQ(10U, r.length());
137
138 r.set_start(42);
139 EXPECT_EQ(42U, r.start());
140 EXPECT_EQ(20U, r.end());
141 EXPECT_EQ(22U, r.length());
142 EXPECT_TRUE(r.is_reversed());
143}
144
145TYPED_TEST(RangeTest, SetEnd) {
146 TypeParam r(10, 13);
147 EXPECT_EQ(10U, r.start());
148 EXPECT_EQ(3U, r.length());
149
150 r.set_end(20);
151 EXPECT_EQ(10U, r.start());
152 EXPECT_EQ(20U, r.end());
153 EXPECT_EQ(10U, r.length());
154}
155
156TYPED_TEST(RangeTest, SetStartAndEnd) {
157 TypeParam r;
158 r.set_end(5);
159 r.set_start(1);
160 EXPECT_EQ(1U, r.start());
161 EXPECT_EQ(5U, r.end());
162 EXPECT_EQ(4U, r.length());
163 EXPECT_EQ(1U, r.GetMin());
164 EXPECT_EQ(5U, r.GetMax());
165}
166
167TYPED_TEST(RangeTest, ReversedRange) {
168 TypeParam r(10, 5);
169 EXPECT_EQ(10U, r.start());
170 EXPECT_EQ(5U, r.end());
171 EXPECT_EQ(5U, r.length());
172 EXPECT_TRUE(r.is_reversed());
173 EXPECT_TRUE(r.IsValid());
174 EXPECT_EQ(5U, r.GetMin());
175 EXPECT_EQ(10U, r.GetMax());
176}
177
178TYPED_TEST(RangeTest, SetReversedRange) {
179 TypeParam r(10, 20);
180 r.set_start(25);
181 EXPECT_EQ(25U, r.start());
182 EXPECT_EQ(20U, r.end());
183 EXPECT_EQ(5U, r.length());
184 EXPECT_TRUE(r.is_reversed());
185 EXPECT_TRUE(r.IsValid());
186
187 r.set_end(21);
188 EXPECT_EQ(25U, r.start());
189 EXPECT_EQ(21U, r.end());
190 EXPECT_EQ(4U, r.length());
191 EXPECT_TRUE(r.IsValid());
192 EXPECT_EQ(21U, r.GetMin());
193 EXPECT_EQ(25U, r.GetMax());
194}
195
196TYPED_TEST(RangeTest, ContainAndIntersect) {
197 {
198 SCOPED_TRACE("contain and intersect");
199 TypeParam r1(10, 12);
200 TypeParam r2(1, 8);
201 TypeParam r3(5, 12);
202 TestContainsAndIntersects(r1, r2, r3);
203 }
204 {
205 SCOPED_TRACE("contain and intersect: reversed");
206 TypeParam r1(12, 10);
207 TypeParam r2(8, 1);
208 TypeParam r3(12, 5);
209 TestContainsAndIntersects(r1, r2, r3);
210 }
211 // Invalid rect tests
212 TypeParam r1(10, 12);
213 TypeParam r2(8, 1);
214 TypeParam invalid = r1.Intersect(r2);
215 EXPECT_FALSE(invalid.IsValid());
216 EXPECT_FALSE(invalid.Contains(invalid));
217 EXPECT_FALSE(invalid.Contains(r1));
218 EXPECT_FALSE(invalid.Intersects(invalid));
219 EXPECT_FALSE(invalid.Intersects(r1));
220 EXPECT_FALSE(r1.Contains(invalid));
221 EXPECT_FALSE(r1.Intersects(invalid));
222}
223
224TEST(RangeTest, RangeOperations) {
225 constexpr gfx::Range invalid_range = gfx::Range::InvalidRange();
226 constexpr gfx::Range ranges[] = {{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1},
227 {1, 2}, {2, 0}, {2, 1}, {2, 2}};
228
229 // Ensures valid behavior over same range.
230 for (const auto& range : ranges) {
231 SCOPED_TRACE(range.ToString());
232 // A range should contain itself.
233 EXPECT_TRUE(range.Contains(range));
234 // A ranges should intersect with itself.
235 EXPECT_TRUE(range.Intersects(range));
236 }
237
238 // Ensures valid behavior with an invalid range.
239 for (const auto& range : ranges) {
240 SCOPED_TRACE(range.ToString());
241 EXPECT_FALSE(invalid_range.Contains(range));
242 EXPECT_FALSE(invalid_range.Intersects(range));
243 EXPECT_FALSE(range.Contains(invalid_range));
244 EXPECT_FALSE(range.Intersects(invalid_range));
245 }
246 EXPECT_FALSE(invalid_range.Contains(invalid_range));
247 EXPECT_FALSE(invalid_range.Intersects(invalid_range));
248
249 // Ensures consistent operations between Contains(...) and Intersects(...).
250 for (const auto& range1 : ranges) {
251 for (const auto& range2 : ranges) {
252 SCOPED_TRACE(testing::Message()
253 << "range1=" << range1 << " range2=" << range2);
254 if (range1.Contains(range2)) {
255 EXPECT_TRUE(range1.Intersects(range2));
256 EXPECT_EQ(range2.Contains(range1),
257 range1.EqualsIgnoringDirection(range2));
258 }
259 EXPECT_EQ(range2.Intersects(range1), range1.Intersects(range2));
260
261 EXPECT_EQ(range1.Intersect(range2) != invalid_range,
262 range1.Intersects(range2));
263 }
264 }
265
266 // Ranges should behave the same way no matter the direction.
267 for (const auto& range1 : ranges) {
268 for (const auto& range2 : ranges) {
269 SCOPED_TRACE(testing::Message()
270 << "range1=" << range1 << " range2=" << range2);
271 EXPECT_EQ(range1.Contains(range2),
272 range1.Contains(gfx::Range(range2.GetMax(), range2.GetMin())));
273 EXPECT_EQ(
274 range1.Intersects(range2),
275 range1.Intersects(gfx::Range(range2.GetMax(), range2.GetMin())));
276 }
277 }
278}
279
280TEST(RangeTest, ContainsAndIntersects) {
281 constexpr gfx::Range r1(0, 0);
282 constexpr gfx::Range r2(0, 1);
283 constexpr gfx::Range r3(1, 2);
284 constexpr gfx::Range r4(1, 0);
285 constexpr gfx::Range r5(2, 1);
286 constexpr gfx::Range r6(0, 2);
287 constexpr gfx::Range r7(2, 0);
288 constexpr gfx::Range r8(1, 1);
289
290 // Ensures Contains(...) handle the open range.
291 EXPECT_TRUE(r2.Contains(r1));
292 EXPECT_TRUE(r4.Contains(r1));
293 EXPECT_TRUE(r3.Contains(r5));
294 EXPECT_TRUE(r5.Contains(r3));
295
296 // Ensures larger ranges contains smaller ranges.
297 EXPECT_TRUE(r6.Contains(r1));
298 EXPECT_TRUE(r6.Contains(r2));
299 EXPECT_TRUE(r6.Contains(r3));
300 EXPECT_TRUE(r6.Contains(r4));
301 EXPECT_TRUE(r6.Contains(r5));
302
303 EXPECT_TRUE(r7.Contains(r1));
304 EXPECT_TRUE(r7.Contains(r2));
305 EXPECT_TRUE(r7.Contains(r3));
306 EXPECT_TRUE(r7.Contains(r4));
307 EXPECT_TRUE(r7.Contains(r5));
308
309 // Ensures Intersects(...) handle the open range.
310 EXPECT_TRUE(r2.Intersects(r1));
311 EXPECT_TRUE(r4.Intersects(r1));
312
313 // Ensures larger ranges intersects smaller ranges.
314 EXPECT_TRUE(r6.Intersects(r1));
315 EXPECT_TRUE(r6.Intersects(r2));
316 EXPECT_TRUE(r6.Intersects(r3));
317 EXPECT_TRUE(r6.Intersects(r4));
318 EXPECT_TRUE(r6.Intersects(r5));
319
320 EXPECT_TRUE(r7.Intersects(r1));
321 EXPECT_TRUE(r7.Intersects(r2));
322 EXPECT_TRUE(r7.Intersects(r3));
323 EXPECT_TRUE(r7.Intersects(r4));
324 EXPECT_TRUE(r7.Intersects(r5));
325
326 // Ensures adjacent ranges don't overlap.
327 EXPECT_FALSE(r2.Intersects(r3));
328 EXPECT_FALSE(r5.Intersects(r4));
329
330 // Ensures empty ranges are handled correctly.
331 EXPECT_FALSE(r1.Contains(r8));
332 EXPECT_FALSE(r2.Contains(r8));
333 EXPECT_TRUE(r3.Contains(r8));
334 EXPECT_TRUE(r8.Contains(r8));
335
336 EXPECT_FALSE(r1.Intersects(r8));
337 EXPECT_FALSE(r2.Intersects(r8));
338 EXPECT_TRUE(r3.Intersects(r8));
339 EXPECT_TRUE(r8.Intersects(r8));
340}
341
342TEST(RangeTest, Intersect) {
343 EXPECT_EQ(gfx::Range(0, 1).Intersect({0, 1}), gfx::Range(0, 1));
344 EXPECT_EQ(gfx::Range(0, 1).Intersect({0, 0}), gfx::Range(0, 0));
345 EXPECT_EQ(gfx::Range(0, 0).Intersect({1, 0}), gfx::Range(0, 0));
346 EXPECT_EQ(gfx::Range(0, 4).Intersect({2, 3}), gfx::Range(2, 3));
347 EXPECT_EQ(gfx::Range(0, 4).Intersect({2, 7}), gfx::Range(2, 4));
348 EXPECT_EQ(gfx::Range(0, 4).Intersect({3, 4}), gfx::Range(3, 4));
349
350 EXPECT_EQ(gfx::Range(0, 1).Intersect({1, 1}), gfx::Range::InvalidRange());
351 EXPECT_EQ(gfx::Range(1, 1).Intersect({1, 0}), gfx::Range::InvalidRange());
352 EXPECT_EQ(gfx::Range(0, 1).Intersect({1, 2}), gfx::Range::InvalidRange());
353 EXPECT_EQ(gfx::Range(0, 1).Intersect({2, 1}), gfx::Range::InvalidRange());
354 EXPECT_EQ(gfx::Range(2, 1).Intersect({1, 0}), gfx::Range::InvalidRange());
355}
356
357TEST(RangeTest, IsBoundedBy) {
358 constexpr gfx::Range r1(0, 0);
359 constexpr gfx::Range r2(0, 1);
360 EXPECT_TRUE(r1.IsBoundedBy(r1));
361 EXPECT_FALSE(r2.IsBoundedBy(r1));
362
363 constexpr gfx::Range r3(0, 2);
364 constexpr gfx::Range r4(2, 2);
365 EXPECT_TRUE(r4.IsBoundedBy(r3));
366 EXPECT_FALSE(r3.IsBoundedBy(r4));
367}
368
369TEST(RangeTest, ToString) {
370 gfx::Range range(4, 7);
371 EXPECT_EQ("{4,7}", range.ToString());
372
373 range = gfx::Range::InvalidRange();
374 std::ostringstream expected;
375 expected << "{" << range.start() << "," << range.end() << "}";
376 EXPECT_EQ(expected.str(), range.ToString());
377}
static bool invalid(const SkISize &size)
constexpr bool Intersects(const Range &range) const
Definition: range.h:81
std::string ToString() const
Definition: range.cc:14
constexpr bool IsBoundedBy(const Range &range) const
Definition: range.h:92
constexpr uint32_t end() const
Definition: range.h:59
constexpr uint32_t start() const
Definition: range.h:56
constexpr bool Contains(const Range &range) const
Definition: range.h:84
static constexpr Range InvalidRange()
Definition: range.h:50
static std::string ToString(CompilerBackend::Type type)
Definition: reflector.cc:559
#define T
Definition: precompiler.cc:65
TEST(RangeTest, RangeOperations)
TYPED_TEST(RangeTest, EmptyInit)
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678