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

Side by Side 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, 1 month 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/basictypes.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "ui/gfx/vector2d.h"
8 #include "ui/gfx/vector2d_f.h"
9
10 #include <cmath>
11 #include <limits>
12
13 namespace gfx {
14
15 TEST(Vector2dTest, ConversionToFloat) {
16 gfx::Vector2d i(3, 4);
17 gfx::Vector2dF f = i;
18 EXPECT_EQ(i, f);
19 }
20
21 TEST(Vector2dTest, IsZero) {
22 gfx::Vector2d int_zero(0, 0);
23 gfx::Vector2d int_nonzero(2, -2);
24 gfx::Vector2dF float_zero(0, 0);
25 gfx::Vector2dF float_nonzero(0.1f, -0.1f);
26
27 EXPECT_TRUE(int_zero.IsZero());
28 EXPECT_FALSE(int_nonzero.IsZero());
29 EXPECT_TRUE(float_zero.IsZero());
30 EXPECT_FALSE(float_nonzero.IsZero());
31 }
32
33 TEST(Vector2dTest, Grow) {
34 gfx::Vector2d i(3, 5);
35 gfx::Vector2dF f(3.1f, 5.1f);
36
37 i.Grow(4, -1);
38 EXPECT_EQ(gfx::Vector2d(3 + 4, 5 - 1).ToString(), i.ToString());
39
40 f.Grow(4.3f, -1.3f);
41 EXPECT_EQ(gfx::Vector2dF(3.1f + 4.3f, 5.1f - 1.3f).ToString(), f.ToString());
42 }
43
44 TEST(Vector2dTest, Add) {
45 gfx::Vector2d i1(3, 5);
46 gfx::Vector2d i2(4, -1);
47
48 static const struct {
Peter Kasting 2012/10/30 01:14:14 Nit: No need for these local consts to be marked s
49 gfx::Vector2d expected;
50 gfx::Vector2d actual;
51 } int_tests[] = {
52 { gfx::Vector2d(3, 5), i1 + gfx::Vector2d() },
53 { gfx::Vector2d(3 + 4, 5 - 1), i1 + i2 },
54 { gfx::Vector2d(3 - 4, 5 + 1), i1 - i2 }
55 };
56
57 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
58 EXPECT_EQ(int_tests[i].expected.ToString(),
59 int_tests[i].actual.ToString());
60
61 gfx::Vector2dF f1(3.1f, 5.1f);
62 gfx::Vector2dF f2(4.3f, -1.3f);
63
64 static const struct {
65 gfx::Vector2dF expected;
66 gfx::Vector2dF actual;
67 } float_tests[] = {
68 { gfx::Vector2dF(3.1F, 5.1F), f1 + gfx::Vector2d() },
69 { gfx::Vector2dF(3.1F, 5.1F), f1 + gfx::Vector2dF() },
70 { gfx::Vector2dF(3.1f + 4.3f, 5.1f - 1.3f), f1 + f2 },
71 { gfx::Vector2dF(3.1f - 4.3f, 5.1f + 1.3f), f1 - f2 }
72 };
73
74 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i)
75 EXPECT_EQ(float_tests[i].expected.ToString(),
76 float_tests[i].actual.ToString());
77 }
78
79 TEST(Vector2dTest, Negative) {
80 static const struct {
81 gfx::Vector2d expected;
82 gfx::Vector2d actual;
83 } int_tests[] = {
84 { gfx::Vector2d(0, 0), -gfx::Vector2d(0, 0) },
85 { gfx::Vector2d(-3, -3), -gfx::Vector2d(3, 3) },
86 { gfx::Vector2d(3, 3), -gfx::Vector2d(-3, -3) },
87 { gfx::Vector2d(-3, 3), -gfx::Vector2d(3, -3) },
88 { gfx::Vector2d(3, -3), -gfx::Vector2d(-3, 3) }
89 };
90
91 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_tests); ++i)
92 EXPECT_EQ(int_tests[i].expected.ToString(),
93 int_tests[i].actual.ToString());
94
95 static const struct {
96 gfx::Vector2dF expected;
97 gfx::Vector2dF actual;
98 } float_tests[] = {
99 { gfx::Vector2dF(0, 0), -gfx::Vector2d(0, 0) },
100 { gfx::Vector2dF(-0.3f, -0.3f), -gfx::Vector2dF(0.3f, 0.3f) },
101 { gfx::Vector2dF(0.3f, 0.3f), -gfx::Vector2dF(-0.3f, -0.3f) },
102 { gfx::Vector2dF(-0.3f, 0.3f), -gfx::Vector2dF(0.3f, -0.3f) },
103 { gfx::Vector2dF(0.3f, -0.3f), -gfx::Vector2dF(-0.3f, 0.3f) }
104 };
105
106 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i)
107 EXPECT_EQ(float_tests[i].expected.ToString(),
108 float_tests[i].actual.ToString());
109 }
110
111 TEST(Vector2dTest, Scale) {
112 float double_values[][4] = {
113 { 4.5f, 1.2f, 3.3f, 5.6f },
114 { 4.5f, -1.2f, 3.3f, 5.6f },
115 { 4.5f, 1.2f, 3.3f, -5.6f },
116 { 4.5f, 1.2f, -3.3f, -5.6f },
117 { -4.5f, 1.2f, 3.3f, 5.6f },
118 { -4.5f, 1.2f, 0, 5.6f },
119 { -4.5f, 1.2f, 3.3f, 0 },
120 { 4.5f, 0, 3.3f, 5.6f },
121 { 0, 1.2f, 3.3f, 5.6f }
122 };
123
124 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(double_values); ++i) {
125 gfx::Vector2dF v(double_values[i][0], double_values[i][1]);
126 v.Scale(double_values[i][2], double_values[i][3]);
127 EXPECT_EQ(v.x(), double_values[i][0] * double_values[i][2]);
128 EXPECT_EQ(v.y(), double_values[i][1] * double_values[i][3]);
129 }
130
131 float single_values[][3] = {
132 { 4.5f, 1.2f, 3.3f },
133 { 4.5f, -1.2f, 3.3f },
134 { 4.5f, 1.2f, 3.3f },
135 { 4.5f, 1.2f, -3.3f },
136 { -4.5f, 1.2f, 3.3f },
137 { -4.5f, 1.2f, 0 },
138 { -4.5f, 1.2f, 3.3f },
139 { 4.5f, 0, 3.3f },
140 { 0, 1.2f, 3.3f }
141 };
142
143 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(single_values); ++i) {
144 gfx::Vector2dF v(single_values[i][0], single_values[i][1]);
145 v.Scale(single_values[i][2]);
146 EXPECT_EQ(v.x(), single_values[i][0] * single_values[i][2]);
147 EXPECT_EQ(v.y(), single_values[i][1] * single_values[i][2]);
148 }
149 }
150
151 TEST(Vector2dTest, Length) {
152 int int_values[][2] = {
153 { 0, 0 },
154 { 10, 20 },
155 { 20, 10 },
156 { -10, -20 },
157 { -20, 10 },
158 { 10, -20 },
159 };
160
161 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_values); ++i) {
162 int v0 = int_values[i][0];
163 int v1 = int_values[i][1];
164 double length_squared = v0 * v0 + v1 * v1;
165 double length = std::sqrt(length_squared);
166 gfx::Vector2d vector(v0, v1);
167 EXPECT_EQ(static_cast<float>(length_squared), vector.LengthSquared());
168 EXPECT_EQ(static_cast<float>(length), vector.Length());
169 }
170
171 float float_values[][2] = {
172 { 0, 0 },
173 { 10.5f, 20.5f },
174 { 20.5f, 10.5f },
175 { -10.5f, -20.5f },
176 { -20.5f, 10.5f },
177 { 10.5f, -20.5f },
178 // A large vector that fails if the Length function doesn't use
179 // double precision internally.
180 { 1236278317862780234892374893213178027.12122348904204230f,
181 335890352589839028212313231225425134332.38123f },
182 };
183
184 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_values); ++i) {
185 double v0 = float_values[i][0];
186 double v1 = float_values[i][1];
187 double length_squared = v0 * v0 + v1 * v1;
188 double length = std::sqrt(length_squared);
189 gfx::Vector2dF vector(v0, v1);
190 EXPECT_EQ(static_cast<float>(length_squared), vector.LengthSquared());
191 EXPECT_EQ(static_cast<float>(length), vector.Length());
192 }
193 }
194
195 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698