1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "TestHarness.h"
13 template <class RectType
>
18 RectType
rect1(10, 20, 30, 40);
20 // Make sure the rectangle was properly initialized
21 if ((rect1
.x
!= 10) || (rect1
.y
!= 20) ||
22 (rect1
.width
!= 30) || (rect1
.height
!= 40)) {
23 fail("[1] Make sure the rectangle was properly initialized with constructor");
27 // Create a second rect using the copy constructor
28 RectType
rect2(rect1
);
30 // Make sure the rectangle was properly initialized
31 if ((rect2
.x
!= rect1
.x
) || (rect2
.y
!= rect1
.y
) ||
32 (rect2
.width
!= rect1
.width
) || (rect2
.height
!= rect1
.height
)) {
33 fail("[2] Make sure the rectangle was properly initialized with copy constructor");
37 passed("TestConstructors");
41 template <class RectType
>
43 TestEqualityOperator()
45 RectType
rect1(10, 20, 30, 40);
46 RectType
rect2(rect1
);
48 // Test the equality operator
49 if (!(rect1
== rect2
)) {
50 fail("[1] Test the equality operator");
54 // Test the inequality operator
56 fail("[2] Test the inequality operator");
60 // Make sure that two empty rects are equal
63 if (!(rect1
== rect2
)) {
64 fail("[3] Make sure that two empty rects are equal");
68 passed("TestEqualityOperator");
72 template <class RectType
>
76 RectType
rect1(10, 10, 50, 50);
78 // Test the point containment methods
81 // Basic test of a point in the middle of the rect
82 if (!rect1
.Contains(rect1
.x
+ rect1
.width
/2, rect1
.y
+ rect1
.height
/2)) {
83 fail("[1] Basic test of a point in the middle of the rect");
87 // Test against a point at the left/top edges
88 if (!rect1
.Contains(rect1
.x
, rect1
.y
)) {
89 fail("[2] Test against a point at the left/top edges");
93 // Test against a point at the right/bottom extents
94 if (rect1
.Contains(rect1
.XMost(), rect1
.YMost())) {
95 fail("[3] Test against a point at the right/bottom extents");
99 // Test the rect containment methods
101 RectType
rect2(rect1
);
103 // Test against a rect that's the same as rect1
104 if (!rect1
.Contains(rect2
)) {
105 fail("[4] Test against a rect that's the same as rect1");
109 // Test against a rect whose left edge (only) is outside of rect1
111 if (rect1
.Contains(rect2
)) {
112 fail("[5] Test against a rect whose left edge (only) is outside of rect1");
117 // Test against a rect whose top edge (only) is outside of rect1
119 if (rect1
.Contains(rect2
)) {
120 fail("[6] Test against a rect whose top edge (only) is outside of rect1");
125 // Test against a rect whose right edge (only) is outside of rect1
127 if (rect1
.Contains(rect2
)) {
128 fail("[7] Test against a rect whose right edge (only) is outside of rect1");
133 // Test against a rect whose bottom edge (only) is outside of rect1
135 if (rect1
.Contains(rect2
)) {
136 fail("[8] Test against a rect whose bottom edge (only) is outside of rect1");
141 passed("TestContainment");
145 // Test the method that returns a boolean result but doesn't return a
147 template <class RectType
>
151 RectType
rect1(10, 10, 50, 50);
152 RectType
rect2(rect1
);
154 // Test against a rect that's the same as rect1
155 if (!rect1
.Intersects(rect2
)) {
156 fail("[1] Test against a rect that's the same as rect1");
160 // Test against a rect that's enclosed by rect1
161 rect2
.Inflate(-1, -1);
162 if (!rect1
.Contains(rect2
) || !rect1
.Intersects(rect2
)) {
163 fail("[2] Test against a rect that's enclosed by rect1");
168 // Make sure inflate and deflate worked correctly
169 if (rect1
!= rect2
) {
170 fail("[3] Make sure inflate and deflate worked correctly");
174 // Test against a rect that overlaps the left edge of rect1
176 if (!rect1
.Intersects(rect2
)) {
177 fail("[4] Test against a rect that overlaps the left edge of rect1");
182 // Test against a rect that's outside of rect1 on the left
183 rect2
.x
-= rect2
.width
;
184 if (rect1
.Intersects(rect2
)) {
185 fail("[5] Test against a rect that's outside of rect1 on the left");
188 rect2
.x
+= rect2
.width
;
190 // Test against a rect that overlaps the top edge of rect1
192 if (!rect1
.Intersects(rect2
)) {
193 fail("[6] Test against a rect that overlaps the top edge of rect1");
198 // Test against a rect that's outside of rect1 on the top
199 rect2
.y
-= rect2
.height
;
200 if (rect1
.Intersects(rect2
)) {
201 fail("[7] Test against a rect that's outside of rect1 on the top");
204 rect2
.y
+= rect2
.height
;
206 // Test against a rect that overlaps the right edge of rect1
208 if (!rect1
.Intersects(rect2
)) {
209 fail("[8] Test against a rect that overlaps the right edge of rect1");
214 // Test against a rect that's outside of rect1 on the right
215 rect2
.x
+= rect2
.width
;
216 if (rect1
.Intersects(rect2
)) {
217 fail("[9] Test against a rect that's outside of rect1 on the right");
220 rect2
.x
-= rect2
.width
;
222 // Test against a rect that overlaps the bottom edge of rect1
224 if (!rect1
.Intersects(rect2
)) {
225 fail("[10] Test against a rect that overlaps the bottom edge of rect1");
230 // Test against a rect that's outside of rect1 on the bottom
231 rect2
.y
+= rect2
.height
;
232 if (rect1
.Intersects(rect2
)) {
233 fail("[11] Test against a rect that's outside of rect1 on the bottom");
236 rect2
.y
-= rect2
.height
;
238 passed("TestIntersects");
242 // Test the method that returns a boolean result and an intersection rect
243 template <class RectType
>
247 RectType
rect1(10, 10, 50, 50);
248 RectType
rect2(rect1
);
251 // Test against a rect that's the same as rect1
252 if (!dest
.IntersectRect(rect1
, rect2
) || (dest
!= rect1
)) {
253 fail("[1] Test against a rect that's the same as rect1");
257 // Test against a rect that's enclosed by rect1
258 rect2
.Inflate(-1, -1);
259 if (!dest
.IntersectRect(rect1
, rect2
) || (dest
!= rect2
)) {
260 fail("[2] Test against a rect that's enclosed by rect1");
265 // Test against a rect that overlaps the left edge of rect1
267 if (!dest
.IntersectRect(rect1
, rect2
) ||
268 (dest
!= RectType(rect1
.x
, rect1
.y
, rect1
.width
- 1, rect1
.height
))) {
269 fail("[3] Test against a rect that overlaps the left edge of rect1");
274 // Test against a rect that's outside of rect1 on the left
275 rect2
.x
-= rect2
.width
;
276 if (dest
.IntersectRect(rect1
, rect2
)) {
277 fail("[4] Test against a rect that's outside of rect1 on the left");
280 // Make sure an empty rect is returned
281 if (!dest
.IsEmpty()) {
282 fail("[4] Make sure an empty rect is returned");
285 rect2
.x
+= rect2
.width
;
287 // Test against a rect that overlaps the top edge of rect1
289 if (!dest
.IntersectRect(rect1
, rect2
) ||
290 (dest
!= RectType(rect1
.x
, rect1
.y
, rect1
.width
, rect1
.height
- 1))) {
291 fail("[5] Test against a rect that overlaps the top edge of rect1");
296 // Test against a rect that's outside of rect1 on the top
297 rect2
.y
-= rect2
.height
;
298 if (dest
.IntersectRect(rect1
, rect2
)) {
299 fail("[6] Test against a rect that's outside of rect1 on the top");
302 // Make sure an empty rect is returned
303 if (!dest
.IsEmpty()) {
304 fail("[6] Make sure an empty rect is returned");
307 rect2
.y
+= rect2
.height
;
309 // Test against a rect that overlaps the right edge of rect1
311 if (!dest
.IntersectRect(rect1
, rect2
) ||
312 (dest
!= RectType(rect1
.x
+ 1, rect1
.y
, rect1
.width
- 1, rect1
.height
))) {
313 fail("[7] Test against a rect that overlaps the right edge of rect1");
318 // Test against a rect that's outside of rect1 on the right
319 rect2
.x
+= rect2
.width
;
320 if (dest
.IntersectRect(rect1
, rect2
)) {
321 fail("[8] Test against a rect that's outside of rect1 on the right");
324 // Make sure an empty rect is returned
325 if (!dest
.IsEmpty()) {
326 fail("[8] Make sure an empty rect is returned");
329 rect2
.x
-= rect2
.width
;
331 // Test against a rect that overlaps the bottom edge of rect1
333 if (!dest
.IntersectRect(rect1
, rect2
) ||
334 (dest
!= RectType(rect1
.x
, rect1
.y
+ 1, rect1
.width
, rect1
.height
- 1))) {
335 fail("[9] Test against a rect that overlaps the bottom edge of rect1");
340 // Test against a rect that's outside of rect1 on the bottom
341 rect2
.y
+= rect2
.height
;
342 if (dest
.IntersectRect(rect1
, rect2
)) {
343 fail("[10] Test against a rect that's outside of rect1 on the bottom");
346 // Make sure an empty rect is returned
347 if (!dest
.IsEmpty()) {
348 fail("[10] Make sure an empty rect is returned");
351 rect2
.y
-= rect2
.height
;
353 // Test against a rect with zero width or height
354 rect1
.SetRect(100, 100, 100, 100);
355 rect2
.SetRect(150, 100, 0, 100);
356 if (dest
.IntersectRect(rect1
, rect2
) || !dest
.IsEmpty()) {
357 fail("[11] Intersection of rects with zero width or height should be empty");
361 // Tests against a rect with negative width or height
364 // Test against a rect with negative width
365 rect1
.SetRect(100, 100, 100, 100);
366 rect2
.SetRect(100, 100, -100, 100);
367 if (dest
.IntersectRect(rect1
, rect2
) || !dest
.IsEmpty()) {
368 fail("[12] Intersection of rects with negative width or height should be empty");
372 // Those two rects exactly overlap in some way...
373 // but we still want to return an empty rect
374 rect1
.SetRect(100, 100, 100, 100);
375 rect2
.SetRect(200, 200, -100, -100);
376 if (dest
.IntersectRect(rect1
, rect2
) || !dest
.IsEmpty()) {
377 fail("[13] Intersection of rects with negative width or height should be empty");
381 // Test against two identical rects with negative height
382 rect1
.SetRect(100, 100, 100, -100);
383 rect2
.SetRect(100, 100, 100, -100);
384 if (dest
.IntersectRect(rect1
, rect2
) || !dest
.IsEmpty()) {
385 fail("[14] Intersection of rects with negative width or height should be empty");
389 passed("TestIntersection");
393 template <class RectType
>
398 RectType
rect2(10, 10, 50, 50);
401 // Check the case where the receiver is an empty rect
403 if (!dest
.UnionRect(rect1
, rect2
) || (dest
!= rect2
)) {
404 fail("[1] Check the case where the receiver is an empty rect");
408 // Check the case where the source rect is an empty rect
411 if (!dest
.UnionRect(rect1
, rect2
) || (dest
!= rect1
)) {
412 fail("[2] Check the case where the source rect is an empty rect");
416 // Test the case where both rects are empty
419 if (dest
.UnionRect(rect1
, rect2
)) {
420 fail("[3] Test the case where both rects are empty");
424 // Test union case where the two rects don't overlap at all
425 rect1
.SetRect(10, 10, 50, 50);
426 rect2
.SetRect(100, 100, 50, 50);
427 if (!dest
.UnionRect(rect1
, rect2
) ||
428 (dest
!= RectType(rect1
.x
, rect1
.y
, rect2
.XMost() - rect1
.x
, rect2
.YMost() - rect1
.y
))) {
429 fail("[4] Test union case where the two rects don't overlap at all");
433 // Test union case where the two rects overlap
434 rect1
.SetRect(30, 30, 50, 50);
435 rect2
.SetRect(10, 10, 50, 50);
436 if (!dest
.UnionRect(rect1
, rect2
) ||
437 (dest
!= RectType(rect2
.x
, rect2
.y
, rect1
.XMost() - rect2
.x
, rect1
.YMost() - rect2
.y
))) {
438 fail("[5] Test union case where the two rects overlap");
446 int main(int argc
, char** argv
)
448 ScopedXPCOM
xpcom("TestRect");
454 //-----------------------
457 printf("===== nsRect tests =====\n");
459 if (!TestConstructors
<nsRect
>())
462 if (!TestEqualityOperator
<nsRect
>())
465 if (!TestContainment
<nsRect
>())
468 if (!TestIntersects
<nsRect
>())
471 if (!TestIntersection
<nsRect
>())
474 if (!TestUnion
<nsRect
>())
477 //-----------------------
480 printf("===== nsIntRect tests =====\n");
482 if (!TestConstructors
<nsIntRect
>())
485 if (!TestEqualityOperator
<nsIntRect
>())
488 if (!TestContainment
<nsIntRect
>())
491 if (!TestIntersects
<nsIntRect
>())
494 if (!TestIntersection
<nsIntRect
>())
497 if (!TestUnion
<nsIntRect
>())