Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: ui/gfx/geometry/rect_unittest.cc

Issue 2744423002: Handle large rects better. (Closed)
Patch Set: Rebased on size patch Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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) \
danakj 2017/03/28 20:40:35 This is pretty magical, instead of this, can you j
Peter Mayo 2017/03/29 18:49:26 Done. Increased the structure of the tests while I
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
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,
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 float epsilon_float = std::numeric_limits<float>::epsilon(); 522 static const float epsilon_float = std::numeric_limits<float>::epsilon();
525 static const struct Test { 523 static const struct Test {
526 float x1; // source 524 float x1; // source
527 float y1; 525 float y1;
528 float w1; 526 float w1;
529 float h1; 527 float h1;
530 int x2; // target 528 int x2; // target
531 int y2; 529 int y2;
532 int w2; 530 int w2;
533 int h2; 531 int h2;
534 } tests[] = { 532 } tests[] = {
535 {0.0f, 0.0f, 0.0f, 0.0f, 0, 0, 0, 0}, 533 {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}, 534 {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}, 535 {3.5f, 2.5f, epsilon_float, -0.0f, 3, 2, 0, 0},
536 {3.5f, 2.5f, 0.f, 0.001f, 3, 2, 0, 1},
538 {-1.5f, -1.5f, 3.0f, 3.0f, -2, -2, 4, 4}, 537 {-1.5f, -1.5f, 3.0f, 3.0f, -2, -2, 4, 4},
539 {-1.5f, -1.5f, 3.5f, 3.5f, -2, -2, 4, 4}, 538 {-1.5f, -1.5f, 3.5f, 3.5f, -2, -2, 4, 4},
540 {max_float, max_float, 2.0f, 2.0f, max_int, max_int, 0, 0}, 539 {max_float, max_float, 2.0f, 2.0f, max_int, max_int, 0, 0},
541 {0.0f, 0.0f, max_float, max_float, 0, 0, max_int, max_int}, 540 {0.0f, 0.0f, max_float, max_float, 0, 0, max_int, max_int},
542 {20000.5f, 20000.5f, 0.5f, 0.5f, 20000, 20000, 1, 1}, 541 {20000.5f, 20000.5f, 0.5f, 0.5f, 20000, 20000, 1, 1},
543 {static_cast<float>(min_int), static_cast<float>(min_int), max_int * 2.f, 542 {static_cast<float>(min_int), static_cast<float>(min_int), max_int * 2.f,
544 max_int * 2.f, min_int, min_int, max_int, max_int}, 543 max_int * 2.f, dont_care, dont_care, max_int, max_int},
545 {static_cast<float>(max_int), static_cast<float>(max_int), 544 {static_cast<float>(max_int), static_cast<float>(max_int),
546 static_cast<float>(max_int), static_cast<float>(max_int), max_int, 545 static_cast<float>(max_int), static_cast<float>(max_int), max_int,
547 max_int, 0, 0}, 546 max_int, 0, 0},
548 {-0.5f, -0.5f, 22777712.f, 1.f, -1, -1, 22777713, 2}}; 547 {-0.5f, -0.5f, 22777712.f, 1.f, -1, -1, 22777713, 2}};
549 548
550 for (size_t i = 0; i < arraysize(tests); ++i) { 549 for (size_t i = 0; i < arraysize(tests); ++i) {
551 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); 550 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
552 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); 551 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
553 552
554 Rect enclosed = ToEnclosingRect(r1); 553 Rect enclosed = ToEnclosingRect(r1);
555 EXPECT_EQ(r2.x(), enclosed.x()); 554 MAYBE_EXPECT_INT_EQ(r2.x(), enclosed.x());
556 EXPECT_EQ(r2.y(), enclosed.y()); 555 MAYBE_EXPECT_INT_EQ(r2.y(), enclosed.y());
557 EXPECT_EQ(r2.width(), enclosed.width()); 556 MAYBE_EXPECT_INT_EQ(r2.width(), enclosed.width());
558 EXPECT_EQ(r2.height(), enclosed.height()); 557 MAYBE_EXPECT_INT_EQ(r2.height(), enclosed.height());
559 } 558 }
560 } 559 }
561 560
562 TEST(RectTest, ToNearestRect) { 561 TEST(RectTest, ToNearestRect) {
563 Rect rect; 562 Rect rect;
564 EXPECT_EQ(rect, ToNearestRect(RectF(rect))); 563 EXPECT_EQ(rect, ToNearestRect(RectF(rect)));
565 564
566 rect = Rect(-1, -1, 3, 3); 565 rect = Rect(-1, -1, 3, 3);
567 EXPECT_EQ(rect, ToNearestRect(RectF(rect))); 566 EXPECT_EQ(rect, ToNearestRect(RectF(rect)));
568 567
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after
1041 Rect positive_origin(100, 100, 500, 500); 1040 Rect positive_origin(100, 100, 500, 500);
1042 1041
1043 // Ideally, this should be (100, 100, limit + 400, limit + 400). 1042 // Ideally, this should be (100, 100, limit + 400, limit + 400).
1044 // However, width overflows and would be clamped to limit, but right 1043 // However, width overflows and would be clamped to limit, but right
1045 // overflows too and so will be clamped to limit - 100. 1044 // overflows too and so will be clamped to limit - 100.
1046 Rect expected(100, 100, limit - 100, limit - 100); 1045 Rect expected(100, 100, limit - 100, limit - 100);
1047 EXPECT_EQ(UnionRects(clamped, positive_origin), expected); 1046 EXPECT_EQ(UnionRects(clamped, positive_origin), expected);
1048 } 1047 }
1049 1048
1050 // Unioning a left=minint rect with a right=maxint rect. 1049 // Unioning a left=minint rect with a right=maxint rect.
1051 // Width is always clamped before adjusting position, so this 1050 // 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. 1051 // Make sure we keep the most useful area.
1053 { 1052 {
1053 int part_limit = min_limit / 3;
1054 Rect left_minint(min_limit, min_limit, 1, 1); 1054 Rect left_minint(min_limit, min_limit, 1, 1);
1055 Rect right_maxint(limit - 1, limit - 1, limit, limit); 1055 Rect right_maxint(limit - 1, limit - 1, limit, limit);
1056 Rect expected(min_limit, min_limit, limit, limit); 1056 Rect expected(part_limit, part_limit, 2 * part_limit, 2 * part_limit);
1057 EXPECT_EQ(UnionRects(left_minint, right_maxint), expected); 1057 EXPECT_TRUE(UnionRects(left_minint, right_maxint).Contains(expected));
danakj 2017/03/28 20:40:35 Can you (also?) verify the width and height exactl
Peter Mayo 2017/03/29 18:49:26 Done.
1058 } 1058 }
1059 } 1059 }
1060 1060
1061 TEST(RectTest, ScaleToEnclosingRectSafe) { 1061 TEST(RectTest, ScaleToEnclosingRectSafe) {
1062 const int max_int = std::numeric_limits<int>::max(); 1062 const int max_int = std::numeric_limits<int>::max();
1063 const int min_int = std::numeric_limits<int>::min(); 1063 const int min_int = std::numeric_limits<int>::min();
1064 1064
1065 Rect xy_underflow(-100000, -123456, 10, 20); 1065 Rect xy_underflow(-100000, -123456, 10, 20);
1066 EXPECT_EQ(ScaleToEnclosingRectSafe(xy_underflow, 100000, 100000), 1066 EXPECT_EQ(ScaleToEnclosingRectSafe(xy_underflow, 100000, 100000),
1067 Rect(min_int, min_int, 1000000, 2000000)); 1067 Rect(min_int, min_int, 1000000, 2000000));
(...skipping 24 matching lines...) Expand all
1092 1092
1093 Rect min_rect(min_int, min_int, max_int, max_int); 1093 Rect min_rect(min_int, min_int, max_int, max_int);
1094 // Min rect can't be scaled up any further in any dimension. 1094 // Min rect can't be scaled up any further in any dimension.
1095 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, 2, 3.5), min_rect); 1095 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, 2, 3.5), min_rect);
1096 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, max_int, max_int), min_rect); 1096 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, max_int, max_int), min_rect);
1097 // Min rect scaled by min is an empty rect at (max, max) 1097 // Min rect scaled by min is an empty rect at (max, max)
1098 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, min_int, min_int), max_rect); 1098 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, min_int, min_int), max_rect);
1099 } 1099 }
1100 1100
1101 } // namespace gfx 1101 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698