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.
5 #include "base/basictypes.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/gfx/rect.h"
8 #include "ui/gfx/rect_conversions.h"
9 #include "ui/gfx/skia_util.h"
15 TEST(RectTest
, Contains
) {
16 static const struct ContainsCase
{
24 } contains_cases
[] = {
25 {0, 0, 10, 10, 0, 0, true},
26 {0, 0, 10, 10, 5, 5, true},
27 {0, 0, 10, 10, 9, 9, true},
28 {0, 0, 10, 10, 5, 10, false},
29 {0, 0, 10, 10, 10, 5, false},
30 {0, 0, 10, 10, -1, -1, false},
31 {0, 0, 10, 10, 50, 50, false},
32 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
33 {0, 0, -10, -10, 0, 0, false},
36 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(contains_cases
); ++i
) {
37 const ContainsCase
& value
= contains_cases
[i
];
38 Rect
rect(value
.rect_x
, value
.rect_y
, value
.rect_width
, value
.rect_height
);
39 EXPECT_EQ(value
.contained
, rect
.Contains(value
.point_x
, value
.point_y
));
43 TEST(RectTest
, Intersects
) {
55 { 0, 0, 0, 0, 0, 0, 0, 0, false },
56 { 0, 0, 10, 10, 0, 0, 10, 10, true },
57 { 0, 0, 10, 10, 10, 10, 10, 10, false },
58 { 10, 10, 10, 10, 0, 0, 10, 10, false },
59 { 10, 10, 10, 10, 5, 5, 10, 10, true },
60 { 10, 10, 10, 10, 15, 15, 10, 10, true },
61 { 10, 10, 10, 10, 20, 15, 10, 10, false },
62 { 10, 10, 10, 10, 21, 15, 10, 10, false }
64 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
65 Rect
r1(tests
[i
].x1
, tests
[i
].y1
, tests
[i
].w1
, tests
[i
].h1
);
66 Rect
r2(tests
[i
].x2
, tests
[i
].y2
, tests
[i
].w2
, tests
[i
].h2
);
67 EXPECT_EQ(tests
[i
].intersects
, r1
.Intersects(r2
));
71 TEST(RectTest
, Intersect
) {
81 int x3
; // rect 3: the union of rects 1 and 2
86 { 0, 0, 0, 0, // zeros
89 { 0, 0, 4, 4, // equal
92 { 0, 0, 4, 4, // neighboring
95 { 0, 0, 4, 4, // overlapping corners
98 { 0, 0, 4, 4, // T junction
105 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
106 Rect
r1(tests
[i
].x1
, tests
[i
].y1
, tests
[i
].w1
, tests
[i
].h1
);
107 Rect
r2(tests
[i
].x2
, tests
[i
].y2
, tests
[i
].w2
, tests
[i
].h2
);
108 Rect
r3(tests
[i
].x3
, tests
[i
].y3
, tests
[i
].w3
, tests
[i
].h3
);
109 Rect ir
= IntersectRects(r1
, r2
);
110 EXPECT_EQ(r3
.x(), ir
.x());
111 EXPECT_EQ(r3
.y(), ir
.y());
112 EXPECT_EQ(r3
.width(), ir
.width());
113 EXPECT_EQ(r3
.height(), ir
.height());
117 TEST(RectTest
, Union
) {
118 static const struct Test
{
127 int x3
; // rect 3: the union of rects 1 and 2
147 { 3, 3, 2, 2, // reverse r1 and r2 from previous test
150 { 0, 0, 0, 0, // union with empty rect
154 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
155 Rect
r1(tests
[i
].x1
, tests
[i
].y1
, tests
[i
].w1
, tests
[i
].h1
);
156 Rect
r2(tests
[i
].x2
, tests
[i
].y2
, tests
[i
].w2
, tests
[i
].h2
);
157 Rect
r3(tests
[i
].x3
, tests
[i
].y3
, tests
[i
].w3
, tests
[i
].h3
);
158 Rect u
= UnionRects(r1
, r2
);
159 EXPECT_EQ(r3
.x(), u
.x());
160 EXPECT_EQ(r3
.y(), u
.y());
161 EXPECT_EQ(r3
.width(), u
.width());
162 EXPECT_EQ(r3
.height(), u
.height());
166 TEST(RectTest
, Equals
) {
167 ASSERT_TRUE(Rect(0, 0, 0, 0) == Rect(0, 0, 0, 0));
168 ASSERT_TRUE(Rect(1, 2, 3, 4) == Rect(1, 2, 3, 4));
169 ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(0, 0, 0, 1));
170 ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(0, 0, 1, 0));
171 ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(0, 1, 0, 0));
172 ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(1, 0, 0, 0));
175 TEST(RectTest
, AdjustToFit
) {
176 static const struct Test
{
185 int x3
; // rect 3: results of invoking AdjustToFit
206 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
207 Rect
r1(tests
[i
].x1
, tests
[i
].y1
, tests
[i
].w1
, tests
[i
].h1
);
208 Rect
r2(tests
[i
].x2
, tests
[i
].y2
, tests
[i
].w2
, tests
[i
].h2
);
209 Rect
r3(tests
[i
].x3
, tests
[i
].y3
, tests
[i
].w3
, tests
[i
].h3
);
212 EXPECT_EQ(r3
.x(), u
.x());
213 EXPECT_EQ(r3
.y(), u
.y());
214 EXPECT_EQ(r3
.width(), u
.width());
215 EXPECT_EQ(r3
.height(), u
.height());
219 TEST(RectTest
, Subtract
) {
223 result
= Rect(10, 10, 20, 20);
224 result
.Subtract(Rect(10, 10, 20, 20));
225 EXPECT_EQ(Rect(0, 0, 0, 0).ToString(), result
.ToString());
228 result
= Rect(10, 10, 20, 20);
229 result
.Subtract(Rect(5, 5, 30, 30));
230 EXPECT_EQ(Rect(0, 0, 0, 0).ToString(), result
.ToString());
233 result
= Rect(10, 10, 20, 20);
234 result
.Subtract(Rect(30, 30, 30, 30));
235 EXPECT_EQ(Rect(10, 10, 20, 20).ToString(), result
.ToString());
237 // Not a complete intersection in either direction
238 result
= Rect(10, 10, 20, 20);
239 result
.Subtract(Rect(15, 15, 20, 20));
240 EXPECT_EQ(Rect(10, 10, 20, 20).ToString(), result
.ToString());
242 // Complete intersection in the x-direction
243 result
= Rect(10, 10, 20, 20);
244 result
.Subtract(Rect(10, 15, 20, 20));
245 EXPECT_EQ(Rect(10, 10, 20, 5).ToString(), result
.ToString());
247 // Complete intersection in the x-direction
248 result
= Rect(10, 10, 20, 20);
249 result
.Subtract(Rect(5, 15, 30, 20));
250 EXPECT_EQ(Rect(10, 10, 20, 5).ToString(), result
.ToString());
252 // Complete intersection in the x-direction
253 result
= Rect(10, 10, 20, 20);
254 result
.Subtract(Rect(5, 5, 30, 20));
255 EXPECT_EQ(Rect(10, 25, 20, 5).ToString(), result
.ToString());
257 // Complete intersection in the y-direction
258 result
= Rect(10, 10, 20, 20);
259 result
.Subtract(Rect(10, 10, 10, 30));
260 EXPECT_EQ(Rect(20, 10, 10, 20).ToString(), result
.ToString());
262 // Complete intersection in the y-direction
263 result
= Rect(10, 10, 20, 20);
264 result
.Subtract(Rect(5, 5, 20, 30));
265 EXPECT_EQ(Rect(25, 10, 5, 20).ToString(), result
.ToString());
268 TEST(RectTest
, IsEmpty
) {
269 EXPECT_TRUE(Rect(0, 0, 0, 0).IsEmpty());
270 EXPECT_TRUE(Rect(0, 0, 0, 0).size().IsEmpty());
271 EXPECT_TRUE(Rect(0, 0, 10, 0).IsEmpty());
272 EXPECT_TRUE(Rect(0, 0, 10, 0).size().IsEmpty());
273 EXPECT_TRUE(Rect(0, 0, 0, 10).IsEmpty());
274 EXPECT_TRUE(Rect(0, 0, 0, 10).size().IsEmpty());
275 EXPECT_FALSE(Rect(0, 0, 10, 10).IsEmpty());
276 EXPECT_FALSE(Rect(0, 0, 10, 10).size().IsEmpty());
279 TEST(RectTest
, SplitVertically
) {
280 Rect left_half
, right_half
;
282 // Splitting when origin is (0, 0).
283 Rect(0, 0, 20, 20).SplitVertically(&left_half
, &right_half
);
284 EXPECT_TRUE(left_half
== Rect(0, 0, 10, 20));
285 EXPECT_TRUE(right_half
== Rect(10, 0, 10, 20));
287 // Splitting when origin is arbitrary.
288 Rect(10, 10, 20, 10).SplitVertically(&left_half
, &right_half
);
289 EXPECT_TRUE(left_half
== Rect(10, 10, 10, 10));
290 EXPECT_TRUE(right_half
== Rect(20, 10, 10, 10));
292 // Splitting a rectangle of zero width.
293 Rect(10, 10, 0, 10).SplitVertically(&left_half
, &right_half
);
294 EXPECT_TRUE(left_half
== Rect(10, 10, 0, 10));
295 EXPECT_TRUE(right_half
== Rect(10, 10, 0, 10));
297 // Splitting a rectangle of odd width.
298 Rect(10, 10, 5, 10).SplitVertically(&left_half
, &right_half
);
299 EXPECT_TRUE(left_half
== Rect(10, 10, 2, 10));
300 EXPECT_TRUE(right_half
== Rect(12, 10, 3, 10));
303 TEST(RectTest
, CenterPoint
) {
306 // When origin is (0, 0).
307 center
= Rect(0, 0, 20, 20).CenterPoint();
308 EXPECT_TRUE(center
== Point(10, 10));
310 // When origin is even.
311 center
= Rect(10, 10, 20, 20).CenterPoint();
312 EXPECT_TRUE(center
== Point(20, 20));
314 // When origin is odd.
315 center
= Rect(11, 11, 20, 20).CenterPoint();
316 EXPECT_TRUE(center
== Point(21, 21));
318 // When 0 width or height.
319 center
= Rect(10, 10, 0, 20).CenterPoint();
320 EXPECT_TRUE(center
== Point(10, 20));
321 center
= Rect(10, 10, 20, 0).CenterPoint();
322 EXPECT_TRUE(center
== Point(20, 10));
325 center
= Rect(10, 10, 21, 21).CenterPoint();
326 EXPECT_TRUE(center
== Point(20, 20));
328 // When an odd size and position.
329 center
= Rect(11, 11, 21, 21).CenterPoint();
330 EXPECT_TRUE(center
== Point(21, 21));
333 TEST(RectTest
, CenterPointF
) {
336 // When origin is (0, 0).
337 center
= RectF(0, 0, 20, 20).CenterPoint();
338 EXPECT_TRUE(center
== PointF(10, 10));
340 // When origin is even.
341 center
= RectF(10, 10, 20, 20).CenterPoint();
342 EXPECT_TRUE(center
== PointF(20, 20));
344 // When origin is odd.
345 center
= RectF(11, 11, 20, 20).CenterPoint();
346 EXPECT_TRUE(center
== PointF(21, 21));
348 // When 0 width or height.
349 center
= RectF(10, 10, 0, 20).CenterPoint();
350 EXPECT_TRUE(center
== PointF(10, 20));
351 center
= RectF(10, 10, 20, 0).CenterPoint();
352 EXPECT_TRUE(center
== PointF(20, 10));
355 center
= RectF(10, 10, 21, 21).CenterPoint();
356 EXPECT_TRUE(center
== PointF(20.5f
, 20.5f
));
358 // When an odd size and position.
359 center
= RectF(11, 11, 21, 21).CenterPoint();
360 EXPECT_TRUE(center
== PointF(21.5f
, 21.5f
));
363 TEST(RectTest
, SharesEdgeWith
) {
366 // Must be non-overlapping
367 EXPECT_FALSE(r
.SharesEdgeWith(r
));
369 Rect
just_above(2, 1, 4, 2);
370 Rect
just_below(2, 8, 4, 2);
371 Rect
just_left(0, 3, 2, 5);
372 Rect
just_right(6, 3, 2, 5);
374 EXPECT_TRUE(r
.SharesEdgeWith(just_above
));
375 EXPECT_TRUE(r
.SharesEdgeWith(just_below
));
376 EXPECT_TRUE(r
.SharesEdgeWith(just_left
));
377 EXPECT_TRUE(r
.SharesEdgeWith(just_right
));
380 Rect
same_height_no_edge(0, 0, 1, 5);
381 Rect
same_width_no_edge(0, 0, 4, 1);
383 EXPECT_FALSE(r
.SharesEdgeWith(same_height_no_edge
));
384 EXPECT_FALSE(r
.SharesEdgeWith(same_width_no_edge
));
386 Rect
just_above_no_edge(2, 1, 5, 2); // too wide
387 Rect
just_below_no_edge(2, 8, 3, 2); // too narrow
388 Rect
just_left_no_edge(0, 3, 2, 6); // too tall
389 Rect
just_right_no_edge(6, 3, 2, 4); // too short
391 EXPECT_FALSE(r
.SharesEdgeWith(just_above_no_edge
));
392 EXPECT_FALSE(r
.SharesEdgeWith(just_below_no_edge
));
393 EXPECT_FALSE(r
.SharesEdgeWith(just_left_no_edge
));
394 EXPECT_FALSE(r
.SharesEdgeWith(just_right_no_edge
));
397 TEST(RectTest
, SkiaRectConversions
) {
398 Rect
isrc(10, 20, 30, 40);
399 RectF
fsrc(10.5f
, 20.5f
, 30.5f
, 40.5f
);
401 SkIRect skirect
= RectToSkIRect(isrc
);
402 EXPECT_EQ(isrc
.ToString(), SkIRectToRect(skirect
).ToString());
404 SkRect skrect
= RectToSkRect(isrc
);
405 EXPECT_EQ(gfx::RectF(isrc
).ToString(), SkRectToRectF(skrect
).ToString());
407 skrect
= RectFToSkRect(fsrc
);
408 EXPECT_EQ(fsrc
.ToString(), SkRectToRectF(skrect
).ToString());
411 // Similar to EXPECT_FLOAT_EQ, but lets NaN equal NaN
412 #define EXPECT_FLOAT_AND_NAN_EQ(a, b) \
413 { if (a == a || b == b) { EXPECT_FLOAT_EQ(a, b); } }
415 TEST(RectTest
, ScaleRect
) {
416 static const struct Test
{
429 4.5f
, 4.5f
, 4.5f
, 4.5f
},
432 0.0f
, 0.0f
, 0.0f
, 0.0f
},
434 std::numeric_limits
<float>::quiet_NaN(),
435 std::numeric_limits
<float>::quiet_NaN(),
436 std::numeric_limits
<float>::quiet_NaN(),
437 std::numeric_limits
<float>::quiet_NaN(),
438 std::numeric_limits
<float>::quiet_NaN() },
440 std::numeric_limits
<float>::max(),
441 std::numeric_limits
<float>::max(),
442 std::numeric_limits
<float>::max(),
443 std::numeric_limits
<float>::max(),
444 std::numeric_limits
<float>::max() }
447 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
448 Rect
r1(tests
[i
].x1
, tests
[i
].y1
, tests
[i
].w1
, tests
[i
].h1
);
449 RectF
r2(tests
[i
].x2
, tests
[i
].y2
, tests
[i
].w2
, tests
[i
].h2
);
451 RectF scaled
= ScaleRect(r1
, tests
[i
].scale
);
452 EXPECT_FLOAT_AND_NAN_EQ(r2
.x(), scaled
.x());
453 EXPECT_FLOAT_AND_NAN_EQ(r2
.y(), scaled
.y());
454 EXPECT_FLOAT_AND_NAN_EQ(r2
.width(), scaled
.width());
455 EXPECT_FLOAT_AND_NAN_EQ(r2
.height(), scaled
.height());
459 TEST(RectTest
, ToEnclosedRect
) {
460 static const struct Test
{
470 { 0.0f
, 0.0f
, 0.0f
, 0.0f
,
472 { -1.5f
, -1.5f
, 3.0f
, 3.0f
,
474 { -1.5f
, -1.5f
, 3.5f
, 3.5f
,
476 { std::numeric_limits
<float>::max(),
477 std::numeric_limits
<float>::max(),
479 std::numeric_limits
<int>::max(),
480 std::numeric_limits
<int>::max(),
483 std::numeric_limits
<float>::max(),
484 std::numeric_limits
<float>::max(),
486 std::numeric_limits
<int>::max(),
487 std::numeric_limits
<int>::max() },
488 { 20000.5f
, 20000.5f
, 0.5f
, 0.5f
,
489 20001, 20001, 0, 0 },
490 { std::numeric_limits
<float>::quiet_NaN(),
491 std::numeric_limits
<float>::quiet_NaN(),
492 std::numeric_limits
<float>::quiet_NaN(),
493 std::numeric_limits
<float>::quiet_NaN(),
497 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
498 RectF
r1(tests
[i
].x1
, tests
[i
].y1
, tests
[i
].w1
, tests
[i
].h1
);
499 Rect
r2(tests
[i
].x2
, tests
[i
].y2
, tests
[i
].w2
, tests
[i
].h2
);
501 Rect enclosed
= ToEnclosedRect(r1
);
502 EXPECT_FLOAT_AND_NAN_EQ(r2
.x(), enclosed
.x());
503 EXPECT_FLOAT_AND_NAN_EQ(r2
.y(), enclosed
.y());
504 EXPECT_FLOAT_AND_NAN_EQ(r2
.width(), enclosed
.width());
505 EXPECT_FLOAT_AND_NAN_EQ(r2
.height(), enclosed
.height());
509 TEST(RectTest
, ToEnclosingRect
) {
510 static const struct Test
{
520 { 0.0f
, 0.0f
, 0.0f
, 0.0f
,
522 { -1.5f
, -1.5f
, 3.0f
, 3.0f
,
524 { -1.5f
, -1.5f
, 3.5f
, 3.5f
,
526 { std::numeric_limits
<float>::max(),
527 std::numeric_limits
<float>::max(),
529 std::numeric_limits
<int>::max(),
530 std::numeric_limits
<int>::max(),
533 std::numeric_limits
<float>::max(),
534 std::numeric_limits
<float>::max(),
536 std::numeric_limits
<int>::max(),
537 std::numeric_limits
<int>::max() },
538 { 20000.5f
, 20000.5f
, 0.5f
, 0.5f
,
539 20000, 20000, 1, 1 },
540 { std::numeric_limits
<float>::quiet_NaN(),
541 std::numeric_limits
<float>::quiet_NaN(),
542 std::numeric_limits
<float>::quiet_NaN(),
543 std::numeric_limits
<float>::quiet_NaN(),
547 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
548 RectF
r1(tests
[i
].x1
, tests
[i
].y1
, tests
[i
].w1
, tests
[i
].h1
);
549 Rect
r2(tests
[i
].x2
, tests
[i
].y2
, tests
[i
].w2
, tests
[i
].h2
);
551 Rect enclosed
= ToEnclosingRect(r1
);
552 EXPECT_FLOAT_AND_NAN_EQ(r2
.x(), enclosed
.x());
553 EXPECT_FLOAT_AND_NAN_EQ(r2
.y(), enclosed
.y());
554 EXPECT_FLOAT_AND_NAN_EQ(r2
.width(), enclosed
.width());
555 EXPECT_FLOAT_AND_NAN_EQ(r2
.height(), enclosed
.height());
559 TEST(RectTest
, ToNearestRect
) {
561 EXPECT_EQ(rect
.ToString(), ToNearestRect(RectF(rect
)).ToString());
563 rect
= Rect(-1, -1, 3, 3);
564 EXPECT_EQ(rect
.ToString(), ToNearestRect(RectF(rect
)).ToString());
566 RectF
rectf(-1.00001f
, -0.999999f
, 3.0000001f
, 2.999999f
);
567 EXPECT_EQ(rect
.ToString(), ToNearestRect(rectf
).ToString());
570 TEST(RectTest
, ToFlooredRect
) {
571 static const struct Test
{
581 { 0.0f
, 0.0f
, 0.0f
, 0.0f
,
583 { -1.5f
, -1.5f
, 3.0f
, 3.0f
,
585 { -1.5f
, -1.5f
, 3.5f
, 3.5f
,
587 { 20000.5f
, 20000.5f
, 0.5f
, 0.5f
,
588 20000, 20000, 0, 0 },
591 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); ++i
) {
592 RectF
r1(tests
[i
].x1
, tests
[i
].y1
, tests
[i
].w1
, tests
[i
].h1
);
593 Rect
r2(tests
[i
].x2
, tests
[i
].y2
, tests
[i
].w2
, tests
[i
].h2
);
595 Rect floored
= ToFlooredRectDeprecated(r1
);
596 EXPECT_FLOAT_EQ(r2
.x(), floored
.x());
597 EXPECT_FLOAT_EQ(r2
.y(), floored
.y());
598 EXPECT_FLOAT_EQ(r2
.width(), floored
.width());
599 EXPECT_FLOAT_EQ(r2
.height(), floored
.height());
604 TEST(RectTest
, ConstructAndAssign
) {
605 const RECT rect_1
= { 0, 0, 10, 10 };
606 const RECT rect_2
= { 0, 0, -10, -10 };
612 TEST(RectTest
, ToRectF
) {
613 // Check that implicit conversion from integer to float compiles.
614 Rect
a(10, 20, 30, 40);
615 RectF
b(10, 20, 30, 40);
617 RectF intersect
= IntersectRects(a
, b
);
618 EXPECT_EQ(b
.ToString(), intersect
.ToString());
624 TEST(RectTest
, BoundingRect
) {
630 // If point B dominates A, then A should be the origin.
631 { Point(4, 6), Point(4, 6), Rect(4, 6, 0, 0) },
632 { Point(4, 6), Point(8, 6), Rect(4, 6, 4, 0) },
633 { Point(4, 6), Point(4, 9), Rect(4, 6, 0, 3) },
634 { Point(4, 6), Point(8, 9), Rect(4, 6, 4, 3) },
635 // If point A dominates B, then B should be the origin.
636 { Point(4, 6), Point(4, 6), Rect(4, 6, 0, 0) },
637 { Point(8, 6), Point(4, 6), Rect(4, 6, 4, 0) },
638 { Point(4, 9), Point(4, 6), Rect(4, 6, 0, 3) },
639 { Point(8, 9), Point(4, 6), Rect(4, 6, 4, 3) },
640 // If neither point dominates, then the origin is a combination of the two.
641 { Point(4, 6), Point(6, 4), Rect(4, 4, 2, 2) },
642 { Point(-4, -6), Point(-6, -4), Rect(-6, -6, 2, 2) },
643 { Point(-4, 6), Point(6, -4), Rect(-4, -4, 10, 10) },
646 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(int_tests
); ++i
) {
647 Rect actual
= BoundingRect(int_tests
[i
].a
, int_tests
[i
].b
);
648 EXPECT_EQ(int_tests
[i
].expected
.ToString(), actual
.ToString());
656 // If point B dominates A, then A should be the origin.
657 { PointF(4.2f
, 6.8f
), PointF(4.2f
, 6.8f
),
658 RectF(4.2f
, 6.8f
, 0, 0) },
659 { PointF(4.2f
, 6.8f
), PointF(8.5f
, 6.8f
),
660 RectF(4.2f
, 6.8f
, 4.3f
, 0) },
661 { PointF(4.2f
, 6.8f
), PointF(4.2f
, 9.3f
),
662 RectF(4.2f
, 6.8f
, 0, 2.5f
) },
663 { PointF(4.2f
, 6.8f
), PointF(8.5f
, 9.3f
),
664 RectF(4.2f
, 6.8f
, 4.3f
, 2.5f
) },
665 // If point A dominates B, then B should be the origin.
666 { PointF(4.2f
, 6.8f
), PointF(4.2f
, 6.8f
),
667 RectF(4.2f
, 6.8f
, 0, 0) },
668 { PointF(8.5f
, 6.8f
), PointF(4.2f
, 6.8f
),
669 RectF(4.2f
, 6.8f
, 4.3f
, 0) },
670 { PointF(4.2f
, 9.3f
), PointF(4.2f
, 6.8f
),
671 RectF(4.2f
, 6.8f
, 0, 2.5f
) },
672 { PointF(8.5f
, 9.3f
), PointF(4.2f
, 6.8f
),
673 RectF(4.2f
, 6.8f
, 4.3f
, 2.5f
) },
674 // If neither point dominates, then the origin is a combination of the two.
675 { PointF(4.2f
, 6.8f
), PointF(6.8f
, 4.2f
),
676 RectF(4.2f
, 4.2f
, 2.6f
, 2.6f
) },
677 { PointF(-4.2f
, -6.8f
), PointF(-6.8f
, -4.2f
),
678 RectF(-6.8f
, -6.8f
, 2.6f
, 2.6f
) },
679 { PointF(-4.2f
, 6.8f
), PointF(6.8f
, -4.2f
),
680 RectF(-4.2f
, -4.2f
, 11.0f
, 11.0f
) }
683 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(float_tests
); ++i
) {
684 RectF actual
= BoundingRect(float_tests
[i
].a
, float_tests
[i
].b
);
685 EXPECT_EQ(float_tests
[i
].expected
.ToString(), actual
.ToString());
689 TEST(RectTest
, IsExpressibleAsRect
) {
690 EXPECT_TRUE(RectF().IsExpressibleAsRect());
692 float min
= std::numeric_limits
<int>::min();
693 float max
= std::numeric_limits
<int>::max();
694 float infinity
= std::numeric_limits
<float>::infinity();
697 min
+ 200, min
+ 200, max
- 200, max
- 200).IsExpressibleAsRect());
699 min
- 200, min
+ 200, max
+ 200, max
+ 200).IsExpressibleAsRect());
701 min
+ 200 , min
- 200, max
+ 200, max
+ 200).IsExpressibleAsRect());
703 min
+ 200, min
+ 200, max
+ 200, max
- 200).IsExpressibleAsRect());
705 min
+ 200, min
+ 200, max
- 200, max
+ 200).IsExpressibleAsRect());
707 EXPECT_TRUE(RectF(0, 0, max
- 200, max
- 200).IsExpressibleAsRect());
708 EXPECT_FALSE(RectF(200, 0, max
+ 200, max
- 200).IsExpressibleAsRect());
709 EXPECT_FALSE(RectF(0, 200, max
- 200, max
+ 200).IsExpressibleAsRect());
710 EXPECT_FALSE(RectF(0, 0, max
+ 200, max
- 200).IsExpressibleAsRect());
711 EXPECT_FALSE(RectF(0, 0, max
- 200, max
+ 200).IsExpressibleAsRect());
713 EXPECT_FALSE(RectF(infinity
, 0, 1, 1).IsExpressibleAsRect());
714 EXPECT_FALSE(RectF(0, infinity
, 1, 1).IsExpressibleAsRect());
715 EXPECT_FALSE(RectF(0, 0, infinity
, 1).IsExpressibleAsRect());
716 EXPECT_FALSE(RectF(0, 0, 1, infinity
).IsExpressibleAsRect());
719 TEST(RectTest
, Offset
) {
722 EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), (i
+ Vector2d(1, -1)).ToString());
723 EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), (Vector2d(1, -1) + i
).ToString());
724 i
+= Vector2d(1, -1);
725 EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), i
.ToString());
726 EXPECT_EQ(Rect(1, 2, 3, 4).ToString(), (i
- Vector2d(1, -1)).ToString());
727 i
-= Vector2d(1, -1);
728 EXPECT_EQ(Rect(1, 2, 3, 4).ToString(), i
.ToString());
730 RectF
f(1.1f
, 2.2f
, 3.3f
, 4.4f
);
731 EXPECT_EQ(RectF(2.2f
, 1.1f
, 3.3f
, 4.4f
).ToString(),
732 (f
+ Vector2dF(1.1f
, -1.1f
)).ToString());
733 EXPECT_EQ(RectF(2.2f
, 1.1f
, 3.3f
, 4.4f
).ToString(),
734 (Vector2dF(1.1f
, -1.1f
) + f
).ToString());
735 f
+= Vector2dF(1.1f
, -1.1f
);
736 EXPECT_EQ(RectF(2.2f
, 1.1f
, 3.3f
, 4.4f
).ToString(), f
.ToString());
737 EXPECT_EQ(RectF(1.1f
, 2.2f
, 3.3f
, 4.4f
).ToString(),
738 (f
- Vector2dF(1.1f
, -1.1f
)).ToString());
739 f
-= Vector2dF(1.1f
, -1.1f
);
740 EXPECT_EQ(RectF(1.1f
, 2.2f
, 3.3f
, 4.4f
).ToString(), f
.ToString());
743 TEST(RectTest
, Corners
) {
745 RectF
f(1.1f
, 2.1f
, 3.1f
, 4.1f
);
747 EXPECT_EQ(Point(1, 2).ToString(), i
.origin().ToString());
748 EXPECT_EQ(Point(4, 2).ToString(), i
.top_right().ToString());
749 EXPECT_EQ(Point(1, 6).ToString(), i
.bottom_left().ToString());
750 EXPECT_EQ(Point(4, 6).ToString(), i
.bottom_right().ToString());
752 EXPECT_EQ(PointF(1.1f
, 2.1f
).ToString(), f
.origin().ToString());
753 EXPECT_EQ(PointF(4.2f
, 2.1f
).ToString(), f
.top_right().ToString());
754 EXPECT_EQ(PointF(1.1f
, 6.2f
).ToString(), f
.bottom_left().ToString());
755 EXPECT_EQ(PointF(4.2f
, 6.2f
).ToString(), f
.bottom_right().ToString());