Flutter Engine
The Flutter Engine
vector2d_unittest.cc
Go to the documentation of this file.
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 <cmath>
6#include <cstddef>
7#include <limits>
8
9#include "gtest/gtest.h"
10#include "vector2d.h"
11#include "vector2d_f.h"
12
13namespace gfx {
14
15template <typename T, size_t N>
16constexpr size_t size(const T (&array)[N]) noexcept {
17 return N;
18}
19
20TEST(Vector2dTest, ConversionToFloat) {
21 Vector2d i(3, 4);
22 Vector2dF f = i;
23 EXPECT_EQ(i, f);
24}
25
26TEST(Vector2dTest, IsZero) {
27 Vector2d int_zero(0, 0);
28 Vector2d int_nonzero(2, -2);
29 Vector2dF float_zero(0, 0);
30 Vector2dF float_nonzero(0.1f, -0.1f);
31
32 EXPECT_TRUE(int_zero.IsZero());
33 EXPECT_FALSE(int_nonzero.IsZero());
34 EXPECT_TRUE(float_zero.IsZero());
35 EXPECT_FALSE(float_nonzero.IsZero());
36}
37
38TEST(Vector2dTest, Add) {
39 Vector2d i1(3, 5);
40 Vector2d i2(4, -1);
41
42 const struct {
43 Vector2d expected;
44 Vector2d actual;
45 } int_tests[] = {{Vector2d(3, 5), i1 + Vector2d()},
46 {Vector2d(3 + 4, 5 - 1), i1 + i2},
47 {Vector2d(3 - 4, 5 + 1), i1 - i2}};
48
49 for (size_t i = 0; i < size(int_tests); ++i)
50 EXPECT_EQ(int_tests[i].expected.ToString(), 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[] = {{Vector2dF(3.1F, 5.1F), f1 + Vector2d()},
59 {Vector2dF(3.1F, 5.1F), f1 + Vector2dF()},
60 {Vector2dF(3.1f + 4.3f, 5.1f - 1.3f), f1 + f2},
61 {Vector2dF(3.1f - 4.3f, 5.1f + 1.3f), f1 - f2}};
62
63 for (size_t i = 0; i < size(float_tests); ++i)
64 EXPECT_EQ(float_tests[i].expected.ToString(),
65 float_tests[i].actual.ToString());
66}
67
68TEST(Vector2dTest, Negative) {
69 const struct {
70 Vector2d expected;
71 Vector2d actual;
72 } int_tests[] = {{Vector2d(0, 0), -Vector2d(0, 0)},
73 {Vector2d(-3, -3), -Vector2d(3, 3)},
74 {Vector2d(3, 3), -Vector2d(-3, -3)},
75 {Vector2d(-3, 3), -Vector2d(3, -3)},
76 {Vector2d(3, -3), -Vector2d(-3, 3)}};
77
78 for (size_t i = 0; i < size(int_tests); ++i)
79 EXPECT_EQ(int_tests[i].expected.ToString(), int_tests[i].actual.ToString());
80
81 const struct {
82 Vector2dF expected;
83 Vector2dF actual;
84 } float_tests[] = {{Vector2dF(0, 0), -Vector2d(0, 0)},
85 {Vector2dF(-0.3f, -0.3f), -Vector2dF(0.3f, 0.3f)},
86 {Vector2dF(0.3f, 0.3f), -Vector2dF(-0.3f, -0.3f)},
87 {Vector2dF(-0.3f, 0.3f), -Vector2dF(0.3f, -0.3f)},
88 {Vector2dF(0.3f, -0.3f), -Vector2dF(-0.3f, 0.3f)}};
89
90 for (size_t i = 0; i < size(float_tests); ++i)
91 EXPECT_EQ(float_tests[i].expected.ToString(),
92 float_tests[i].actual.ToString());
93}
94
95TEST(Vector2dTest, Scale) {
96 float double_values[][4] = {
97 {4.5f, 1.2f, 3.3f, 5.6f}, {4.5f, -1.2f, 3.3f, 5.6f},
98 {4.5f, 1.2f, 3.3f, -5.6f}, {4.5f, 1.2f, -3.3f, -5.6f},
99 {-4.5f, 1.2f, 3.3f, 5.6f}, {-4.5f, 1.2f, 0, 5.6f},
100 {-4.5f, 1.2f, 3.3f, 0}, {4.5f, 0, 3.3f, 5.6f},
101 {0, 1.2f, 3.3f, 5.6f}};
102
103 for (size_t i = 0; i < size(double_values); ++i) {
104 Vector2dF v(double_values[i][0], double_values[i][1]);
105 v.Scale(double_values[i][2], double_values[i][3]);
106 EXPECT_EQ(v.x(), double_values[i][0] * double_values[i][2]);
107 EXPECT_EQ(v.y(), double_values[i][1] * double_values[i][3]);
108
109 Vector2dF v2 =
110 ScaleVector2d(gfx::Vector2dF(double_values[i][0], double_values[i][1]),
111 double_values[i][2], double_values[i][3]);
112 EXPECT_EQ(double_values[i][0] * double_values[i][2], v2.x());
113 EXPECT_EQ(double_values[i][1] * double_values[i][3], v2.y());
114 }
115
116 float single_values[][3] = {
117 {4.5f, 1.2f, 3.3f}, {4.5f, -1.2f, 3.3f}, {4.5f, 1.2f, 3.3f},
118 {4.5f, 1.2f, -3.3f}, {-4.5f, 1.2f, 3.3f}, {-4.5f, 1.2f, 0},
119 {-4.5f, 1.2f, 3.3f}, {4.5f, 0, 3.3f}, {0, 1.2f, 3.3f}};
120
121 for (size_t i = 0; i < size(single_values); ++i) {
122 Vector2dF v(single_values[i][0], single_values[i][1]);
123 v.Scale(single_values[i][2]);
124 EXPECT_EQ(v.x(), single_values[i][0] * single_values[i][2]);
125 EXPECT_EQ(v.y(), single_values[i][1] * single_values[i][2]);
126
127 Vector2dF v2 =
128 ScaleVector2d(gfx::Vector2dF(double_values[i][0], double_values[i][1]),
129 double_values[i][2]);
130 EXPECT_EQ(single_values[i][0] * single_values[i][2], v2.x());
131 EXPECT_EQ(single_values[i][1] * single_values[i][2], v2.y());
132 }
133}
134
135TEST(Vector2dTest, Length) {
136 int int_values[][2] = {
137 {0, 0}, {10, 20}, {20, 10}, {-10, -20}, {-20, 10}, {10, -20},
138 };
139
140 for (size_t i = 0; i < size(int_values); ++i) {
141 int v0 = int_values[i][0];
142 int v1 = int_values[i][1];
143 double length_squared =
144 static_cast<double>(v0) * v0 + static_cast<double>(v1) * v1;
145 double length = std::sqrt(length_squared);
146 Vector2d vector(v0, v1);
147 EXPECT_EQ(static_cast<float>(length_squared), vector.LengthSquared());
148 EXPECT_EQ(static_cast<float>(length), vector.Length());
149 }
150
151 float float_values[][2] = {
152 {0, 0},
153 {10.5f, 20.5f},
154 {20.5f, 10.5f},
155 {-10.5f, -20.5f},
156 {-20.5f, 10.5f},
157 {10.5f, -20.5f},
158 // A large vector that fails if the Length function doesn't use
159 // double precision internally.
160 {1236278317862780234892374893213178027.12122348904204230f,
161 335890352589839028212313231225425134332.38123f},
162 };
163
164 for (size_t i = 0; i < size(float_values); ++i) {
165 double v0 = float_values[i][0];
166 double v1 = float_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 Vector2dF vector(v0, v1);
171 EXPECT_DOUBLE_EQ(length_squared, vector.LengthSquared());
172 EXPECT_FLOAT_EQ(static_cast<float>(length), vector.Length());
173 }
174}
175
176TEST(Vector2dTest, ClampVector2d) {
177 Vector2d a;
178
179 a = Vector2d(3, 5);
180 EXPECT_EQ(Vector2d(3, 5).ToString(), a.ToString());
181 a.SetToMax(Vector2d(2, 4));
182 EXPECT_EQ(Vector2d(3, 5).ToString(), a.ToString());
183 a.SetToMax(Vector2d(3, 5));
184 EXPECT_EQ(Vector2d(3, 5).ToString(), a.ToString());
185 a.SetToMax(Vector2d(4, 2));
186 EXPECT_EQ(Vector2d(4, 5).ToString(), a.ToString());
187 a.SetToMax(Vector2d(8, 10));
188 EXPECT_EQ(Vector2d(8, 10).ToString(), a.ToString());
189
190 a.SetToMin(Vector2d(9, 11));
191 EXPECT_EQ(Vector2d(8, 10).ToString(), a.ToString());
192 a.SetToMin(Vector2d(8, 10));
193 EXPECT_EQ(Vector2d(8, 10).ToString(), a.ToString());
194 a.SetToMin(Vector2d(11, 9));
195 EXPECT_EQ(Vector2d(8, 9).ToString(), a.ToString());
196 a.SetToMin(Vector2d(7, 11));
197 EXPECT_EQ(Vector2d(7, 9).ToString(), a.ToString());
198 a.SetToMin(Vector2d(3, 5));
199 EXPECT_EQ(Vector2d(3, 5).ToString(), a.ToString());
200}
201
202TEST(Vector2dTest, ClampVector2dF) {
203 Vector2dF a;
204
205 a = Vector2dF(3.5f, 5.5f);
206 EXPECT_EQ(Vector2dF(3.5f, 5.5f).ToString(), a.ToString());
207 a.SetToMax(Vector2dF(2.5f, 4.5f));
208 EXPECT_EQ(Vector2dF(3.5f, 5.5f).ToString(), a.ToString());
209 a.SetToMax(Vector2dF(3.5f, 5.5f));
210 EXPECT_EQ(Vector2dF(3.5f, 5.5f).ToString(), a.ToString());
211 a.SetToMax(Vector2dF(4.5f, 2.5f));
212 EXPECT_EQ(Vector2dF(4.5f, 5.5f).ToString(), a.ToString());
213 a.SetToMax(Vector2dF(8.5f, 10.5f));
214 EXPECT_EQ(Vector2dF(8.5f, 10.5f).ToString(), a.ToString());
215
216 a.SetToMin(Vector2dF(9.5f, 11.5f));
217 EXPECT_EQ(Vector2dF(8.5f, 10.5f).ToString(), a.ToString());
218 a.SetToMin(Vector2dF(8.5f, 10.5f));
219 EXPECT_EQ(Vector2dF(8.5f, 10.5f).ToString(), a.ToString());
220 a.SetToMin(Vector2dF(11.5f, 9.5f));
221 EXPECT_EQ(Vector2dF(8.5f, 9.5f).ToString(), a.ToString());
222 a.SetToMin(Vector2dF(7.5f, 11.5f));
223 EXPECT_EQ(Vector2dF(7.5f, 9.5f).ToString(), a.ToString());
224 a.SetToMin(Vector2dF(3.5f, 5.5f));
225 EXPECT_EQ(Vector2dF(3.5f, 5.5f).ToString(), a.ToString());
226}
227
228TEST(Vector2dTest, IntegerOverflow) {
229 int int_max = std::numeric_limits<int>::max();
230 int int_min = std::numeric_limits<int>::min();
231
232 Vector2d max_vector(int_max, int_max);
233 Vector2d min_vector(int_min, int_min);
235
236 test = Vector2d();
237 test += Vector2d(int_max, int_max);
238 EXPECT_EQ(test, max_vector);
239
240 test = Vector2d();
241 test += Vector2d(int_min, int_min);
242 EXPECT_EQ(test, min_vector);
243
244 test = Vector2d(10, 20);
245 test += Vector2d(int_max, int_max);
246 EXPECT_EQ(test, max_vector);
247
248 test = Vector2d(-10, -20);
249 test += Vector2d(int_min, int_min);
250 EXPECT_EQ(test, min_vector);
251
252 test = Vector2d();
253 test -= Vector2d(int_max, int_max);
254 EXPECT_EQ(test, Vector2d(-int_max, -int_max));
255
256 test = Vector2d();
257 test -= Vector2d(int_min, int_min);
258 EXPECT_EQ(test, max_vector);
259
260 test = Vector2d(10, 20);
261 test -= Vector2d(int_min, int_min);
262 EXPECT_EQ(test, max_vector);
263
264 test = Vector2d(-10, -20);
265 test -= Vector2d(int_max, int_max);
266 EXPECT_EQ(test, min_vector);
267}
268
269} // namespace gfx
#define test(name)
Vec2Value v2
#define N
Definition: beziers.cpp:19
constexpr float y() const
Definition: vector2d_f.h:29
bool IsZero() const
Definition: vector2d_f.cc:17
void Scale(float scale)
Definition: vector2d_f.h:59
float Length() const
Definition: vector2d_f.cc:35
constexpr float x() const
Definition: vector2d_f.h:26
double LengthSquared() const
Definition: vector2d_f.cc:31
bool IsZero() const
Definition: vector2d.cc:14
float Length() const
Definition: vector2d.cc:32
int64_t LengthSquared() const
Definition: vector2d.cc:28
struct MyStruct a[10]
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
Definition: dart.idl:629
size_t length
bool IsZero(char *begin, char *end)
Definition: insets.cc:10
Vector2dF ScaleVector2d(const Vector2dF &v, float x_scale, float y_scale)
Definition: vector2d_f.cc:54
constexpr size_t size(const T(&array)[N]) noexcept
TEST(PointTest, ToPointF)
static std::string ToString(CompilerBackend::Type type)
Definition: reflector.cc:559
SIN Vec< N, float > sqrt(const Vec< N, float > &x)
Definition: SkVx.h:706
#define T
Definition: precompiler.cc:65
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678