2 * Copyright (c) 2012, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "platform/LayoutUnit.h"
34 #include <gtest/gtest.h>
39 TEST(LayoutUnitTest
, LayoutUnitInt
)
41 EXPECT_EQ(intMinForLayoutUnit
, LayoutUnit(INT_MIN
).toInt());
42 EXPECT_EQ(intMinForLayoutUnit
, LayoutUnit(INT_MIN
/ 2).toInt());
43 EXPECT_EQ(intMinForLayoutUnit
, LayoutUnit(intMinForLayoutUnit
- 1).toInt());
44 EXPECT_EQ(intMinForLayoutUnit
, LayoutUnit(intMinForLayoutUnit
).toInt());
45 EXPECT_EQ(intMinForLayoutUnit
+ 1, LayoutUnit(intMinForLayoutUnit
+ 1).toInt());
46 EXPECT_EQ(intMinForLayoutUnit
/ 2, LayoutUnit(intMinForLayoutUnit
/ 2).toInt());
47 EXPECT_EQ(-10000, LayoutUnit(-10000).toInt());
48 EXPECT_EQ(-1000, LayoutUnit(-1000).toInt());
49 EXPECT_EQ(-100, LayoutUnit(-100).toInt());
50 EXPECT_EQ(-10, LayoutUnit(-10).toInt());
51 EXPECT_EQ(-1, LayoutUnit(-1).toInt());
52 EXPECT_EQ(0, LayoutUnit(0).toInt());
53 EXPECT_EQ(1, LayoutUnit(1).toInt());
54 EXPECT_EQ(100, LayoutUnit(100).toInt());
55 EXPECT_EQ(1000, LayoutUnit(1000).toInt());
56 EXPECT_EQ(10000, LayoutUnit(10000).toInt());
57 EXPECT_EQ(intMaxForLayoutUnit
/ 2, LayoutUnit(intMaxForLayoutUnit
/ 2).toInt());
58 EXPECT_EQ(intMaxForLayoutUnit
- 1, LayoutUnit(intMaxForLayoutUnit
- 1).toInt());
59 EXPECT_EQ(intMaxForLayoutUnit
, LayoutUnit(intMaxForLayoutUnit
).toInt());
60 EXPECT_EQ(intMaxForLayoutUnit
, LayoutUnit(intMaxForLayoutUnit
+ 1).toInt());
61 EXPECT_EQ(intMaxForLayoutUnit
, LayoutUnit(INT_MAX
/ 2).toInt());
62 EXPECT_EQ(intMaxForLayoutUnit
, LayoutUnit(INT_MAX
).toInt());
65 TEST(LayoutUnitTest
, LayoutUnitFloat
)
67 const float tolerance
= 1.0f
/ kFixedPointDenominator
;
68 EXPECT_FLOAT_EQ(1.0f
, LayoutUnit(1.0f
).toFloat());
69 EXPECT_FLOAT_EQ(1.25f
, LayoutUnit(1.25f
).toFloat());
70 EXPECT_NEAR(LayoutUnit(1.1f
).toFloat(), 1.1f
, tolerance
);
71 EXPECT_NEAR(LayoutUnit(1.33f
).toFloat(), 1.33f
, tolerance
);
72 EXPECT_NEAR(LayoutUnit(1.3333f
).toFloat(), 1.3333f
, tolerance
);
73 EXPECT_NEAR(LayoutUnit(1.53434f
).toFloat(), 1.53434f
, tolerance
);
74 EXPECT_NEAR(LayoutUnit(345634).toFloat(), 345634.0f
, tolerance
);
75 EXPECT_NEAR(LayoutUnit(345634.12335f
).toFloat(), 345634.12335f
, tolerance
);
76 EXPECT_NEAR(LayoutUnit(-345634.12335f
).toFloat(), -345634.12335f
, tolerance
);
77 EXPECT_NEAR(LayoutUnit(-345634).toFloat(), -345634.0f
, tolerance
);
80 TEST(LayoutUnitTest
, LayoutUnitRounding
)
82 EXPECT_EQ(-2, LayoutUnit(-1.9f
).round());
83 EXPECT_EQ(-2, LayoutUnit(-1.6f
).round());
84 EXPECT_EQ(-2, LayoutUnit::fromFloatRound(-1.51f
).round());
85 EXPECT_EQ(-1, LayoutUnit::fromFloatRound(-1.5f
).round());
86 EXPECT_EQ(-1, LayoutUnit::fromFloatRound(-1.49f
).round());
87 EXPECT_EQ(-1, LayoutUnit(-1.0f
).round());
88 EXPECT_EQ(-1, LayoutUnit::fromFloatRound(-0.99f
).round());
89 EXPECT_EQ(-1, LayoutUnit::fromFloatRound(-0.51f
).round());
90 EXPECT_EQ(0, LayoutUnit::fromFloatRound(-0.50f
).round());
91 EXPECT_EQ(0, LayoutUnit::fromFloatRound(-0.49f
).round());
92 EXPECT_EQ(0, LayoutUnit(-0.1f
).round());
93 EXPECT_EQ(0, LayoutUnit(0.0f
).round());
94 EXPECT_EQ(0, LayoutUnit(0.1f
).round());
95 EXPECT_EQ(0, LayoutUnit::fromFloatRound(0.49f
).round());
96 EXPECT_EQ(1, LayoutUnit::fromFloatRound(0.50f
).round());
97 EXPECT_EQ(1, LayoutUnit::fromFloatRound(0.51f
).round());
98 EXPECT_EQ(1, LayoutUnit(0.99f
).round());
99 EXPECT_EQ(1, LayoutUnit(1.0f
).round());
100 EXPECT_EQ(1, LayoutUnit::fromFloatRound(1.49f
).round());
101 EXPECT_EQ(2, LayoutUnit::fromFloatRound(1.5f
).round());
102 EXPECT_EQ(2, LayoutUnit::fromFloatRound(1.51f
).round());
105 TEST(LayoutUnitTest
, LayoutUnitSnapSizeToPixel
)
107 EXPECT_EQ(1, snapSizeToPixel(LayoutUnit(1), LayoutUnit(0)));
108 EXPECT_EQ(1, snapSizeToPixel(LayoutUnit(1), LayoutUnit(0.5)));
109 EXPECT_EQ(2, snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0)));
110 EXPECT_EQ(2, snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0.49)));
111 EXPECT_EQ(1, snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0.5)));
112 EXPECT_EQ(1, snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0.75)));
113 EXPECT_EQ(1, snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(0.99)));
114 EXPECT_EQ(2, snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(1)));
116 EXPECT_EQ(0, snapSizeToPixel(LayoutUnit(0.5), LayoutUnit(1.5)));
117 EXPECT_EQ(0, snapSizeToPixel(LayoutUnit(0.99), LayoutUnit(1.5)));
118 EXPECT_EQ(1, snapSizeToPixel(LayoutUnit(1.0), LayoutUnit(1.5)));
119 EXPECT_EQ(1, snapSizeToPixel(LayoutUnit(1.49), LayoutUnit(1.5)));
120 EXPECT_EQ(1, snapSizeToPixel(LayoutUnit(1.5), LayoutUnit(1.5)));
122 EXPECT_EQ(101, snapSizeToPixel(LayoutUnit(100.5), LayoutUnit(100)));
123 EXPECT_EQ(intMaxForLayoutUnit
, snapSizeToPixel(LayoutUnit(intMaxForLayoutUnit
), LayoutUnit(0.3)));
124 EXPECT_EQ(intMinForLayoutUnit
, snapSizeToPixel(LayoutUnit(intMinForLayoutUnit
), LayoutUnit(-0.3)));
127 TEST(LayoutUnitTest
, LayoutUnitMultiplication
)
129 EXPECT_EQ(1, (LayoutUnit(1) * LayoutUnit(1)).toInt());
130 EXPECT_EQ(2, (LayoutUnit(1) * LayoutUnit(2)).toInt());
131 EXPECT_EQ(2, (LayoutUnit(2) * LayoutUnit(1)).toInt());
132 EXPECT_EQ(1, (LayoutUnit(2) * LayoutUnit(0.5)).toInt());
133 EXPECT_EQ(1, (LayoutUnit(0.5) * LayoutUnit(2)).toInt());
134 EXPECT_EQ(100, (LayoutUnit(100) * LayoutUnit(1)).toInt());
136 EXPECT_EQ(-1, (LayoutUnit(-1) * LayoutUnit(1)).toInt());
137 EXPECT_EQ(-2, (LayoutUnit(-1) * LayoutUnit(2)).toInt());
138 EXPECT_EQ(-2, (LayoutUnit(-2) * LayoutUnit(1)).toInt());
139 EXPECT_EQ(-1, (LayoutUnit(-2) * LayoutUnit(0.5)).toInt());
140 EXPECT_EQ(-1, (LayoutUnit(-0.5) * LayoutUnit(2)).toInt());
141 EXPECT_EQ(-100, (LayoutUnit(-100) * LayoutUnit(1)).toInt());
143 EXPECT_EQ(1, (LayoutUnit(-1) * LayoutUnit(-1)).toInt());
144 EXPECT_EQ(2, (LayoutUnit(-1) * LayoutUnit(-2)).toInt());
145 EXPECT_EQ(2, (LayoutUnit(-2) * LayoutUnit(-1)).toInt());
146 EXPECT_EQ(1, (LayoutUnit(-2) * LayoutUnit(-0.5)).toInt());
147 EXPECT_EQ(1, (LayoutUnit(-0.5) * LayoutUnit(-2)).toInt());
148 EXPECT_EQ(100, (LayoutUnit(-100) * LayoutUnit(-1)).toInt());
150 EXPECT_EQ(333, (LayoutUnit(100) * LayoutUnit(3.33)).round());
151 EXPECT_EQ(-333, (LayoutUnit(-100) * LayoutUnit(3.33)).round());
152 EXPECT_EQ(333, (LayoutUnit(-100) * LayoutUnit(-3.33)).round());
154 size_t aHundredSizeT
= 100;
155 EXPECT_EQ(100, (LayoutUnit(aHundredSizeT
) * LayoutUnit(1)).toInt());
156 EXPECT_EQ(400, (aHundredSizeT
* LayoutUnit(4)).toInt());
157 EXPECT_EQ(400, (LayoutUnit(4) * aHundredSizeT
).toInt());
159 int quarterMax
= intMaxForLayoutUnit
/ 4;
160 EXPECT_EQ(quarterMax
* 2, (LayoutUnit(quarterMax
) * LayoutUnit(2)).toInt());
161 EXPECT_EQ(quarterMax
* 3, (LayoutUnit(quarterMax
) * LayoutUnit(3)).toInt());
162 EXPECT_EQ(quarterMax
* 4, (LayoutUnit(quarterMax
) * LayoutUnit(4)).toInt());
163 EXPECT_EQ(intMaxForLayoutUnit
, (LayoutUnit(quarterMax
) * LayoutUnit(5)).toInt());
165 size_t overflowIntSizeT
= intMaxForLayoutUnit
* 4;
166 EXPECT_EQ(intMaxForLayoutUnit
, (LayoutUnit(overflowIntSizeT
) * LayoutUnit(2)).toInt());
167 EXPECT_EQ(intMaxForLayoutUnit
, (overflowIntSizeT
* LayoutUnit(4)).toInt());
168 EXPECT_EQ(intMaxForLayoutUnit
, (LayoutUnit(4) * overflowIntSizeT
).toInt());
171 TEST(LayoutUnitTest
, LayoutUnitDivision
)
173 EXPECT_EQ(1, (LayoutUnit(1) / LayoutUnit(1)).toInt());
174 EXPECT_EQ(0, (LayoutUnit(1) / LayoutUnit(2)).toInt());
175 EXPECT_EQ(2, (LayoutUnit(2) / LayoutUnit(1)).toInt());
176 EXPECT_EQ(4, (LayoutUnit(2) / LayoutUnit(0.5)).toInt());
177 EXPECT_EQ(0, (LayoutUnit(0.5) / LayoutUnit(2)).toInt());
178 EXPECT_EQ(10, (LayoutUnit(100) / LayoutUnit(10)).toInt());
179 EXPECT_FLOAT_EQ(0.5f
, (LayoutUnit(1) / LayoutUnit(2)).toFloat());
180 EXPECT_FLOAT_EQ(0.25f
, (LayoutUnit(0.5) / LayoutUnit(2)).toFloat());
182 EXPECT_EQ(-1, (LayoutUnit(-1) / LayoutUnit(1)).toInt());
183 EXPECT_EQ(0, (LayoutUnit(-1) / LayoutUnit(2)).toInt());
184 EXPECT_EQ(-2, (LayoutUnit(-2) / LayoutUnit(1)).toInt());
185 EXPECT_EQ(-4, (LayoutUnit(-2) / LayoutUnit(0.5)).toInt());
186 EXPECT_EQ(0, (LayoutUnit(-0.5) / LayoutUnit(2)).toInt());
187 EXPECT_EQ(-10, (LayoutUnit(-100) / LayoutUnit(10)).toInt());
188 EXPECT_FLOAT_EQ(-0.5f
, (LayoutUnit(-1) / LayoutUnit(2)).toFloat());
189 EXPECT_FLOAT_EQ(-0.25f
, (LayoutUnit(-0.5) / LayoutUnit(2)).toFloat());
191 EXPECT_EQ(1, (LayoutUnit(-1) / LayoutUnit(-1)).toInt());
192 EXPECT_EQ(0, (LayoutUnit(-1) / LayoutUnit(-2)).toInt());
193 EXPECT_EQ(2, (LayoutUnit(-2) / LayoutUnit(-1)).toInt());
194 EXPECT_EQ(4, (LayoutUnit(-2) / LayoutUnit(-0.5)).toInt());
195 EXPECT_EQ(0, (LayoutUnit(-0.5) / LayoutUnit(-2)).toInt());
196 EXPECT_EQ(10, (LayoutUnit(-100) / LayoutUnit(-10)).toInt());
197 EXPECT_FLOAT_EQ(0.5f
, (LayoutUnit(-1) / LayoutUnit(-2)).toFloat());
198 EXPECT_FLOAT_EQ(0.25f
, (LayoutUnit(-0.5) / LayoutUnit(-2)).toFloat());
200 size_t aHundredSizeT
= 100;
201 EXPECT_EQ(50, (LayoutUnit(aHundredSizeT
) / LayoutUnit(2)).toInt());
202 EXPECT_EQ(25, (aHundredSizeT
/ LayoutUnit(4)).toInt());
203 EXPECT_EQ(4, (LayoutUnit(400) / aHundredSizeT
).toInt());
205 EXPECT_EQ(intMaxForLayoutUnit
/ 2, (LayoutUnit(intMaxForLayoutUnit
) / LayoutUnit(2)).toInt());
206 EXPECT_EQ(intMaxForLayoutUnit
, (LayoutUnit(intMaxForLayoutUnit
) / LayoutUnit(0.5)).toInt());
209 TEST(LayoutUnitTest
, LayoutUnitCeil
)
211 EXPECT_EQ(0, LayoutUnit(0).ceil());
212 EXPECT_EQ(1, LayoutUnit(0.1).ceil());
213 EXPECT_EQ(1, LayoutUnit(0.5).ceil());
214 EXPECT_EQ(1, LayoutUnit(0.9).ceil());
215 EXPECT_EQ(1, LayoutUnit(1.0).ceil());
216 EXPECT_EQ(2, LayoutUnit(1.1).ceil());
218 EXPECT_EQ(0, LayoutUnit(-0.1).ceil());
219 EXPECT_EQ(0, LayoutUnit(-0.5).ceil());
220 EXPECT_EQ(0, LayoutUnit(-0.9).ceil());
221 EXPECT_EQ(-1, LayoutUnit(-1.0).ceil());
223 EXPECT_EQ(intMaxForLayoutUnit
, LayoutUnit(intMaxForLayoutUnit
).ceil());
224 EXPECT_EQ(intMaxForLayoutUnit
, (LayoutUnit(intMaxForLayoutUnit
) - LayoutUnit(0.5)).ceil());
225 EXPECT_EQ(intMaxForLayoutUnit
- 1, (LayoutUnit(intMaxForLayoutUnit
) - LayoutUnit(1)).ceil());
227 EXPECT_EQ(intMinForLayoutUnit
, LayoutUnit(intMinForLayoutUnit
).ceil());
230 TEST(LayoutUnitTest
, LayoutUnitFloor
)
232 EXPECT_EQ(0, LayoutUnit(0).floor());
233 EXPECT_EQ(0, LayoutUnit(0.1).floor());
234 EXPECT_EQ(0, LayoutUnit(0.5).floor());
235 EXPECT_EQ(0, LayoutUnit(0.9).floor());
236 EXPECT_EQ(1, LayoutUnit(1.0).floor());
237 EXPECT_EQ(1, LayoutUnit(1.1).floor());
239 EXPECT_EQ(-1, LayoutUnit(-0.1).floor());
240 EXPECT_EQ(-1, LayoutUnit(-0.5).floor());
241 EXPECT_EQ(-1, LayoutUnit(-0.9).floor());
242 EXPECT_EQ(-1, LayoutUnit(-1.0).floor());
244 EXPECT_EQ(intMaxForLayoutUnit
, LayoutUnit(intMaxForLayoutUnit
).floor());
246 EXPECT_EQ(intMinForLayoutUnit
, LayoutUnit(intMinForLayoutUnit
).floor());
247 EXPECT_EQ(intMinForLayoutUnit
, (LayoutUnit(intMinForLayoutUnit
) + LayoutUnit(0.5)).floor());
248 EXPECT_EQ(intMinForLayoutUnit
+ 1, (LayoutUnit(intMinForLayoutUnit
) + LayoutUnit(1)).floor());
251 TEST(LayoutUnitTest
, LayoutUnitFloatOverflow
)
253 // These should overflow to the max/min according to their sign.
254 EXPECT_EQ(intMaxForLayoutUnit
, LayoutUnit(176972000.0f
).toInt());
255 EXPECT_EQ(intMinForLayoutUnit
, LayoutUnit(-176972000.0f
).toInt());
256 EXPECT_EQ(intMaxForLayoutUnit
, LayoutUnit(176972000.0).toInt());
257 EXPECT_EQ(intMinForLayoutUnit
, LayoutUnit(-176972000.0).toInt());