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

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

Issue 2744423002: Handle large rects better. (Closed)
Patch Set: Rebased, Use HasHeight, Symmetric test 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) \
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 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},
536 {3.5f, 2.5f, 0.01f, -0.0f, 3, 2, 0, 0}, 534 {3.5f, 2.5f, 0.01f, -0.0f, 3, 2, 1, 0},
535 {3.5f, 2.5f, 0.f, 0.001f, 3, 2, 0, 1},
537 {-1.5f, -1.5f, 3.0f, 3.0f, -2, -2, 4, 4}, 536 {-1.5f, -1.5f, 3.0f, 3.0f, -2, -2, 4, 4},
538 {-1.5f, -1.5f, 3.5f, 3.5f, -2, -2, 4, 4}, 537 {-1.5f, -1.5f, 3.5f, 3.5f, -2, -2, 4, 4},
539 {max_float, max_float, 2.0f, 2.0f, max_int, max_int, 0, 0}, 538 {max_float, max_float, 2.0f, 2.0f, max_int, max_int, 0, 0},
540 {0.0f, 0.0f, max_float, max_float, 0, 0, max_int, max_int}, 539 {0.0f, 0.0f, max_float, max_float, 0, 0, max_int, max_int},
541 {20000.5f, 20000.5f, 0.5f, 0.5f, 20000, 20000, 1, 1}, 540 {20000.5f, 20000.5f, 0.5f, 0.5f, 20000, 20000, 1, 1},
542 {static_cast<float>(min_int), static_cast<float>(min_int), max_int * 2.f, 541 {static_cast<float>(min_int), static_cast<float>(min_int), max_int * 2.f,
543 max_int * 2.f, min_int, min_int, max_int, max_int}, 542 max_int * 2.f, dont_care, dont_care, max_int, max_int},
544 {static_cast<float>(max_int), static_cast<float>(max_int), 543 {static_cast<float>(max_int), static_cast<float>(max_int),
545 static_cast<float>(max_int), static_cast<float>(max_int), max_int, 544 static_cast<float>(max_int), static_cast<float>(max_int), max_int,
546 max_int, 0, 0}, 545 max_int, 0, 0},
547 {-0.5f, -0.5f, 22777712.f, 1.f, -1, -1, 22777713, 2}}; 546 {-0.5f, -0.5f, 22777712.f, 1.f, -1, -1, 22777713, 2}};
548 547
549 for (size_t i = 0; i < arraysize(tests); ++i) { 548 for (size_t i = 0; i < arraysize(tests); ++i) {
550 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1); 549 RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
551 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2); 550 Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
552 551
553 Rect enclosed = ToEnclosingRect(r1); 552 Rect enclosed = ToEnclosingRect(r1);
554 EXPECT_EQ(r2.x(), enclosed.x()); 553 MAYBE_EXPECT_INT_EQ(r2.x(), enclosed.x());
555 EXPECT_EQ(r2.y(), enclosed.y()); 554 MAYBE_EXPECT_INT_EQ(r2.y(), enclosed.y());
556 EXPECT_EQ(r2.width(), enclosed.width()); 555 MAYBE_EXPECT_INT_EQ(r2.width(), enclosed.width());
557 EXPECT_EQ(r2.height(), enclosed.height()); 556 MAYBE_EXPECT_INT_EQ(r2.height(), enclosed.height());
558 } 557 }
559 } 558 }
560 559
561 TEST(RectTest, ToNearestRect) { 560 TEST(RectTest, ToNearestRect) {
562 Rect rect; 561 Rect rect;
563 EXPECT_EQ(rect, ToNearestRect(RectF(rect))); 562 EXPECT_EQ(rect, ToNearestRect(RectF(rect)));
564 563
565 rect = Rect(-1, -1, 3, 3); 564 rect = Rect(-1, -1, 3, 3);
566 EXPECT_EQ(rect, ToNearestRect(RectF(rect))); 565 EXPECT_EQ(rect, ToNearestRect(RectF(rect)));
567 566
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after
1040 Rect positive_origin(100, 100, 500, 500); 1039 Rect positive_origin(100, 100, 500, 500);
1041 1040
1042 // Ideally, this should be (100, 100, limit + 400, limit + 400). 1041 // Ideally, this should be (100, 100, limit + 400, limit + 400).
1043 // However, width overflows and would be clamped to limit, but right 1042 // However, width overflows and would be clamped to limit, but right
1044 // overflows too and so will be clamped to limit - 100. 1043 // overflows too and so will be clamped to limit - 100.
1045 Rect expected(100, 100, limit - 100, limit - 100); 1044 Rect expected(100, 100, limit - 100, limit - 100);
1046 EXPECT_EQ(UnionRects(clamped, positive_origin), expected); 1045 EXPECT_EQ(UnionRects(clamped, positive_origin), expected);
1047 } 1046 }
1048 1047
1049 // Unioning a left=minint rect with a right=maxint rect. 1048 // Unioning a left=minint rect with a right=maxint rect.
1050 // Width is always clamped before adjusting position, so this 1049 // We can't represent both ends of the spectrum in the same rect.
1051 // should taking the min left/top and then finding the max width. 1050 // Make sure we keep the most useful area.
1052 { 1051 {
1052 int part_limit = min_limit / 3;
1053 Rect left_minint(min_limit, min_limit, 1, 1); 1053 Rect left_minint(min_limit, min_limit, 1, 1);
1054 Rect right_maxint(limit - 1, limit - 1, limit, limit); 1054 Rect right_maxint(limit - 1, limit - 1, limit, limit);
1055 Rect expected(min_limit, min_limit, limit, limit); 1055 Rect expected(part_limit, part_limit, 2 * part_limit, 2 * part_limit);
1056 EXPECT_EQ(UnionRects(left_minint, right_maxint), expected); 1056 EXPECT_TRUE(UnionRects(left_minint, right_maxint).Contains(expected));
1057 } 1057 }
1058 } 1058 }
1059 1059
1060 TEST(RectTest, ScaleToEnclosingRectSafe) { 1060 TEST(RectTest, ScaleToEnclosingRectSafe) {
1061 const int max_int = std::numeric_limits<int>::max(); 1061 const int max_int = std::numeric_limits<int>::max();
1062 const int min_int = std::numeric_limits<int>::min(); 1062 const int min_int = std::numeric_limits<int>::min();
1063 1063
1064 Rect xy_underflow(-100000, -123456, 10, 20); 1064 Rect xy_underflow(-100000, -123456, 10, 20);
1065 EXPECT_EQ(ScaleToEnclosingRectSafe(xy_underflow, 100000, 100000), 1065 EXPECT_EQ(ScaleToEnclosingRectSafe(xy_underflow, 100000, 100000),
1066 Rect(min_int, min_int, 1000000, 2000000)); 1066 Rect(min_int, min_int, 1000000, 2000000));
(...skipping 24 matching lines...) Expand all
1091 1091
1092 Rect min_rect(min_int, min_int, max_int, max_int); 1092 Rect min_rect(min_int, min_int, max_int, max_int);
1093 // Min rect can't be scaled up any further in any dimension. 1093 // Min rect can't be scaled up any further in any dimension.
1094 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, 2, 3.5), min_rect); 1094 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, 2, 3.5), min_rect);
1095 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, max_int, max_int), min_rect); 1095 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, max_int, max_int), min_rect);
1096 // Min rect scaled by min is an empty rect at (max, max) 1096 // Min rect scaled by min is an empty rect at (max, max)
1097 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, min_int, min_int), max_rect); 1097 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, min_int, min_int), max_rect);
1098 } 1098 }
1099 1099
1100 } // namespace gfx 1100 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698