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

Unified Diff: ui/gfx/rect_unittest.cc

Issue 10996037: Do not convert from RectF to Rect by flooring. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing mac build. Created 8 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/rect_f.cc ('k') | ui/gfx/safe_floor_ceil.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/rect_unittest.cc
diff --git a/ui/gfx/rect_unittest.cc b/ui/gfx/rect_unittest.cc
index 4eefdd69f5622da8e6a058cfafd21e77eb37efd4..abc94c1bdd8f68edf50df33bc97871bba7847af5 100644
--- a/ui/gfx/rect_unittest.cc
+++ b/ui/gfx/rect_unittest.cc
@@ -5,8 +5,11 @@
#include "base/basictypes.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/rect.h"
+#include "ui/gfx/rect_conversions.h"
#include "ui/gfx/skia_util.h"
+#include <limits>
+
namespace ui {
TEST(RectTest, Contains) {
@@ -344,6 +347,158 @@ TEST(RectTest, SkRectToRect) {
EXPECT_EQ(src, gfx::SkRectToRect(skrect));
}
+// Similar to EXPECT_FLOAT_EQ, but lets NaN equal NaN
+#define EXPECT_FLOAT_AND_NAN_EQ(a, b) \
+ { if (a == a || b == b) { EXPECT_FLOAT_EQ(a, b); } }
+
+
+TEST(RectTest, ScaleRect) {
+ static const struct Test {
+ int x1; // source
+ int y1;
+ int w1;
+ int h1;
+ float scale;
+ float x2; // target
+ float y2;
+ float w2;
+ float h2;
+ } tests[] = {
+ { 3, 3, 3, 3,
+ 1.5f,
+ 4.5f, 4.5f, 4.5f, 4.5f },
+ { 3, 3, 3, 3,
+ 0.0f,
+ 0.0f, 0.0f, 0.0f, 0.0f },
+ { 3, 3, 3, 3,
+ std::numeric_limits<float>::quiet_NaN(),
+ std::numeric_limits<float>::quiet_NaN(),
+ std::numeric_limits<float>::quiet_NaN(),
+ std::numeric_limits<float>::quiet_NaN(),
+ std::numeric_limits<float>::quiet_NaN() },
+ { 3, 3, 3, 3,
+ std::numeric_limits<float>::max(),
+ std::numeric_limits<float>::max(),
+ std::numeric_limits<float>::max(),
+ std::numeric_limits<float>::max(),
+ std::numeric_limits<float>::max() },
+ { 3, 3, 3, 3,
+ -1.0f,
+ -3.0f, -3.0f, 0.0f, 0.0f }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
+ gfx::Rect r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
+ gfx::RectF r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
+
+ gfx::RectF scaled = r1.Scale(tests[i].scale);
+ EXPECT_FLOAT_AND_NAN_EQ(r2.x(), scaled.x());
+ EXPECT_FLOAT_AND_NAN_EQ(r2.y(), scaled.y());
+ EXPECT_FLOAT_AND_NAN_EQ(r2.width(), scaled.width());
+ EXPECT_FLOAT_AND_NAN_EQ(r2.height(), scaled.height());
+ }
+}
+
+TEST(RectTest, ToEnclosedRect) {
+ static const struct Test {
+ float x1; // source
+ float y1;
+ float w1;
+ float h1;
+ int x2; // target
+ int y2;
+ int w2;
+ int h2;
+ } tests [] = {
+ { 0.0f, 0.0f, 0.0f, 0.0f,
+ 0, 0, 0, 0 },
+ { -1.5f, -1.5f, 3.0f, 3.0f,
+ -1, -1, 2, 2 },
+ { -1.5f, -1.5f, 3.5f, 3.5f,
+ -1, -1, 3, 3 },
+ { std::numeric_limits<float>::max(),
+ std::numeric_limits<float>::max(),
+ 2.0f, 2.0f,
+ std::numeric_limits<int>::max(),
+ std::numeric_limits<int>::max(),
+ 0, 0 },
+ { 0.0f, 0.0f,
+ std::numeric_limits<float>::max(),
+ std::numeric_limits<float>::max(),
+ 0, 0,
+ std::numeric_limits<int>::max(),
+ std::numeric_limits<int>::max() },
+ { 20000.5f, 20000.5f, 0.5f, 0.5f,
+ 20001, 20001, 0, 0 },
+ { std::numeric_limits<float>::quiet_NaN(),
+ std::numeric_limits<float>::quiet_NaN(),
+ std::numeric_limits<float>::quiet_NaN(),
+ std::numeric_limits<float>::quiet_NaN(),
+ 0, 0, 0, 0 }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
+ gfx::RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
+ gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
+
+ gfx::Rect enclosed = ToEnclosedRect(r1);
+ EXPECT_FLOAT_AND_NAN_EQ(r2.x(), enclosed.x());
+ EXPECT_FLOAT_AND_NAN_EQ(r2.y(), enclosed.y());
+ EXPECT_FLOAT_AND_NAN_EQ(r2.width(), enclosed.width());
+ EXPECT_FLOAT_AND_NAN_EQ(r2.height(), enclosed.height());
+ }
+}
+
+TEST(RectTest, ToEnclosingRect) {
+ static const struct Test {
+ float x1; // source
+ float y1;
+ float w1;
+ float h1;
+ int x2; // target
+ int y2;
+ int w2;
+ int h2;
+ } tests [] = {
+ { 0.0f, 0.0f, 0.0f, 0.0f,
+ 0, 0, 0, 0 },
+ { -1.5f, -1.5f, 3.0f, 3.0f,
+ -2, -2, 4, 4 },
+ { -1.5f, -1.5f, 3.5f, 3.5f,
+ -2, -2, 4, 4 },
+ { std::numeric_limits<float>::max(),
+ std::numeric_limits<float>::max(),
+ 2.0f, 2.0f,
+ std::numeric_limits<int>::max(),
+ std::numeric_limits<int>::max(),
+ 0, 0 },
+ { 0.0f, 0.0f,
+ std::numeric_limits<float>::max(),
+ std::numeric_limits<float>::max(),
+ 0, 0,
+ std::numeric_limits<int>::max(),
+ std::numeric_limits<int>::max() },
+ { 20000.5f, 20000.5f, 0.5f, 0.5f,
+ 20000, 20000, 1, 1 },
+ { std::numeric_limits<float>::quiet_NaN(),
+ std::numeric_limits<float>::quiet_NaN(),
+ std::numeric_limits<float>::quiet_NaN(),
+ std::numeric_limits<float>::quiet_NaN(),
+ 0, 0, 0, 0 }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
+ gfx::RectF r1(tests[i].x1, tests[i].y1, tests[i].w1, tests[i].h1);
+ gfx::Rect r2(tests[i].x2, tests[i].y2, tests[i].w2, tests[i].h2);
+
+ gfx::Rect enclosed = ToEnclosingRect(r1);
+ EXPECT_FLOAT_AND_NAN_EQ(r2.x(), enclosed.x());
+ EXPECT_FLOAT_AND_NAN_EQ(r2.y(), enclosed.y());
+ EXPECT_FLOAT_AND_NAN_EQ(r2.width(), enclosed.width());
+ EXPECT_FLOAT_AND_NAN_EQ(r2.height(), enclosed.height());
+ }
+}
+
#if defined(OS_WIN)
TEST(RectTest, ConstructAndAssign) {
const RECT rect_1 = { 0, 0, 10, 10 };
« no previous file with comments | « ui/gfx/rect_f.cc ('k') | ui/gfx/safe_floor_ceil.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698