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

Unified Diff: ui/gfx/vector2d_unittest.cc

Issue 11269022: Add Vector2d classes that represent offsets, instead of using Point. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: more vector use fixes Created 8 years, 2 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
Index: ui/gfx/vector2d_unittest.cc
diff --git a/ui/gfx/vector2d_unittest.cc b/ui/gfx/vector2d_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9f49583dc11794d7eafc4254122cd22a210c33ea
--- /dev/null
+++ b/ui/gfx/vector2d_unittest.cc
@@ -0,0 +1,195 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/basictypes.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "ui/gfx/vector2d.h"
+#include "ui/gfx/vector2d_f.h"
+
+#include <cmath>
+#include <limits>
+
+namespace gfx {
+
+TEST(Vector2dTest, ConversionToFloat) {
+ gfx::Vector2d i(3, 4);
+ gfx::Vector2dF f = i;
+ EXPECT_EQ(i, f);
+}
+
+TEST(Vector2dTest, IsZero) {
+ gfx::Vector2d int_zero(0, 0);
+ gfx::Vector2d int_nonzero(2, -2);
+ gfx::Vector2dF float_zero(0, 0);
+ gfx::Vector2dF float_nonzero(0.1f, -0.1f);
+
+ EXPECT_TRUE(int_zero.IsZero());
+ EXPECT_FALSE(int_nonzero.IsZero());
+ EXPECT_TRUE(float_zero.IsZero());
+ EXPECT_FALSE(float_nonzero.IsZero());
+}
+
+TEST(Vector2dTest, Grow) {
+ gfx::Vector2d i(3, 5);
+ gfx::Vector2dF f(3.1f, 5.1f);
+
+ i.Grow(4, -1);
+ EXPECT_EQ(gfx::Vector2d(3 + 4, 5 - 1).ToString(), i.ToString());
+
+ f.Grow(4.3f, -1.3f);
+ EXPECT_EQ(gfx::Vector2dF(3.1f + 4.3f, 5.1f - 1.3f).ToString(), f.ToString());
+}
+
+TEST(Vector2dTest, Add) {
+ gfx::Vector2d i1(3, 5);
+ gfx::Vector2d i2(4, -1);
+
+ static const struct {
Peter Kasting 2012/10/30 01:14:14 Nit: No need for these local consts to be marked s
+ gfx::Vector2d expected;
+ gfx::Vector2d actual;
+ } int_tests[] = {
+ { gfx::Vector2d(3, 5), i1 + gfx::Vector2d() },
+ { gfx::Vector2d(3 + 4, 5 - 1), i1 + i2 },
+ { gfx::Vector2d(3 - 4, 5 + 1), i1 - i2 }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_tests); ++i)
Peter Kasting 2012/10/30 01:14:14 Nit: Can you use arraysize() instead if you give t
danakj 2012/10/30 19:21:21 Was following from other tests. Why is this prefer
Peter Kasting 2012/10/30 20:24:37 Does it? My hope was that simply changing from:
danakj 2012/10/30 21:14:52 // One caveat is that arraysize() doesn't accept a
+ EXPECT_EQ(int_tests[i].expected.ToString(),
+ int_tests[i].actual.ToString());
+
+ gfx::Vector2dF f1(3.1f, 5.1f);
+ gfx::Vector2dF f2(4.3f, -1.3f);
+
+ static const struct {
+ gfx::Vector2dF expected;
+ gfx::Vector2dF actual;
+ } float_tests[] = {
+ { gfx::Vector2dF(3.1F, 5.1F), f1 + gfx::Vector2d() },
+ { gfx::Vector2dF(3.1F, 5.1F), f1 + gfx::Vector2dF() },
+ { gfx::Vector2dF(3.1f + 4.3f, 5.1f - 1.3f), f1 + f2 },
+ { gfx::Vector2dF(3.1f - 4.3f, 5.1f + 1.3f), f1 - f2 }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i)
+ EXPECT_EQ(float_tests[i].expected.ToString(),
+ float_tests[i].actual.ToString());
+}
+
+TEST(Vector2dTest, Negative) {
+ static const struct {
+ gfx::Vector2d expected;
+ gfx::Vector2d actual;
+ } int_tests[] = {
+ { gfx::Vector2d(0, 0), -gfx::Vector2d(0, 0) },
+ { gfx::Vector2d(-3, -3), -gfx::Vector2d(3, 3) },
+ { gfx::Vector2d(3, 3), -gfx::Vector2d(-3, -3) },
+ { gfx::Vector2d(-3, 3), -gfx::Vector2d(3, -3) },
+ { gfx::Vector2d(3, -3), -gfx::Vector2d(-3, 3) }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_tests); ++i)
+ EXPECT_EQ(int_tests[i].expected.ToString(),
+ int_tests[i].actual.ToString());
+
+ static const struct {
+ gfx::Vector2dF expected;
+ gfx::Vector2dF actual;
+ } float_tests[] = {
+ { gfx::Vector2dF(0, 0), -gfx::Vector2d(0, 0) },
+ { gfx::Vector2dF(-0.3f, -0.3f), -gfx::Vector2dF(0.3f, 0.3f) },
+ { gfx::Vector2dF(0.3f, 0.3f), -gfx::Vector2dF(-0.3f, -0.3f) },
+ { gfx::Vector2dF(-0.3f, 0.3f), -gfx::Vector2dF(0.3f, -0.3f) },
+ { gfx::Vector2dF(0.3f, -0.3f), -gfx::Vector2dF(-0.3f, 0.3f) }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i)
+ EXPECT_EQ(float_tests[i].expected.ToString(),
+ float_tests[i].actual.ToString());
+}
+
+TEST(Vector2dTest, Scale) {
+ float double_values[][4] = {
+ { 4.5f, 1.2f, 3.3f, 5.6f },
+ { 4.5f, -1.2f, 3.3f, 5.6f },
+ { 4.5f, 1.2f, 3.3f, -5.6f },
+ { 4.5f, 1.2f, -3.3f, -5.6f },
+ { -4.5f, 1.2f, 3.3f, 5.6f },
+ { -4.5f, 1.2f, 0, 5.6f },
+ { -4.5f, 1.2f, 3.3f, 0 },
+ { 4.5f, 0, 3.3f, 5.6f },
+ { 0, 1.2f, 3.3f, 5.6f }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(double_values); ++i) {
+ gfx::Vector2dF v(double_values[i][0], double_values[i][1]);
+ v.Scale(double_values[i][2], double_values[i][3]);
+ EXPECT_EQ(v.x(), double_values[i][0] * double_values[i][2]);
+ EXPECT_EQ(v.y(), double_values[i][1] * double_values[i][3]);
+ }
+
+ float single_values[][3] = {
+ { 4.5f, 1.2f, 3.3f },
+ { 4.5f, -1.2f, 3.3f },
+ { 4.5f, 1.2f, 3.3f },
+ { 4.5f, 1.2f, -3.3f },
+ { -4.5f, 1.2f, 3.3f },
+ { -4.5f, 1.2f, 0 },
+ { -4.5f, 1.2f, 3.3f },
+ { 4.5f, 0, 3.3f },
+ { 0, 1.2f, 3.3f }
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(single_values); ++i) {
+ gfx::Vector2dF v(single_values[i][0], single_values[i][1]);
+ v.Scale(single_values[i][2]);
+ EXPECT_EQ(v.x(), single_values[i][0] * single_values[i][2]);
+ EXPECT_EQ(v.y(), single_values[i][1] * single_values[i][2]);
+ }
+}
+
+TEST(Vector2dTest, Length) {
+ int int_values[][2] = {
+ { 0, 0 },
+ { 10, 20 },
+ { 20, 10 },
+ { -10, -20 },
+ { -20, 10 },
+ { 10, -20 },
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_values); ++i) {
+ int v0 = int_values[i][0];
+ int v1 = int_values[i][1];
+ double length_squared = v0 * v0 + v1 * v1;
+ double length = std::sqrt(length_squared);
+ gfx::Vector2d vector(v0, v1);
+ EXPECT_EQ(static_cast<float>(length_squared), vector.LengthSquared());
+ EXPECT_EQ(static_cast<float>(length), vector.Length());
+ }
+
+ float float_values[][2] = {
+ { 0, 0 },
+ { 10.5f, 20.5f },
+ { 20.5f, 10.5f },
+ { -10.5f, -20.5f },
+ { -20.5f, 10.5f },
+ { 10.5f, -20.5f },
+ // A large vector that fails if the Length function doesn't use
+ // double precision internally.
+ { 1236278317862780234892374893213178027.12122348904204230f,
+ 335890352589839028212313231225425134332.38123f },
+ };
+
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_values); ++i) {
+ double v0 = float_values[i][0];
+ double v1 = float_values[i][1];
+ double length_squared = v0 * v0 + v1 * v1;
+ double length = std::sqrt(length_squared);
+ gfx::Vector2dF vector(v0, v1);
+ EXPECT_EQ(static_cast<float>(length_squared), vector.LengthSquared());
+ EXPECT_EQ(static_cast<float>(length), vector.Length());
+ }
+}
+
+} // namespace ui

Powered by Google App Engine
This is Rietveld 408576698