Flutter Engine
The Flutter Engine
point_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 <cstddef>
6
7#include "gtest/gtest.h"
8#include "point.h"
9#include "point_conversions.h"
10#include "point_f.h"
11
12namespace gfx {
13
14TEST(PointTest, ToPointF) {
15 // Check that explicit conversion from integer to float compiles.
16 Point a(10, 20);
17 PointF b = PointF(a);
18
19 EXPECT_EQ(static_cast<float>(a.x()), b.x());
20 EXPECT_EQ(static_cast<float>(a.y()), b.y());
21}
22
23TEST(PointTest, IsOrigin) {
24 EXPECT_FALSE(Point(1, 0).IsOrigin());
25 EXPECT_FALSE(Point(0, 1).IsOrigin());
26 EXPECT_FALSE(Point(1, 2).IsOrigin());
27 EXPECT_FALSE(Point(-1, 0).IsOrigin());
28 EXPECT_FALSE(Point(0, -1).IsOrigin());
29 EXPECT_FALSE(Point(-1, -2).IsOrigin());
30 EXPECT_TRUE(Point(0, 0).IsOrigin());
31
32 EXPECT_FALSE(PointF(0.1f, 0).IsOrigin());
33 EXPECT_FALSE(PointF(0, 0.1f).IsOrigin());
34 EXPECT_FALSE(PointF(0.1f, 2).IsOrigin());
35 EXPECT_FALSE(PointF(-0.1f, 0).IsOrigin());
36 EXPECT_FALSE(PointF(0, -0.1f).IsOrigin());
37 EXPECT_FALSE(PointF(-0.1f, -2).IsOrigin());
38 EXPECT_TRUE(PointF(0, 0).IsOrigin());
39}
40
41TEST(PointTest, VectorArithmetic) {
42 Point a(1, 5);
43 Vector2d v1(3, -3);
44 Vector2d v2(-8, 1);
45
46 static const struct {
47 Point expected;
48 Point actual;
49 } tests[] = {{Point(4, 2), a + v1},
50 {Point(-2, 8), a - v1},
51 {a, a - v1 + v1},
52 {a, a + v1 - v1},
53 {a, a + Vector2d()},
54 {Point(12, 1), a + v1 - v2},
55 {Point(-10, 9), a - v1 + v2}};
56
57 for (size_t i = 0; i < 7; ++i)
58 EXPECT_EQ(tests[i].expected.ToString(), tests[i].actual.ToString());
59}
60
61TEST(PointTest, OffsetFromPoint) {
62 Point a(1, 5);
63 Point b(-20, 8);
64 EXPECT_EQ(Vector2d(-20 - 1, 8 - 5).ToString(), (b - a).ToString());
65}
66
67TEST(PointTest, ToRoundedPoint) {
68 EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0, 0)));
69 EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0.0001f, 0.0001f)));
70 EXPECT_EQ(Point(0, 0), ToRoundedPoint(PointF(0.4999f, 0.4999f)));
71 EXPECT_EQ(Point(1, 1), ToRoundedPoint(PointF(0.5f, 0.5f)));
72 EXPECT_EQ(Point(1, 1), ToRoundedPoint(PointF(0.9999f, 0.9999f)));
73
74 EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10, 10)));
75 EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10.0001f, 10.0001f)));
76 EXPECT_EQ(Point(10, 10), ToRoundedPoint(PointF(10.4999f, 10.4999f)));
77 EXPECT_EQ(Point(11, 11), ToRoundedPoint(PointF(10.5f, 10.5f)));
78 EXPECT_EQ(Point(11, 11), ToRoundedPoint(PointF(10.9999f, 10.9999f)));
79
80 EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10, -10)));
81 EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10.0001f, -10.0001f)));
82 EXPECT_EQ(Point(-10, -10), ToRoundedPoint(PointF(-10.4999f, -10.4999f)));
83 EXPECT_EQ(Point(-11, -11), ToRoundedPoint(PointF(-10.5f, -10.5f)));
84 EXPECT_EQ(Point(-11, -11), ToRoundedPoint(PointF(-10.9999f, -10.9999f)));
85}
86
87TEST(PointTest, Scale) {
88 EXPECT_EQ(PointF().ToString(), ScalePoint(PointF(), 2).ToString());
89 EXPECT_EQ(PointF().ToString(), ScalePoint(PointF(), 2, 2).ToString());
90
91 EXPECT_EQ(PointF(2, -2).ToString(), ScalePoint(PointF(1, -1), 2).ToString());
92 EXPECT_EQ(PointF(2, -2).ToString(),
93 ScalePoint(PointF(1, -1), 2, 2).ToString());
94
95 PointF zero;
96 PointF one(1, -1);
97
98 zero.Scale(2);
99 zero.Scale(3, 1.5);
100
101 one.Scale(2);
102 one.Scale(3, 1.5);
103
104 EXPECT_EQ(PointF().ToString(), zero.ToString());
105 EXPECT_EQ(PointF(6, -3).ToString(), one.ToString());
106}
107
108TEST(PointTest, ClampPoint) {
109 Point a;
110
111 a = Point(3, 5);
112 EXPECT_EQ(Point(3, 5).ToString(), a.ToString());
113 a.SetToMax(Point(2, 4));
114 EXPECT_EQ(Point(3, 5).ToString(), a.ToString());
115 a.SetToMax(Point(3, 5));
116 EXPECT_EQ(Point(3, 5).ToString(), a.ToString());
117 a.SetToMax(Point(4, 2));
118 EXPECT_EQ(Point(4, 5).ToString(), a.ToString());
119 a.SetToMax(Point(8, 10));
120 EXPECT_EQ(Point(8, 10).ToString(), a.ToString());
121
122 a.SetToMin(Point(9, 11));
123 EXPECT_EQ(Point(8, 10).ToString(), a.ToString());
124 a.SetToMin(Point(8, 10));
125 EXPECT_EQ(Point(8, 10).ToString(), a.ToString());
126 a.SetToMin(Point(11, 9));
127 EXPECT_EQ(Point(8, 9).ToString(), a.ToString());
128 a.SetToMin(Point(7, 11));
129 EXPECT_EQ(Point(7, 9).ToString(), a.ToString());
130 a.SetToMin(Point(3, 5));
131 EXPECT_EQ(Point(3, 5).ToString(), a.ToString());
132}
133
134TEST(PointTest, ClampPointF) {
135 PointF a;
136
137 a = PointF(3.5f, 5.5f);
138 EXPECT_EQ(PointF(3.5f, 5.5f).ToString(), a.ToString());
139 a.SetToMax(PointF(2.5f, 4.5f));
140 EXPECT_EQ(PointF(3.5f, 5.5f).ToString(), a.ToString());
141 a.SetToMax(PointF(3.5f, 5.5f));
142 EXPECT_EQ(PointF(3.5f, 5.5f).ToString(), a.ToString());
143 a.SetToMax(PointF(4.5f, 2.5f));
144 EXPECT_EQ(PointF(4.5f, 5.5f).ToString(), a.ToString());
145 a.SetToMax(PointF(8.5f, 10.5f));
146 EXPECT_EQ(PointF(8.5f, 10.5f).ToString(), a.ToString());
147
148 a.SetToMin(PointF(9.5f, 11.5f));
149 EXPECT_EQ(PointF(8.5f, 10.5f).ToString(), a.ToString());
150 a.SetToMin(PointF(8.5f, 10.5f));
151 EXPECT_EQ(PointF(8.5f, 10.5f).ToString(), a.ToString());
152 a.SetToMin(PointF(11.5f, 9.5f));
153 EXPECT_EQ(PointF(8.5f, 9.5f).ToString(), a.ToString());
154 a.SetToMin(PointF(7.5f, 11.5f));
155 EXPECT_EQ(PointF(7.5f, 9.5f).ToString(), a.ToString());
156 a.SetToMin(PointF(3.5f, 5.5f));
157 EXPECT_EQ(PointF(3.5f, 5.5f).ToString(), a.ToString());
158}
159
160TEST(PointTest, Offset) {
161 Point test(3, 4);
162 test.Offset(5, -8);
163 EXPECT_EQ(test, Point(8, -4));
164}
165
166TEST(PointTest, VectorMath) {
167 Point test = Point(3, 4);
168 test += Vector2d(5, -8);
169 EXPECT_EQ(test, Point(8, -4));
170
171 Point test2 = Point(3, 4);
172 test2 -= Vector2d(5, -8);
173 EXPECT_EQ(test2, Point(-2, 12));
174}
175
176TEST(PointTest, IntegerOverflow) {
177 int int_max = std::numeric_limits<int>::max();
178 int int_min = std::numeric_limits<int>::min();
179
180 Point max_point(int_max, int_max);
181 Point min_point(int_min, int_min);
182 Point test;
183
184 test = Point();
185 test.Offset(int_max, int_max);
186 EXPECT_EQ(test, max_point);
187
188 test = Point();
189 test.Offset(int_min, int_min);
190 EXPECT_EQ(test, min_point);
191
192 test = Point(10, 20);
193 test.Offset(int_max, int_max);
194 EXPECT_EQ(test, max_point);
195
196 test = Point(-10, -20);
197 test.Offset(int_min, int_min);
198 EXPECT_EQ(test, min_point);
199
200 test = Point();
201 test += Vector2d(int_max, int_max);
202 EXPECT_EQ(test, max_point);
203
204 test = Point();
205 test += Vector2d(int_min, int_min);
206 EXPECT_EQ(test, min_point);
207
208 test = Point(10, 20);
209 test += Vector2d(int_max, int_max);
210 EXPECT_EQ(test, max_point);
211
212 test = Point(-10, -20);
213 test += Vector2d(int_min, int_min);
214 EXPECT_EQ(test, min_point);
215
216 test = Point();
217 test -= Vector2d(int_max, int_max);
218 EXPECT_EQ(test, Point(-int_max, -int_max));
219
220 test = Point();
221 test -= Vector2d(int_min, int_min);
222 EXPECT_EQ(test, max_point);
223
224 test = Point(10, 20);
225 test -= Vector2d(int_min, int_min);
226 EXPECT_EQ(test, max_point);
227
228 test = Point(-10, -20);
229 test -= Vector2d(int_max, int_max);
230 EXPECT_EQ(test, min_point);
231}
232
233} // namespace gfx
static BlurTest tests[]
Definition: BlurTest.cpp:84
#define test(name)
Vec2Value v2
std::string ToString() const
Definition: point_f.cc:21
void Scale(float scale)
Definition: point_f.h:69
static bool b
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
Definition: insets.cc:10
Point ToRoundedPoint(const PointF &point)
PointF ScalePoint(const PointF &p, float x_scale, float y_scale)
Definition: point_f.cc:25
TEST(PointTest, ToPointF)
static std::string ToString(CompilerBackend::Type type)
Definition: reflector.cc:559
TPoint< Scalar > Point
Definition: point.h:322
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678