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 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 1.5f, | 676 1.5f, |
677 Rect(1, 3, 5, 6), | 677 Rect(1, 3, 5, 6), |
678 }, { | 678 }, { |
679 Rect(-1, -2, 0, 0), | 679 Rect(-1, -2, 0, 0), |
680 1.5f, | 680 1.5f, |
681 Rect(-2, -3, 0, 0), | 681 Rect(-2, -3, 0, 0), |
682 } | 682 } |
683 }; | 683 }; |
684 | 684 |
685 for (size_t i = 0; i < arraysize(tests); ++i) { | 685 for (size_t i = 0; i < arraysize(tests); ++i) { |
686 Rect result = ScaleToEnclosingRect(tests[i].input_rect, | 686 Rect result = |
687 tests[i].input_scale); | 687 ScaleToEnclosingRect(tests[i].input_rect, tests[i].input_scale); |
688 EXPECT_EQ(tests[i].expected_rect, result); | 688 EXPECT_EQ(tests[i].expected_rect, result); |
| 689 Rect result_safe = |
| 690 ScaleToEnclosingRectSafe(tests[i].input_rect, tests[i].input_scale); |
| 691 EXPECT_EQ(tests[i].expected_rect, result_safe); |
689 } | 692 } |
690 } | 693 } |
691 | 694 |
692 #if defined(OS_WIN) | 695 #if defined(OS_WIN) |
693 TEST(RectTest, ConstructAndAssign) { | 696 TEST(RectTest, ConstructAndAssign) { |
694 const RECT rect_1 = { 0, 0, 10, 10 }; | 697 const RECT rect_1 = { 0, 0, 10, 10 }; |
695 const RECT rect_2 = { 0, 0, -10, -10 }; | 698 const RECT rect_2 = { 0, 0, -10, -10 }; |
696 Rect test1(rect_1); | 699 Rect test1(rect_1); |
697 Rect test2(rect_2); | 700 Rect test2(rect_2); |
698 } | 701 } |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
976 offset_overflow.Offset(large_number, large_number); | 979 offset_overflow.Offset(large_number, large_number); |
977 EXPECT_EQ(large_offset, offset_overflow.origin()); | 980 EXPECT_EQ(large_offset, offset_overflow.origin()); |
978 EXPECT_EQ(expected_size, offset_overflow.size()); | 981 EXPECT_EQ(expected_size, offset_overflow.size()); |
979 | 982 |
980 Rect operator_overflow(0, 0, 100, 100); | 983 Rect operator_overflow(0, 0, 100, 100); |
981 operator_overflow += Vector2d(large_number, large_number); | 984 operator_overflow += Vector2d(large_number, large_number); |
982 EXPECT_EQ(large_offset, operator_overflow.origin()); | 985 EXPECT_EQ(large_offset, operator_overflow.origin()); |
983 EXPECT_EQ(expected_size, operator_overflow.size()); | 986 EXPECT_EQ(expected_size, operator_overflow.size()); |
984 } | 987 } |
985 | 988 |
| 989 TEST(RectTest, ScaleToEnclosingRectSafe) { |
| 990 const int max_int = std::numeric_limits<int>::max(); |
| 991 const int min_int = std::numeric_limits<int>::min(); |
| 992 |
| 993 Rect xy_underflow(-100000, -123456, 10, 20); |
| 994 EXPECT_EQ(ScaleToEnclosingRectSafe(xy_underflow, 100000, 100000), |
| 995 Rect(min_int, min_int, 1000000, 2000000)); |
| 996 |
| 997 // A location overflow means that width/right and bottom/top also |
| 998 // overflow so need to be clamped. |
| 999 Rect xy_overflow(100000, 123456, 10, 20); |
| 1000 EXPECT_EQ(ScaleToEnclosingRectSafe(xy_overflow, 100000, 100000), |
| 1001 Rect(max_int, max_int, 0, 0)); |
| 1002 |
| 1003 // In practice all rects are clamped to 0 width / 0 height so |
| 1004 // negative sizes don't matter, but try this for the sake of testing. |
| 1005 Rect size_underflow(-1, -2, 100000, 100000); |
| 1006 EXPECT_EQ(ScaleToEnclosingRectSafe(size_underflow, -100000, -100000), |
| 1007 Rect(100000, 200000, 0, 0)); |
| 1008 |
| 1009 Rect size_overflow(-1, -2, 123456, 234567); |
| 1010 EXPECT_EQ(ScaleToEnclosingRectSafe(size_overflow, 100000, 100000), |
| 1011 Rect(-100000, -200000, max_int, max_int)); |
| 1012 // Verify width/right gets clamped properly too if x/y positive. |
| 1013 Rect size_overflow2(1, 2, 123456, 234567); |
| 1014 EXPECT_EQ(ScaleToEnclosingRectSafe(size_overflow2, 100000, 100000), |
| 1015 Rect(100000, 200000, max_int - 100000, max_int - 200000)); |
| 1016 |
| 1017 Rect max_rect(max_int, max_int, max_int, max_int); |
| 1018 EXPECT_EQ(ScaleToEnclosingRectSafe(max_rect, max_int, max_int), |
| 1019 Rect(max_int, max_int, 0, 0)); |
| 1020 |
| 1021 Rect min_rect(min_int, min_int, max_int, max_int); |
| 1022 // Min rect can't be scaled up any further in any dimension. |
| 1023 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, 2, 3.5), min_rect); |
| 1024 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, max_int, max_int), min_rect); |
| 1025 // Min rect scaled by min is an empty rect at (max, max) |
| 1026 EXPECT_EQ(ScaleToEnclosingRectSafe(min_rect, min_int, min_int), max_rect); |
| 1027 } |
| 1028 |
986 } // namespace gfx | 1029 } // namespace gfx |
OLD | NEW |