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 ***** */
48 nsRect
rect1(10, 20, 30, 40);
50 // Make sure the rectangle was properly initialized
51 if ((rect1
.x
!= 10) || (rect1
.y
!= 20) ||
52 (rect1
.width
!= 30) || (rect1
.height
!= 40)) {
53 printf("rect initialization failed!\n");
57 // Create a second rect using the copy constructor
60 // Make sure the rectangle was properly initialized
61 if ((rect2
.x
!= rect1
.x
) || (rect2
.y
!= rect1
.y
) ||
62 (rect2
.width
!= rect1
.width
) || (rect2
.height
!= rect1
.height
)) {
63 printf("rect copy constructor failed!\n");
71 TestEqualityOperator()
73 nsRect
rect1(10, 20, 30, 40);
76 // Test the equality operator
77 if (!(rect1
== rect2
)) {
78 printf("rect equality operator failed!\n");
82 // Test the inequality operator
84 printf("rect inequality operator failed!\n");
88 // Make sure that two empty rects are equal
91 if (!(rect1
== rect2
)) {
92 printf("rect equality operator failed for empty rects!\n");
102 nsRect
rect1(10, 10, 50, 50);
104 // Test the point containment methods
107 // Basic test of a point in the middle of the rect
108 if (!rect1
.Contains(rect1
.x
+ rect1
.width
/2, rect1
.y
+ rect1
.height
/2)) {
109 printf("point containment test #1 failed!\n");
113 // Test against a point at the left/top edges
114 if (!rect1
.Contains(rect1
.x
, rect1
.y
)) {
115 printf("point containment test #2 failed!\n");
119 // Test against a point at the right/bottom extents
120 if (rect1
.Contains(rect1
.XMost(), rect1
.YMost())) {
121 printf("point containment test #3 failed!\n");
125 // Test the rect containment methods
129 // Test against a rect that's the same as rect1
130 if (!rect1
.Contains(rect2
)) {
131 printf("rect containment test #1 failed!\n");
135 // Test against a rect whose left edge (only) is outside of rect1
137 if (rect1
.Contains(rect2
)) {
138 printf("rect containment test #2 failed!\n");
143 // Test against a rect whose top edge (only) is outside of rect1
145 if (rect1
.Contains(rect2
)) {
146 printf("rect containment test #3 failed!\n");
151 // Test against a rect whose right edge (only) is outside of rect1
153 if (rect1
.Contains(rect2
)) {
154 printf("rect containment test #2 failed!\n");
159 // Test against a rect whose bottom edge (only) is outside of rect1
161 if (rect1
.Contains(rect2
)) {
162 printf("rect containment test #3 failed!\n");
170 // Test the method that returns a boolean result but doesn't return a
175 nsRect
rect1(10, 10, 50, 50);
178 // Test against a rect that's the same as rect1
179 if (!rect1
.Intersects(rect2
)) {
180 printf("rect intersects test #1 failed!\n");
184 // Test against a rect that's enclosed by rect1
186 if (!rect1
.Contains(rect2
) || !rect1
.Intersects(rect2
)) {
187 printf("rect intersects test #2 failed!\n");
192 // Make sure inflate and deflate worked correctly
193 if (rect1
!= rect2
) {
194 printf("rect inflate or deflate failed!\n");
198 // Test against a rect that overlaps the left edge of rect1
200 if (!rect1
.Intersects(rect2
)) {
201 printf("rect containment test #3 failed!\n");
206 // Test against a rect that's outside of rect1 on the left
207 rect2
.x
-= rect2
.width
;
208 if (rect1
.Intersects(rect2
)) {
209 printf("rect containment test #4 failed!\n");
212 rect2
.x
+= rect2
.width
;
214 // Test against a rect that overlaps the top edge of rect1
216 if (!rect1
.Intersects(rect2
)) {
217 printf("rect containment test #5 failed!\n");
222 // Test against a rect that's outside of rect1 on the top
223 rect2
.y
-= rect2
.height
;
224 if (rect1
.Intersects(rect2
)) {
225 printf("rect containment test #6 failed!\n");
228 rect2
.y
+= rect2
.height
;
230 // Test against a rect that overlaps the right edge of rect1
232 if (!rect1
.Intersects(rect2
)) {
233 printf("rect containment test #7 failed!\n");
238 // Test against a rect that's outside of rect1 on the right
239 rect2
.x
+= rect2
.width
;
240 if (rect1
.Intersects(rect2
)) {
241 printf("rect containment test #8 failed!\n");
244 rect2
.x
-= rect2
.width
;
246 // Test against a rect that overlaps the bottom edge of rect1
248 if (!rect1
.Intersects(rect2
)) {
249 printf("rect containment test #9 failed!\n");
254 // Test against a rect that's outside of rect1 on the bottom
255 rect2
.y
+= rect2
.height
;
256 if (rect1
.Intersects(rect2
)) {
257 printf("rect containment test #10 failed!\n");
260 rect2
.y
-= rect2
.height
;
265 // Test the method that returns a boolean result and an intersection rect
269 nsRect
rect1(10, 10, 50, 50);
273 // Test against a rect that's the same as rect1
274 if (!dest
.IntersectRect(rect1
, rect2
) || (dest
!= rect1
)) {
275 printf("rect intersection test #1 failed!\n");
279 // Test against a rect that's enclosed by rect1
281 if (!dest
.IntersectRect(rect1
, rect2
) || (dest
!= rect2
)) {
282 printf("rect intersection test #2 failed!\n");
287 // Test against a rect that overlaps the left edge of rect1
289 if (!dest
.IntersectRect(rect1
, rect2
) ||
290 (dest
!= nsRect(rect1
.x
, rect1
.y
, rect1
.width
- 1, rect1
.height
))) {
291 printf("rect intersection test #3 failed!\n");
296 // Test against a rect that's outside of rect1 on the left
297 rect2
.x
-= rect2
.width
;
298 if (dest
.IntersectRect(rect1
, rect2
)) {
299 printf("rect intersection test #4 failed!\n");
302 // Make sure an empty rect is returned
303 if (!dest
.IsEmpty()) {
304 printf("rect intersection test #4 no empty rect!\n");
307 rect2
.x
+= rect2
.width
;
309 // Test against a rect that overlaps the top edge of rect1
311 if (!dest
.IntersectRect(rect1
, rect2
) ||
312 (dest
!= nsRect(rect1
.x
, rect1
.y
, rect1
.width
, rect1
.height
- 1))) {
313 printf("rect intersection test #5 failed!\n");
318 // Test against a rect that's outside of rect1 on the top
319 rect2
.y
-= rect2
.height
;
320 if (dest
.IntersectRect(rect1
, rect2
)) {
321 printf("rect intersection test #6 failed!\n");
324 // Make sure an empty rect is returned
325 if (!dest
.IsEmpty()) {
326 printf("rect intersection test #6 no empty rect!\n");
329 rect2
.y
+= rect2
.height
;
331 // Test against a rect that overlaps the right edge of rect1
333 if (!dest
.IntersectRect(rect1
, rect2
) ||
334 (dest
!= nsRect(rect1
.x
+ 1, rect1
.y
, rect1
.width
- 1, rect1
.height
))) {
335 printf("rect intersection test #7 failed!\n");
340 // Test against a rect that's outside of rect1 on the right
341 rect2
.x
+= rect2
.width
;
342 if (dest
.IntersectRect(rect1
, rect2
)) {
343 printf("rect intersection test #8 failed!\n");
346 // Make sure an empty rect is returned
347 if (!dest
.IsEmpty()) {
348 printf("rect intersection test #8 no empty rect!\n");
351 rect2
.x
-= rect2
.width
;
353 // Test against a rect that overlaps the bottom edge of rect1
355 if (!dest
.IntersectRect(rect1
, rect2
) ||
356 (dest
!= nsRect(rect1
.x
, rect1
.y
+ 1, rect1
.width
, rect1
.height
- 1))) {
357 printf("rect intersection test #9 failed!\n");
362 // Test against a rect that's outside of rect1 on the bottom
363 rect2
.y
+= rect2
.height
;
364 if (dest
.IntersectRect(rect1
, rect2
)) {
365 printf("rect intersection test #10 failed!\n");
368 // Make sure an empty rect is returned
369 if (!dest
.IsEmpty()) {
370 printf("rect intersection test #10 no empty rect!\n");
373 rect2
.y
-= rect2
.height
;
382 nsRect
rect2(10, 10, 50, 50);
385 // Check the case where the receiver is an empty rect
387 if (!dest
.UnionRect(rect1
, rect2
) || (dest
!= rect2
)) {
388 printf("rect union test #1 failed!\n");
392 // Check the case where the source rect is an empty rect
395 if (!dest
.UnionRect(rect1
, rect2
) || (dest
!= rect1
)) {
396 printf("rect union test #2 failed!\n");
400 // Test the case where both rects are empty. This should fail
403 if (dest
.UnionRect(rect1
, rect2
)) {
404 printf("rect union test #3 failed!\n");
408 // Test union case where the two rects don't overlap at all
409 rect1
.SetRect(10, 10, 50, 50);
410 rect2
.SetRect(100, 100, 50, 50);
411 if (!dest
.UnionRect(rect1
, rect2
) ||
412 (dest
!= nsRect(rect1
.x
, rect1
.y
, rect2
.XMost() - rect1
.x
, rect2
.YMost() - rect1
.y
))) {
413 printf("rect union test #4 failed!\n");
417 // Test union case where the two rects overlap
418 rect1
.SetRect(30, 30, 50, 50);
419 rect2
.SetRect(10, 10, 50, 50);
420 if (!dest
.UnionRect(rect1
, rect2
) ||
421 (dest
!= nsRect(rect2
.x
, rect2
.y
, rect1
.XMost() - rect2
.x
, rect1
.YMost() - rect2
.y
))) {
422 printf("rect union test #5 failed!\n");
429 int main(int argc
, char** argv
)
431 if (!TestConstructors())
434 if (!TestEqualityOperator())
437 if (!TestContainment())
440 if (!TestIntersects())
443 if (!TestIntersection())