xref: /aosp_15_r20/external/libchrome/ui/gfx/geometry/vector2d_unittest.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
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 <stddef.h>
6 
7 #include <cmath>
8 #include <limits>
9 
10 #include "base/macros.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/gfx/geometry/vector2d.h"
13 #include "ui/gfx/geometry/vector2d_f.h"
14 
15 namespace gfx {
16 
TEST(Vector2dTest,ConversionToFloat)17 TEST(Vector2dTest, ConversionToFloat) {
18   Vector2d i(3, 4);
19   Vector2dF f = i;
20   EXPECT_EQ(i, f);
21 }
22 
TEST(Vector2dTest,IsZero)23 TEST(Vector2dTest, IsZero) {
24   Vector2d int_zero(0, 0);
25   Vector2d int_nonzero(2, -2);
26   Vector2dF float_zero(0, 0);
27   Vector2dF float_nonzero(0.1f, -0.1f);
28 
29   EXPECT_TRUE(int_zero.IsZero());
30   EXPECT_FALSE(int_nonzero.IsZero());
31   EXPECT_TRUE(float_zero.IsZero());
32   EXPECT_FALSE(float_nonzero.IsZero());
33 }
34 
TEST(Vector2dTest,Add)35 TEST(Vector2dTest, Add) {
36   Vector2d i1(3, 5);
37   Vector2d i2(4, -1);
38 
39   const struct {
40     Vector2d expected;
41     Vector2d actual;
42   } int_tests[] = {
43     { Vector2d(3, 5), i1 + Vector2d() },
44     { Vector2d(3 + 4, 5 - 1), i1 + i2 },
45     { Vector2d(3 - 4, 5 + 1), i1 - i2 }
46   };
47 
48   for (size_t i = 0; i < arraysize(int_tests); ++i)
49     EXPECT_EQ(int_tests[i].expected.ToString(),
50               int_tests[i].actual.ToString());
51 
52   Vector2dF f1(3.1f, 5.1f);
53   Vector2dF f2(4.3f, -1.3f);
54 
55   const struct {
56     Vector2dF expected;
57     Vector2dF actual;
58   } float_tests[] = {
59     { Vector2dF(3.1F, 5.1F), f1 + Vector2d() },
60     { Vector2dF(3.1F, 5.1F), f1 + Vector2dF() },
61     { Vector2dF(3.1f + 4.3f, 5.1f - 1.3f), f1 + f2 },
62     { Vector2dF(3.1f - 4.3f, 5.1f + 1.3f), f1 - f2 }
63   };
64 
65   for (size_t i = 0; i < arraysize(float_tests); ++i)
66     EXPECT_EQ(float_tests[i].expected.ToString(),
67               float_tests[i].actual.ToString());
68 }
69 
TEST(Vector2dTest,Negative)70 TEST(Vector2dTest, Negative) {
71   const struct {
72     Vector2d expected;
73     Vector2d actual;
74   } int_tests[] = {
75     { Vector2d(0, 0), -Vector2d(0, 0) },
76     { Vector2d(-3, -3), -Vector2d(3, 3) },
77     { Vector2d(3, 3), -Vector2d(-3, -3) },
78     { Vector2d(-3, 3), -Vector2d(3, -3) },
79     { Vector2d(3, -3), -Vector2d(-3, 3) }
80   };
81 
82   for (size_t i = 0; i < arraysize(int_tests); ++i)
83     EXPECT_EQ(int_tests[i].expected.ToString(),
84               int_tests[i].actual.ToString());
85 
86   const struct {
87     Vector2dF expected;
88     Vector2dF actual;
89   } float_tests[] = {
90     { Vector2dF(0, 0), -Vector2d(0, 0) },
91     { Vector2dF(-0.3f, -0.3f), -Vector2dF(0.3f, 0.3f) },
92     { Vector2dF(0.3f, 0.3f), -Vector2dF(-0.3f, -0.3f) },
93     { Vector2dF(-0.3f, 0.3f), -Vector2dF(0.3f, -0.3f) },
94     { Vector2dF(0.3f, -0.3f), -Vector2dF(-0.3f, 0.3f) }
95   };
96 
97   for (size_t i = 0; i < arraysize(float_tests); ++i)
98     EXPECT_EQ(float_tests[i].expected.ToString(),
99               float_tests[i].actual.ToString());
100 }
101 
TEST(Vector2dTest,Scale)102 TEST(Vector2dTest, Scale) {
103   float double_values[][4] = {
104     { 4.5f, 1.2f, 3.3f, 5.6f },
105     { 4.5f, -1.2f, 3.3f, 5.6f },
106     { 4.5f, 1.2f, 3.3f, -5.6f },
107     { 4.5f, 1.2f, -3.3f, -5.6f },
108     { -4.5f, 1.2f, 3.3f, 5.6f },
109     { -4.5f, 1.2f, 0, 5.6f },
110     { -4.5f, 1.2f, 3.3f, 0 },
111     { 4.5f, 0, 3.3f, 5.6f },
112     { 0, 1.2f, 3.3f, 5.6f }
113   };
114 
115   for (size_t i = 0; i < arraysize(double_values); ++i) {
116     Vector2dF v(double_values[i][0], double_values[i][1]);
117     v.Scale(double_values[i][2], double_values[i][3]);
118     EXPECT_EQ(v.x(), double_values[i][0] * double_values[i][2]);
119     EXPECT_EQ(v.y(), double_values[i][1] * double_values[i][3]);
120 
121     Vector2dF v2 = ScaleVector2d(
122         gfx::Vector2dF(double_values[i][0], double_values[i][1]),
123         double_values[i][2], double_values[i][3]);
124     EXPECT_EQ(double_values[i][0] * double_values[i][2], v2.x());
125     EXPECT_EQ(double_values[i][1] * double_values[i][3], v2.y());
126   }
127 
128   float single_values[][3] = {
129     { 4.5f, 1.2f, 3.3f },
130     { 4.5f, -1.2f, 3.3f },
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, 0 },
135     { -4.5f, 1.2f, 3.3f },
136     { 4.5f, 0, 3.3f },
137     { 0, 1.2f, 3.3f }
138   };
139 
140   for (size_t i = 0; i < arraysize(single_values); ++i) {
141     Vector2dF v(single_values[i][0], single_values[i][1]);
142     v.Scale(single_values[i][2]);
143     EXPECT_EQ(v.x(), single_values[i][0] * single_values[i][2]);
144     EXPECT_EQ(v.y(), single_values[i][1] * single_values[i][2]);
145 
146     Vector2dF v2 = ScaleVector2d(
147         gfx::Vector2dF(double_values[i][0], double_values[i][1]),
148         double_values[i][2]);
149     EXPECT_EQ(single_values[i][0] * single_values[i][2], v2.x());
150     EXPECT_EQ(single_values[i][1] * single_values[i][2], v2.y());
151   }
152 }
153 
TEST(Vector2dTest,Length)154 TEST(Vector2dTest, Length) {
155   int int_values[][2] = {
156     { 0, 0 },
157     { 10, 20 },
158     { 20, 10 },
159     { -10, -20 },
160     { -20, 10 },
161     { 10, -20 },
162   };
163 
164   for (size_t i = 0; i < arraysize(int_values); ++i) {
165     int v0 = int_values[i][0];
166     int v1 = int_values[i][1];
167     double length_squared =
168         static_cast<double>(v0) * v0 + static_cast<double>(v1) * v1;
169     double length = std::sqrt(length_squared);
170     Vector2d vector(v0, v1);
171     EXPECT_EQ(static_cast<float>(length_squared), vector.LengthSquared());
172     EXPECT_EQ(static_cast<float>(length), vector.Length());
173   }
174 
175   float float_values[][2] = {
176     { 0, 0 },
177     { 10.5f, 20.5f },
178     { 20.5f, 10.5f },
179     { -10.5f, -20.5f },
180     { -20.5f, 10.5f },
181     { 10.5f, -20.5f },
182     // A large vector that fails if the Length function doesn't use
183     // double precision internally.
184     { 1236278317862780234892374893213178027.12122348904204230f,
185       335890352589839028212313231225425134332.38123f },
186   };
187 
188   for (size_t i = 0; i < arraysize(float_values); ++i) {
189     double v0 = float_values[i][0];
190     double v1 = float_values[i][1];
191     double length_squared =
192         static_cast<double>(v0) * v0 + static_cast<double>(v1) * v1;
193     double length = std::sqrt(length_squared);
194     Vector2dF vector(v0, v1);
195     EXPECT_DOUBLE_EQ(length_squared, vector.LengthSquared());
196     EXPECT_FLOAT_EQ(static_cast<float>(length), vector.Length());
197   }
198 }
199 
TEST(Vector2dTest,ClampVector2d)200 TEST(Vector2dTest, ClampVector2d) {
201   Vector2d a;
202 
203   a = Vector2d(3, 5);
204   EXPECT_EQ(Vector2d(3, 5).ToString(), a.ToString());
205   a.SetToMax(Vector2d(2, 4));
206   EXPECT_EQ(Vector2d(3, 5).ToString(), a.ToString());
207   a.SetToMax(Vector2d(3, 5));
208   EXPECT_EQ(Vector2d(3, 5).ToString(), a.ToString());
209   a.SetToMax(Vector2d(4, 2));
210   EXPECT_EQ(Vector2d(4, 5).ToString(), a.ToString());
211   a.SetToMax(Vector2d(8, 10));
212   EXPECT_EQ(Vector2d(8, 10).ToString(), a.ToString());
213 
214   a.SetToMin(Vector2d(9, 11));
215   EXPECT_EQ(Vector2d(8, 10).ToString(), a.ToString());
216   a.SetToMin(Vector2d(8, 10));
217   EXPECT_EQ(Vector2d(8, 10).ToString(), a.ToString());
218   a.SetToMin(Vector2d(11, 9));
219   EXPECT_EQ(Vector2d(8, 9).ToString(), a.ToString());
220   a.SetToMin(Vector2d(7, 11));
221   EXPECT_EQ(Vector2d(7, 9).ToString(), a.ToString());
222   a.SetToMin(Vector2d(3, 5));
223   EXPECT_EQ(Vector2d(3, 5).ToString(), a.ToString());
224 }
225 
TEST(Vector2dTest,ClampVector2dF)226 TEST(Vector2dTest, ClampVector2dF) {
227   Vector2dF a;
228 
229   a = Vector2dF(3.5f, 5.5f);
230   EXPECT_EQ(Vector2dF(3.5f, 5.5f).ToString(), a.ToString());
231   a.SetToMax(Vector2dF(2.5f, 4.5f));
232   EXPECT_EQ(Vector2dF(3.5f, 5.5f).ToString(), a.ToString());
233   a.SetToMax(Vector2dF(3.5f, 5.5f));
234   EXPECT_EQ(Vector2dF(3.5f, 5.5f).ToString(), a.ToString());
235   a.SetToMax(Vector2dF(4.5f, 2.5f));
236   EXPECT_EQ(Vector2dF(4.5f, 5.5f).ToString(), a.ToString());
237   a.SetToMax(Vector2dF(8.5f, 10.5f));
238   EXPECT_EQ(Vector2dF(8.5f, 10.5f).ToString(), a.ToString());
239 
240   a.SetToMin(Vector2dF(9.5f, 11.5f));
241   EXPECT_EQ(Vector2dF(8.5f, 10.5f).ToString(), a.ToString());
242   a.SetToMin(Vector2dF(8.5f, 10.5f));
243   EXPECT_EQ(Vector2dF(8.5f, 10.5f).ToString(), a.ToString());
244   a.SetToMin(Vector2dF(11.5f, 9.5f));
245   EXPECT_EQ(Vector2dF(8.5f, 9.5f).ToString(), a.ToString());
246   a.SetToMin(Vector2dF(7.5f, 11.5f));
247   EXPECT_EQ(Vector2dF(7.5f, 9.5f).ToString(), a.ToString());
248   a.SetToMin(Vector2dF(3.5f, 5.5f));
249   EXPECT_EQ(Vector2dF(3.5f, 5.5f).ToString(), a.ToString());
250 }
251 
TEST(Vector2dTest,IntegerOverflow)252 TEST(Vector2dTest, IntegerOverflow) {
253   int int_max = std::numeric_limits<int>::max();
254   int int_min = std::numeric_limits<int>::min();
255 
256   Vector2d max_vector(int_max, int_max);
257   Vector2d min_vector(int_min, int_min);
258   Vector2d test;
259 
260   test = Vector2d();
261   test += Vector2d(int_max, int_max);
262   EXPECT_EQ(test, max_vector);
263 
264   test = Vector2d();
265   test += Vector2d(int_min, int_min);
266   EXPECT_EQ(test, min_vector);
267 
268   test = Vector2d(10, 20);
269   test += Vector2d(int_max, int_max);
270   EXPECT_EQ(test, max_vector);
271 
272   test = Vector2d(-10, -20);
273   test += Vector2d(int_min, int_min);
274   EXPECT_EQ(test, min_vector);
275 
276   test = Vector2d();
277   test -= Vector2d(int_max, int_max);
278   EXPECT_EQ(test, Vector2d(-int_max, -int_max));
279 
280   test = Vector2d();
281   test -= Vector2d(int_min, int_min);
282   EXPECT_EQ(test, max_vector);
283 
284   test = Vector2d(10, 20);
285   test -= Vector2d(int_min, int_min);
286   EXPECT_EQ(test, max_vector);
287 
288   test = Vector2d(-10, -20);
289   test -= Vector2d(int_max, int_max);
290   EXPECT_EQ(test, min_vector);
291 }
292 
293 }  // namespace gfx
294