1 // Copyright (c) 2013 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.
7 #include "base/basictypes.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/gfx/geometry/rect.h"
10 #include "ui/gfx/geometry/rect_conversions.h"
18 TEST(RectTest, Contains) {
19 static const struct ContainsCase {
27 } contains_cases[] = {
28 {0, 0, 10, 10, 0, 0, true},
29 {0, 0, 10, 10, 5, 5, true},
30 {0, 0, 10, 10, 9, 9, true},
31 {0, 0, 10, 10, 5, 10, false},
32 {0, 0, 10, 10, 10, 5, false},
33 {0, 0, 10, 10, -1, -1, false},
34 {0, 0, 10, 10, 50, 50, false},
35 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
36 {0, 0, -10, -10, 0, 0, false},
39 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(contains_cases); ++i) {
40 const ContainsCase& value = contains_cases[i];
41 Rect rect(value.rect_x, value.rect_y, value.rect_width, value.rect_height);
42 EXPECT_EQ(value.contained, rect.Contains(value.point_x, value.point_y));
46 TEST(RectTest, Intersects) {
58 { 0, 0, 0, 0, 0, 0, 0, 0, false },
59 { 0, 0, 0, 0, -10, -10, 20, 20, false },
60 { -10, 0, 0, 20, 0, -10, 20, 0, false },
61 { 0, 0, 10, 10, 0, 0, 10, 10, true },
62 { 0, 0, 10, 10, 10, 10, 10, 10, false },
63 { 10, 10, 10, 10, 0, 0, 10, 10, false },
64 { 10, 10, 10, 10, 5, 5, 10, 10, true },
65 { 10, 10, 10, 10, 15, 15, 10, 10, true },
66 { 10, 10, 10, 10, 20, 15, 10, 10, false },
67 { 10, 10, 10, 10, 21, 15, 10, 10, false }
69 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
70 Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
71 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
72 EXPECT_EQ(tests[i].intersects, r1.Intersects(r2));
73 EXPECT_EQ(tests[i].intersects, r2.Intersects(r1));
77 TEST(RectTest, Intersect) {
87 int x3; // rect 3: the union of rects 1 and 2
92 { 0, 0, 0, 0, // zeros
95 { 0, 0, 4, 4, // equal
98 { 0, 0, 4, 4, // neighboring
101 { 0, 0, 4, 4, // overlapping corners
104 { 0, 0, 4, 4, // T junction
111 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
112 Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
113 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
114 Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3);
115 Rect ir = IntersectRects(r1, r2);
116 EXPECT_EQ(r3.x(), ir.x());
117 EXPECT_EQ(r3.y(), ir.y());
118 EXPECT_EQ(r3.width(), ir.width());
119 EXPECT_EQ(r3.height(), ir.height());
123 TEST(RectTest, Union) {
124 static const struct Test {
133 int x3; // rect 3: the union of rects 1 and 2
153 { 3, 3, 2, 2, // reverse r1 and r2 from previous test
156 { 0, 0, 0, 0, // union with empty rect
160 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
161 Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
162 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
163 Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3);
164 Rect u = UnionRects(r1, r2);
165 EXPECT_EQ(r3.x(), u.x());
166 EXPECT_EQ(r3.y(), u.y());
167 EXPECT_EQ(r3.width(), u.width());
168 EXPECT_EQ(r3.height(), u.height());
172 TEST(RectTest, Equals) {
173 ASSERT_TRUE(Rect(0, 0, 0, 0) == Rect(0, 0, 0, 0));
174 ASSERT_TRUE(Rect(1, 2, 3, 4) == Rect(1, 2, 3, 4));
175 ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(0, 0, 0, 1));
176 ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(0, 0, 1, 0));
177 ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(0, 1, 0, 0));
178 ASSERT_FALSE(Rect(0, 0, 0, 0) == Rect(1, 0, 0, 0));
181 TEST(RectTest, AdjustToFit) {
182 static const struct Test {
191 int x3; // rect 3: results of invoking AdjustToFit
212 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
213 Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
214 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
215 Rect r3(tests[i].x3, tests[i].y3, tests[i].w3, tests[i].h3);
218 EXPECT_EQ(r3.x(), u.x());
219 EXPECT_EQ(r3.y(), u.y());
220 EXPECT_EQ(r3.width(), u.width());
221 EXPECT_EQ(r3.height(), u.height());
225 TEST(RectTest, Subtract) {
229 result = Rect(10, 10, 20, 20);
230 result.Subtract(Rect(10, 10, 20, 20));
231 EXPECT_EQ(Rect(0, 0, 0, 0).ToString(), result.ToString());
234 result = Rect(10, 10, 20, 20);
235 result.Subtract(Rect(5, 5, 30, 30));
236 EXPECT_EQ(Rect(0, 0, 0, 0).ToString(), result.ToString());
239 result = Rect(10, 10, 20, 20);
240 result.Subtract(Rect(30, 30, 30, 30));
241 EXPECT_EQ(Rect(10, 10, 20, 20).ToString(), result.ToString());
243 // Not a complete intersection in either direction
244 result = Rect(10, 10, 20, 20);
245 result.Subtract(Rect(15, 15, 20, 20));
246 EXPECT_EQ(Rect(10, 10, 20, 20).ToString(), result.ToString());
248 // Complete intersection in the x-direction, top edge is fully covered.
249 result = Rect(10, 10, 20, 20);
250 result.Subtract(Rect(10, 15, 20, 20));
251 EXPECT_EQ(Rect(10, 10, 20, 5).ToString(), result.ToString());
253 // Complete intersection in the x-direction, top edge is fully covered.
254 result = Rect(10, 10, 20, 20);
255 result.Subtract(Rect(5, 15, 30, 20));
256 EXPECT_EQ(Rect(10, 10, 20, 5).ToString(), result.ToString());
258 // Complete intersection in the x-direction, bottom edge is fully covered.
259 result = Rect(10, 10, 20, 20);
260 result.Subtract(Rect(5, 5, 30, 20));
261 EXPECT_EQ(Rect(10, 25, 20, 5).ToString(), result.ToString());
263 // Complete intersection in the x-direction, none of the edges is fully
265 result = Rect(10, 10, 20, 20);
266 result.Subtract(Rect(5, 15, 30, 1));
267 EXPECT_EQ(Rect(10, 10, 20, 20).ToString(), result.ToString());
269 // Complete intersection in the y-direction, left edge is fully covered.
270 result = Rect(10, 10, 20, 20);
271 result.Subtract(Rect(10, 10, 10, 30));
272 EXPECT_EQ(Rect(20, 10, 10, 20).ToString(), result.ToString());
274 // Complete intersection in the y-direction, left edge is fully covered.
275 result = Rect(10, 10, 20, 20);
276 result.Subtract(Rect(5, 5, 20, 30));
277 EXPECT_EQ(Rect(25, 10, 5, 20).ToString(), result.ToString());
279 // Complete intersection in the y-direction, right edge is fully covered.
280 result = Rect(10, 10, 20, 20);
281 result.Subtract(Rect(20, 5, 20, 30));
282 EXPECT_EQ(Rect(10, 10, 10, 20).ToString(), result.ToString());
284 // Complete intersection in the y-direction, none of the edges is fully
286 result = Rect(10, 10, 20, 20);
287 result.Subtract(Rect(15, 5, 1, 30));
288 EXPECT_EQ(Rect(10, 10, 20, 20).ToString(), result.ToString());
291 TEST(RectTest, IsEmpty) {
292 EXPECT_TRUE(Rect(0, 0, 0, 0).IsEmpty());
293 EXPECT_TRUE(Rect(0, 0, 0, 0).size().IsEmpty());
294 EXPECT_TRUE(Rect(0, 0, 10, 0).IsEmpty());
295 EXPECT_TRUE(Rect(0, 0, 10, 0).size().IsEmpty());
296 EXPECT_TRUE(Rect(0, 0, 0, 10).IsEmpty());
297 EXPECT_TRUE(Rect(0, 0, 0, 10).size().IsEmpty());
298 EXPECT_FALSE(Rect(0, 0, 10, 10).IsEmpty());
299 EXPECT_FALSE(Rect(0, 0, 10, 10).size().IsEmpty());
302 TEST(RectTest, SplitVertically) {
303 Rect left_half, right_half;
305 // Splitting when origin is (0, 0).
306 Rect(0, 0, 20, 20).SplitVertically(&left_half, &right_half);
307 EXPECT_TRUE(left_half == Rect(0, 0, 10, 20));
308 EXPECT_TRUE(right_half == Rect(10, 0, 10, 20));
310 // Splitting when origin is arbitrary.
311 Rect(10, 10, 20, 10).SplitVertically(&left_half, &right_half);
312 EXPECT_TRUE(left_half == Rect(10, 10, 10, 10));
313 EXPECT_TRUE(right_half == Rect(20, 10, 10, 10));
315 // Splitting a rectangle of zero width.
316 Rect(10, 10, 0, 10).SplitVertically(&left_half, &right_half);
317 EXPECT_TRUE(left_half == Rect(10, 10, 0, 10));
318 EXPECT_TRUE(right_half == Rect(10, 10, 0, 10));
320 // Splitting a rectangle of odd width.
321 Rect(10, 10, 5, 10).SplitVertically(&left_half, &right_half);
322 EXPECT_TRUE(left_half == Rect(10, 10, 2, 10));
323 EXPECT_TRUE(right_half == Rect(12, 10, 3, 10));
326 TEST(RectTest, CenterPoint) {
329 // When origin is (0, 0).
330 center = Rect(0, 0, 20, 20).CenterPoint();
331 EXPECT_TRUE(center == Point(10, 10));
333 // When origin is even.
334 center = Rect(10, 10, 20, 20).CenterPoint();
335 EXPECT_TRUE(center == Point(20, 20));
337 // When origin is odd.
338 center = Rect(11, 11, 20, 20).CenterPoint();
339 EXPECT_TRUE(center == Point(21, 21));
341 // When 0 width or height.
342 center = Rect(10, 10, 0, 20).CenterPoint();
343 EXPECT_TRUE(center == Point(10, 20));
344 center = Rect(10, 10, 20, 0).CenterPoint();
345 EXPECT_TRUE(center == Point(20, 10));
348 center = Rect(10, 10, 21, 21).CenterPoint();
349 EXPECT_TRUE(center == Point(20, 20));
351 // When an odd size and position.
352 center = Rect(11, 11, 21, 21).CenterPoint();
353 EXPECT_TRUE(center == Point(21, 21));
356 TEST(RectTest, CenterPointF) {
359 // When origin is (0, 0).
360 center = RectF(0, 0, 20, 20).CenterPoint();
361 EXPECT_TRUE(center == PointF(10, 10));
363 // When origin is even.
364 center = RectF(10, 10, 20, 20).CenterPoint();
365 EXPECT_TRUE(center == PointF(20, 20));
367 // When origin is odd.
368 center = RectF(11, 11, 20, 20).CenterPoint();
369 EXPECT_TRUE(center == PointF(21, 21));
371 // When 0 width or height.
372 center = RectF(10, 10, 0, 20).CenterPoint();
373 EXPECT_TRUE(center == PointF(10, 20));
374 center = RectF(10, 10, 20, 0).CenterPoint();
375 EXPECT_TRUE(center == PointF(20, 10));
378 center = RectF(10, 10, 21, 21).CenterPoint();
379 EXPECT_TRUE(center == PointF(20.5f, 20.5f));
381 // When an odd size and position.
382 center = RectF(11, 11, 21, 21).CenterPoint();
383 EXPECT_TRUE(center == PointF(21.5f, 21.5f));
386 TEST(RectTest, SharesEdgeWith) {
389 // Must be non-overlapping
390 EXPECT_FALSE(r.SharesEdgeWith(r));
392 Rect just_above(2, 1, 4, 2);
393 Rect just_below(2, 8, 4, 2);
394 Rect just_left(0, 3, 2, 5);
395 Rect just_right(6, 3, 2, 5);
397 EXPECT_TRUE(r.SharesEdgeWith(just_above));
398 EXPECT_TRUE(r.SharesEdgeWith(just_below));
399 EXPECT_TRUE(r.SharesEdgeWith(just_left));
400 EXPECT_TRUE(r.SharesEdgeWith(just_right));
403 Rect same_height_no_edge(0, 0, 1, 5);
404 Rect same_width_no_edge(0, 0, 4, 1);
406 EXPECT_FALSE(r.SharesEdgeWith(same_height_no_edge));
407 EXPECT_FALSE(r.SharesEdgeWith(same_width_no_edge));
409 Rect just_above_no_edge(2, 1, 5, 2); // too wide
410 Rect just_below_no_edge(2, 8, 3, 2); // too narrow
411 Rect just_left_no_edge(0, 3, 2, 6); // too tall
412 Rect just_right_no_edge(6, 3, 2, 4); // too short
414 EXPECT_FALSE(r.SharesEdgeWith(just_above_no_edge));
415 EXPECT_FALSE(r.SharesEdgeWith(just_below_no_edge));
416 EXPECT_FALSE(r.SharesEdgeWith(just_left_no_edge));
417 EXPECT_FALSE(r.SharesEdgeWith(just_right_no_edge));
420 // Similar to EXPECT_FLOAT_EQ, but lets NaN equal NaN
421 #define EXPECT_FLOAT_AND_NAN_EQ(a, b) \
422 { if (a == a || b == b) { EXPECT_FLOAT_EQ(a, b); } }
424 TEST(RectTest, ScaleRect) {
425 static const struct Test {
438 4.5f, 4.5f, 4.5f, 4.5f },
441 0.0f, 0.0f, 0.0f, 0.0f },
443 std::numeric_limits<float>::quiet_NaN(),
444 std::numeric_limits<float>::quiet_NaN(),
445 std::numeric_limits<float>::quiet_NaN(),
446 std::numeric_limits<float>::quiet_NaN(),
447 std::numeric_limits<float>::quiet_NaN() },
449 std::numeric_limits<float>::max(),
450 std::numeric_limits<float>::max(),
451 std::numeric_limits<float>::max(),
452 std::numeric_limits<float>::max(),
453 std::numeric_limits<float>::max() }
456 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
457 Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
458 RectF r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
460 RectF scaled = ScaleRect(r1, tests[i].scale);
461 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), scaled.x());
462 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), scaled.y());
463 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), scaled.width());
464 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), scaled.height());
468 TEST(RectTest, ToEnclosedRect) {
469 static const struct Test {
479 { 0.0f, 0.0f, 0.0f, 0.0f,
481 { -1.5f, -1.5f, 3.0f, 3.0f,
483 { -1.5f, -1.5f, 3.5f, 3.5f,
485 { std::numeric_limits<float>::max(),
486 std::numeric_limits<float>::max(),
488 std::numeric_limits<int>::max(),
489 std::numeric_limits<int>::max(),
492 std::numeric_limits<float>::max(),
493 std::numeric_limits<float>::max(),
495 std::numeric_limits<int>::max(),
496 std::numeric_limits<int>::max() },
497 { 20000.5f, 20000.5f, 0.5f, 0.5f,
498 20001, 20001, 0, 0 },
499 { std::numeric_limits<float>::quiet_NaN(),
500 std::numeric_limits<float>::quiet_NaN(),
501 std::numeric_limits<float>::quiet_NaN(),
502 std::numeric_limits<float>::quiet_NaN(),
506 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
507 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
508 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
510 Rect enclosed = ToEnclosedRect(r1);
511 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), enclosed.x());
512 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), enclosed.y());
513 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), enclosed.width());
514 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), enclosed.height());
518 TEST(RectTest, ToEnclosingRect) {
519 static const struct Test {
529 { 0.0f, 0.0f, 0.0f, 0.0f,
531 { 5.5f, 5.5f, 0.0f, 0.0f,
533 { -1.5f, -1.5f, 3.0f, 3.0f,
535 { -1.5f, -1.5f, 3.5f, 3.5f,
537 { std::numeric_limits<float>::max(),
538 std::numeric_limits<float>::max(),
540 std::numeric_limits<int>::max(),
541 std::numeric_limits<int>::max(),
544 std::numeric_limits<float>::max(),
545 std::numeric_limits<float>::max(),
547 std::numeric_limits<int>::max(),
548 std::numeric_limits<int>::max() },
549 { 20000.5f, 20000.5f, 0.5f, 0.5f,
550 20000, 20000, 1, 1 },
551 { std::numeric_limits<float>::quiet_NaN(),
552 std::numeric_limits<float>::quiet_NaN(),
553 std::numeric_limits<float>::quiet_NaN(),
554 std::numeric_limits<float>::quiet_NaN(),
558 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
559 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
560 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
562 Rect enclosed = ToEnclosingRect(r1);
563 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), enclosed.x());
564 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), enclosed.y());
565 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), enclosed.width());
566 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), enclosed.height());
570 TEST(RectTest, ToNearestRect) {
572 EXPECT_EQ(rect.ToString(), ToNearestRect(RectF(rect)).ToString());
574 rect = Rect(-1, -1, 3, 3);
575 EXPECT_EQ(rect.ToString(), ToNearestRect(RectF(rect)).ToString());
577 RectF rectf(-1.00001f, -0.999999f, 3.0000001f, 2.999999f);
578 EXPECT_EQ(rect.ToString(), ToNearestRect(rectf).ToString());
581 TEST(RectTest, ToFlooredRect) {
582 static const struct Test {
592 { 0.0f, 0.0f, 0.0f, 0.0f,
594 { -1.5f, -1.5f, 3.0f, 3.0f,
596 { -1.5f, -1.5f, 3.5f, 3.5f,
598 { 20000.5f, 20000.5f, 0.5f, 0.5f,
599 20000, 20000, 0, 0 },
602 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
603 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
604 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
606 Rect floored = ToFlooredRectDeprecated(r1);
607 EXPECT_FLOAT_EQ(r2.x(), floored.x());
608 EXPECT_FLOAT_EQ(r2.y(), floored.y());
609 EXPECT_FLOAT_EQ(r2.width(), floored.width());
610 EXPECT_FLOAT_EQ(r2.height(), floored.height());
614 TEST(RectTest, ScaleToEnclosedRect) {
615 static const struct Test {
651 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
652 Rect result = ScaleToEnclosedRect(tests[i].input_rect,
653 tests[i].input_scale);
654 EXPECT_EQ(tests[i].expected_rect.ToString(), result.ToString());
658 TEST(RectTest, ScaleToEnclosingRect) {
659 static const struct Test {
695 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
696 Rect result = ScaleToEnclosingRect(tests[i].input_rect,
697 tests[i].input_scale);
698 EXPECT_EQ(tests[i].expected_rect.ToString(), result.ToString());
703 TEST(RectTest, ConstructAndAssign) {
704 const RECT rect_1 = { 0, 0, 10, 10 };
705 const RECT rect_2 = { 0, 0, -10, -10 };
711 TEST(RectTest, ToRectF) {
712 // Check that implicit conversion from integer to float compiles.
713 Rect a(10, 20, 30, 40);
714 RectF b(10, 20, 30, 40);
716 RectF intersect = IntersectRects(a, b);
717 EXPECT_EQ(b.ToString(), intersect.ToString());
723 TEST(RectTest, BoundingRect) {
729 // If point B dominates A, then A should be the origin.
730 { Point(4, 6), Point(4, 6), Rect(4, 6, 0, 0) },
731 { Point(4, 6), Point(8, 6), Rect(4, 6, 4, 0) },
732 { Point(4, 6), Point(4, 9), Rect(4, 6, 0, 3) },
733 { Point(4, 6), Point(8, 9), Rect(4, 6, 4, 3) },
734 // If point A dominates B, then B should be the origin.
735 { Point(4, 6), Point(4, 6), Rect(4, 6, 0, 0) },
736 { Point(8, 6), Point(4, 6), Rect(4, 6, 4, 0) },
737 { Point(4, 9), Point(4, 6), Rect(4, 6, 0, 3) },
738 { Point(8, 9), Point(4, 6), Rect(4, 6, 4, 3) },
739 // If neither point dominates, then the origin is a combination of the two.
740 { Point(4, 6), Point(6, 4), Rect(4, 4, 2, 2) },
741 { Point(-4, -6), Point(-6, -4), Rect(-6, -6, 2, 2) },
742 { Point(-4, 6), Point(6, -4), Rect(-4, -4, 10, 10) },
745 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_tests); ++i) {
746 Rect actual = BoundingRect(int_tests[i].a, int_tests[i].b);
747 EXPECT_EQ(int_tests[i].expected.ToString(), actual.ToString());
755 // If point B dominates A, then A should be the origin.
756 { PointF(4.2f, 6.8f), PointF(4.2f, 6.8f),
757 RectF(4.2f, 6.8f, 0, 0) },
758 { PointF(4.2f, 6.8f), PointF(8.5f, 6.8f),
759 RectF(4.2f, 6.8f, 4.3f, 0) },
760 { PointF(4.2f, 6.8f), PointF(4.2f, 9.3f),
761 RectF(4.2f, 6.8f, 0, 2.5f) },
762 { PointF(4.2f, 6.8f), PointF(8.5f, 9.3f),
763 RectF(4.2f, 6.8f, 4.3f, 2.5f) },
764 // If point A dominates B, then B should be the origin.
765 { PointF(4.2f, 6.8f), PointF(4.2f, 6.8f),
766 RectF(4.2f, 6.8f, 0, 0) },
767 { PointF(8.5f, 6.8f), PointF(4.2f, 6.8f),
768 RectF(4.2f, 6.8f, 4.3f, 0) },
769 { PointF(4.2f, 9.3f), PointF(4.2f, 6.8f),
770 RectF(4.2f, 6.8f, 0, 2.5f) },
771 { PointF(8.5f, 9.3f), PointF(4.2f, 6.8f),
772 RectF(4.2f, 6.8f, 4.3f, 2.5f) },
773 // If neither point dominates, then the origin is a combination of the two.
774 { PointF(4.2f, 6.8f), PointF(6.8f, 4.2f),
775 RectF(4.2f, 4.2f, 2.6f, 2.6f) },
776 { PointF(-4.2f, -6.8f), PointF(-6.8f, -4.2f),
777 RectF(-6.8f, -6.8f, 2.6f, 2.6f) },
778 { PointF(-4.2f, 6.8f), PointF(6.8f, -4.2f),
779 RectF(-4.2f, -4.2f, 11.0f, 11.0f) }
782 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i) {
783 RectF actual = BoundingRect(float_tests[i].a, float_tests[i].b);
784 EXPECT_EQ(float_tests[i].expected.ToString(), actual.ToString());
788 TEST(RectTest, IsExpressibleAsRect) {
789 EXPECT_TRUE(RectF().IsExpressibleAsRect());
791 float min = std::numeric_limits<int>::min();
792 float max = std::numeric_limits<int>::max();
793 float infinity = std::numeric_limits<float>::infinity();
796 min + 200, min + 200, max - 200, max - 200).IsExpressibleAsRect());
798 min - 200, min + 200, max + 200, max + 200).IsExpressibleAsRect());
800 min + 200 , min - 200, max + 200, max + 200).IsExpressibleAsRect());
802 min + 200, min + 200, max + 200, max - 200).IsExpressibleAsRect());
804 min + 200, min + 200, max - 200, max + 200).IsExpressibleAsRect());
806 EXPECT_TRUE(RectF(0, 0, max - 200, max - 200).IsExpressibleAsRect());
807 EXPECT_FALSE(RectF(200, 0, max + 200, max - 200).IsExpressibleAsRect());
808 EXPECT_FALSE(RectF(0, 200, max - 200, max + 200).IsExpressibleAsRect());
809 EXPECT_FALSE(RectF(0, 0, max + 200, max - 200).IsExpressibleAsRect());
810 EXPECT_FALSE(RectF(0, 0, max - 200, max + 200).IsExpressibleAsRect());
812 EXPECT_FALSE(RectF(infinity, 0, 1, 1).IsExpressibleAsRect());
813 EXPECT_FALSE(RectF(0, infinity, 1, 1).IsExpressibleAsRect());
814 EXPECT_FALSE(RectF(0, 0, infinity, 1).IsExpressibleAsRect());
815 EXPECT_FALSE(RectF(0, 0, 1, infinity).IsExpressibleAsRect());
818 TEST(RectTest, Offset) {
821 EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), (i + Vector2d(1, -1)).ToString());
822 EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), (Vector2d(1, -1) + i).ToString());
823 i += Vector2d(1, -1);
824 EXPECT_EQ(Rect(2, 1, 3, 4).ToString(), i.ToString());
825 EXPECT_EQ(Rect(1, 2, 3, 4).ToString(), (i - Vector2d(1, -1)).ToString());
826 i -= Vector2d(1, -1);
827 EXPECT_EQ(Rect(1, 2, 3, 4).ToString(), i.ToString());
829 RectF f(1.1f, 2.2f, 3.3f, 4.4f);
830 EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f).ToString(),
831 (f + Vector2dF(1.1f, -1.1f)).ToString());
832 EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f).ToString(),
833 (Vector2dF(1.1f, -1.1f) + f).ToString());
834 f += Vector2dF(1.1f, -1.1f);
835 EXPECT_EQ(RectF(2.2f, 1.1f, 3.3f, 4.4f).ToString(), f.ToString());
836 EXPECT_EQ(RectF(1.1f, 2.2f, 3.3f, 4.4f).ToString(),
837 (f - Vector2dF(1.1f, -1.1f)).ToString());
838 f -= Vector2dF(1.1f, -1.1f);
839 EXPECT_EQ(RectF(1.1f, 2.2f, 3.3f, 4.4f).ToString(), f.ToString());
842 TEST(RectTest, Corners) {
844 RectF f(1.1f, 2.1f, 3.1f, 4.1f);
846 EXPECT_EQ(Point(1, 2).ToString(), i.origin().ToString());
847 EXPECT_EQ(Point(4, 2).ToString(), i.top_right().ToString());
848 EXPECT_EQ(Point(1, 6).ToString(), i.bottom_left().ToString());
849 EXPECT_EQ(Point(4, 6).ToString(), i.bottom_right().ToString());
851 EXPECT_EQ(PointF(1.1f, 2.1f).ToString(), f.origin().ToString());
852 EXPECT_EQ(PointF(4.2f, 2.1f).ToString(), f.top_right().ToString());
853 EXPECT_EQ(PointF(1.1f, 6.2f).ToString(), f.bottom_left().ToString());
854 EXPECT_EQ(PointF(4.2f, 6.2f).ToString(), f.bottom_right().ToString());
857 TEST(RectTest, ManhattanDistanceToPoint) {
859 EXPECT_EQ(0, i.ManhattanDistanceToPoint(Point(1, 2)));
860 EXPECT_EQ(0, i.ManhattanDistanceToPoint(Point(4, 6)));
861 EXPECT_EQ(0, i.ManhattanDistanceToPoint(Point(2, 4)));
862 EXPECT_EQ(3, i.ManhattanDistanceToPoint(Point(0, 0)));
863 EXPECT_EQ(2, i.ManhattanDistanceToPoint(Point(2, 0)));
864 EXPECT_EQ(3, i.ManhattanDistanceToPoint(Point(5, 0)));
865 EXPECT_EQ(1, i.ManhattanDistanceToPoint(Point(5, 4)));
866 EXPECT_EQ(3, i.ManhattanDistanceToPoint(Point(5, 8)));
867 EXPECT_EQ(2, i.ManhattanDistanceToPoint(Point(3, 8)));
868 EXPECT_EQ(2, i.ManhattanDistanceToPoint(Point(0, 7)));
869 EXPECT_EQ(1, i.ManhattanDistanceToPoint(Point(0, 3)));
871 RectF f(1.1f, 2.1f, 3.1f, 4.1f);
872 EXPECT_FLOAT_EQ(0.f, f.ManhattanDistanceToPoint(PointF(1.1f, 2.1f)));
873 EXPECT_FLOAT_EQ(0.f, f.ManhattanDistanceToPoint(PointF(4.2f, 6.f)));
874 EXPECT_FLOAT_EQ(0.f, f.ManhattanDistanceToPoint(PointF(2.f, 4.f)));
875 EXPECT_FLOAT_EQ(3.2f, f.ManhattanDistanceToPoint(PointF(0.f, 0.f)));
876 EXPECT_FLOAT_EQ(2.1f, f.ManhattanDistanceToPoint(PointF(2.f, 0.f)));
877 EXPECT_FLOAT_EQ(2.9f, f.ManhattanDistanceToPoint(PointF(5.f, 0.f)));
878 EXPECT_FLOAT_EQ(.8f, f.ManhattanDistanceToPoint(PointF(5.f, 4.f)));
879 EXPECT_FLOAT_EQ(2.6f, f.ManhattanDistanceToPoint(PointF(5.f, 8.f)));
880 EXPECT_FLOAT_EQ(1.8f, f.ManhattanDistanceToPoint(PointF(3.f, 8.f)));
881 EXPECT_FLOAT_EQ(1.9f, f.ManhattanDistanceToPoint(PointF(0.f, 7.f)));
882 EXPECT_FLOAT_EQ(1.1f, f.ManhattanDistanceToPoint(PointF(0.f, 3.f)));
885 TEST(RectTest, ManhattanInternalDistance) {
886 Rect i(0, 0, 400, 400);
887 EXPECT_EQ(0, i.ManhattanInternalDistance(gfx::Rect(-1, 0, 2, 1)));
888 EXPECT_EQ(1, i.ManhattanInternalDistance(gfx::Rect(400, 0, 1, 400)));
889 EXPECT_EQ(2, i.ManhattanInternalDistance(gfx::Rect(-100, -100, 100, 100)));
890 EXPECT_EQ(2, i.ManhattanInternalDistance(gfx::Rect(-101, 100, 100, 100)));
891 EXPECT_EQ(4, i.ManhattanInternalDistance(gfx::Rect(-101, -101, 100, 100)));
892 EXPECT_EQ(435, i.ManhattanInternalDistance(gfx::Rect(630, 603, 100, 100)));
894 RectF f(0.0f, 0.0f, 400.0f, 400.0f);
895 static const float kEpsilon = std::numeric_limits<float>::epsilon();
898 0.0f, f.ManhattanInternalDistance(gfx::RectF(-1.0f, 0.0f, 2.0f, 1.0f)));
901 f.ManhattanInternalDistance(gfx::RectF(400.0f, 0.0f, 1.0f, 400.0f)));
902 EXPECT_FLOAT_EQ(2.0f * kEpsilon,
903 f.ManhattanInternalDistance(
904 gfx::RectF(-100.0f, -100.0f, 100.0f, 100.0f)));
907 f.ManhattanInternalDistance(gfx::RectF(-101.0f, 100.0f, 100.0f, 100.0f)));
908 EXPECT_FLOAT_EQ(2.0f + 2.0f * kEpsilon,
909 f.ManhattanInternalDistance(
910 gfx::RectF(-101.0f, -101.0f, 100.0f, 100.0f)));
912 433.0f + 2.0f * kEpsilon,
913 f.ManhattanInternalDistance(gfx::RectF(630.0f, 603.0f, 100.0f, 100.0f)));
916 0.0f, f.ManhattanInternalDistance(gfx::RectF(-1.0f, 0.0f, 1.1f, 1.0f)));
919 f.ManhattanInternalDistance(gfx::RectF(-1.5f, 0.0f, 1.4f, 1.0f)));
922 f.ManhattanInternalDistance(gfx::RectF(-1.5f, 0.0f, 1.5f, 1.0f)));