1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestHarness.h"
45 template <class RectType
>
50 RectType
rect1(10, 20, 30, 40);
52 // Make sure the rectangle was properly initialized
53 if ((rect1
.x
!= 10) || (rect1
.y
!= 20) ||
54 (rect1
.width
!= 30) || (rect1
.height
!= 40)) {
55 fail("[1] Make sure the rectangle was properly initialized with constructor");
59 // Create a second rect using the copy constructor
60 RectType
rect2(rect1
);
62 // Make sure the rectangle was properly initialized
63 if ((rect2
.x
!= rect1
.x
) || (rect2
.y
!= rect1
.y
) ||
64 (rect2
.width
!= rect1
.width
) || (rect2
.height
!= rect1
.height
)) {
65 fail("[2] Make sure the rectangle was properly initialized with copy constructor");
69 passed("TestConstructors");
73 template <class RectType
>
75 TestEqualityOperator()
77 RectType
rect1(10, 20, 30, 40);
78 RectType
rect2(rect1
);
80 // Test the equality operator
81 if (!(rect1
== rect2
)) {
82 fail("[1] Test the equality operator");
86 // Test the inequality operator
88 fail("[2] Test the inequality operator");
92 // Make sure that two empty rects are equal
95 if (!(rect1
== rect2
)) {
96 fail("[3] Make sure that two empty rects are equal");
100 passed("TestEqualityOperator");
104 template <class RectType
>
108 RectType
rect1(10, 10, 50, 50);
110 // Test the point containment methods
113 // Basic test of a point in the middle of the rect
114 if (!rect1
.Contains(rect1
.x
+ rect1
.width
/2, rect1
.y
+ rect1
.height
/2)) {
115 fail("[1] Basic test of a point in the middle of the rect");
119 // Test against a point at the left/top edges
120 if (!rect1
.Contains(rect1
.x
, rect1
.y
)) {
121 fail("[2] Test against a point at the left/top edges");
125 // Test against a point at the right/bottom extents
126 if (rect1
.Contains(rect1
.XMost(), rect1
.YMost())) {
127 fail("[3] Test against a point at the right/bottom extents");
131 // Test the rect containment methods
133 RectType
rect2(rect1
);
135 // Test against a rect that's the same as rect1
136 if (!rect1
.Contains(rect2
)) {
137 fail("[4] Test against a rect that's the same as rect1");
141 // Test against a rect whose left edge (only) is outside of rect1
143 if (rect1
.Contains(rect2
)) {
144 fail("[5] Test against a rect whose left edge (only) is outside of rect1");
149 // Test against a rect whose top edge (only) is outside of rect1
151 if (rect1
.Contains(rect2
)) {
152 fail("[6] Test against a rect whose top edge (only) is outside of rect1");
157 // Test against a rect whose right edge (only) is outside of rect1
159 if (rect1
.Contains(rect2
)) {
160 fail("[7] Test against a rect whose right edge (only) is outside of rect1");
165 // Test against a rect whose bottom edge (only) is outside of rect1
167 if (rect1
.Contains(rect2
)) {
168 fail("[8] Test against a rect whose bottom edge (only) is outside of rect1");
173 passed("TestContainment");
177 // Test the method that returns a boolean result but doesn't return a
179 template <class RectType
>
183 RectType
rect1(10, 10, 50, 50);
184 RectType
rect2(rect1
);
186 // Test against a rect that's the same as rect1
187 if (!rect1
.Intersects(rect2
)) {
188 fail("[1] Test against a rect that's the same as rect1");
192 // Test against a rect that's enclosed by rect1
193 rect2
.Inflate(-1, -1);
194 if (!rect1
.Contains(rect2
) || !rect1
.Intersects(rect2
)) {
195 fail("[2] Test against a rect that's enclosed by rect1");
200 // Make sure inflate and deflate worked correctly
201 if (rect1
!= rect2
) {
202 fail("[3] Make sure inflate and deflate worked correctly");
206 // Test against a rect that overlaps the left edge of rect1
208 if (!rect1
.Intersects(rect2
)) {
209 fail("[4] Test against a rect that overlaps the left edge of rect1");
214 // Test against a rect that's outside of rect1 on the left
215 rect2
.x
-= rect2
.width
;
216 if (rect1
.Intersects(rect2
)) {
217 fail("[5] Test against a rect that's outside of rect1 on the left");
220 rect2
.x
+= rect2
.width
;
222 // Test against a rect that overlaps the top edge of rect1
224 if (!rect1
.Intersects(rect2
)) {
225 fail("[6] Test against a rect that overlaps the top edge of rect1");
230 // Test against a rect that's outside of rect1 on the top
231 rect2
.y
-= rect2
.height
;
232 if (rect1
.Intersects(rect2
)) {
233 fail("[7] Test against a rect that's outside of rect1 on the top");
236 rect2
.y
+= rect2
.height
;
238 // Test against a rect that overlaps the right edge of rect1
240 if (!rect1
.Intersects(rect2
)) {
241 fail("[8] Test against a rect that overlaps the right edge of rect1");
246 // Test against a rect that's outside of rect1 on the right
247 rect2
.x
+= rect2
.width
;
248 if (rect1
.Intersects(rect2
)) {
249 fail("[9] Test against a rect that's outside of rect1 on the right");
252 rect2
.x
-= rect2
.width
;
254 // Test against a rect that overlaps the bottom edge of rect1
256 if (!rect1
.Intersects(rect2
)) {
257 fail("[10] Test against a rect that overlaps the bottom edge of rect1");
262 // Test against a rect that's outside of rect1 on the bottom
263 rect2
.y
+= rect2
.height
;
264 if (rect1
.Intersects(rect2
)) {
265 fail("[11] Test against a rect that's outside of rect1 on the bottom");
268 rect2
.y
-= rect2
.height
;
270 passed("TestIntersects");
274 // Test the method that returns a boolean result and an intersection rect
275 template <class RectType
>
279 RectType
rect1(10, 10, 50, 50);
280 RectType
rect2(rect1
);
283 // Test against a rect that's the same as rect1
284 if (!dest
.IntersectRect(rect1
, rect2
) || (dest
!= rect1
)) {
285 fail("[1] Test against a rect that's the same as rect1");
289 // Test against a rect that's enclosed by rect1
290 rect2
.Inflate(-1, -1);
291 if (!dest
.IntersectRect(rect1
, rect2
) || (dest
!= rect2
)) {
292 fail("[2] Test against a rect that's enclosed by rect1");
297 // Test against a rect that overlaps the left edge of rect1
299 if (!dest
.IntersectRect(rect1
, rect2
) ||
300 (dest
!= RectType(rect1
.x
, rect1
.y
, rect1
.width
- 1, rect1
.height
))) {
301 fail("[3] Test against a rect that overlaps the left edge of rect1");
306 // Test against a rect that's outside of rect1 on the left
307 rect2
.x
-= rect2
.width
;
308 if (dest
.IntersectRect(rect1
, rect2
)) {
309 fail("[4] Test against a rect that's outside of rect1 on the left");
312 // Make sure an empty rect is returned
313 if (!dest
.IsEmpty()) {
314 fail("[4] Make sure an empty rect is returned");
317 rect2
.x
+= rect2
.width
;
319 // Test against a rect that overlaps the top edge of rect1
321 if (!dest
.IntersectRect(rect1
, rect2
) ||
322 (dest
!= RectType(rect1
.x
, rect1
.y
, rect1
.width
, rect1
.height
- 1))) {
323 fail("[5] Test against a rect that overlaps the top edge of rect1");
328 // Test against a rect that's outside of rect1 on the top
329 rect2
.y
-= rect2
.height
;
330 if (dest
.IntersectRect(rect1
, rect2
)) {
331 fail("[6] Test against a rect that's outside of rect1 on the top");
334 // Make sure an empty rect is returned
335 if (!dest
.IsEmpty()) {
336 fail("[6] Make sure an empty rect is returned");
339 rect2
.y
+= rect2
.height
;
341 // Test against a rect that overlaps the right edge of rect1
343 if (!dest
.IntersectRect(rect1
, rect2
) ||
344 (dest
!= RectType(rect1
.x
+ 1, rect1
.y
, rect1
.width
- 1, rect1
.height
))) {
345 fail("[7] Test against a rect that overlaps the right edge of rect1");
350 // Test against a rect that's outside of rect1 on the right
351 rect2
.x
+= rect2
.width
;
352 if (dest
.IntersectRect(rect1
, rect2
)) {
353 fail("[8] Test against a rect that's outside of rect1 on the right");
356 // Make sure an empty rect is returned
357 if (!dest
.IsEmpty()) {
358 fail("[8] Make sure an empty rect is returned");
361 rect2
.x
-= rect2
.width
;
363 // Test against a rect that overlaps the bottom edge of rect1
365 if (!dest
.IntersectRect(rect1
, rect2
) ||
366 (dest
!= RectType(rect1
.x
, rect1
.y
+ 1, rect1
.width
, rect1
.height
- 1))) {
367 fail("[9] Test against a rect that overlaps the bottom edge of rect1");
372 // Test against a rect that's outside of rect1 on the bottom
373 rect2
.y
+= rect2
.height
;
374 if (dest
.IntersectRect(rect1
, rect2
)) {
375 fail("[10] Test against a rect that's outside of rect1 on the bottom");
378 // Make sure an empty rect is returned
379 if (!dest
.IsEmpty()) {
380 fail("[10] Make sure an empty rect is returned");
383 rect2
.y
-= rect2
.height
;
385 // Test against a rect with zero width or height
386 rect1
.SetRect(100, 100, 100, 100);
387 rect2
.SetRect(150, 100, 0, 100);
388 if (dest
.IntersectRect(rect1
, rect2
) || !dest
.IsEmpty()) {
389 fail("[11] Intersection of rects with zero width or height should be empty");
393 // Tests against a rect with negative width or height
396 // Test against a rect with negative width
397 rect1
.SetRect(100, 100, 100, 100);
398 rect2
.SetRect(100, 100, -100, 100);
399 if (dest
.IntersectRect(rect1
, rect2
) || !dest
.IsEmpty()) {
400 fail("[12] Intersection of rects with negative width or height should be empty");
404 // Those two rects exactly overlap in some way...
405 // but we still want to return an empty rect
406 rect1
.SetRect(100, 100, 100, 100);
407 rect2
.SetRect(200, 200, -100, -100);
408 if (dest
.IntersectRect(rect1
, rect2
) || !dest
.IsEmpty()) {
409 fail("[13] Intersection of rects with negative width or height should be empty");
413 // Test against two identical rects with negative height
414 rect1
.SetRect(100, 100, 100, -100);
415 rect2
.SetRect(100, 100, 100, -100);
416 if (dest
.IntersectRect(rect1
, rect2
) || !dest
.IsEmpty()) {
417 fail("[14] Intersection of rects with negative width or height should be empty");
421 passed("TestIntersection");
425 template <class RectType
>
430 RectType
rect2(10, 10, 50, 50);
433 // Check the case where the receiver is an empty rect
435 if (!dest
.UnionRect(rect1
, rect2
) || (dest
!= rect2
)) {
436 fail("[1] Check the case where the receiver is an empty rect");
440 // Check the case where the source rect is an empty rect
443 if (!dest
.UnionRect(rect1
, rect2
) || (dest
!= rect1
)) {
444 fail("[2] Check the case where the source rect is an empty rect");
448 // Test the case where both rects are empty
451 if (dest
.UnionRect(rect1
, rect2
)) {
452 fail("[3] Test the case where both rects are empty");
456 // Test union case where the two rects don't overlap at all
457 rect1
.SetRect(10, 10, 50, 50);
458 rect2
.SetRect(100, 100, 50, 50);
459 if (!dest
.UnionRect(rect1
, rect2
) ||
460 (dest
!= RectType(rect1
.x
, rect1
.y
, rect2
.XMost() - rect1
.x
, rect2
.YMost() - rect1
.y
))) {
461 fail("[4] Test union case where the two rects don't overlap at all");
465 // Test union case where the two rects overlap
466 rect1
.SetRect(30, 30, 50, 50);
467 rect2
.SetRect(10, 10, 50, 50);
468 if (!dest
.UnionRect(rect1
, rect2
) ||
469 (dest
!= RectType(rect2
.x
, rect2
.y
, rect1
.XMost() - rect2
.x
, rect1
.YMost() - rect2
.y
))) {
470 fail("[5] Test union case where the two rects overlap");
478 int main(int argc
, char** argv
)
480 ScopedXPCOM
xpcom("TestRect");
486 //-----------------------
489 printf("===== nsRect tests =====\n");
491 if (!TestConstructors
<nsRect
>())
494 if (!TestEqualityOperator
<nsRect
>())
497 if (!TestContainment
<nsRect
>())
500 if (!TestIntersects
<nsRect
>())
503 if (!TestIntersection
<nsRect
>())
506 if (!TestUnion
<nsRect
>())
509 //-----------------------
512 printf("===== nsIntRect tests =====\n");
514 if (!TestConstructors
<nsIntRect
>())
517 if (!TestEqualityOperator
<nsIntRect
>())
520 if (!TestContainment
<nsIntRect
>())
523 if (!TestIntersects
<nsIntRect
>())
526 if (!TestIntersection
<nsIntRect
>())
529 if (!TestUnion
<nsIntRect
>())