/src/mozilla-central/gfx/tests/gtest/TestRect.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | #include <limits> |
7 | | |
8 | | #include "gtest/gtest.h" |
9 | | |
10 | | #include "nsRect.h" |
11 | | #include "gfxRect.h" |
12 | | #include "mozilla/WritingModes.h" |
13 | | #ifdef XP_WIN |
14 | | #include <windows.h> |
15 | | #endif |
16 | | |
17 | | template <class RectType> |
18 | | static bool |
19 | | TestConstructors() |
20 | 0 | { |
21 | 0 | // Create a rectangle |
22 | 0 | RectType rect1(10, 20, 30, 40); |
23 | 0 |
|
24 | 0 | // Make sure the rectangle was properly initialized |
25 | 0 | EXPECT_TRUE(rect1.IsEqualRect(10, 20, 30, 40) && |
26 | 0 | rect1.IsEqualXY(10, 20) && rect1.IsEqualSize(30, 40)) << |
27 | 0 | "[1] Make sure the rectangle was properly initialized with constructor"; |
28 | 0 |
|
29 | 0 | // Create a second rect using the copy constructor |
30 | 0 | RectType rect2(rect1); |
31 | 0 |
|
32 | 0 | // Make sure the rectangle was properly initialized |
33 | 0 | EXPECT_TRUE(rect2.IsEqualEdges(rect1) && |
34 | 0 | rect2.IsEqualXY(rect1.X(), rect1.Y()) && |
35 | 0 | rect2.IsEqualSize(rect1.Width(), rect1.Height())) << |
36 | 0 | "[2] Make sure the rectangle was properly initialized with copy constructor"; |
37 | 0 |
|
38 | 0 |
|
39 | 0 | EXPECT_TRUE(!rect1.IsEmpty() && !rect1.IsZeroArea() && rect1.IsFinite() && |
40 | 0 | !rect2.IsEmpty() && !rect2.IsZeroArea() && rect2.IsFinite()) << |
41 | 0 | "[3] These rectangles are not empty and are finite"; |
42 | 0 |
|
43 | 0 |
|
44 | 0 | rect1.SetRect(1, 2, 30, 40); |
45 | 0 | EXPECT_TRUE(rect1.X() == 1 && rect1.Y() == 2 && |
46 | 0 | rect1.Width() == 30 && rect1.Height() == 40 && |
47 | 0 | rect1.XMost() == 31 && rect1.YMost() == 42); |
48 | 0 |
|
49 | 0 | rect1.SetRectX(11, 50); |
50 | 0 | EXPECT_TRUE(rect1.X() == 11 && rect1.Y() == 2 && |
51 | 0 | rect1.Width() == 50 && rect1.Height() == 40 && |
52 | 0 | rect1.XMost() == 61 && rect1.YMost() == 42); |
53 | 0 |
|
54 | 0 | rect1.SetRectY(22, 60); |
55 | 0 | EXPECT_TRUE(rect1.X() == 11 && rect1.Y() == 22 && |
56 | 0 | rect1.Width() == 50 && rect1.Height() == 60 && |
57 | 0 | rect1.XMost() == 61 && rect1.YMost() == 82); |
58 | 0 |
|
59 | 0 | rect1.SetBox(1, 2, 31, 42); |
60 | 0 | EXPECT_TRUE(rect1.X() == 1 && rect1.Y() == 2 && |
61 | 0 | rect1.Width() == 30 && rect1.Height() == 40 && |
62 | 0 | rect1.XMost() == 31 && rect1.YMost() == 42); |
63 | 0 |
|
64 | 0 | rect1.SetBoxX(11, 61); |
65 | 0 | EXPECT_TRUE(rect1.X() == 11 && rect1.Y() == 2 && |
66 | 0 | rect1.Width() == 50 && rect1.Height() == 40 && |
67 | 0 | rect1.XMost() == 61 && rect1.YMost() == 42); |
68 | 0 |
|
69 | 0 | rect1.SetBoxY(22, 82); |
70 | 0 | EXPECT_TRUE(rect1.X() == 11 && rect1.Y() == 22 && |
71 | 0 | rect1.Width() == 50 && rect1.Height() == 60 && |
72 | 0 | rect1.XMost() == 61 && rect1.YMost() == 82); |
73 | 0 |
|
74 | 0 | rect1.SetRect(1, 2, 30, 40); |
75 | 0 | EXPECT_TRUE(rect1.X() == 1 && rect1.Y() == 2 && |
76 | 0 | rect1.Width() == 30 && rect1.Height() == 40 && |
77 | 0 | rect1.XMost() == 31 && rect1.YMost() == 42); |
78 | 0 |
|
79 | 0 | rect1.MoveByX(10); |
80 | 0 | EXPECT_TRUE(rect1.X() == 11 && rect1.Y() == 2 && |
81 | 0 | rect1.Width() == 30 && rect1.Height() == 40 && |
82 | 0 | rect1.XMost() == 41 && rect1.YMost() == 42); |
83 | 0 |
|
84 | 0 | rect1.MoveByY(20); |
85 | 0 | EXPECT_TRUE(rect1.X() == 11 && rect1.Y() == 22 && |
86 | 0 | rect1.Width() == 30 && rect1.Height() == 40 && |
87 | 0 | rect1.XMost() == 41 && rect1.YMost() == 62); |
88 | 0 |
|
89 | 0 | return true; |
90 | 0 | } Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestConstructors<nsRect>() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestConstructors<mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> >() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestConstructors<mozilla::gfx::RectTyped<mozilla::gfx::UnknownUnits, double> >() |
91 | | |
92 | | template <class RectType> |
93 | | static bool |
94 | | TestEqualityOperator() |
95 | 0 | { |
96 | 0 | RectType rect1(10, 20, 30, 40); |
97 | 0 | RectType rect2(rect1); |
98 | 0 |
|
99 | 0 | // Test the equality operator |
100 | 0 | EXPECT_TRUE(rect1 == rect2) << |
101 | 0 | "[1] Test the equality operator"; |
102 | 0 |
|
103 | 0 | EXPECT_FALSE(!rect1.IsEqualInterior(rect2)) << |
104 | 0 | "[2] Test the inequality operator"; |
105 | 0 |
|
106 | 0 | // Make sure that two empty rects are equal |
107 | 0 | rect1.SetEmpty(); |
108 | 0 | rect2.SetEmpty(); |
109 | 0 | EXPECT_TRUE(rect1 == rect2) << |
110 | 0 | "[3] Make sure that two empty rects are equal"; |
111 | 0 |
|
112 | 0 | return true; |
113 | 0 | } Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestEqualityOperator<nsRect>() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestEqualityOperator<mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> >() |
114 | | |
115 | | template <class RectType> |
116 | | static bool |
117 | | TestContainment() |
118 | 0 | { |
119 | 0 | RectType rect1(10, 10, 50, 50); |
120 | 0 |
|
121 | 0 | // Test the point containment methods |
122 | 0 | // |
123 | 0 |
|
124 | 0 | // Basic test of a point in the middle of the rect |
125 | 0 | EXPECT_TRUE(rect1.Contains(rect1.Center()) && |
126 | 0 | rect1.ContainsX(rect1.Center().x) && |
127 | 0 | rect1.ContainsY(rect1.Center().y)) << |
128 | 0 | "[1] Basic test of a point in the middle of the rect"; |
129 | 0 |
|
130 | 0 | // Test against a point at the left/top edges |
131 | 0 | EXPECT_TRUE(rect1.Contains(rect1.X(), rect1.Y()) && |
132 | 0 | rect1.ContainsX(rect1.X()) && |
133 | 0 | rect1.ContainsY(rect1.Y())) << |
134 | 0 | "[2] Test against a point at the left/top edges"; |
135 | 0 |
|
136 | 0 | // Test against a point at the right/bottom extents |
137 | 0 | EXPECT_FALSE(rect1.Contains(rect1.XMost(), rect1.YMost()) || |
138 | 0 | rect1.ContainsX(rect1.XMost()) || |
139 | 0 | rect1.ContainsY(rect1.YMost())) << |
140 | 0 | "[3] Test against a point at the right/bottom extents"; |
141 | 0 |
|
142 | 0 | // Test the rect containment methods |
143 | 0 | // |
144 | 0 | RectType rect2(rect1); |
145 | 0 |
|
146 | 0 | // Test against a rect that's the same as rect1 |
147 | 0 | EXPECT_FALSE(!rect1.Contains(rect2)) << |
148 | 0 | "[4] Test against a rect that's the same as rect1"; |
149 | 0 |
|
150 | 0 | // Test against a rect whose left edge (only) is outside of rect1 |
151 | 0 | rect2.MoveByX(-1); |
152 | 0 | EXPECT_FALSE(rect1.Contains(rect2)) << |
153 | 0 | "[5] Test against a rect whose left edge (only) is outside of rect1"; |
154 | 0 | rect2.MoveByX(1); |
155 | 0 |
|
156 | 0 | // Test against a rect whose top edge (only) is outside of rect1 |
157 | 0 | rect2.MoveByY(-1); |
158 | 0 | EXPECT_FALSE(rect1.Contains(rect2)) << |
159 | 0 | "[6] Test against a rect whose top edge (only) is outside of rect1"; |
160 | 0 | rect2.MoveByY(1); |
161 | 0 |
|
162 | 0 | // Test against a rect whose right edge (only) is outside of rect1 |
163 | 0 | rect2.MoveByX(1); |
164 | 0 | EXPECT_FALSE(rect1.Contains(rect2)) << |
165 | 0 | "[7] Test against a rect whose right edge (only) is outside of rect1"; |
166 | 0 | rect2.MoveByX(-1); |
167 | 0 |
|
168 | 0 | // Test against a rect whose bottom edge (only) is outside of rect1 |
169 | 0 | rect2.MoveByY(1); |
170 | 0 | EXPECT_FALSE(rect1.Contains(rect2)) << |
171 | 0 | "[8] Test against a rect whose bottom edge (only) is outside of rect1"; |
172 | 0 | rect2.MoveByY(-1); |
173 | 0 |
|
174 | 0 | return true; |
175 | 0 | } Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestContainment<nsRect>() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestContainment<mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> >() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestContainment<mozilla::gfx::RectTyped<mozilla::gfx::UnknownUnits, double> >() |
176 | | |
177 | | // Test the method that returns a boolean result but doesn't return a |
178 | | // a rectangle |
179 | | template <class RectType> |
180 | | static bool |
181 | | TestIntersects() |
182 | 0 | { |
183 | 0 | RectType rect1(10, 10, 50, 50); |
184 | 0 | RectType rect2(rect1); |
185 | 0 |
|
186 | 0 | // Test against a rect that's the same as rect1 |
187 | 0 | EXPECT_FALSE(!rect1.Intersects(rect2)) << |
188 | 0 | "[1] Test against a rect that's the same as rect1"; |
189 | 0 |
|
190 | 0 | // Test against a rect that's enclosed by rect1 |
191 | 0 | rect2.Inflate(-1, -1); |
192 | 0 | EXPECT_FALSE(!rect1.Contains(rect2) || !rect1.Intersects(rect2)) << |
193 | 0 | "[2] Test against a rect that's enclosed by rect1"; |
194 | 0 | rect2.Inflate(1, 1); |
195 | 0 |
|
196 | 0 | // Make sure inflate and deflate worked correctly |
197 | 0 | EXPECT_TRUE(rect1.IsEqualInterior(rect2)) << |
198 | 0 | "[3] Make sure inflate and deflate worked correctly"; |
199 | 0 |
|
200 | 0 | // Test against a rect that overlaps the left edge of rect1 |
201 | 0 | rect2.MoveByX(-1); |
202 | 0 | EXPECT_FALSE(!rect1.Intersects(rect2)) << |
203 | 0 | "[4] Test against a rect that overlaps the left edge of rect1"; |
204 | 0 | rect2.MoveByX(1); |
205 | 0 |
|
206 | 0 | // Test against a rect that's outside of rect1 on the left |
207 | 0 | rect2.MoveByX(-rect2.Width()); |
208 | 0 | EXPECT_FALSE(rect1.Intersects(rect2)) << |
209 | 0 | "[5] Test against a rect that's outside of rect1 on the left"; |
210 | 0 | rect2.MoveByX(rect2.Width()); |
211 | 0 |
|
212 | 0 | // Test against a rect that overlaps the top edge of rect1 |
213 | 0 | rect2.MoveByY(-1); |
214 | 0 | EXPECT_FALSE(!rect1.Intersects(rect2)) << |
215 | 0 | "[6] Test against a rect that overlaps the top edge of rect1"; |
216 | 0 | rect2.MoveByY(1); |
217 | 0 |
|
218 | 0 | // Test against a rect that's outside of rect1 on the top |
219 | 0 | rect2.MoveByY(-rect2.Height()); |
220 | 0 | EXPECT_FALSE(rect1.Intersects(rect2)) << |
221 | 0 | "[7] Test against a rect that's outside of rect1 on the top"; |
222 | 0 | rect2.MoveByY(rect2.Height()); |
223 | 0 |
|
224 | 0 | // Test against a rect that overlaps the right edge of rect1 |
225 | 0 | rect2.MoveByX(1); |
226 | 0 | EXPECT_FALSE(!rect1.Intersects(rect2)) << |
227 | 0 | "[8] Test against a rect that overlaps the right edge of rect1"; |
228 | 0 | rect2.MoveByX(-1); |
229 | 0 |
|
230 | 0 | // Test against a rect that's outside of rect1 on the right |
231 | 0 | rect2.MoveByX(rect2.Width()); |
232 | 0 | EXPECT_FALSE(rect1.Intersects(rect2)) << |
233 | 0 | "[9] Test against a rect that's outside of rect1 on the right"; |
234 | 0 | rect2.MoveByX(-rect2.Width()); |
235 | 0 |
|
236 | 0 | // Test against a rect that overlaps the bottom edge of rect1 |
237 | 0 | rect2.MoveByY(1); |
238 | 0 | EXPECT_FALSE(!rect1.Intersects(rect2)) << |
239 | 0 | "[10] Test against a rect that overlaps the bottom edge of rect1"; |
240 | 0 | rect2.MoveByY(-1); |
241 | 0 |
|
242 | 0 | // Test against a rect that's outside of rect1 on the bottom |
243 | 0 | rect2.MoveByY(rect2.Height()); |
244 | 0 | EXPECT_FALSE(rect1.Intersects(rect2)) << |
245 | 0 | "[11] Test against a rect that's outside of rect1 on the bottom"; |
246 | 0 | rect2.MoveByY(-rect2.Height()); |
247 | 0 |
|
248 | 0 | return true; |
249 | 0 | } Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestIntersects<nsRect>() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestIntersects<mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> >() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestIntersects<mozilla::gfx::RectTyped<mozilla::gfx::UnknownUnits, double> >() |
250 | | |
251 | | // Test the method that returns a boolean result and an intersection rect |
252 | | template <class RectType> |
253 | | static bool |
254 | | TestIntersection() |
255 | 0 | { |
256 | 0 | RectType rect1(10, 10, 50, 50); |
257 | 0 | RectType rect2(rect1); |
258 | 0 | RectType dest; |
259 | 0 |
|
260 | 0 | // Test against a rect that's the same as rect1 |
261 | 0 | EXPECT_FALSE(!dest.IntersectRect(rect1, rect2) || !(dest.IsEqualInterior(rect1))) << |
262 | 0 | "[1] Test against a rect that's the same as rect1"; |
263 | 0 |
|
264 | 0 | // Test against a rect that's enclosed by rect1 |
265 | 0 | rect2.Inflate(-1, -1); |
266 | 0 | EXPECT_FALSE(!dest.IntersectRect(rect1, rect2) || !(dest.IsEqualInterior(rect2))) << |
267 | 0 | "[2] Test against a rect that's enclosed by rect1"; |
268 | 0 | rect2.Inflate(1, 1); |
269 | 0 |
|
270 | 0 | // Test against a rect that overlaps the left edge of rect1 |
271 | 0 | rect2.MoveByX(-1); |
272 | 0 | EXPECT_FALSE(!dest.IntersectRect(rect1, rect2) || |
273 | 0 | !(dest.IsEqualInterior(RectType(rect1.X(), rect1.Y(), rect1.Width() - 1, rect1.Height())))) << |
274 | 0 | "[3] Test against a rect that overlaps the left edge of rect1"; |
275 | 0 | rect2.MoveByX(1); |
276 | 0 |
|
277 | 0 | // Test against a rect that's outside of rect1 on the left |
278 | 0 | rect2.MoveByX(-rect2.Width()); |
279 | 0 | EXPECT_FALSE(dest.IntersectRect(rect1, rect2)) << |
280 | 0 | "[4] Test against a rect that's outside of rect1 on the left"; |
281 | 0 | // Make sure an empty rect is returned |
282 | 0 | EXPECT_TRUE(dest.IsEmpty() && dest.IsZeroArea()) << |
283 | 0 | "[4] Make sure an empty rect is returned"; |
284 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[4b] Should be finite"; |
285 | 0 | rect2.MoveByX(rect2.Width()); |
286 | 0 |
|
287 | 0 | // Test against a rect that overlaps the top edge of rect1 |
288 | 0 | rect2.MoveByY(-1); |
289 | 0 | EXPECT_FALSE(!dest.IntersectRect(rect1, rect2) || |
290 | 0 | !(dest.IsEqualInterior(RectType(rect1.X(), rect1.Y(), rect1.Width(), rect1.Height() - 1)))) << |
291 | 0 | "[5] Test against a rect that overlaps the top edge of rect1"; |
292 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[5b] Should be finite"; |
293 | 0 | rect2.MoveByY(1); |
294 | 0 |
|
295 | 0 | // Test against a rect that's outside of rect1 on the top |
296 | 0 | rect2.MoveByY(-rect2.Height()); |
297 | 0 | EXPECT_FALSE(dest.IntersectRect(rect1, rect2)) << |
298 | 0 | "[6] Test against a rect that's outside of rect1 on the top"; |
299 | 0 | // Make sure an empty rect is returned |
300 | 0 | EXPECT_TRUE(dest.IsEmpty() && dest.IsZeroArea()) << |
301 | 0 | "[6] Make sure an empty rect is returned"; |
302 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[6b] Should be finite"; |
303 | 0 | rect2.MoveByY(rect2.Height()); |
304 | 0 |
|
305 | 0 | // Test against a rect that overlaps the right edge of rect1 |
306 | 0 | rect2.MoveByX(1); |
307 | 0 | EXPECT_FALSE(!dest.IntersectRect(rect1, rect2) || |
308 | 0 | !(dest.IsEqualInterior(RectType(rect1.X() + 1, rect1.Y(), rect1.Width() - 1, rect1.Height())))) << |
309 | 0 | "[7] Test against a rect that overlaps the right edge of rect1"; |
310 | 0 | rect2.MoveByX(-1); |
311 | 0 |
|
312 | 0 | // Test against a rect that's outside of rect1 on the right |
313 | 0 | rect2.MoveByX(rect2.Width()); |
314 | 0 | EXPECT_FALSE(dest.IntersectRect(rect1, rect2)) << |
315 | 0 | "[8] Test against a rect that's outside of rect1 on the right"; |
316 | 0 | // Make sure an empty rect is returned |
317 | 0 | EXPECT_TRUE(dest.IsEmpty() && dest.IsZeroArea()) << |
318 | 0 | "[8] Make sure an empty rect is returned"; |
319 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[8b] Should be finite"; |
320 | 0 | rect2.MoveByX(-rect2.Width()); |
321 | 0 |
|
322 | 0 | // Test against a rect that overlaps the bottom edge of rect1 |
323 | 0 | rect2.MoveByY(1); |
324 | 0 | EXPECT_FALSE(!dest.IntersectRect(rect1, rect2) || |
325 | 0 | !(dest.IsEqualInterior(RectType(rect1.X(), rect1.Y() + 1, rect1.Width(), rect1.Height() - 1)))) << |
326 | 0 | "[9] Test against a rect that overlaps the bottom edge of rect1"; |
327 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[9b] Should be finite"; |
328 | 0 | rect2.MoveByY(-1); |
329 | 0 |
|
330 | 0 | // Test against a rect that's outside of rect1 on the bottom |
331 | 0 | rect2.MoveByY(rect2.Height()); |
332 | 0 | EXPECT_FALSE(dest.IntersectRect(rect1, rect2)) << |
333 | 0 | "[10] Test against a rect that's outside of rect1 on the bottom"; |
334 | 0 | // Make sure an empty rect is returned |
335 | 0 | EXPECT_TRUE(dest.IsEmpty() && dest.IsZeroArea()) << |
336 | 0 | "[10] Make sure an empty rect is returned"; |
337 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[10b] Should be finite"; |
338 | 0 | rect2.MoveByY(-rect2.Height()); |
339 | 0 |
|
340 | 0 | // Test against a rect with zero width or height |
341 | 0 | rect1.SetRect(100, 100, 100, 100); |
342 | 0 | rect2.SetRect(150, 100, 0, 100); |
343 | 0 | EXPECT_TRUE(!dest.IntersectRect(rect1, rect2) && dest.IsEmpty() && dest.IsZeroArea()) << |
344 | 0 | "[11] Intersection of rects with zero width or height should be empty"; |
345 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[11b] Should be finite"; |
346 | 0 |
|
347 | 0 | // Tests against a rect with negative width or height |
348 | 0 | // |
349 | 0 |
|
350 | 0 | // Test against a rect with negative width |
351 | 0 | rect1.SetRect(100, 100, 100, 100); |
352 | 0 | rect2.SetRect(100, 100, -100, 100); |
353 | 0 | EXPECT_TRUE(!dest.IntersectRect(rect1, rect2) && dest.IsEmpty() && dest.IsZeroArea()) << |
354 | 0 | "[12] Intersection of rects with negative width or height should be empty"; |
355 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[12b] Should be finite"; |
356 | 0 |
|
357 | 0 | // Those two rects exactly overlap in some way... |
358 | 0 | // but we still want to return an empty rect |
359 | 0 | rect1.SetRect(100, 100, 100, 100); |
360 | 0 | rect2.SetRect(200, 200, -100, -100); |
361 | 0 | EXPECT_TRUE(!dest.IntersectRect(rect1, rect2) && dest.IsEmpty() && dest.IsZeroArea()) << |
362 | 0 | "[13] Intersection of rects with negative width or height should be empty"; |
363 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[13b] Should be finite"; |
364 | 0 |
|
365 | 0 | // Test against two identical rects with negative height |
366 | 0 | rect1.SetRect(100, 100, 100, -100); |
367 | 0 | rect2.SetRect(100, 100, 100, -100); |
368 | 0 | EXPECT_TRUE(!dest.IntersectRect(rect1, rect2) && dest.IsEmpty() && dest.IsZeroArea()) << |
369 | 0 | "[14] Intersection of rects with negative width or height should be empty"; |
370 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[14b] Should be finite"; |
371 | 0 |
|
372 | 0 | return true; |
373 | 0 | } Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestIntersection<nsRect>() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestIntersection<mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> >() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestIntersection<mozilla::gfx::RectTyped<mozilla::gfx::UnknownUnits, double> >() |
374 | | |
375 | | template <class RectType> |
376 | | static bool |
377 | | TestUnion() |
378 | 0 | { |
379 | 0 | RectType rect1; |
380 | 0 | RectType rect2(10, 10, 50, 50); |
381 | 0 | RectType dest; |
382 | 0 |
|
383 | 0 | // Check the case where the receiver is an empty rect |
384 | 0 | rect1.SetEmpty(); |
385 | 0 | dest.UnionRect(rect1, rect2); |
386 | 0 | EXPECT_TRUE(!dest.IsEmpty() && !dest.IsZeroArea() && dest.IsEqualInterior(rect2)) << |
387 | 0 | "[1] Check the case where the receiver is an empty rect"; |
388 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[1b] Should be finite"; |
389 | 0 |
|
390 | 0 | // Check the case where the source rect is an empty rect |
391 | 0 | rect1 = rect2; |
392 | 0 | rect2.SetEmpty(); |
393 | 0 | dest.UnionRect(rect1, rect2); |
394 | 0 | EXPECT_TRUE(!dest.IsEmpty() && !dest.IsZeroArea() && dest.IsEqualInterior(rect1)) << |
395 | 0 | "[2] Check the case where the source rect is an empty rect"; |
396 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[2b] Should be finite"; |
397 | 0 |
|
398 | 0 | // Test the case where both rects are empty |
399 | 0 | rect1.SetEmpty(); |
400 | 0 | rect2.SetEmpty(); |
401 | 0 | dest.UnionRect(rect1, rect2); |
402 | 0 | EXPECT_TRUE(dest.IsEmpty() && dest.IsZeroArea()) << |
403 | 0 | "[3] Test the case where both rects are empty"; |
404 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[3b] Should be finite"; |
405 | 0 |
|
406 | 0 | // Test union case where the two rects don't overlap at all |
407 | 0 | rect1.SetRect(10, 10, 50, 50); |
408 | 0 | rect2.SetRect(100, 100, 50, 50); |
409 | 0 | dest.UnionRect(rect1, rect2); |
410 | 0 | EXPECT_TRUE(!dest.IsEmpty() && !dest.IsZeroArea() && |
411 | 0 | (dest.IsEqualInterior(RectType(rect1.X(), rect1.Y(), rect2.XMost() - rect1.X(), rect2.YMost() - rect1.Y())))) << |
412 | 0 | "[4] Test union case where the two rects don't overlap at all"; |
413 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[4b] Should be finite"; |
414 | 0 |
|
415 | 0 | // Test union case where the two rects overlap |
416 | 0 | rect1.SetRect(30, 30, 50, 50); |
417 | 0 | rect2.SetRect(10, 10, 50, 50); |
418 | 0 | dest.UnionRect(rect1, rect2); |
419 | 0 | EXPECT_TRUE(!dest.IsEmpty() && !dest.IsZeroArea() && |
420 | 0 | (dest.IsEqualInterior(RectType(rect2.X(), rect2.Y(), rect1.XMost() - rect2.X(), rect1.YMost() - rect2.Y())))) << |
421 | 0 | "[5] Test union case where the two rects overlap"; |
422 | 0 | EXPECT_TRUE(dest.IsFinite()) << "[5b] Should be finite"; |
423 | 0 |
|
424 | 0 | return true; |
425 | 0 | } Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestUnion<nsRect>() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestUnion<mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> >() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestUnion<mozilla::gfx::RectTyped<mozilla::gfx::UnknownUnits, double> >() |
426 | | |
427 | | static bool |
428 | | TestFiniteGfx() |
429 | 0 | { |
430 | 0 | float posInf = std::numeric_limits<float>::infinity(); |
431 | 0 | float negInf = -std::numeric_limits<float>::infinity(); |
432 | 0 | float justNaN = std::numeric_limits<float>::quiet_NaN(); |
433 | 0 |
|
434 | 0 | gfxFloat values[4] = {5.0, 10.0, 15.0, 20.0}; |
435 | 0 |
|
436 | 0 | // Try the "non-finite" values for x, y, width, height, one at a time |
437 | 0 | for (int i=0; i<4; i+=1) { |
438 | 0 | values[i] = posInf; |
439 | 0 | gfxRect rectPosInf(values[0], values[1], values[2], values[3]); |
440 | 0 | EXPECT_FALSE(rectPosInf.IsFinite()) << "For +inf (" << values[0] << "," << values[1] << "," << values[2] << "," << values[3] << ")"; |
441 | 0 |
|
442 | 0 | values[i] = negInf; |
443 | 0 | gfxRect rectNegInf(values[0], values[1], values[2], values[3]); |
444 | 0 | EXPECT_FALSE(rectNegInf.IsFinite()) << "For -inf (" << values[0] << "," << values[1] << "," << values[2] << "," << values[3] << ")"; |
445 | 0 |
|
446 | 0 | values[i] = justNaN; |
447 | 0 | gfxRect rectNaN(values[0], values[1], values[2], values[3]); |
448 | 0 | EXPECT_FALSE(rectNaN.IsFinite()) << "For NaN (" << values[0] << "," << values[1] << "," << values[2] << "," << values[3] << ")"; |
449 | 0 |
|
450 | 0 | // Reset to a finite value... |
451 | 0 | values[i] = 5.0*i; |
452 | 0 | } |
453 | 0 |
|
454 | 0 | return true; |
455 | 0 | } |
456 | | |
457 | | // We want to test nsRect values that are still in range but where |
458 | | // the implementation is at risk of overflowing |
459 | | template <class RectType> |
460 | | static bool |
461 | | TestBug1135677() |
462 | 0 | { |
463 | 0 | RectType rect1(1073741344, 1073741344, 1073756696, 1073819936); |
464 | 0 | RectType rect2(1073741820, 1073741820, 14400, 77640); |
465 | 0 | RectType dest; |
466 | 0 |
|
467 | 0 | dest = rect1.Intersect(rect2); |
468 | 0 |
|
469 | 0 | EXPECT_TRUE(dest.IsEqualRect(1073741820, 1073741820, 14400, 77640)) << |
470 | 0 | "[1] Operation should not overflow internally."; |
471 | 0 |
|
472 | 0 | return true; |
473 | 0 | } Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestBug1135677<nsRect>() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestBug1135677<mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> >() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestBug1135677<mozilla::gfx::RectTyped<mozilla::gfx::UnknownUnits, double> >() |
474 | | |
475 | | template <class RectType> |
476 | | static bool |
477 | | TestSetWH() |
478 | 0 | { |
479 | 0 | RectType rect(1, 2, 3, 4); |
480 | 0 | EXPECT_TRUE(rect.IsEqualRect(1, 2, 3, 4)); |
481 | 0 | rect.SetWidth(13); |
482 | 0 | EXPECT_TRUE(rect.IsEqualRect(1, 2, 13, 4)); |
483 | 0 | rect.SetHeight(14); |
484 | 0 | EXPECT_TRUE(rect.IsEqualRect(1, 2, 13, 14)); |
485 | 0 | rect.SizeTo(23, 24); |
486 | 0 | EXPECT_TRUE(rect.IsEqualRect(1, 2, 23, 24)); |
487 | 0 | return true; |
488 | 0 | } Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestSetWH<nsRect>() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestSetWH<mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> >() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestSetWH<mozilla::gfx::RectTyped<mozilla::gfx::UnknownUnits, double> >() |
489 | | |
490 | | template <class RectType> |
491 | | static bool |
492 | | TestSwap() |
493 | 0 | { |
494 | 0 | RectType rect(1, 2, 3, 4); |
495 | 0 | EXPECT_TRUE(rect.IsEqualRect(1, 2, 3, 4)); |
496 | 0 | rect.Swap(); |
497 | 0 | EXPECT_TRUE(rect.IsEqualRect(2, 1, 4, 3)); |
498 | 0 | return true; |
499 | 0 | } Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestSwap<nsRect>() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestSwap<mozilla::gfx::IntRectTyped<mozilla::gfx::UnknownUnits> >() Unexecuted instantiation: Unified_cpp_gfx_tests_gtest1.cpp:bool TestSwap<mozilla::gfx::RectTyped<mozilla::gfx::UnknownUnits, double> >() |
500 | | |
501 | | static void |
502 | | TestIntersectionLogicalHelper(nscoord x1, nscoord y1, nscoord w1, nscoord h1, |
503 | | nscoord x2, nscoord y2, nscoord w2, nscoord h2, |
504 | | nscoord xR, nscoord yR, nscoord wR, nscoord hR, |
505 | | bool isNonEmpty) |
506 | 0 | { |
507 | 0 | nsRect rect1(x1, y1, w1, h1); |
508 | 0 | nsRect rect2(x2, y2, w2, h2); |
509 | 0 | nsRect rectDebug; |
510 | 0 | EXPECT_TRUE(isNonEmpty == rectDebug.IntersectRect(rect1, rect2)); |
511 | 0 | EXPECT_TRUE(rectDebug.IsEqualEdges(nsRect(xR, yR, wR, hR))); |
512 | 0 |
|
513 | 0 | mozilla::LogicalRect r1(mozilla::WritingMode(), rect1.X(), rect1.Y(), rect1.Width(), rect1.Height()); |
514 | 0 | mozilla::LogicalRect r2(mozilla::WritingMode(), rect2.X(), rect2.Y(), rect2.Width(), rect2.Height()); |
515 | 0 | EXPECT_TRUE(isNonEmpty == r1.IntersectRect(r1, r2)); |
516 | 0 | EXPECT_TRUE(rectDebug.IsEqualEdges(nsRect(r1.IStart(WritingMode()), r1.BStart(WritingMode()), |
517 | 0 | r1.ISize(WritingMode()), r1.BSize(WritingMode())))); |
518 | 0 |
|
519 | 0 | mozilla::LogicalRect r3(mozilla::WritingMode(), rect1.X(), rect1.Y(), rect1.Width(), rect1.Height()); |
520 | 0 | mozilla::LogicalRect r4(mozilla::WritingMode(), rect2.X(), rect2.Y(), rect2.Width(), rect2.Height()); |
521 | 0 | EXPECT_TRUE(isNonEmpty == r4.IntersectRect(r3, r4)); |
522 | 0 | EXPECT_TRUE(rectDebug.IsEqualEdges(nsRect(r4.IStart(WritingMode()), r4.BStart(WritingMode()), |
523 | 0 | r4.ISize(WritingMode()), r4.BSize(WritingMode())))); |
524 | 0 |
|
525 | 0 | mozilla::LogicalRect r5(mozilla::WritingMode(), rect1.X(), rect1.Y(), rect1.Width(), rect1.Height()); |
526 | 0 | mozilla::LogicalRect r6(mozilla::WritingMode(), rect2.X(), rect2.Y(), rect2.Width(), rect2.Height()); |
527 | 0 | mozilla::LogicalRect r7(mozilla::WritingMode(), 0, 0, 1, 1); |
528 | 0 | EXPECT_TRUE(isNonEmpty == r7.IntersectRect(r5, r6)); |
529 | 0 | EXPECT_TRUE(rectDebug.IsEqualEdges(nsRect(r7.IStart(WritingMode()), r7.BStart(WritingMode()), |
530 | 0 | r7.ISize(WritingMode()), r7.BSize(WritingMode())))); |
531 | 0 | } |
532 | | |
533 | | static void |
534 | | TestIntersectionLogical(nscoord x1, nscoord y1, nscoord w1, nscoord h1, |
535 | | nscoord x2, nscoord y2, nscoord w2, nscoord h2, |
536 | | nscoord xR, nscoord yR, nscoord wR, nscoord hR, |
537 | | bool isNonEmpty) |
538 | 0 | { |
539 | 0 | TestIntersectionLogicalHelper(x1, y1, w1, h1, x2, y2, w2, h2, xR, yR, wR, hR, isNonEmpty); |
540 | 0 | TestIntersectionLogicalHelper(x2, y2, w2, h2, x1, y1, w1, h1, xR, yR, wR, hR, isNonEmpty); |
541 | 0 | } |
542 | | |
543 | | TEST(Gfx, Logical) |
544 | 0 | { |
545 | 0 | TestIntersectionLogical(578, 0, 2650, 1152, 1036, 0, 2312, 1, 1036, 0, 2192, 1, true); |
546 | 0 | TestIntersectionLogical(0, 0, 1000, 1000, 500, 500, 1000, 1000, 500, 500, 500, 500, true); |
547 | 0 | TestIntersectionLogical(100, 200, 300, 400, 50, 250, 100, 100, 100, 250, 50, 100, true); |
548 | 0 | TestIntersectionLogical(0, 100, 200, 300, 300, 100, 100, 300, 300, 100, 0, 0, false); |
549 | 0 | } |
550 | | |
551 | 0 | TEST(Gfx, nsRect) { |
552 | 0 | TestConstructors<nsRect>(); |
553 | 0 | TestEqualityOperator<nsRect>(); |
554 | 0 | TestContainment<nsRect>(); |
555 | 0 | TestIntersects<nsRect>(); |
556 | 0 | TestIntersection<nsRect>(); |
557 | 0 | TestUnion<nsRect>(); |
558 | 0 | TestBug1135677<nsRect>(); |
559 | 0 | TestSetWH<nsRect>(); |
560 | 0 | TestSwap<nsRect>(); |
561 | 0 | } |
562 | | |
563 | 0 | TEST(Gfx, nsIntRect) { |
564 | 0 | TestConstructors<nsIntRect>(); |
565 | 0 | TestEqualityOperator<nsIntRect>(); |
566 | 0 | TestContainment<nsIntRect>(); |
567 | 0 | TestIntersects<nsIntRect>(); |
568 | 0 | TestIntersection<nsIntRect>(); |
569 | 0 | TestUnion<nsIntRect>(); |
570 | 0 | TestBug1135677<nsIntRect>(); |
571 | 0 | TestSetWH<nsIntRect>(); |
572 | 0 | TestSwap<nsIntRect>(); |
573 | 0 | } |
574 | | |
575 | 0 | TEST(Gfx, gfxRect) { |
576 | 0 | TestConstructors<gfxRect>(); |
577 | 0 | // Skip TestEqualityOperator<gfxRect>(); as gfxRect::operator== is private |
578 | 0 | TestContainment<gfxRect>(); |
579 | 0 | TestIntersects<gfxRect>(); |
580 | 0 | TestIntersection<gfxRect>(); |
581 | 0 | TestUnion<gfxRect>(); |
582 | 0 | TestBug1135677<gfxRect>(); |
583 | 0 | TestFiniteGfx(); |
584 | 0 | TestSetWH<gfxRect>(); |
585 | 0 | TestSwap<gfxRect>(); |
586 | 0 | } |
587 | | |