OLD | NEW |
---|---|
(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 <limits> | |
11 | |
12 namespace ui { | |
tfarina
2012/10/26 22:04:19
Looks like only the unittests you added are wrappe
danakj
2012/10/26 22:07:58
Oh, thanks. I was assuming all tests were in ui si
| |
13 | |
14 TEST(Vector2dTest, ConversionToFloat) { | |
15 gfx::Vector2d i(3, 4); | |
16 gfx::Vector2dF f = i; | |
17 EXPECT_EQ(i, f); | |
18 } | |
19 | |
20 TEST(Vector2dTest, IsZero) { | |
21 gfx::Vector2d int_zero(0, 0); | |
22 gfx::Vector2d int_nonzero(2, -2); | |
23 gfx::Vector2dF float_zero(0, 0); | |
24 gfx::Vector2dF float_nonzero(0.1f, -0.1f); | |
25 | |
26 EXPECT_TRUE(int_zero.IsZero()); | |
27 EXPECT_FALSE(int_nonzero.IsZero()); | |
28 EXPECT_TRUE(float_zero.IsZero()); | |
29 EXPECT_FALSE(float_nonzero.IsZero()); | |
30 } | |
31 | |
32 TEST(Vector2dTest, Grow) { | |
33 gfx::Vector2d i(3, 5); | |
34 gfx::Vector2dF f(3.1f, 5.1f); | |
35 | |
36 i.Grow(4, -1); | |
37 EXPECT_EQ(gfx::Vector2d(3 + 4, 5 - 1).ToString(), i.ToString()); | |
38 | |
39 f.Grow(4.3f, -1.3f); | |
40 EXPECT_EQ(gfx::Vector2dF(3.1f + 4.3f, 5.1f - 1.3f).ToString(), f.ToString()); | |
41 } | |
42 | |
43 TEST(Vector2dTest, Add) { | |
44 gfx::Vector2d i1(3, 5); | |
45 gfx::Vector2d i2(4, -1); | |
46 | |
47 static const struct { | |
48 gfx::Vector2d expected; | |
49 gfx::Vector2d actual; | |
50 } int_tests[] = { | |
51 { gfx::Vector2d(3, 5), i1 + gfx::Vector2d() }, | |
52 { gfx::Vector2d(3 + 4, 5 - 1), i1 + i2 }, | |
53 { gfx::Vector2d(3 - 4, 5 + 1), i1 - i2 } | |
54 }; | |
55 | |
56 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_tests); ++i) | |
57 EXPECT_EQ(int_tests[i].expected.ToString(), | |
58 int_tests[i].actual.ToString()); | |
59 | |
60 gfx::Vector2dF f1(3.1f, 5.1f); | |
61 gfx::Vector2dF f2(4.3f, -1.3f); | |
62 | |
63 static const struct { | |
64 gfx::Vector2dF expected; | |
65 gfx::Vector2dF actual; | |
66 } float_tests[] = { | |
67 { gfx::Vector2dF(3.1F, 5.1F), f1 + gfx::Vector2d() }, | |
68 { gfx::Vector2dF(3.1F, 5.1F), f1 + gfx::Vector2dF() }, | |
69 { gfx::Vector2dF(3.1f + 4.3f, 5.1f - 1.3f), f1 + f2 }, | |
70 { gfx::Vector2dF(3.1f - 4.3f, 5.1f + 1.3f), f1 - f2 } | |
71 }; | |
72 | |
73 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i) | |
74 EXPECT_EQ(float_tests[i].expected.ToString(), | |
75 float_tests[i].actual.ToString()); | |
76 } | |
77 | |
78 TEST(Vector2dTest, Negative) { | |
79 static const struct { | |
80 gfx::Vector2d expected; | |
81 gfx::Vector2d actual; | |
82 } int_tests[] = { | |
83 { gfx::Vector2d(0, 0), -gfx::Vector2d(0, 0) }, | |
84 { gfx::Vector2d(-3, -3), -gfx::Vector2d(3, 3) }, | |
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 }; | |
89 | |
90 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_tests); ++i) | |
91 EXPECT_EQ(int_tests[i].expected.ToString(), | |
92 int_tests[i].actual.ToString()); | |
93 | |
94 static const struct { | |
95 gfx::Vector2dF expected; | |
96 gfx::Vector2dF actual; | |
97 } float_tests[] = { | |
98 { gfx::Vector2dF(0, 0), -gfx::Vector2d(0, 0) }, | |
99 { gfx::Vector2dF(-0.3f, -0.3f), -gfx::Vector2dF(0.3f, 0.3f) }, | |
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 }; | |
104 | |
105 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_tests); ++i) | |
106 EXPECT_EQ(float_tests[i].expected.ToString(), | |
107 float_tests[i].actual.ToString()); | |
108 } | |
109 | |
110 TEST(Vector2dTest, Scale) { | |
111 float double_values[][4] = { | |
112 { 4.5f, 1.2f, 3.3f, 5.6f }, | |
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, 0, 5.6f }, | |
118 { -4.5f, 1.2f, 3.3f, 0 }, | |
119 { 4.5f, 0, 3.3f, 5.6f }, | |
120 { 0, 1.2f, 3.3f, 5.6f } | |
121 }; | |
122 | |
123 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(double_values); ++i) { | |
124 gfx::Vector2dF v(double_values[i][0], double_values[i][1]); | |
125 v.Scale(double_values[i][2], double_values[i][3]); | |
126 EXPECT_EQ(v.x(), double_values[i][0] * double_values[i][2]); | |
127 EXPECT_EQ(v.y(), double_values[i][1] * double_values[i][3]); | |
128 } | |
129 | |
130 float single_values[][3] = { | |
131 { 4.5f, 1.2f, 3.3f }, | |
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, 0 }, | |
137 { -4.5f, 1.2f, 3.3f }, | |
138 { 4.5f, 0, 3.3f }, | |
139 { 0, 1.2f, 3.3f } | |
140 }; | |
141 | |
142 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(single_values); ++i) { | |
143 gfx::Vector2dF v(single_values[i][0], single_values[i][1]); | |
144 v.Scale(single_values[i][2]); | |
145 EXPECT_EQ(v.x(), single_values[i][0] * single_values[i][2]); | |
146 EXPECT_EQ(v.y(), single_values[i][1] * single_values[i][2]); | |
147 } | |
148 } | |
149 | |
150 TEST(Vector2dTest, Length) { | |
151 int int_values[][2] = { | |
152 { 0, 0 }, | |
153 { 10, 20 }, | |
154 { 20, 10 }, | |
155 { -10, -20 }, | |
156 { -20, 10 }, | |
157 { 10, -20 }, | |
158 }; | |
159 | |
160 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(int_values); ++i) { | |
161 int v0 = int_values[i][0]; | |
162 int v1 = int_values[i][1]; | |
163 double length_squared = v0 * v0 + v1 * v1; | |
164 double length = std::sqrt(length_squared); | |
165 gfx::Vector2d vector(v0, v1); | |
166 EXPECT_EQ(static_cast<float>(length_squared), vector.LengthSquared()); | |
167 EXPECT_EQ(static_cast<float>(length), vector.Length()); | |
168 } | |
169 | |
170 float float_values[][2] = { | |
171 { 0, 0 }, | |
172 { 10.5f, 20.5f }, | |
173 { 20.5f, 10.5f }, | |
174 { -10.5f, -20.5f }, | |
175 { -20.5f, 10.5f }, | |
176 { 10.5f, -20.5f }, | |
177 // A large vector that fails if the Length function doesn't use | |
178 // double precision internally. | |
179 { 1236278317862780234892374893213178027.12122348904204230f, | |
180 335890352589839028212313231225425134332.38123f }, | |
181 }; | |
182 | |
183 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(float_values); ++i) { | |
184 double v0 = float_values[i][0]; | |
185 double v1 = float_values[i][1]; | |
186 double length_squared = v0 * v0 + v1 * v1; | |
187 double length = std::sqrt(length_squared); | |
188 gfx::Vector2dF vector(v0, v1); | |
189 EXPECT_EQ(static_cast<float>(length_squared), vector.LengthSquared()); | |
190 EXPECT_EQ(static_cast<float>(length), vector.Length()); | |
191 } | |
192 } | |
193 | |
194 } // namespace ui | |
OLD | NEW |