Fix search results being clipped in app list.
[chromium-blink-merge.git] / ui / gfx / geometry / vector3d_unittest.cc
blob2a3a1a0c5d836503e0814c43dbb77e5d41952af7
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.
5 #include <cmath>
6 #include <limits>
8 #include "base/basictypes.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gfx/geometry/vector3d_f.h"
12 namespace gfx {
14 TEST(Vector3dTest, IsZero) {
15 gfx::Vector3dF float_zero(0, 0, 0);
16 gfx::Vector3dF float_nonzero(0.1f, -0.1f, 0.1f);
18 EXPECT_TRUE(float_zero.IsZero());
19 EXPECT_FALSE(float_nonzero.IsZero());
22 TEST(Vector3dTest, Add) {
23 gfx::Vector3dF f1(3.1f, 5.1f, 2.7f);
24 gfx::Vector3dF f2(4.3f, -1.3f, 8.1f);
26 const struct {
27 gfx::Vector3dF expected;
28 gfx::Vector3dF actual;
29 } float_tests[] = {
30 { gfx::Vector3dF(3.1F, 5.1F, 2.7f), f1 + gfx::Vector3dF() },
31 { gfx::Vector3dF(3.1f + 4.3f, 5.1f - 1.3f, 2.7f + 8.1f), f1 + f2 },
32 { gfx::Vector3dF(3.1f - 4.3f, 5.1f + 1.3f, 2.7f - 8.1f), f1 - f2 }
35 for (size_t i = 0; i < arraysize(float_tests); ++i)
36 EXPECT_EQ(float_tests[i].expected.ToString(),
37 float_tests[i].actual.ToString());
40 TEST(Vector3dTest, Negative) {
41 const struct {
42 gfx::Vector3dF expected;
43 gfx::Vector3dF actual;
44 } float_tests[] = {
45 { gfx::Vector3dF(-0.0f, -0.0f, -0.0f), -gfx::Vector3dF(0, 0, 0) },
46 { gfx::Vector3dF(-0.3f, -0.3f, -0.3f), -gfx::Vector3dF(0.3f, 0.3f, 0.3f) },
47 { gfx::Vector3dF(0.3f, 0.3f, 0.3f), -gfx::Vector3dF(-0.3f, -0.3f, -0.3f) },
48 { gfx::Vector3dF(-0.3f, 0.3f, -0.3f), -gfx::Vector3dF(0.3f, -0.3f, 0.3f) },
49 { gfx::Vector3dF(0.3f, -0.3f, -0.3f), -gfx::Vector3dF(-0.3f, 0.3f, 0.3f) },
50 { gfx::Vector3dF(-0.3f, -0.3f, 0.3f), -gfx::Vector3dF(0.3f, 0.3f, -0.3f) }
53 for (size_t i = 0; i < arraysize(float_tests); ++i)
54 EXPECT_EQ(float_tests[i].expected.ToString(),
55 float_tests[i].actual.ToString());
58 TEST(Vector3dTest, Scale) {
59 float triple_values[][6] = {
60 { 4.5f, 1.2f, 1.8f, 3.3f, 5.6f, 4.2f },
61 { 4.5f, -1.2f, -1.8f, 3.3f, 5.6f, 4.2f },
62 { 4.5f, 1.2f, -1.8f, 3.3f, 5.6f, 4.2f },
63 { 4.5f, -1.2f -1.8f, 3.3f, 5.6f, 4.2f },
65 { 4.5f, 1.2f, 1.8f, 3.3f, -5.6f, -4.2f },
66 { 4.5f, 1.2f, 1.8f, -3.3f, -5.6f, -4.2f },
67 { 4.5f, 1.2f, -1.8f, 3.3f, -5.6f, -4.2f },
68 { 4.5f, 1.2f, -1.8f, -3.3f, -5.6f, -4.2f },
70 { -4.5f, 1.2f, 1.8f, 3.3f, 5.6f, 4.2f },
71 { -4.5f, 1.2f, 1.8f, 0, 5.6f, 4.2f },
72 { -4.5f, 1.2f, -1.8f, 3.3f, 5.6f, 4.2f },
73 { -4.5f, 1.2f, -1.8f, 0, 5.6f, 4.2f },
75 { -4.5f, 1.2f, 1.8f, 3.3f, 0, 4.2f },
76 { 4.5f, 0, 1.8f, 3.3f, 5.6f, 4.2f },
77 { -4.5f, 1.2f, -1.8f, 3.3f, 0, 4.2f },
78 { 4.5f, 0, -1.8f, 3.3f, 5.6f, 4.2f },
79 { -4.5f, 1.2f, 1.8f, 3.3f, 5.6f, 0 },
80 { -4.5f, 1.2f, -1.8f, 3.3f, 5.6f, 0 },
82 { 0, 1.2f, 0, 3.3f, 5.6f, 4.2f },
83 { 0, 1.2f, 1.8f, 3.3f, 5.6f, 4.2f }
86 for (size_t i = 0; i < arraysize(triple_values); ++i) {
87 gfx::Vector3dF v(triple_values[i][0],
88 triple_values[i][1],
89 triple_values[i][2]);
90 v.Scale(triple_values[i][3], triple_values[i][4], triple_values[i][5]);
91 EXPECT_EQ(triple_values[i][0] * triple_values[i][3], v.x());
92 EXPECT_EQ(triple_values[i][1] * triple_values[i][4], v.y());
93 EXPECT_EQ(triple_values[i][2] * triple_values[i][5], v.z());
95 Vector3dF v2 = ScaleVector3d(
96 gfx::Vector3dF(triple_values[i][0],
97 triple_values[i][1],
98 triple_values[i][2]),
99 triple_values[i][3], triple_values[i][4], triple_values[i][5]);
100 EXPECT_EQ(triple_values[i][0] * triple_values[i][3], v2.x());
101 EXPECT_EQ(triple_values[i][1] * triple_values[i][4], v2.y());
102 EXPECT_EQ(triple_values[i][2] * triple_values[i][5], v2.z());
105 float single_values[][4] = {
106 { 4.5f, 1.2f, 1.8f, 3.3f },
107 { 4.5f, -1.2f, 1.8f, 3.3f },
108 { 4.5f, 1.2f, -1.8f, 3.3f },
109 { 4.5f, -1.2f, -1.8f, 3.3f },
110 { -4.5f, 1.2f, 3.3f },
111 { -4.5f, 1.2f, 0 },
112 { -4.5f, 1.2f, 1.8f, 3.3f },
113 { -4.5f, 1.2f, 1.8f, 0 },
114 { 4.5f, 0, 1.8f, 3.3f },
115 { 0, 1.2f, 1.8f, 3.3f },
116 { 4.5f, 0, 1.8f, 3.3f },
117 { 0, 1.2f, 1.8f, 3.3f },
118 { 4.5f, 1.2f, 0, 3.3f },
119 { 4.5f, 1.2f, 0, 3.3f }
122 for (size_t i = 0; i < arraysize(single_values); ++i) {
123 gfx::Vector3dF v(single_values[i][0],
124 single_values[i][1],
125 single_values[i][2]);
126 v.Scale(single_values[i][3]);
127 EXPECT_EQ(single_values[i][0] * single_values[i][3], v.x());
128 EXPECT_EQ(single_values[i][1] * single_values[i][3], v.y());
129 EXPECT_EQ(single_values[i][2] * single_values[i][3], v.z());
131 Vector3dF v2 = ScaleVector3d(
132 gfx::Vector3dF(single_values[i][0],
133 single_values[i][1],
134 single_values[i][2]),
135 single_values[i][3]);
136 EXPECT_EQ(single_values[i][0] * single_values[i][3], v2.x());
137 EXPECT_EQ(single_values[i][1] * single_values[i][3], v2.y());
138 EXPECT_EQ(single_values[i][2] * single_values[i][3], v2.z());
142 TEST(Vector3dTest, Length) {
143 float float_values[][3] = {
144 { 0, 0, 0 },
145 { 10.5f, 20.5f, 8.5f },
146 { 20.5f, 10.5f, 8.5f },
147 { 8.5f, 20.5f, 10.5f },
148 { 10.5f, 8.5f, 20.5f },
149 { -10.5f, -20.5f, -8.5f },
150 { -20.5f, 10.5f, -8.5f },
151 { -8.5f, -20.5f, -10.5f },
152 { -10.5f, -8.5f, -20.5f },
153 { 10.5f, -20.5f, 8.5f },
154 { -10.5f, 20.5f, 8.5f },
155 { 10.5f, -20.5f, -8.5f },
156 { -10.5f, 20.5f, -8.5f },
157 // A large vector that fails if the Length function doesn't use
158 // double precision internally.
159 { 1236278317862780234892374893213178027.12122348904204230f,
160 335890352589839028212313231225425134332.38123f,
161 27861786423846742743236423478236784678.236713617231f }
164 for (size_t i = 0; i < arraysize(float_values); ++i) {
165 double v0 = float_values[i][0];
166 double v1 = float_values[i][1];
167 double v2 = float_values[i][2];
168 double length_squared =
169 static_cast<double>(v0) * v0 +
170 static_cast<double>(v1) * v1 +
171 static_cast<double>(v2) * v2;
172 double length = std::sqrt(length_squared);
173 gfx::Vector3dF vector(v0, v1, v2);
174 EXPECT_DOUBLE_EQ(length_squared, vector.LengthSquared());
175 EXPECT_FLOAT_EQ(static_cast<float>(length), vector.Length());
179 TEST(Vector3dTest, DotProduct) {
180 const struct {
181 float expected;
182 gfx::Vector3dF input1;
183 gfx::Vector3dF input2;
184 } tests[] = {
185 { 0, gfx::Vector3dF(1, 0, 0), gfx::Vector3dF(0, 1, 1) },
186 { 0, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(1, 0, 1) },
187 { 0, gfx::Vector3dF(0, 0, 1), gfx::Vector3dF(1, 1, 0) },
189 { 3, gfx::Vector3dF(1, 1, 1), gfx::Vector3dF(1, 1, 1) },
191 { 1.2f, gfx::Vector3dF(1.2f, -1.2f, 1.2f), gfx::Vector3dF(1, 1, 1) },
192 { 1.2f, gfx::Vector3dF(1, 1, 1), gfx::Vector3dF(1.2f, -1.2f, 1.2f) },
194 { 38.72f,
195 gfx::Vector3dF(1.1f, 2.2f, 3.3f), gfx::Vector3dF(4.4f, 5.5f, 6.6f) }
198 for (size_t i = 0; i < arraysize(tests); ++i) {
199 float actual = gfx::DotProduct(tests[i].input1, tests[i].input2);
200 EXPECT_EQ(tests[i].expected, actual);
204 TEST(Vector3dTest, CrossProduct) {
205 const struct {
206 gfx::Vector3dF expected;
207 gfx::Vector3dF input1;
208 gfx::Vector3dF input2;
209 } tests[] = {
210 { Vector3dF(), Vector3dF(), Vector3dF(1, 1, 1) },
211 { Vector3dF(), Vector3dF(1, 1, 1), Vector3dF() },
212 { Vector3dF(), Vector3dF(1, 1, 1), Vector3dF(1, 1, 1) },
213 { Vector3dF(),
214 Vector3dF(1.6f, 10.6f, -10.6f),
215 Vector3dF(1.6f, 10.6f, -10.6f) },
217 { Vector3dF(1, -1, 0), Vector3dF(1, 1, 1), Vector3dF(0, 0, 1) },
218 { Vector3dF(-1, 0, 1), Vector3dF(1, 1, 1), Vector3dF(0, 1, 0) },
219 { Vector3dF(0, 1, -1), Vector3dF(1, 1, 1), Vector3dF(1, 0, 0) },
221 { Vector3dF(-1, 1, 0), Vector3dF(0, 0, 1), Vector3dF(1, 1, 1) },
222 { Vector3dF(1, 0, -1), Vector3dF(0, 1, 0), Vector3dF(1, 1, 1) },
223 { Vector3dF(0, -1, 1), Vector3dF(1, 0, 0), Vector3dF(1, 1, 1) }
226 for (size_t i = 0; i < arraysize(tests); ++i) {
227 Vector3dF actual = gfx::CrossProduct(tests[i].input1, tests[i].input2);
228 EXPECT_EQ(tests[i].expected.ToString(), actual.ToString());
232 TEST(Vector3dFTest, ClampVector3dF) {
233 Vector3dF a;
235 a = Vector3dF(3.5f, 5.5f, 7.5f);
236 EXPECT_EQ(Vector3dF(3.5f, 5.5f, 7.5f).ToString(), a.ToString());
237 a.SetToMax(Vector3dF(2, 4.5f, 6.5f));
238 EXPECT_EQ(Vector3dF(3.5f, 5.5f, 7.5f).ToString(), a.ToString());
239 a.SetToMax(Vector3dF(3.5f, 5.5f, 7.5f));
240 EXPECT_EQ(Vector3dF(3.5f, 5.5f, 7.5f).ToString(), a.ToString());
241 a.SetToMax(Vector3dF(4.5f, 2, 6.5f));
242 EXPECT_EQ(Vector3dF(4.5f, 5.5f, 7.5f).ToString(), a.ToString());
243 a.SetToMax(Vector3dF(3.5f, 6.5f, 6.5f));
244 EXPECT_EQ(Vector3dF(4.5f, 6.5f, 7.5f).ToString(), a.ToString());
245 a.SetToMax(Vector3dF(3.5f, 5.5f, 8.5f));
246 EXPECT_EQ(Vector3dF(4.5f, 6.5f, 8.5f).ToString(), a.ToString());
247 a.SetToMax(Vector3dF(8.5f, 10.5f, 12.5f));
248 EXPECT_EQ(Vector3dF(8.5f, 10.5f, 12.5f).ToString(), a.ToString());
250 a.SetToMin(Vector3dF(9.5f, 11.5f, 13.5f));
251 EXPECT_EQ(Vector3dF(8.5f, 10.5f, 12.5f).ToString(), a.ToString());
252 a.SetToMin(Vector3dF(8.5f, 10.5f, 12.5f));
253 EXPECT_EQ(Vector3dF(8.5f, 10.5f, 12.5f).ToString(), a.ToString());
254 a.SetToMin(Vector3dF(7.5f, 11.5f, 13.5f));
255 EXPECT_EQ(Vector3dF(7.5f, 10.5f, 12.5f).ToString(), a.ToString());
256 a.SetToMin(Vector3dF(9.5f, 9.5f, 13.5f));
257 EXPECT_EQ(Vector3dF(7.5f, 9.5f, 12.5f).ToString(), a.ToString());
258 a.SetToMin(Vector3dF(9.5f, 11.5f, 11.5f));
259 EXPECT_EQ(Vector3dF(7.5f, 9.5f, 11.5f).ToString(), a.ToString());
260 a.SetToMin(Vector3dF(3.5f, 5.5f, 7.5f));
261 EXPECT_EQ(Vector3dF(3.5f, 5.5f, 7.5f).ToString(), a.ToString());
264 TEST(Vector3dTest, AngleBetweenVectorsInDegress) {
265 const struct {
266 float expected;
267 gfx::Vector3dF input1;
268 gfx::Vector3dF input2;
269 } tests[] = {
270 {0, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(0, 1, 0)},
271 {90, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(0, 0, 1)},
272 {45,
273 gfx::Vector3dF(0, 1, 0),
274 gfx::Vector3dF(0, 0.70710678188f, 0.70710678188f)},
275 {180, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(0, -1, 0)},
278 for (size_t i = 0; i < arraysize(tests); ++i) {
279 float actual =
280 gfx::AngleBetweenVectorsInDegrees(tests[i].input1, tests[i].input2);
281 EXPECT_FLOAT_EQ(tests[i].expected, actual);
282 actual =
283 gfx::AngleBetweenVectorsInDegrees(tests[i].input2, tests[i].input1);
284 EXPECT_FLOAT_EQ(tests[i].expected, actual);
288 TEST(Vector3dTest, ClockwiseAngleBetweenVectorsInDegress) {
289 const struct {
290 float expected;
291 gfx::Vector3dF input1;
292 gfx::Vector3dF input2;
293 } tests[] = {
294 {0, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(0, 1, 0)},
295 {90, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(0, 0, -1)},
296 {45,
297 gfx::Vector3dF(0, -1, 0),
298 gfx::Vector3dF(0, -0.70710678188f, 0.70710678188f)},
299 {180, gfx::Vector3dF(0, -1, 0), gfx::Vector3dF(0, 1, 0)},
300 {270, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(0, 0, 1)},
303 const gfx::Vector3dF normal_vector(1.0f, 0.0f, 0.0f);
305 for (size_t i = 0; i < arraysize(tests); ++i) {
306 float actual = gfx::ClockwiseAngleBetweenVectorsInDegrees(
307 tests[i].input1, tests[i].input2, normal_vector);
308 EXPECT_FLOAT_EQ(tests[i].expected, actual);
309 actual = -gfx::ClockwiseAngleBetweenVectorsInDegrees(
310 tests[i].input2, tests[i].input1, normal_vector);
311 if (actual < 0.0f)
312 actual += 360.0f;
313 EXPECT_FLOAT_EQ(tests[i].expected, actual);
317 } // namespace gfx