OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <limits> | 5 #include <limits> |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
(...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), scaled.y()); | 466 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), scaled.y()); |
467 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), scaled.width()); | 467 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), scaled.width()); |
468 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), scaled.height()); | 468 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), scaled.height()); |
469 } | 469 } |
470 } | 470 } |
471 | 471 |
472 TEST(RectTest, ToEnclosedRect) { | 472 TEST(RectTest, ToEnclosedRect) { |
473 static const int max_int = std::numeric_limits<int>::max(); | 473 static const int max_int = std::numeric_limits<int>::max(); |
474 static const int min_int = std::numeric_limits<int>::min(); | 474 static const int min_int = std::numeric_limits<int>::min(); |
475 static const float max_float = std::numeric_limits<float>::max(); | 475 static const float max_float = std::numeric_limits<float>::max(); |
| 476 static const float max_int_f = static_cast<float>(max_int); |
| 477 static const float min_int_f = static_cast<float>(min_int); |
| 478 |
476 static const struct Test { | 479 static const struct Test { |
477 float x1; // source | 480 struct { |
478 float y1; | 481 float x; |
479 float w1; | 482 float y; |
480 float h1; | 483 float width; |
481 int x2; // target | 484 float height; |
482 int y2; | 485 } in; |
483 int w2; | 486 struct { |
484 int h2; | 487 int x; |
485 } tests[] = {{0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 0, 0}, | 488 int y; |
486 {-1.5f, -1.5f, 3.0f, 3.0f, -1, -1, 2, 2}, | 489 int width; |
487 {-1.5f, -1.5f, 3.5f, 3.5f, -1, -1, 3, 3}, | 490 int height; |
488 {max_float, max_float, 2.0f, 2.0f, max_int, max_int, 0, 0}, | 491 } expected; |
489 {0.0f, 0.0f, max_float, max_float, 0, 0, max_int, max_int}, | 492 } tests[] = { |
490 {20000.5f, 20000.5f, 0.5f, 0.5f, 20001, 20001, 0, 0}, | 493 {{0.0f, 0.0f, 0.0f, 0.0f}, {0, 0, 0, 0}}, |
491 {static_cast<float>(min_int), | 494 {{-1.5f, -1.5f, 3.0f, 3.0f}, {-1, -1, 2, 2}}, |
492 static_cast<float>(min_int), | 495 {{-1.5f, -1.5f, 3.5f, 3.5f}, {-1, -1, 3, 3}}, |
493 max_int * 2.f, | 496 {{max_float, max_float, 2.0f, 2.0f}, {max_int, max_int, 0, 0}}, |
494 max_int * 2.f, | 497 {{0.0f, 0.0f, max_float, max_float}, {0, 0, max_int, max_int}}, |
495 min_int, | 498 {{20000.5f, 20000.5f, 0.5f, 0.5f}, {20001, 20001, 0, 0}}, |
496 min_int, | 499 {{max_int_f, max_int_f, max_int_f, max_int_f}, {max_int, max_int, 0, 0}}}; |
497 max_int, | |
498 max_int}, | |
499 {static_cast<float>(max_int), | |
500 static_cast<float>(max_int), | |
501 static_cast<float>(max_int), | |
502 static_cast<float>(max_int), | |
503 max_int, | |
504 max_int, | |
505 0, | |
506 0}}; | |
507 | 500 |
508 for (size_t i = 0; i < arraysize(tests); ++i) { | 501 for (size_t i = 0; i < arraysize(tests); ++i) { |
509 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); | 502 RectF source(tests[i].in.x, tests[i].in.y, tests[i].in.width, |
510 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); | 503 tests[i].in.height); |
| 504 Rect enclosed = ToEnclosedRect(source); |
511 | 505 |
512 Rect enclosed = ToEnclosedRect(r1); | 506 EXPECT_EQ(tests[i].expected.x, enclosed.x()); |
513 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), enclosed.x()); | 507 EXPECT_EQ(tests[i].expected.y, enclosed.y()); |
514 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), enclosed.y()); | 508 EXPECT_EQ(tests[i].expected.width, enclosed.width()); |
515 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), enclosed.width()); | 509 EXPECT_EQ(tests[i].expected.height, enclosed.height()); |
516 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), enclosed.height()); | 510 } |
| 511 |
| 512 { |
| 513 RectF source(min_int_f, min_int_f, max_int_f * 3.f, max_int_f * 3.f); |
| 514 Rect enclosed = ToEnclosedRect(source); |
| 515 |
| 516 // That rect can't be represented, but it should be big. |
| 517 EXPECT_EQ(max_int, enclosed.width()); |
| 518 EXPECT_EQ(max_int, enclosed.height()); |
| 519 // It should include some axis near the global origin. |
| 520 EXPECT_GT(1, enclosed.x()); |
| 521 EXPECT_GT(1, enclosed.y()); |
| 522 // And it should not cause computation issues for itself. |
| 523 EXPECT_LT(0, enclosed.right()); |
| 524 EXPECT_LT(0, enclosed.bottom()); |
517 } | 525 } |
518 } | 526 } |
519 | 527 |
520 TEST(RectTest, ToEnclosingRect) { | 528 TEST(RectTest, ToEnclosingRect) { |
521 static const int max_int = std::numeric_limits<int>::max(); | 529 static const int max_int = std::numeric_limits<int>::max(); |
522 static const int min_int = std::numeric_limits<int>::min(); | 530 static const int min_int = std::numeric_limits<int>::min(); |
523 static const float max_float = std::numeric_limits<float>::max(); | 531 static const float max_float = std::numeric_limits<float>::max(); |
524 static const float epsilon_float = std::numeric_limits<float>::epsilon(); | 532 static const float epsilon_float = std::numeric_limits<float>::epsilon(); |
| 533 static const float max_int_f = static_cast<float>(max_int); |
| 534 static const float min_int_f = static_cast<float>(min_int); |
525 static const struct Test { | 535 static const struct Test { |
526 float x1; // source | 536 struct { |
527 float y1; | 537 float x; |
528 float w1; | 538 float y; |
529 float h1; | 539 float width; |
530 int x2; // target | 540 float height; |
531 int y2; | 541 } in; |
532 int w2; | 542 struct { |
533 int h2; | 543 int x; |
| 544 int y; |
| 545 int width; |
| 546 int height; |
| 547 } expected; |
534 } tests[] = { | 548 } tests[] = { |
535 {0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 0, 0}, | 549 {{0.0f, 0.0f, 0.0f, 0.0f}, {0, 0, 0, 0}}, |
536 {5.5f, 5.5f, 0.0f, 0.0f, 5, 5, 0, 0}, | 550 {{5.5f, 5.5f, 0.0f, 0.0f}, {5, 5, 0, 0}}, |
537 {3.5f, 2.5f, epsilon_float, -0.0f, 3, 2, 0, 0}, | 551 {{3.5f, 2.5f, epsilon_float, -0.0f}, {3, 2, 0, 0}}, |
538 {-1.5f, -1.5f, 3.0f, 3.0f, -2, -2, 4, 4}, | 552 {{3.5f, 2.5f, 0.f, 0.001f}, {3, 2, 0, 1}}, |
539 {-1.5f, -1.5f, 3.5f, 3.5f, -2, -2, 4, 4}, | 553 {{-1.5f, -1.5f, 3.0f, 3.0f}, {-2, -2, 4, 4}}, |
540 {max_float, max_float, 2.0f, 2.0f, max_int, max_int, 0, 0}, | 554 {{-1.5f, -1.5f, 3.5f, 3.5f}, {-2, -2, 4, 4}}, |
541 {0.0f, 0.0f, max_float, max_float, 0, 0, max_int, max_int}, | 555 {{max_float, max_float, 2.0f, 2.0f}, {max_int, max_int, 0, 0}}, |
542 {20000.5f, 20000.5f, 0.5f, 0.5f, 20000, 20000, 1, 1}, | 556 {{0.0f, 0.0f, max_float, max_float}, {0, 0, max_int, max_int}}, |
543 {static_cast<float>(min_int), static_cast<float>(min_int), max_int * 2.f, | 557 {{20000.5f, 20000.5f, 0.5f, 0.5f}, {20000, 20000, 1, 1}}, |
544 max_int * 2.f, min_int, min_int, max_int, max_int}, | 558 {{max_int_f, max_int_f, max_int_f, max_int_f}, {max_int, max_int, 0, 0}}, |
545 {static_cast<float>(max_int), static_cast<float>(max_int), | 559 {{-0.5f, -0.5f, 22777712.f, 1.f}, {-1, -1, 22777713, 2}}}; |
546 static_cast<float>(max_int), static_cast<float>(max_int), max_int, | |
547 max_int, 0, 0}, | |
548 {-0.5f, -0.5f, 22777712.f, 1.f, -1, -1, 22777713, 2}}; | |
549 | 560 |
550 for (size_t i = 0; i < arraysize(tests); ++i) { | 561 for (size_t i = 0; i < arraysize(tests); ++i) { |
551 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); | 562 RectF source(tests[i].in.x, tests[i].in.y, tests[i].in.width, |
552 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); | 563 tests[i].in.height); |
553 | 564 |
554 Rect enclosed = ToEnclosingRect(r1); | 565 Rect enclosing = ToEnclosingRect(source); |
555 EXPECT_EQ(r2.x(), enclosed.x()); | 566 EXPECT_EQ(tests[i].expected.x, enclosing.x()); |
556 EXPECT_EQ(r2.y(), enclosed.y()); | 567 EXPECT_EQ(tests[i].expected.y, enclosing.y()); |
557 EXPECT_EQ(r2.width(), enclosed.width()); | 568 EXPECT_EQ(tests[i].expected.width, enclosing.width()); |
558 EXPECT_EQ(r2.height(), enclosed.height()); | 569 EXPECT_EQ(tests[i].expected.height, enclosing.height()); |
| 570 } |
| 571 |
| 572 { |
| 573 RectF source(min_int_f, min_int_f, max_int_f * 3.f, max_int_f * 3.f); |
| 574 Rect enclosing = ToEnclosingRect(source); |
| 575 |
| 576 // That rect can't be represented, but it should be big. |
| 577 EXPECT_EQ(max_int, enclosing.width()); |
| 578 EXPECT_EQ(max_int, enclosing.height()); |
| 579 // It should include some axis near the global origin. |
| 580 EXPECT_GT(1, enclosing.x()); |
| 581 EXPECT_GT(1, enclosing.y()); |
| 582 // And it should cause computation issues for itself. |
| 583 EXPECT_LT(0, enclosing.right()); |
| 584 EXPECT_LT(0, enclosing.bottom()); |
559 } | 585 } |
560 } | 586 } |
561 | 587 |
562 TEST(RectTest, ToNearestRect) { | 588 TEST(RectTest, ToNearestRect) { |
563 Rect rect; | 589 Rect rect; |
564 EXPECT_EQ(rect, ToNearestRect(RectF(rect))); | 590 EXPECT_EQ(rect, ToNearestRect(RectF(rect))); |
565 | 591 |
566 rect = Rect(-1, -1, 3, 3); | 592 rect = Rect(-1, -1, 3, 3); |
567 EXPECT_EQ(rect, ToNearestRect(RectF(rect))); | 593 EXPECT_EQ(rect, ToNearestRect(RectF(rect))); |
568 | 594 |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1041 Rect positive_origin(100, 100, 500, 500); | 1067 Rect positive_origin(100, 100, 500, 500); |
1042 | 1068 |
1043 // Ideally, this should be (100, 100, limit + 400, limit + 400). | 1069 // Ideally, this should be (100, 100, limit + 400, limit + 400). |
1044 // However, width overflows and would be clamped to limit, but right | 1070 // However, width overflows and would be clamped to limit, but right |
1045 // overflows too and so will be clamped to limit - 100. | 1071 // overflows too and so will be clamped to limit - 100. |
1046 Rect expected(100, 100, limit - 100, limit - 100); | 1072 Rect expected(100, 100, limit - 100, limit - 100); |
1047 EXPECT_EQ(UnionRects(clamped, positive_origin), expected); | 1073 EXPECT_EQ(UnionRects(clamped, positive_origin), expected); |
1048 } | 1074 } |
1049 | 1075 |
1050 // Unioning a left=minint rect with a right=maxint rect. | 1076 // Unioning a left=minint rect with a right=maxint rect. |
1051 // Width is always clamped before adjusting position, so this | 1077 // We can't represent both ends of the spectrum in the same rect. |
1052 // should taking the min left/top and then finding the max width. | 1078 // Make sure we keep the most useful area. |
1053 { | 1079 { |
| 1080 int part_limit = min_limit / 3; |
1054 Rect left_minint(min_limit, min_limit, 1, 1); | 1081 Rect left_minint(min_limit, min_limit, 1, 1); |
1055 Rect right_maxint(limit - 1, limit - 1, limit, limit); | 1082 Rect right_maxint(limit - 1, limit - 1, limit, limit); |
1056 Rect expected(min_limit, min_limit, limit, limit); | 1083 Rect expected(part_limit, part_limit, 2 * part_limit, 2 * part_limit); |
1057 EXPECT_EQ(UnionRects(left_minint, right_maxint), expected); | 1084 Rect result = UnionRects(left_minint, right_maxint); |
| 1085 |
| 1086 // The result should be maximally big. |
| 1087 EXPECT_EQ(limit, result.height()); |
| 1088 EXPECT_EQ(limit, result.width()); |
| 1089 |
| 1090 // The result should include the area near the origin. |
| 1091 EXPECT_GT(-part_limit, result.x()); |
| 1092 EXPECT_LT(part_limit, result.right()); |
| 1093 EXPECT_GT(-part_limit, result.y()); |
| 1094 EXPECT_LT(part_limit, result.bottom()); |
| 1095 |
| 1096 // More succinctly, but harder to read in the results. |
| 1097 EXPECT_TRUE(UnionRects(left_minint, right_maxint).Contains(expected)); |
1058 } | 1098 } |
1059 } | 1099 } |
1060 | 1100 |
1061 TEST(RectTest, ScaleToEnclosingRectSafe) { | 1101 TEST(RectTest, ScaleToEnclosingRectSafe) { |
1062 const int max_int = std::numeric_limits<int>::max(); | 1102 const int max_int = std::numeric_limits<int>::max(); |
1063 const int min_int = std::numeric_limits<int>::min(); | 1103 const int min_int = std::numeric_limits<int>::min(); |
1064 | 1104 |
1065 Rect xy_underflow(-100000, -123456, 10, 20); | 1105 Rect xy_underflow(-100000, -123456, 10, 20); |
1066 EXPECT_EQ(ScaleToEnclosingRectSafe(xy_underflow, 100000, 100000), | 1106 EXPECT_EQ(ScaleToEnclosingRectSafe(xy_underflow, 100000, 100000), |
1067 Rect(min_int, min_int, 1000000, 2000000)); | 1107 Rect(min_int, min_int, 1000000, 2000000)); |
(...skipping 24 matching lines...) Expand all Loading... |
1092 | 1132 |
1093 Rect min_rect(min_int, min_int, max_int, max_int); | 1133 Rect min_rect(min_int, min_int, max_int, max_int); |
1094 // Min rect can't be scaled up any further in any dimension. | 1134 // Min rect can't be scaled up any further in any dimension. |
1095 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, 2, 3.5), min_rect); | 1135 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, 2, 3.5), min_rect); |
1096 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, max_int, max_int), min_rect); | 1136 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, max_int, max_int), min_rect); |
1097 // Min rect scaled by min is an empty rect at (max, max) | 1137 // Min rect scaled by min is an empty rect at (max, max) |
1098 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, min_int, min_int), max_rect); | 1138 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, min_int, min_int), max_rect); |
1099 } | 1139 } |
1100 | 1140 |
1101 } // namespace gfx | 1141 } // namespace gfx |
OLD | NEW |