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

Side by Side Diff: ui/gfx/vector3d_unittest.cc

Issue 11367025: ui: Add the gfx::Vector3dF class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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/vector3d_f.h"
8
9 #include <cmath>
10 #include <limits>
11
12 namespace gfx {
13
14 TEST(Vector3dTest, IsZero) {
15 gfx::Vector3dF float_zero(0, 0, 0);
16 gfx::Vector3dF float_nonzero(0.1f, -0.1f, 0.1f);
17
18 EXPECT_TRUE(float_zero.IsZero());
19 EXPECT_FALSE(float_nonzero.IsZero());
20 }
21
22 TEST(Vector3dTest, Add) {
23 gfx::Vector3dF f1(3.1f, 5.1f, 2.7f);
24 gfx::Vector3dF f2(4.3f, -1.3f, 8.1f);
25
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 }
33 };
34
35 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i)
36 EXPECT_EQ(float_tests[i].expected.ToString(),
37 float_tests[i].actual.ToString());
38 }
39
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) }
51 };
52
53 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i)
54 EXPECT_EQ(float_tests[i].expected.ToString(),
55 float_tests[i].actual.ToString());
56 }
57
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 },
64
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 },
69
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 },
74
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 },
81
82 { 0, 1.2f, 0, 3.3f, 5.6f, 4.2f },
83 { 0, 1.2f, 1.8f, 3.3f, 5.6f, 4.2f }
84 };
85
86 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(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(v.x(), triple_values[i][0] * triple_values[i][3]);
92 EXPECT_EQ(v.y(), triple_values[i][1] * triple_values[i][4]);
93 EXPECT_EQ(v.z(), triple_values[i][2] * triple_values[i][5]);
94 }
95
96 float single_values[][4] = {
97 { 4.5f, 1.2f, 1.8f, 3.3f },
98 { 4.5f, -1.2f, 1.8f, 3.3f },
99 { 4.5f, 1.2f, -1.8f, 3.3f },
100 { 4.5f, -1.2f, -1.8f, 3.3f },
101 { -4.5f, 1.2f, 3.3f },
102 { -4.5f, 1.2f, 0 },
103 { -4.5f, 1.2f, 1.8f, 3.3f },
104 { -4.5f, 1.2f, 1.8f, 0 },
105 { 4.5f, 0, 1.8f, 3.3f },
106 { 0, 1.2f, 1.8f, 3.3f },
107 { 4.5f, 0, 1.8f, 3.3f },
108 { 0, 1.2f, 1.8f, 3.3f },
109 { 4.5f, 1.2f, 0, 3.3f },
110 { 4.5f, 1.2f, 0, 3.3f }
111 };
112
113 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(single_values); ++i) {
114 gfx::Vector3dF v(single_values[i][0],
115 single_values[i][1],
116 single_values[i][2]);
117 v.Scale(single_values[i][3]);
118 EXPECT_EQ(v.x(), single_values[i][0] * single_values[i][3]);
119 EXPECT_EQ(v.y(), single_values[i][1] * single_values[i][3]);
120 EXPECT_EQ(v.z(), single_values[i][2] * single_values[i][3]);
121 }
122 }
123
124 TEST(Vector3dTest, Length) {
125 float float_values[][3] = {
126 { 0, 0, 0 },
127 { 10.5f, 20.5f, 8.5f },
128 { 20.5f, 10.5f, 8.5f },
129 { 8.5f, 20.5f, 10.5f },
130 { 10.5f, 8.5f, 20.5f },
131 { -10.5f, -20.5f, -8.5f },
132 { -20.5f, 10.5f, -8.5f },
133 { -8.5f, -20.5f, -10.5f },
134 { -10.5f, -8.5f, -20.5f },
135 { 10.5f, -20.5f, 8.5f },
136 { -10.5f, 20.5f, 8.5f },
137 { 10.5f, -20.5f, -8.5f },
138 { -10.5f, 20.5f, -8.5f },
139 // A large vector that fails if the Length function doesn't use
140 // double precision internally.
141 { 1236278317862780234892374893213178027.12122348904204230f,
142 335890352589839028212313231225425134332.38123f,
143 27861786423846742743236423478236784678.236713617231f }
144 };
145
146 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_values); ++i) {
147 double v0 = float_values[i][0];
148 double v1 = float_values[i][1];
149 double v2 = float_values[i][2];
150 double length_squared =
151 static_cast<double>(v0) * v0 +
152 static_cast<double>(v1) * v1 +
153 static_cast<double>(v2) * v2;
154 double length = std::sqrt(length_squared);
155 gfx::Vector3dF vector(v0, v1, v2);
156 EXPECT_EQ(length_squared, vector.LengthSquared());
157 EXPECT_EQ(static_cast<float>(length), vector.Length());
158 }
159 }
160
161 TEST(Vector3dTest, DotProduct) {
162 const struct {
163 float expected;
164 gfx::Vector3dF input1;
165 gfx::Vector3dF input2;
166 } tests[] = {
167 { 0, gfx::Vector3dF(1, 0, 0), gfx::Vector3dF(0, 1, 1) },
168 { 0, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(1, 0, 1) },
169 { 0, gfx::Vector3dF(0, 0, 1), gfx::Vector3dF(1, 1, 0) },
170
171 { 3, gfx::Vector3dF(1, 1, 1), gfx::Vector3dF(1, 1, 1) },
172
173 { 1.2f, gfx::Vector3dF(1.2f, -1.2f, 1.2f), gfx::Vector3dF(1, 1, 1) },
174 { 1.2f, gfx::Vector3dF(1, 1, 1), gfx::Vector3dF(1.2f, -1.2f, 1.2f) },
175
176 { 38.72f,
177 gfx::Vector3dF(1.1f, 2.2f, 3.3f), gfx::Vector3dF(4.4f, 5.5f, 6.6f) }
178 };
179
180 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
181 float actual = gfx::DotProduct(tests[i].input1, tests[i].input2);
182 EXPECT_EQ(tests[i].expected, actual);
183 }
184 }
185
186 TEST(Vector3dTest, CrossProduct) {
187 const struct {
188 gfx::Vector3dF expected;
189 gfx::Vector3dF input1;
190 gfx::Vector3dF input2;
191 } tests[] = {
192 { Vector3dF(), Vector3dF(), Vector3dF(1, 1, 1) },
193 { Vector3dF(), Vector3dF(1, 1, 1), Vector3dF() },
194 { Vector3dF(), Vector3dF(1, 1, 1), Vector3dF(1, 1, 1) },
195 { Vector3dF(),
196 Vector3dF(1.6f, 10.6f, -10.6f),
197 Vector3dF(1.6f, 10.6f, -10.6f) },
198
199 { Vector3dF(1, -1, 0), Vector3dF(1, 1, 1), Vector3dF(0, 0, 1) },
200 { Vector3dF(-1, 0, 1), Vector3dF(1, 1, 1), Vector3dF(0, 1, 0) },
201 { Vector3dF(0, 1, -1), Vector3dF(1, 1, 1), Vector3dF(1, 0, 0) },
202
203 { Vector3dF(-1, 1, 0), Vector3dF(0, 0, 1), Vector3dF(1, 1, 1) },
204 { Vector3dF(1, 0, -1), Vector3dF(0, 1, 0), Vector3dF(1, 1, 1) },
205 { Vector3dF(0, -1, 1), Vector3dF(1, 0, 0), Vector3dF(1, 1, 1) }
206 };
207
208 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
209 Vector3dF actual = gfx::CrossProduct(tests[i].input1, tests[i].input2);
210 EXPECT_EQ(tests[i].expected.ToString(), actual.ToString());
211 }
212
213 }
214
215 } // namespace gfx
OLDNEW
« ui/gfx/vector3d_f.cc ('K') | « ui/gfx/vector3d_f.cc ('k') | ui/ui.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698