Flutter Engine
The Flutter Engine
size_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 "size.h"
6#include "gtest/gtest.h"
7#include "size_conversions.h"
8#include "size_f.h"
9
10namespace gfx {
11
12namespace {
13
14static constexpr float kTrivial = 8.f * std::numeric_limits<float>::epsilon();
15
16int TestSizeF(const SizeF& s) {
17 return s.width();
18}
19
20} // namespace
21
22TEST(SizeTest, ToSizeF) {
23 // Check that explicit conversion from integer to float compiles.
24 Size a(10, 20);
25 float width = TestSizeF(gfx::SizeF(a));
26 EXPECT_EQ(width, a.width());
27
28 SizeF b(10, 20);
29
30 EXPECT_EQ(b, gfx::SizeF(a));
31}
32
33TEST(SizeTest, ToFlooredSize) {
34 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0, 0)));
35 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.0001f, 0.0001f)));
36 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.4999f, 0.4999f)));
37 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.5f, 0.5f)));
38 EXPECT_EQ(Size(0, 0), ToFlooredSize(SizeF(0.9999f, 0.9999f)));
39
40 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10, 10)));
41 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.0001f, 10.0001f)));
42 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.4999f, 10.4999f)));
43 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.5f, 10.5f)));
44 EXPECT_EQ(Size(10, 10), ToFlooredSize(SizeF(10.9999f, 10.9999f)));
45}
46
47TEST(SizeTest, ToCeiledSize) {
48 EXPECT_EQ(Size(0, 0), ToCeiledSize(SizeF(0, 0)));
49 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.0001f, 0.0001f)));
50 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.4999f, 0.4999f)));
51 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.5f, 0.5f)));
52 EXPECT_EQ(Size(1, 1), ToCeiledSize(SizeF(0.9999f, 0.9999f)));
53
54 EXPECT_EQ(Size(10, 10), ToCeiledSize(SizeF(10, 10)));
55 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.0001f, 10.0001f)));
56 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.4999f, 10.4999f)));
57 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.5f, 10.5f)));
58 EXPECT_EQ(Size(11, 11), ToCeiledSize(SizeF(10.9999f, 10.9999f)));
59}
60
61TEST(SizeTest, ToRoundedSize) {
62 EXPECT_EQ(Size(0, 0), ToRoundedSize(SizeF(0, 0)));
63 EXPECT_EQ(Size(0, 0), ToRoundedSize(SizeF(0.0001f, 0.0001f)));
64 EXPECT_EQ(Size(0, 0), ToRoundedSize(SizeF(0.4999f, 0.4999f)));
65 EXPECT_EQ(Size(1, 1), ToRoundedSize(SizeF(0.5f, 0.5f)));
66 EXPECT_EQ(Size(1, 1), ToRoundedSize(SizeF(0.9999f, 0.9999f)));
67
68 EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10, 10)));
69 EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10.0001f, 10.0001f)));
70 EXPECT_EQ(Size(10, 10), ToRoundedSize(SizeF(10.4999f, 10.4999f)));
71 EXPECT_EQ(Size(11, 11), ToRoundedSize(SizeF(10.5f, 10.5f)));
72 EXPECT_EQ(Size(11, 11), ToRoundedSize(SizeF(10.9999f, 10.9999f)));
73}
74
75TEST(SizeTest, ClampSize) {
76 Size a;
77
78 a = Size(3, 5);
79 EXPECT_EQ(Size(3, 5).ToString(), a.ToString());
80 a.SetToMax(Size(2, 4));
81 EXPECT_EQ(Size(3, 5).ToString(), a.ToString());
82 a.SetToMax(Size(3, 5));
83 EXPECT_EQ(Size(3, 5).ToString(), a.ToString());
84 a.SetToMax(Size(4, 2));
85 EXPECT_EQ(Size(4, 5).ToString(), a.ToString());
86 a.SetToMax(Size(8, 10));
87 EXPECT_EQ(Size(8, 10).ToString(), a.ToString());
88
89 a.SetToMin(Size(9, 11));
90 EXPECT_EQ(Size(8, 10).ToString(), a.ToString());
91 a.SetToMin(Size(8, 10));
92 EXPECT_EQ(Size(8, 10).ToString(), a.ToString());
93 a.SetToMin(Size(11, 9));
94 EXPECT_EQ(Size(8, 9).ToString(), a.ToString());
95 a.SetToMin(Size(7, 11));
96 EXPECT_EQ(Size(7, 9).ToString(), a.ToString());
97 a.SetToMin(Size(3, 5));
98 EXPECT_EQ(Size(3, 5).ToString(), a.ToString());
99}
100
101TEST(SizeTest, ClampSizeF) {
102 SizeF a;
103
104 a = SizeF(3.5f, 5.5f);
105 EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString());
106 a.SetToMax(SizeF(2.5f, 4.5f));
107 EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString());
108 a.SetToMax(SizeF(3.5f, 5.5f));
109 EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString());
110 a.SetToMax(SizeF(4.5f, 2.5f));
111 EXPECT_EQ(SizeF(4.5f, 5.5f).ToString(), a.ToString());
112 a.SetToMax(SizeF(8.5f, 10.5f));
113 EXPECT_EQ(SizeF(8.5f, 10.5f).ToString(), a.ToString());
114
115 a.SetToMin(SizeF(9.5f, 11.5f));
116 EXPECT_EQ(SizeF(8.5f, 10.5f).ToString(), a.ToString());
117 a.SetToMin(SizeF(8.5f, 10.5f));
118 EXPECT_EQ(SizeF(8.5f, 10.5f).ToString(), a.ToString());
119 a.SetToMin(SizeF(11.5f, 9.5f));
120 EXPECT_EQ(SizeF(8.5f, 9.5f).ToString(), a.ToString());
121 a.SetToMin(SizeF(7.5f, 11.5f));
122 EXPECT_EQ(SizeF(7.5f, 9.5f).ToString(), a.ToString());
123 a.SetToMin(SizeF(3.5f, 5.5f));
124 EXPECT_EQ(SizeF(3.5f, 5.5f).ToString(), a.ToString());
125}
126
127TEST(SizeTest, Enlarge) {
128 Size test(3, 4);
129 test.Enlarge(5, -8);
130 EXPECT_EQ(test, Size(8, -4));
131}
132
133TEST(SizeTest, IntegerOverflow) {
134 int int_max = std::numeric_limits<int>::max();
135 int int_min = std::numeric_limits<int>::min();
136
137 Size max_size(int_max, int_max);
138 Size min_size(int_min, int_min);
139 Size test;
140
141 test = Size();
142 test.Enlarge(int_max, int_max);
143 EXPECT_EQ(test, max_size);
144
145 test = Size();
146 test.Enlarge(int_min, int_min);
147 EXPECT_EQ(test, min_size);
148
149 test = Size(10, 20);
150 test.Enlarge(int_max, int_max);
151 EXPECT_EQ(test, max_size);
152
153 test = Size(-10, -20);
154 test.Enlarge(int_min, int_min);
155 EXPECT_EQ(test, min_size);
156}
157
158// This checks that we set IsEmpty appropriately.
159TEST(SizeTest, TrivialDimensionTests) {
160 const float clearly_trivial = kTrivial / 2.f;
161 const float massize_dimension = 4e13f;
162
163 // First, using the constructor.
164 EXPECT_TRUE(SizeF(clearly_trivial, 1.f).IsEmpty());
165 EXPECT_TRUE(SizeF(.01f, clearly_trivial).IsEmpty());
166 EXPECT_TRUE(SizeF(0.f, 0.f).IsEmpty());
167 EXPECT_FALSE(SizeF(.01f, .01f).IsEmpty());
168
169 // Then use the setter.
170 SizeF test(2.f, 1.f);
171 EXPECT_FALSE(test.IsEmpty());
172
173 test.SetSize(clearly_trivial, 1.f);
174 EXPECT_TRUE(test.IsEmpty());
175
176 test.SetSize(.01f, clearly_trivial);
177 EXPECT_TRUE(test.IsEmpty());
178
179 test.SetSize(0.f, 0.f);
180 EXPECT_TRUE(test.IsEmpty());
181
182 test.SetSize(.01f, .01f);
183 EXPECT_FALSE(test.IsEmpty());
184
185 // Now just one dimension at a time.
186 test.set_width(clearly_trivial);
187 EXPECT_TRUE(test.IsEmpty());
188
189 test.set_width(massize_dimension);
190 test.set_height(clearly_trivial);
191 EXPECT_TRUE(test.IsEmpty());
192
193 test.set_width(clearly_trivial);
194 test.set_height(massize_dimension);
195 EXPECT_TRUE(test.IsEmpty());
196
197 test.set_width(2.f);
198 EXPECT_FALSE(test.IsEmpty());
199}
200
201// These are the ramifications of the decision to keep the recorded size
202// at zero for trivial sizes.
203TEST(SizeTest, ClampsToZero) {
204 const float clearly_trivial = kTrivial / 2.f;
205 const float nearly_trivial = kTrivial * 1.5f;
206
207 SizeF test(clearly_trivial, 1.f);
208
209 EXPECT_FLOAT_EQ(0.f, test.width());
210 EXPECT_FLOAT_EQ(1.f, test.height());
211
212 test.SetSize(.01f, clearly_trivial);
213
214 EXPECT_FLOAT_EQ(.01f, test.width());
215 EXPECT_FLOAT_EQ(0.f, test.height());
216
217 test.SetSize(nearly_trivial, nearly_trivial);
218
219 EXPECT_FLOAT_EQ(nearly_trivial, test.width());
220 EXPECT_FLOAT_EQ(nearly_trivial, test.height());
221
222 test.Scale(0.5f);
223
224 EXPECT_FLOAT_EQ(0.f, test.width());
225 EXPECT_FLOAT_EQ(0.f, test.height());
226
227 test.SetSize(0.f, 0.f);
228 test.Enlarge(clearly_trivial, clearly_trivial);
229 test.Enlarge(clearly_trivial, clearly_trivial);
230 test.Enlarge(clearly_trivial, clearly_trivial);
231
232 EXPECT_EQ(SizeF(0.f, 0.f), test);
233}
234
235// These make sure the constructor and setter have the same effect on the
236// boundary case. This claims to know the boundary, but not which way it goes.
237TEST(SizeTest, ConsistentClamping) {
238 SizeF resized;
239
240 resized.SetSize(kTrivial, 0.f);
241 EXPECT_EQ(SizeF(kTrivial, 0.f), resized);
242
243 resized.SetSize(0.f, kTrivial);
244 EXPECT_EQ(SizeF(0.f, kTrivial), resized);
245}
246
247// Let's make sure we don't unexpectedly grow the struct by adding constants.
248// Also, if some platform packs floats inefficiently, it would be worth noting.
249TEST(SizeTest, StaysSmall) {
250 EXPECT_EQ(2 * sizeof(float), sizeof(SizeF));
251}
252
253TEST(SizeTest, OperatorAddSub) {
254 Size lhs(100, 20);
255 Size rhs(50, 10);
256
257 lhs += rhs;
258 EXPECT_EQ(Size(150, 30), lhs);
259
260 lhs = Size(100, 20);
261 EXPECT_EQ(Size(150, 30), lhs + rhs);
262
263 lhs = Size(100, 20);
264 lhs -= rhs;
265 EXPECT_EQ(Size(50, 10), lhs);
266
267 lhs = Size(100, 20);
268 EXPECT_EQ(Size(50, 10), lhs - rhs);
269}
270
271TEST(SizeTest, OperatorAddOverflow) {
272 int int_max = std::numeric_limits<int>::max();
273
274 Size lhs(int_max, int_max);
275 Size rhs(int_max, int_max);
276 EXPECT_EQ(Size(int_max, int_max), lhs + rhs);
277}
278
279TEST(SizeTest, OperatorSubClampAtZero) {
280 Size lhs(10, 10);
281 Size rhs(100, 100);
282 EXPECT_EQ(Size(0, 0), lhs - rhs);
283
284 lhs = Size(10, 10);
285 rhs = Size(100, 100);
286 lhs -= rhs;
287 EXPECT_EQ(Size(0, 0), lhs);
288}
289
290TEST(SizeTest, OperatorCompare) {
291 Size lhs(100, 20);
292 Size rhs(50, 10);
293
294 EXPECT_TRUE(lhs != rhs);
295 EXPECT_FALSE(lhs == rhs);
296
297 rhs = Size(100, 20);
298 EXPECT_TRUE(lhs == rhs);
299 EXPECT_FALSE(lhs != rhs);
300}
301
302} // namespace gfx
#define test(name)
void SetSize(float width, float height)
Definition: size_f.h:36
Definition: size.h:26
static bool b
struct MyStruct s
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: insets.cc:10
Size ToFlooredSize(const SizeF &size)
Size ToCeiledSize(const SizeF &size)
Size ToRoundedSize(const SizeF &size)
TEST(PointTest, ToPointF)
static std::string ToString(CompilerBackend::Type type)
Definition: reflector.cc:559
TSize< Scalar > Size
Definition: size.h:137
bool EMSCRIPTEN_KEEPALIVE IsEmpty(const SkPath &path)
int32_t width
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678