Bug 470165 - Cleanup the GTK nsFilePicker code; r+sr=roc
[wine-gecko.git] / gfx / tests / TestRect.cpp
blobf549bed950603d8e7054986d384a1a678627a12b
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
13 * License.
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.
22 * Contributor(s):
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 "nsRect.h"
39 #include <stdio.h>
40 #ifdef XP_WIN
41 #include <windows.h>
42 #endif
44 static PRBool
45 TestConstructors()
47 // Create a rectangle
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");
54 return PR_FALSE;
57 // Create a second rect using the copy constructor
58 nsRect rect2(rect1);
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");
64 return PR_FALSE;
67 return PR_TRUE;
70 static PRBool
71 TestEqualityOperator()
73 nsRect rect1(10, 20, 30, 40);
74 nsRect rect2(rect1);
76 // Test the equality operator
77 if (!(rect1 == rect2)) {
78 printf("rect equality operator failed!\n");
79 return PR_FALSE;
82 // Test the inequality operator
83 if (rect1 != rect2) {
84 printf("rect inequality operator failed!\n");
85 return PR_FALSE;
88 // Make sure that two empty rects are equal
89 rect1.Empty();
90 rect2.Empty();
91 if (!(rect1 == rect2)) {
92 printf("rect equality operator failed for empty rects!\n");
93 return PR_FALSE;
96 return PR_TRUE;
99 static PRBool
100 TestContainment()
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");
110 return PR_FALSE;
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");
116 return PR_FALSE;
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");
122 return PR_FALSE;
125 // Test the rect containment methods
127 nsRect rect2(rect1);
129 // Test against a rect that's the same as rect1
130 if (!rect1.Contains(rect2)) {
131 printf("rect containment test #1 failed!\n");
132 return PR_FALSE;
135 // Test against a rect whose left edge (only) is outside of rect1
136 rect2.x--;
137 if (rect1.Contains(rect2)) {
138 printf("rect containment test #2 failed!\n");
139 return PR_FALSE;
141 rect2.x++;
143 // Test against a rect whose top edge (only) is outside of rect1
144 rect2.y--;
145 if (rect1.Contains(rect2)) {
146 printf("rect containment test #3 failed!\n");
147 return PR_FALSE;
149 rect2.y++;
151 // Test against a rect whose right edge (only) is outside of rect1
152 rect2.x++;
153 if (rect1.Contains(rect2)) {
154 printf("rect containment test #2 failed!\n");
155 return PR_FALSE;
157 rect2.x--;
159 // Test against a rect whose bottom edge (only) is outside of rect1
160 rect2.y++;
161 if (rect1.Contains(rect2)) {
162 printf("rect containment test #3 failed!\n");
163 return PR_FALSE;
165 rect2.y--;
167 return PR_TRUE;
170 // Test the method that returns a boolean result but doesn't return a
171 // a rectangle
172 static PRBool
173 TestIntersects()
175 nsRect rect1(10, 10, 50, 50);
176 nsRect rect2(rect1);
178 // Test against a rect that's the same as rect1
179 if (!rect1.Intersects(rect2)) {
180 printf("rect intersects test #1 failed!\n");
181 return PR_FALSE;
184 // Test against a rect that's enclosed by rect1
185 rect2.Deflate(1, 1);
186 if (!rect1.Contains(rect2) || !rect1.Intersects(rect2)) {
187 printf("rect intersects test #2 failed!\n");
188 return PR_FALSE;
190 rect2.Inflate(1, 1);
192 // Make sure inflate and deflate worked correctly
193 if (rect1 != rect2) {
194 printf("rect inflate or deflate failed!\n");
195 return PR_FALSE;
198 // Test against a rect that overlaps the left edge of rect1
199 rect2.x--;
200 if (!rect1.Intersects(rect2)) {
201 printf("rect containment test #3 failed!\n");
202 return PR_FALSE;
204 rect2.x++;
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");
210 return PR_FALSE;
212 rect2.x += rect2.width;
214 // Test against a rect that overlaps the top edge of rect1
215 rect2.y--;
216 if (!rect1.Intersects(rect2)) {
217 printf("rect containment test #5 failed!\n");
218 return PR_FALSE;
220 rect2.y++;
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");
226 return PR_FALSE;
228 rect2.y += rect2.height;
230 // Test against a rect that overlaps the right edge of rect1
231 rect2.x++;
232 if (!rect1.Intersects(rect2)) {
233 printf("rect containment test #7 failed!\n");
234 return PR_FALSE;
236 rect2.x--;
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");
242 return PR_FALSE;
244 rect2.x -= rect2.width;
246 // Test against a rect that overlaps the bottom edge of rect1
247 rect2.y++;
248 if (!rect1.Intersects(rect2)) {
249 printf("rect containment test #9 failed!\n");
250 return PR_FALSE;
252 rect2.y--;
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");
258 return PR_FALSE;
260 rect2.y -= rect2.height;
262 return PR_TRUE;
265 // Test the method that returns a boolean result and an intersection rect
266 static PRBool
267 TestIntersection()
269 nsRect rect1(10, 10, 50, 50);
270 nsRect rect2(rect1);
271 nsRect dest;
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");
276 return PR_FALSE;
279 // Test against a rect that's enclosed by rect1
280 rect2.Deflate(1, 1);
281 if (!dest.IntersectRect(rect1, rect2) || (dest != rect2)) {
282 printf("rect intersection test #2 failed!\n");
283 return PR_FALSE;
285 rect2.Inflate(1, 1);
287 // Test against a rect that overlaps the left edge of rect1
288 rect2.x--;
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");
292 return PR_FALSE;
294 rect2.x++;
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");
300 return PR_FALSE;
302 // Make sure an empty rect is returned
303 if (!dest.IsEmpty()) {
304 printf("rect intersection test #4 no empty rect!\n");
305 return PR_FALSE;
307 rect2.x += rect2.width;
309 // Test against a rect that overlaps the top edge of rect1
310 rect2.y--;
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");
314 return PR_FALSE;
316 rect2.y++;
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");
322 return PR_FALSE;
324 // Make sure an empty rect is returned
325 if (!dest.IsEmpty()) {
326 printf("rect intersection test #6 no empty rect!\n");
327 return PR_FALSE;
329 rect2.y += rect2.height;
331 // Test against a rect that overlaps the right edge of rect1
332 rect2.x++;
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");
336 return PR_FALSE;
338 rect2.x--;
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");
344 return PR_FALSE;
346 // Make sure an empty rect is returned
347 if (!dest.IsEmpty()) {
348 printf("rect intersection test #8 no empty rect!\n");
349 return PR_FALSE;
351 rect2.x -= rect2.width;
353 // Test against a rect that overlaps the bottom edge of rect1
354 rect2.y++;
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");
358 return PR_FALSE;
360 rect2.y--;
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");
366 return PR_FALSE;
368 // Make sure an empty rect is returned
369 if (!dest.IsEmpty()) {
370 printf("rect intersection test #10 no empty rect!\n");
371 return PR_FALSE;
373 rect2.y -= rect2.height;
375 return PR_TRUE;
378 static PRBool
379 TestUnion()
381 nsRect rect1;
382 nsRect rect2(10, 10, 50, 50);
383 nsRect dest;
385 // Check the case where the receiver is an empty rect
386 rect1.Empty();
387 if (!dest.UnionRect(rect1, rect2) || (dest != rect2)) {
388 printf("rect union test #1 failed!\n");
389 return PR_FALSE;
392 // Check the case where the source rect is an empty rect
393 rect1 = rect2;
394 rect2.Empty();
395 if (!dest.UnionRect(rect1, rect2) || (dest != rect1)) {
396 printf("rect union test #2 failed!\n");
397 return PR_FALSE;
400 // Test the case where both rects are empty. This should fail
401 rect1.Empty();
402 rect2.Empty();
403 if (dest.UnionRect(rect1, rect2)) {
404 printf("rect union test #3 failed!\n");
405 return PR_FALSE;
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");
414 return PR_FALSE;
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");
423 return PR_FALSE;
426 return PR_TRUE;
429 int main(int argc, char** argv)
431 if (!TestConstructors())
432 return -1;
434 if (!TestEqualityOperator())
435 return -1;
437 if (!TestContainment())
438 return -1;
440 if (!TestIntersects())
441 return -1;
443 if (!TestIntersection())
444 return -1;
446 if (!TestUnion())
447 return -1;
449 return 0;