Chromium Code Reviews| 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 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 418 EXPECT_FALSE(r.SharesEdgeWith(just_above_no_edge)); | 418 EXPECT_FALSE(r.SharesEdgeWith(just_above_no_edge)); |
| 419 EXPECT_FALSE(r.SharesEdgeWith(just_below_no_edge)); | 419 EXPECT_FALSE(r.SharesEdgeWith(just_below_no_edge)); |
| 420 EXPECT_FALSE(r.SharesEdgeWith(just_left_no_edge)); | 420 EXPECT_FALSE(r.SharesEdgeWith(just_left_no_edge)); |
| 421 EXPECT_FALSE(r.SharesEdgeWith(just_right_no_edge)); | 421 EXPECT_FALSE(r.SharesEdgeWith(just_right_no_edge)); |
| 422 } | 422 } |
| 423 | 423 |
| 424 // Similar to EXPECT_FLOAT_EQ, but lets NaN equal NaN | 424 // Similar to EXPECT_FLOAT_EQ, but lets NaN equal NaN |
| 425 #define EXPECT_FLOAT_AND_NAN_EQ(a, b) \ | 425 #define EXPECT_FLOAT_AND_NAN_EQ(a, b) \ |
| 426 { if (a == a || b == b) { EXPECT_FLOAT_EQ(a, b); } } | 426 { if (a == a || b == b) { EXPECT_FLOAT_EQ(a, b); } } |
| 427 | 427 |
| 428 static const int dont_care = -451329; | |
| 429 #define MAYBE_EXPECT_INT_EQ(a, b) \ | |
| 430 { \ | |
| 431 if (a != dont_care) { \ | |
| 432 EXPECT_EQ(a, b); \ | |
| 433 } \ | |
| 434 } | |
| 435 | |
| 428 TEST(RectTest, ScaleRect) { | 436 TEST(RectTest, ScaleRect) { |
| 429 static const struct Test { | 437 static const struct Test { |
| 430 int x1; // source | 438 int x1; // source |
| 431 int y1; | 439 int y1; |
| 432 int w1; | 440 int w1; |
| 433 int h1; | 441 int h1; |
| 434 float scale; | 442 float scale; |
| 435 float x2; // target | 443 float x2; // target |
| 436 float y2; | 444 float y2; |
| 437 float w2; | 445 float w2; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 475 static const float max_float = std::numeric_limits<float>::max(); | 483 static const float max_float = std::numeric_limits<float>::max(); |
| 476 static const struct Test { | 484 static const struct Test { |
| 477 float x1; // source | 485 float x1; // source |
| 478 float y1; | 486 float y1; |
| 479 float w1; | 487 float w1; |
| 480 float h1; | 488 float h1; |
| 481 int x2; // target | 489 int x2; // target |
| 482 int y2; | 490 int y2; |
| 483 int w2; | 491 int w2; |
| 484 int h2; | 492 int h2; |
| 485 } tests[] = {{0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 0, 0}, | 493 } tests[] = { |
| 486 {-1.5f, -1.5f, 3.0f, 3.0f, -1, -1, 2, 2}, | 494 {0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 0, 0}, |
| 487 {-1.5f, -1.5f, 3.5f, 3.5f, -1, -1, 3, 3}, | 495 {-1.5f, -1.5f, 3.0f, 3.0f, -1, -1, 2, 2}, |
| 488 {max_float, max_float, 2.0f, 2.0f, max_int, max_int, 0, 0}, | 496 {-1.5f, -1.5f, 3.5f, 3.5f, -1, -1, 3, 3}, |
| 489 {0.0f, 0.0f, max_float, max_float, 0, 0, max_int, max_int}, | 497 {max_float, max_float, 2.0f, 2.0f, max_int, max_int, 0, 0}, |
| 490 {20000.5f, 20000.5f, 0.5f, 0.5f, 20001, 20001, 0, 0}, | 498 {0.0f, 0.0f, max_float, max_float, 0, 0, max_int, max_int}, |
| 491 {static_cast<float>(min_int), | 499 {20000.5f, 20000.5f, 0.5f, 0.5f, 20001, 20001, 0, 0}, |
| 492 static_cast<float>(min_int), | 500 {static_cast<float>(min_int), static_cast<float>(min_int), max_int * 2.f, |
|
Peter Mayo
2017/03/17 00:30:48
ICK - git cl upload insisted this should be reform
| |
| 493 max_int * 2.f, | 501 max_int * 2.f, dont_care, dont_care, max_int, max_int}, |
| 494 max_int * 2.f, | 502 {static_cast<float>(max_int), static_cast<float>(max_int), |
| 495 min_int, | 503 static_cast<float>(max_int), static_cast<float>(max_int), max_int, |
| 496 min_int, | 504 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 | 505 |
| 508 for (size_t i = 0; i < arraysize(tests); ++i) { | 506 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 509 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); | 507 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); |
| 510 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); | 508 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); |
| 511 | 509 |
| 512 Rect enclosed = ToEnclosedRect(r1); | 510 Rect enclosed = ToEnclosedRect(r1); |
| 513 EXPECT_FLOAT_AND_NAN_EQ(r2.x(), enclosed.x()); | 511 MAYBE_EXPECT_INT_EQ(r2.x(), enclosed.x()); |
| 514 EXPECT_FLOAT_AND_NAN_EQ(r2.y(), enclosed.y()); | 512 MAYBE_EXPECT_INT_EQ(r2.y(), enclosed.y()); |
| 515 EXPECT_FLOAT_AND_NAN_EQ(r2.width(), enclosed.width()); | 513 MAYBE_EXPECT_INT_EQ(r2.width(), enclosed.width()); |
| 516 EXPECT_FLOAT_AND_NAN_EQ(r2.height(), enclosed.height()); | 514 MAYBE_EXPECT_INT_EQ(r2.height(), enclosed.height()); |
| 517 } | 515 } |
| 518 } | 516 } |
| 519 | 517 |
| 520 TEST(RectTest, ToEnclosingRect) { | 518 TEST(RectTest, ToEnclosingRect) { |
| 521 static const int max_int = std::numeric_limits<int>::max(); | 519 static const int max_int = std::numeric_limits<int>::max(); |
| 522 static const int min_int = std::numeric_limits<int>::min(); | 520 static const int min_int = std::numeric_limits<int>::min(); |
| 523 static const float max_float = std::numeric_limits<float>::max(); | 521 static const float max_float = std::numeric_limits<float>::max(); |
| 524 static const struct Test { | 522 static const struct Test { |
| 525 float x1; // source | 523 float x1; // source |
| 526 float y1; | 524 float y1; |
| 527 float w1; | 525 float w1; |
| 528 float h1; | 526 float h1; |
| 529 int x2; // target | 527 int x2; // target |
| 530 int y2; | 528 int y2; |
| 531 int w2; | 529 int w2; |
| 532 int h2; | 530 int h2; |
| 533 } tests[] = { | 531 } tests[] = { |
| 534 {0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 0, 0}, | 532 {0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 0, 0}, |
| 535 {5.5f, 5.5f, 0.0f, 0.0f, 5, 5, 0, 0}, | 533 {5.5f, 5.5f, 0.0f, 0.0f, 5, 5, 0, 0}, |
| 534 {3.5f, 2.5f, 0.01f, 0.01f, 3, 2, 0, 0}, | |
| 536 {-1.5f, -1.5f, 3.0f, 3.0f, -2, -2, 4, 4}, | 535 {-1.5f, -1.5f, 3.0f, 3.0f, -2, -2, 4, 4}, |
| 537 {-1.5f, -1.5f, 3.5f, 3.5f, -2, -2, 4, 4}, | 536 {-1.5f, -1.5f, 3.5f, 3.5f, -2, -2, 4, 4}, |
| 538 {max_float, max_float, 2.0f, 2.0f, max_int, max_int, 0, 0}, | 537 {max_float, max_float, 2.0f, 2.0f, max_int, max_int, 0, 0}, |
| 539 {0.0f, 0.0f, max_float, max_float, 0, 0, max_int, max_int}, | 538 {0.0f, 0.0f, max_float, max_float, 0, 0, max_int, max_int}, |
| 540 {20000.5f, 20000.5f, 0.5f, 0.5f, 20000, 20000, 1, 1}, | 539 {20000.5f, 20000.5f, 0.5f, 0.5f, 20000, 20000, 1, 1}, |
| 541 {static_cast<float>(min_int), static_cast<float>(min_int), max_int * 2.f, | 540 {static_cast<float>(min_int), static_cast<float>(min_int), max_int * 2.f, |
| 542 max_int * 2.f, min_int, min_int, max_int, max_int}, | 541 max_int * 2.f, dont_care, dont_care, max_int, max_int}, |
| 543 {static_cast<float>(max_int), static_cast<float>(max_int), | 542 {static_cast<float>(max_int), static_cast<float>(max_int), |
| 544 static_cast<float>(max_int), static_cast<float>(max_int), max_int, | 543 static_cast<float>(max_int), static_cast<float>(max_int), max_int, |
| 545 max_int, 0, 0}, | 544 max_int, 0, 0}, |
| 546 {-0.5f, -0.5f, 22777712.f, 1.f, -1, -1, 22777713, 2}}; | 545 {-0.5f, -0.5f, 22777712.f, 1.f, -1, -1, 22777713, 2}}; |
| 547 | 546 |
| 548 for (size_t i = 0; i < arraysize(tests); ++i) { | 547 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 549 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); | 548 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); |
| 550 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); | 549 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); |
| 551 | 550 |
| 552 Rect enclosed = ToEnclosingRect(r1); | 551 Rect enclosed = ToEnclosingRect(r1); |
| 553 EXPECT_EQ(r2.x(), enclosed.x()); | 552 MAYBE_EXPECT_INT_EQ(r2.x(), enclosed.x()); |
| 554 EXPECT_EQ(r2.y(), enclosed.y()); | 553 MAYBE_EXPECT_INT_EQ(r2.y(), enclosed.y()); |
| 555 EXPECT_EQ(r2.width(), enclosed.width()); | 554 MAYBE_EXPECT_INT_EQ(r2.width(), enclosed.width()); |
| 556 EXPECT_EQ(r2.height(), enclosed.height()); | 555 MAYBE_EXPECT_INT_EQ(r2.height(), enclosed.height()); |
| 557 } | 556 } |
| 558 } | 557 } |
| 559 | 558 |
| 560 TEST(RectTest, ToNearestRect) { | 559 TEST(RectTest, ToNearestRect) { |
| 561 Rect rect; | 560 Rect rect; |
| 562 EXPECT_EQ(rect, ToNearestRect(RectF(rect))); | 561 EXPECT_EQ(rect, ToNearestRect(RectF(rect))); |
| 563 | 562 |
| 564 rect = Rect(-1, -1, 3, 3); | 563 rect = Rect(-1, -1, 3, 3); |
| 565 EXPECT_EQ(rect, ToNearestRect(RectF(rect))); | 564 EXPECT_EQ(rect, ToNearestRect(RectF(rect))); |
| 566 | 565 |
| (...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1039 Rect positive_origin(100, 100, 500, 500); | 1038 Rect positive_origin(100, 100, 500, 500); |
| 1040 | 1039 |
| 1041 // Ideally, this should be (100, 100, limit + 400, limit + 400). | 1040 // Ideally, this should be (100, 100, limit + 400, limit + 400). |
| 1042 // However, width overflows and would be clamped to limit, but right | 1041 // However, width overflows and would be clamped to limit, but right |
| 1043 // overflows too and so will be clamped to limit - 100. | 1042 // overflows too and so will be clamped to limit - 100. |
| 1044 Rect expected(100, 100, limit - 100, limit - 100); | 1043 Rect expected(100, 100, limit - 100, limit - 100); |
| 1045 EXPECT_EQ(UnionRects(clamped, positive_origin), expected); | 1044 EXPECT_EQ(UnionRects(clamped, positive_origin), expected); |
| 1046 } | 1045 } |
| 1047 | 1046 |
| 1048 // Unioning a left=minint rect with a right=maxint rect. | 1047 // Unioning a left=minint rect with a right=maxint rect. |
| 1049 // Width is always clamped before adjusting position, so this | 1048 // We can't represent both ends of the spectrum in the same rect. |
| 1050 // should taking the min left/top and then finding the max width. | 1049 // Make sure we keep the most useful area. |
| 1051 { | 1050 { |
| 1051 int part_limit = min_limit / 3; | |
| 1052 Rect left_minint(min_limit, min_limit, 1, 1); | 1052 Rect left_minint(min_limit, min_limit, 1, 1); |
| 1053 Rect right_maxint(limit - 1, limit - 1, limit, limit); | 1053 Rect right_maxint(limit - 1, limit - 1, limit, limit); |
| 1054 Rect expected(min_limit, min_limit, limit, limit); | 1054 Rect expected(part_limit, part_limit, 2 * part_limit, 2 * part_limit); |
| 1055 EXPECT_EQ(UnionRects(left_minint, right_maxint), expected); | 1055 EXPECT_TRUE(UnionRects(left_minint, right_maxint).Contains(expected)); |
| 1056 } | 1056 } |
| 1057 } | 1057 } |
| 1058 | 1058 |
| 1059 TEST(RectTest, ScaleToEnclosingRectSafe) { | 1059 TEST(RectTest, ScaleToEnclosingRectSafe) { |
| 1060 const int max_int = std::numeric_limits<int>::max(); | 1060 const int max_int = std::numeric_limits<int>::max(); |
| 1061 const int min_int = std::numeric_limits<int>::min(); | 1061 const int min_int = std::numeric_limits<int>::min(); |
| 1062 | 1062 |
| 1063 Rect xy_underflow(-100000, -123456, 10, 20); | 1063 Rect xy_underflow(-100000, -123456, 10, 20); |
| 1064 EXPECT_EQ(ScaleToEnclosingRectSafe(xy_underflow, 100000, 100000), | 1064 EXPECT_EQ(ScaleToEnclosingRectSafe(xy_underflow, 100000, 100000), |
| 1065 Rect(min_int, min_int, 1000000, 2000000)); | 1065 Rect(min_int, min_int, 1000000, 2000000)); |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 1090 | 1090 |
| 1091 Rect min_rect(min_int, min_int, max_int, max_int); | 1091 Rect min_rect(min_int, min_int, max_int, max_int); |
| 1092 // Min rect can't be scaled up any further in any dimension. | 1092 // Min rect can't be scaled up any further in any dimension. |
| 1093 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, 2, 3.5), min_rect); | 1093 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, 2, 3.5), min_rect); |
| 1094 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, max_int, max_int), min_rect); | 1094 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, max_int, max_int), min_rect); |
| 1095 // Min rect scaled by min is an empty rect at (max, max) | 1095 // Min rect scaled by min is an empty rect at (max, max) |
| 1096 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, min_int, min_int), max_rect); | 1096 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, min_int, min_int), max_rect); |
| 1097 } | 1097 } |
| 1098 | 1098 |
| 1099 } // namespace gfx | 1099 } // namespace gfx |
| OLD | NEW |