53TEST(RectTest, Intersects) {
64 } tests[] = {{0, 0, 0, 0, 0, 0, 0, 0,
false},
65 {0, 0, 0, 0, -10, -10, 20, 20,
false},
66 {-10, 0, 0, 20, 0, -10, 20, 0,
false},
67 {0, 0, 10, 10, 0, 0, 10, 10,
true},
68 {0, 0, 10, 10, 10, 10, 10, 10,
false},
69 {10, 10, 10, 10, 0, 0, 10, 10,
false},
70 {10, 10, 10, 10, 5, 5, 10, 10,
true},
71 {10, 10, 10, 10, 15, 15, 10, 10,
true},
72 {10, 10, 10, 10, 20, 15, 10, 10,
false},
73 {10, 10, 10, 10, 21, 15, 10, 10,
false}};
74 for (
size_t i = 0;
i <
size(tests); ++
i) {
75 Rect r1(tests[
i].x1, tests[
i].y1, tests[
i].w1, tests[
i].h1);
76 Rect r2(tests[
i].x2, tests[
i].y2, tests[
i].w2, tests[
i].h2);
121 static const struct Test {
134 } tests[] = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
135 {0, 0, 4, 4, 0, 0, 4, 4, 0, 0, 4, 4},
136 {0, 0, 4, 4, 4, 4, 4, 4, 0, 0, 8, 8},
137 {0, 0, 4, 4, 0, 5, 4, 4, 0, 0, 4, 9},
138 {0, 0, 2, 2, 3, 3, 2, 2, 0, 0, 5, 5},
140 0, 0, 2, 2, 0, 0, 5, 5},
142 2, 2, 2, 2, 2, 2, 2, 2}};
143 for (
size_t i = 0;
i <
size(tests); ++
i) {
144 Rect r1(tests[
i].x1, tests[
i].y1, tests[
i].w1, tests[
i].h1);
145 Rect r2(tests[
i].x2, tests[
i].y2, tests[
i].w2, tests[
i].h2);
146 Rect r3(tests[
i].x3, tests[
i].y3, tests[
i].w3, tests[
i].h3);
148 EXPECT_EQ(r3.
x(), u.
x());
149 EXPECT_EQ(r3.
y(), u.
y());
165 static const struct Test {
178 } tests[] = {{0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 2, 2},
179 {2, 2, 3, 3, 0, 0, 4, 4, 1, 1, 3, 3},
180 {-1, -1, 5, 5, 0, 0, 4, 4, 0, 0, 4, 4},
181 {2, 2, 4, 4, 0, 0, 3, 3, 0, 0, 3, 3},
182 {2, 2, 1, 1, 0, 0, 3, 3, 2, 2, 1, 1}};
183 for (
size_t i = 0;
i <
size(tests); ++
i) {
184 Rect r1(tests[
i].x1, tests[
i].y1, tests[
i].w1, tests[
i].h1);
185 Rect r2(tests[
i].x2, tests[
i].y2, tests[
i].w2, tests[
i].h2);
186 Rect r3(tests[
i].x3, tests[
i].y3, tests[
i].w3, tests[
i].h3);
189 EXPECT_EQ(r3.
x(), u.
x());
190 EXPECT_EQ(r3.
y(), u.
y());
200 result =
Rect(10, 10, 20, 20);
202 EXPECT_EQ(
Rect(0, 0, 0, 0), result);
205 result =
Rect(10, 10, 20, 20);
207 EXPECT_EQ(
Rect(0, 0, 0, 0), result);
210 result =
Rect(10, 10, 20, 20);
212 EXPECT_EQ(
Rect(10, 10, 20, 20), result);
215 result =
Rect(10, 10, 20, 20);
217 EXPECT_EQ(
Rect(10, 10, 20, 20), result);
220 result =
Rect(10, 10, 20, 20);
222 EXPECT_EQ(
Rect(10, 10, 20, 5), result);
225 result =
Rect(10, 10, 20, 20);
227 EXPECT_EQ(
Rect(10, 10, 20, 5), result);
230 result =
Rect(10, 10, 20, 20);
232 EXPECT_EQ(
Rect(10, 25, 20, 5), result);
236 result =
Rect(10, 10, 20, 20);
238 EXPECT_EQ(
Rect(10, 10, 20, 20), result);
241 result =
Rect(10, 10, 20, 20);
243 EXPECT_EQ(
Rect(20, 10, 10, 20), result);
246 result =
Rect(10, 10, 20, 20);
248 EXPECT_EQ(
Rect(25, 10, 5, 20), result);
251 result =
Rect(10, 10, 20, 20);
253 EXPECT_EQ(
Rect(10, 10, 10, 20), result);
257 result =
Rect(10, 10, 20, 20);
259 EXPECT_EQ(
Rect(10, 10, 20, 20), result);
435 static const int max_int = std::numeric_limits<int>::max();
436 static const int min_int = std::numeric_limits<int>::min();
437 static const float max_float = std::numeric_limits<float>::max();
438 static const float max_int_f =
static_cast<float>(max_int);
439 static const float min_int_f =
static_cast<float>(min_int);
441 static const struct Test {
455 {{0.0f, 0.0f, 0.0f, 0.0f}, {0, 0, 0, 0}},
456 {{-1.5f, -1.5f, 3.0f, 3.0f}, {-1, -1, 2, 2}},
457 {{-1.5f, -1.5f, 3.5f, 3.5f}, {-1, -1, 3, 3}},
458 {{max_float, max_float, 2.0f, 2.0f}, {max_int, max_int, 0, 0}},
459 {{0.0f, 0.0f, max_float, max_float}, {0, 0, max_int, max_int}},
460 {{20000.5f, 20000.5f, 0.5f, 0.5f}, {20001, 20001, 0, 0}},
461 {{max_int_f, max_int_f, max_int_f, max_int_f}, {max_int, max_int, 0, 0}},
462 {{1.9999f, 2.0002f, 5.9998f, 6.0001f}, {2, 3, 5, 5}},
463 {{1.9999f, 2.0001f, 6.0002f, 5.9998f}, {2, 3, 6, 4}},
464 {{1.9998f, 2.0002f, 6.0001f, 5.9999f}, {2, 3, 5, 5}}};
466 for (
size_t i = 0;
i <
size(tests); ++
i) {
467 RectF source(tests[
i].in.x, tests[
i].in.y, tests[
i].in.width,
471 EXPECT_EQ(tests[
i].expected.x, enclosed.
x());
472 EXPECT_EQ(tests[
i].expected.y, enclosed.
y());
473 EXPECT_EQ(tests[
i].expected.width, enclosed.
width());
474 EXPECT_EQ(tests[
i].expected.height, enclosed.
height());
478 RectF source(min_int_f, min_int_f, max_int_f * 3.f, max_int_f * 3.f);
482 EXPECT_EQ(max_int, enclosed.
width());
483 EXPECT_EQ(max_int, enclosed.
height());
485 EXPECT_GT(1, enclosed.
x());
486 EXPECT_GT(1, enclosed.
y());
488 EXPECT_LT(0, enclosed.
right());
489 EXPECT_LT(0, enclosed.
bottom());
494 static const int max_int = std::numeric_limits<int>::max();
495 static const int min_int = std::numeric_limits<int>::min();
496 static const float max_float = std::numeric_limits<float>::max();
497 static const float epsilon_float = std::numeric_limits<float>::epsilon();
498 static const float max_int_f =
static_cast<float>(max_int);
499 static const float min_int_f =
static_cast<float>(min_int);
500 static const struct Test {
514 {{0.0f, 0.0f, 0.0f, 0.0f}, {0, 0, 0, 0}},
515 {{5.5f, 5.5f, 0.0f, 0.0f}, {5, 5, 0, 0}},
516 {{3.5f, 2.5f, epsilon_float, -0.0f}, {3, 2, 0, 0}},
517 {{3.5f, 2.5f, 0.f, 0.001f}, {3, 2, 0, 1}},
518 {{-1.5f, -1.5f, 3.0f, 3.0f}, {-2, -2, 4, 4}},
519 {{-1.5f, -1.5f, 3.5f, 3.5f}, {-2, -2, 4, 4}},
520 {{max_float, max_float, 2.0f, 2.0f}, {max_int, max_int, 0, 0}},
521 {{0.0f, 0.0f, max_float, max_float}, {0, 0, max_int, max_int}},
522 {{20000.5f, 20000.5f, 0.5f, 0.5f}, {20000, 20000, 1, 1}},
523 {{max_int_f, max_int_f, max_int_f, max_int_f}, {max_int, max_int, 0, 0}},
524 {{-0.5f, -0.5f, 22777712.f, 1.f}, {-1, -1, 22777713, 2}},
525 {{1.9999f, 2.0002f, 5.9998f, 6.0001f}, {1, 2, 7, 7}},
526 {{1.9999f, 2.0001f, 6.0002f, 5.9998f}, {1, 2, 8, 6}},
527 {{1.9998f, 2.0002f, 6.0001f, 5.9999f}, {1, 2, 7, 7}}};
529 for (
size_t i = 0;
i <
size(tests); ++
i) {
530 RectF source(tests[
i].in.x, tests[
i].in.y, tests[
i].in.width,
534 EXPECT_EQ(tests[
i].expected.x, enclosing.
x());
535 EXPECT_EQ(tests[
i].expected.y, enclosing.
y());
536 EXPECT_EQ(tests[
i].expected.width, enclosing.
width());
537 EXPECT_EQ(tests[
i].expected.height, enclosing.
height());
541 RectF source(min_int_f, min_int_f, max_int_f * 3.f, max_int_f * 3.f);
545 EXPECT_EQ(max_int, enclosing.
width());
546 EXPECT_EQ(max_int, enclosing.
height());
548 EXPECT_GT(1, enclosing.
x());
549 EXPECT_GT(1, enclosing.
y());
551 EXPECT_LT(0, enclosing.
right());
552 EXPECT_LT(0, enclosing.
bottom());
557 static const int max_int = std::numeric_limits<int>::max();
558 static const float max_float = std::numeric_limits<float>::max();
559 static const float epsilon_float = std::numeric_limits<float>::epsilon();
560 static const float max_int_f =
static_cast<float>(max_int);
561 static const float error = 0.001f;
562 static const struct Test {
576 {{0.0f, 0.0f, 0.0f, 0.0f}, {0, 0, 0, 0}},
577 {{5.5f, 5.5f, 0.0f, 0.0f}, {5, 5, 0, 0}},
578 {{3.5f, 2.5f, epsilon_float, -0.0f}, {3, 2, 0, 0}},
579 {{3.5f, 2.5f, 0.f, 0.001f}, {3, 2, 0, 1}},
580 {{-1.5f, -1.5f, 3.0f, 3.0f}, {-2, -2, 4, 4}},
581 {{-1.5f, -1.5f, 3.5f, 3.5f}, {-2, -2, 4, 4}},
582 {{max_float, max_float, 2.0f, 2.0f}, {max_int, max_int, 0, 0}},
583 {{0.0f, 0.0f, max_float, max_float}, {0, 0, max_int, max_int}},
584 {{20000.5f, 20000.5f, 0.5f, 0.5f}, {20000, 20000, 1, 1}},
585 {{max_int_f, max_int_f, max_int_f, max_int_f}, {max_int, max_int, 0, 0}},
586 {{-0.5f, -0.5f, 22777712.f, 1.f}, {-1, -1, 22777713, 2}},
587 {{1.9999f, 2.0002f, 5.9998f, 6.0001f}, {2, 2, 6, 6}},
588 {{1.9999f, 2.0001f, 6.0002f, 5.9998f}, {2, 2, 6, 6}},
589 {{1.9998f, 2.0002f, 6.0001f, 5.9999f}, {2, 2, 6, 6}}};
591 for (
size_t i = 0;
i <
size(tests); ++
i) {
592 RectF source(tests[
i].in.x, tests[
i].in.y, tests[
i].in.width,
596 EXPECT_EQ(tests[
i].expected.x, enclosing.
x());
597 EXPECT_EQ(tests[
i].expected.y, enclosing.
y());
598 EXPECT_EQ(tests[
i].expected.width, enclosing.
width());
599 EXPECT_EQ(tests[
i].expected.height, enclosing.
height());
817TEST(RectTest, IsExpressibleAsRect) {
818 EXPECT_TRUE(
RectF().IsExpressibleAsRect());
820 float min =
static_cast<float>(std::numeric_limits<int>::min());
821 float max =
static_cast<float>(std::numeric_limits<int>::max());
822 float infinity = std::numeric_limits<float>::infinity();
825 RectF(min + 200, min + 200, max - 200, max - 200).IsExpressibleAsRect());
827 RectF(min - 200, min + 200, max + 200, max + 200).IsExpressibleAsRect());
829 RectF(min + 200, min - 200, max + 200, max + 200).IsExpressibleAsRect());
831 RectF(min + 200, min + 200, max + 200, max - 200).IsExpressibleAsRect());
833 RectF(min + 200, min + 200, max - 200, max + 200).IsExpressibleAsRect());
835 EXPECT_TRUE(
RectF(0, 0, max - 200, max - 200).IsExpressibleAsRect());
836 EXPECT_FALSE(
RectF(200, 0, max + 200, max - 200).IsExpressibleAsRect());
837 EXPECT_FALSE(
RectF(0, 200, max - 200, max + 200).IsExpressibleAsRect());
838 EXPECT_FALSE(
RectF(0, 0, max + 200, max - 200).IsExpressibleAsRect());
839 EXPECT_FALSE(
RectF(0, 0, max - 200, max + 200).IsExpressibleAsRect());
841 EXPECT_FALSE(
RectF(infinity, 0, 1, 1).IsExpressibleAsRect());
842 EXPECT_FALSE(
RectF(0, infinity, 1, 1).IsExpressibleAsRect());
843 EXPECT_FALSE(
RectF(0, 0, infinity, 1).IsExpressibleAsRect());
844 EXPECT_FALSE(
RectF(0, 0, 1, infinity).IsExpressibleAsRect());
936TEST(RectTest, ManhattanInternalDistance) {
937 Rect i(0, 0, 400, 400);
938 EXPECT_EQ(0,
i.ManhattanInternalDistance(
gfx::Rect(-1, 0, 2, 1)));
939 EXPECT_EQ(1,
i.ManhattanInternalDistance(
gfx::Rect(400, 0, 1, 400)));
940 EXPECT_EQ(2,
i.ManhattanInternalDistance(
gfx::Rect(-100, -100, 100, 100)));
941 EXPECT_EQ(2,
i.ManhattanInternalDistance(
gfx::Rect(-101, 100, 100, 100)));
942 EXPECT_EQ(4,
i.ManhattanInternalDistance(
gfx::Rect(-101, -101, 100, 100)));
943 EXPECT_EQ(435,
i.ManhattanInternalDistance(
gfx::Rect(630, 603, 100, 100)));
945 RectF f(0.0f, 0.0f, 400.0f, 400.0f);
946 static const float kEpsilon = std::numeric_limits<float>::epsilon();
953 -100.0f, -100.0f, 100.0f, 100.0f)));
955 -101.0f, 100.0f, 100.0f, 100.0f)));
956 EXPECT_FLOAT_EQ(2.0f + 2.0f * kEpsilon,
958 gfx::RectF(-101.0f, -101.0f, 100.0f, 100.0f)));
960 433.0f + 2.0f * kEpsilon,
971TEST(RectTest, IntegerOverflow) {
972 int limit = std::numeric_limits<int>::max();
973 int min_limit = std::numeric_limits<int>::min();
975 int large_number = limit - expected;
977 Rect height_overflow(0, large_number, 100, 100);
978 EXPECT_EQ(large_number, height_overflow.
y());
979 EXPECT_EQ(expected, height_overflow.
height());
981 Rect width_overflow(large_number, 0, 100, 100);
982 EXPECT_EQ(large_number, width_overflow.
x());
983 EXPECT_EQ(expected, width_overflow.
width());
985 Rect size_height_overflow(
Point(0, large_number),
Size(100, 100));
986 EXPECT_EQ(large_number, size_height_overflow.
y());
987 EXPECT_EQ(expected, size_height_overflow.
height());
989 Rect size_width_overflow(
Point(large_number, 0),
Size(100, 100));
990 EXPECT_EQ(large_number, size_width_overflow.
x());
991 EXPECT_EQ(expected, size_width_overflow.
width());
993 Rect set_height_overflow(0, large_number, 100, 5);
994 EXPECT_EQ(5, set_height_overflow.
height());
996 EXPECT_EQ(expected, set_height_overflow.
height());
998 Rect set_y_overflow(100, 100, 100, 100);
999 EXPECT_EQ(100, set_y_overflow.
height());
1000 set_y_overflow.
set_y(large_number);
1001 EXPECT_EQ(expected, set_y_overflow.
height());
1003 Rect set_width_overflow(large_number, 0, 5, 100);
1004 EXPECT_EQ(5, set_width_overflow.
width());
1006 EXPECT_EQ(expected, set_width_overflow.
width());
1008 Rect set_x_overflow(100, 100, 100, 100);
1009 EXPECT_EQ(100, set_x_overflow.
width());
1010 set_x_overflow.
set_x(large_number);
1011 EXPECT_EQ(expected, set_x_overflow.
width());
1013 Point large_offset(large_number, large_number);
1015 Size expected_size(10, 10);
1017 Rect set_origin_overflow(100, 100, 100, 100);
1018 EXPECT_EQ(
size, set_origin_overflow.
size());
1019 set_origin_overflow.
set_origin(large_offset);
1020 EXPECT_EQ(large_offset, set_origin_overflow.
origin());
1021 EXPECT_EQ(expected_size, set_origin_overflow.
size());
1023 Rect set_size_overflow(large_number, large_number, 5, 5);
1024 EXPECT_EQ(
Size(5, 5), set_size_overflow.
size());
1026 EXPECT_EQ(large_offset, set_size_overflow.
origin());
1027 EXPECT_EQ(expected_size, set_size_overflow.
size());
1029 Rect set_rect_overflow;
1030 set_rect_overflow.
SetRect(large_number, large_number, 100, 100);
1031 EXPECT_EQ(large_offset, set_rect_overflow.
origin());
1032 EXPECT_EQ(expected_size, set_rect_overflow.
size());
1035 Rect inset_overflow;
1036 inset_overflow.
Inset(large_number, large_number, 100, 100);
1037 EXPECT_EQ(large_offset, inset_overflow.
origin());
1043 Rect inset_overflow2;
1044 inset_overflow2.
Inset(min_limit, min_limit, min_limit, min_limit);
1045 EXPECT_EQ(inset_overflow2,
gfx::Rect(min_limit, min_limit, limit, limit));
1053 Rect inset_overflow3(0, 0, limit, limit);
1054 inset_overflow3.
Inset(-100, -100, 100, 100);
1055 EXPECT_EQ(inset_overflow3,
gfx::Rect(-100, -100, limit, limit));
1057 Rect inset_overflow4(-1000, -1000, limit, limit);
1058 inset_overflow4.
Inset(100, 100, -100, -100);
1059 EXPECT_EQ(inset_overflow4,
gfx::Rect(-900, -900, limit, limit));
1061 Rect offset_overflow(0, 0, 100, 100);
1062 offset_overflow.
Offset(large_number, large_number);
1063 EXPECT_EQ(large_offset, offset_overflow.
origin());
1064 EXPECT_EQ(expected_size, offset_overflow.
size());
1066 Rect operator_overflow(0, 0, 100, 100);
1067 operator_overflow +=
Vector2d(large_number, large_number);
1068 EXPECT_EQ(large_offset, operator_overflow.
origin());
1069 EXPECT_EQ(expected_size, operator_overflow.
size());
1071 Rect origin_maxint(limit, limit, limit, limit);
1077 Rect origin_small(0, 0, 100, 100);
1078 Rect big_clamped(50, 50, limit, limit);
1079 EXPECT_EQ(big_clamped.
right(), limit);
1082 Rect rect_limit(0, 0, limit, limit);
1083 EXPECT_EQ(unioned, rect_limit);
1089 Rect small(-500, -400, 100, 100);
1090 Rect big(-400, -500, limit, limit);
1092 EXPECT_EQ(
UnionRects(small, big),
Rect(-500, -500, limit, limit));
1097 Rect clamped(500, 500, limit, limit);
1098 Rect positive_origin(100, 100, 500, 500);
1103 Rect expected(100, 100, limit - 100, limit - 100);
1104 EXPECT_EQ(
UnionRects(clamped, positive_origin), expected);
1111 int part_limit = min_limit / 3;
1112 Rect left_minint(min_limit, min_limit, 1, 1);
1113 Rect right_maxint(limit - 1, limit - 1, limit, limit);
1114 Rect expected(part_limit, part_limit, 2 * part_limit, 2 * part_limit);
1118 EXPECT_EQ(limit, result.
height());
1119 EXPECT_EQ(limit, result.
width());
1122 EXPECT_GT(-part_limit, result.
x());
1123 EXPECT_LT(part_limit, result.
right());
1124 EXPECT_GT(-part_limit, result.
y());
1125 EXPECT_LT(part_limit, result.
bottom());
1128 EXPECT_TRUE(
UnionRects(left_minint, right_maxint).Contains(expected));
1133 const int max_int = std::numeric_limits<int>::max();
1134 const int min_int = std::numeric_limits<int>::min();
1135 const float max_float = std::numeric_limits<float>::max();
1137 Rect xy_underflow(-100000, -123456, 10, 20);
1139 Rect(min_int, min_int, 1000000, 2000000));
1143 Rect xy_overflow(100000, 123456, 10, 20);
1145 Rect(max_int, max_int, 0, 0));
1149 Rect size_underflow(-1, -2, 100000, 100000);
1151 Rect(100000, 200000, 0, 0));
1153 Rect size_overflow(-1, -2, 123456, 234567);
1155 Rect(-100000, -200000, max_int, max_int));
1157 Rect size_overflow2(1, 2, 123456, 234567);
1159 Rect(100000, 200000, max_int - 100000, max_int - 200000));
1161 Rect max_rect(max_int, max_int, max_int, max_int);
1163 Rect(max_int, max_int, 0, 0));
1165 Rect min_rect(min_int, min_int, max_int, max_int);