Back to index

lightning-sunbird  0.9+nobinonly
TestRect.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsRect.h"
00039 #include <stdio.h>
00040 #ifdef XP_WIN
00041 #include <windows.h>
00042 #endif
00043 
00044 static PRBool
00045 TestConstructors()
00046 {
00047   // Create a rectangle
00048   nsRect  rect1(10, 20, 30, 40);
00049 
00050   // Make sure the rectangle was properly initialized
00051   if ((rect1.x != 10) || (rect1.y != 20) ||
00052       (rect1.width != 30) || (rect1.height != 40)) {
00053     printf("rect initialization failed!\n");
00054     return PR_FALSE;
00055   }
00056 
00057   // Create a second rect using the copy constructor
00058   nsRect  rect2(rect1);
00059 
00060   // Make sure the rectangle was properly initialized
00061   if ((rect2.x != rect1.x) || (rect2.y != rect1.y) ||
00062       (rect2.width != rect1.width) || (rect2.height != rect1.height)) {
00063     printf("rect copy constructor failed!\n");
00064     return PR_FALSE;
00065   }
00066 
00067   return PR_TRUE;
00068 }
00069 
00070 static PRBool
00071 TestEqualityOperator()
00072 {
00073   nsRect  rect1(10, 20, 30, 40);
00074   nsRect  rect2(rect1);
00075 
00076   // Test the equality operator
00077   if (!(rect1 == rect2)) {
00078     printf("rect equality operator failed!\n");
00079     return PR_FALSE;
00080   }
00081 
00082   // Test the inequality operator
00083   if (rect1 != rect2) {
00084     printf("rect inequality operator failed!\n");
00085     return PR_FALSE;
00086   }
00087 
00088   // Make sure that two empty rects are equal
00089   rect1.Empty();
00090   rect2.Empty();
00091   if (!(rect1 == rect2)) {
00092     printf("rect equality operator failed for empty rects!\n");
00093     return PR_FALSE;
00094   }
00095 
00096   return PR_TRUE;
00097 }
00098 
00099 static PRBool
00100 TestContainment()
00101 {
00102   nsRect  rect1(10, 10, 50, 50);
00103 
00104   // Test the point containment methods
00105   //
00106 
00107   // Basic test of a point in the middle of the rect
00108   if (!rect1.Contains(rect1.x + rect1.width/2, rect1.y + rect1.height/2)) {
00109     printf("point containment test #1 failed!\n");
00110     return PR_FALSE;
00111   }
00112 
00113   // Test against a point at the left/top edges
00114   if (!rect1.Contains(rect1.x, rect1.y)) {
00115     printf("point containment test #2 failed!\n");
00116     return PR_FALSE;
00117   }
00118 
00119   // Test against a point at the right/bottom extents
00120   if (rect1.Contains(rect1.XMost(), rect1.YMost())) {
00121     printf("point containment test #3 failed!\n");
00122     return PR_FALSE;
00123   }
00124 
00125   // Test the rect containment methods
00126   //
00127   nsRect  rect2(rect1);
00128 
00129   // Test against a rect that's the same as rect1
00130   if (!rect1.Contains(rect2)) {
00131     printf("rect containment test #1 failed!\n");
00132     return PR_FALSE;
00133   }
00134 
00135   // Test against a rect whose left edge (only) is outside of rect1
00136   rect2.x--;
00137   if (rect1.Contains(rect2)) {
00138     printf("rect containment test #2 failed!\n");
00139     return PR_FALSE;
00140   }
00141   rect2.x++;
00142 
00143   // Test against a rect whose top edge (only) is outside of rect1
00144   rect2.y--;
00145   if (rect1.Contains(rect2)) {
00146     printf("rect containment test #3 failed!\n");
00147     return PR_FALSE;
00148   }
00149   rect2.y++;
00150 
00151   // Test against a rect whose right edge (only) is outside of rect1
00152   rect2.x++;
00153   if (rect1.Contains(rect2)) {
00154     printf("rect containment test #2 failed!\n");
00155     return PR_FALSE;
00156   }
00157   rect2.x--;
00158 
00159   // Test against a rect whose bottom edge (only) is outside of rect1
00160   rect2.y++;
00161   if (rect1.Contains(rect2)) {
00162     printf("rect containment test #3 failed!\n");
00163     return PR_FALSE;
00164   }
00165   rect2.y--;
00166 
00167   return PR_TRUE;
00168 }
00169 
00170 // Test the method that returns a boolean result but doesn't return a
00171 // a rectangle
00172 static PRBool
00173 TestIntersects()
00174 {
00175   nsRect  rect1(10, 10, 50, 50);
00176   nsRect  rect2(rect1);
00177 
00178   // Test against a rect that's the same as rect1
00179   if (!rect1.Intersects(rect2)) {
00180     printf("rect intersects test #1 failed!\n");
00181     return PR_FALSE;
00182   }
00183 
00184   // Test against a rect that's enclosed by rect1
00185   rect2.Deflate(1, 1);
00186   if (!rect1.Contains(rect2) || !rect1.Intersects(rect2)) {
00187     printf("rect intersects test #2 failed!\n");
00188     return PR_FALSE;
00189   }
00190   rect2.Inflate(1, 1);
00191 
00192   // Make sure inflate and deflate worked correctly
00193   if (rect1 != rect2) {
00194     printf("rect inflate or deflate failed!\n");
00195     return PR_FALSE;
00196   }
00197 
00198   // Test against a rect that overlaps the left edge of rect1
00199   rect2.x--;
00200   if (!rect1.Intersects(rect2)) {
00201     printf("rect containment test #3 failed!\n");
00202     return PR_FALSE;
00203   }
00204   rect2.x++;
00205 
00206   // Test against a rect that's outside of rect1 on the left
00207   rect2.x -= rect2.width;
00208   if (rect1.Intersects(rect2)) {
00209     printf("rect containment test #4 failed!\n");
00210     return PR_FALSE;
00211   }
00212   rect2.x += rect2.width;
00213 
00214   // Test against a rect that overlaps the top edge of rect1
00215   rect2.y--;
00216   if (!rect1.Intersects(rect2)) {
00217     printf("rect containment test #5 failed!\n");
00218     return PR_FALSE;
00219   }
00220   rect2.y++;
00221 
00222   // Test against a rect that's outside of rect1 on the top
00223   rect2.y -= rect2.height;
00224   if (rect1.Intersects(rect2)) {
00225     printf("rect containment test #6 failed!\n");
00226     return PR_FALSE;
00227   }
00228   rect2.y += rect2.height;
00229 
00230   // Test against a rect that overlaps the right edge of rect1
00231   rect2.x++;
00232   if (!rect1.Intersects(rect2)) {
00233     printf("rect containment test #7 failed!\n");
00234     return PR_FALSE;
00235   }
00236   rect2.x--;
00237 
00238   // Test against a rect that's outside of rect1 on the right
00239   rect2.x += rect2.width;
00240   if (rect1.Intersects(rect2)) {
00241     printf("rect containment test #8 failed!\n");
00242     return PR_FALSE;
00243   }
00244   rect2.x -= rect2.width;
00245 
00246   // Test against a rect that overlaps the bottom edge of rect1
00247   rect2.y++;
00248   if (!rect1.Intersects(rect2)) {
00249     printf("rect containment test #9 failed!\n");
00250     return PR_FALSE;
00251   }
00252   rect2.y--;
00253 
00254   // Test against a rect that's outside of rect1 on the bottom
00255   rect2.y += rect2.height;
00256   if (rect1.Intersects(rect2)) {
00257     printf("rect containment test #10 failed!\n");
00258     return PR_FALSE;
00259   }
00260   rect2.y -= rect2.height;
00261 
00262   return PR_TRUE;
00263 }
00264 
00265 // Test the method that returns a boolean result and an intersection rect
00266 static PRBool
00267 TestIntersection()
00268 {
00269   nsRect  rect1(10, 10, 50, 50);
00270   nsRect  rect2(rect1);
00271   nsRect  dest;
00272 
00273   // Test against a rect that's the same as rect1
00274   if (!dest.IntersectRect(rect1, rect2) || (dest != rect1)) {
00275     printf("rect intersection test #1 failed!\n");
00276     return PR_FALSE;
00277   }
00278 
00279   // Test against a rect that's enclosed by rect1
00280   rect2.Deflate(1, 1);
00281   if (!dest.IntersectRect(rect1, rect2) || (dest != rect2)) {
00282     printf("rect intersection test #2 failed!\n");
00283     return PR_FALSE;
00284   }
00285   rect2.Inflate(1, 1);
00286 
00287   // Test against a rect that overlaps the left edge of rect1
00288   rect2.x--;
00289   if (!dest.IntersectRect(rect1, rect2) ||
00290      (dest != nsRect(rect1.x, rect1.y, rect1.width - 1, rect1.height))) {
00291     printf("rect intersection test #3 failed!\n");
00292     return PR_FALSE;
00293   }
00294   rect2.x++;
00295 
00296   // Test against a rect that's outside of rect1 on the left
00297   rect2.x -= rect2.width;
00298   if (dest.IntersectRect(rect1, rect2)) {
00299     printf("rect intersection test #4 failed!\n");
00300     return PR_FALSE;
00301   }
00302   // Make sure an empty rect is returned
00303   if (!dest.IsEmpty()) {
00304     printf("rect intersection test #4 no empty rect!\n");
00305     return PR_FALSE;
00306   }
00307   rect2.x += rect2.width;
00308 
00309   // Test against a rect that overlaps the top edge of rect1
00310   rect2.y--;
00311   if (!dest.IntersectRect(rect1, rect2) ||
00312      (dest != nsRect(rect1.x, rect1.y, rect1.width, rect1.height - 1))) {
00313     printf("rect intersection test #5 failed!\n");
00314     return PR_FALSE;
00315   }
00316   rect2.y++;
00317 
00318   // Test against a rect that's outside of rect1 on the top
00319   rect2.y -= rect2.height;
00320   if (dest.IntersectRect(rect1, rect2)) {
00321     printf("rect intersection test #6 failed!\n");
00322     return PR_FALSE;
00323   }
00324   // Make sure an empty rect is returned
00325   if (!dest.IsEmpty()) {
00326     printf("rect intersection test #6 no empty rect!\n");
00327     return PR_FALSE;
00328   }
00329   rect2.y += rect2.height;
00330 
00331   // Test against a rect that overlaps the right edge of rect1
00332   rect2.x++;
00333   if (!dest.IntersectRect(rect1, rect2) ||
00334      (dest != nsRect(rect1.x + 1, rect1.y, rect1.width - 1, rect1.height))) {
00335     printf("rect intersection test #7 failed!\n");
00336     return PR_FALSE;
00337   }
00338   rect2.x--;
00339 
00340   // Test against a rect that's outside of rect1 on the right
00341   rect2.x += rect2.width;
00342   if (dest.IntersectRect(rect1, rect2)) {
00343     printf("rect intersection test #8 failed!\n");
00344     return PR_FALSE;
00345   }
00346   // Make sure an empty rect is returned
00347   if (!dest.IsEmpty()) {
00348     printf("rect intersection test #8 no empty rect!\n");
00349     return PR_FALSE;
00350   }
00351   rect2.x -= rect2.width;
00352 
00353   // Test against a rect that overlaps the bottom edge of rect1
00354   rect2.y++;
00355   if (!dest.IntersectRect(rect1, rect2) ||
00356      (dest != nsRect(rect1.x, rect1.y + 1, rect1.width, rect1.height - 1))) {
00357     printf("rect intersection test #9 failed!\n");
00358     return PR_FALSE;
00359   }
00360   rect2.y--;
00361 
00362   // Test against a rect that's outside of rect1 on the bottom
00363   rect2.y += rect2.height;
00364   if (dest.IntersectRect(rect1, rect2)) {
00365     printf("rect intersection test #10 failed!\n");
00366     return PR_FALSE;
00367   }
00368   // Make sure an empty rect is returned
00369   if (!dest.IsEmpty()) {
00370     printf("rect intersection test #10 no empty rect!\n");
00371     return PR_FALSE;
00372   }
00373   rect2.y -= rect2.height;
00374 
00375   return PR_TRUE;
00376 }
00377 
00378 static PRBool
00379 TestUnion()
00380 {
00381   nsRect  rect1;
00382   nsRect  rect2(10, 10, 50, 50);
00383   nsRect  dest;
00384 
00385   // Check the case where the receiver is an empty rect
00386   rect1.Empty();
00387   if (!dest.UnionRect(rect1, rect2) || (dest != rect2)) {
00388     printf("rect union test #1 failed!\n");
00389     return PR_FALSE;
00390   }
00391 
00392   // Check the case where the source rect is an empty rect
00393   rect1 = rect2;
00394   rect2.Empty();
00395   if (!dest.UnionRect(rect1, rect2) || (dest != rect1)) {
00396     printf("rect union test #2 failed!\n");
00397     return PR_FALSE;
00398   }
00399 
00400   // Test the case where both rects are empty. This should fail
00401   rect1.Empty();
00402   rect2.Empty();
00403   if (dest.UnionRect(rect1, rect2)) {
00404     printf("rect union test #3 failed!\n");
00405     return PR_FALSE;
00406   }
00407 
00408   // Test union case where the two rects don't overlap at all
00409   rect1.SetRect(10, 10, 50, 50);
00410   rect2.SetRect(100, 100, 50, 50);
00411   if (!dest.UnionRect(rect1, rect2) ||
00412      (dest != nsRect(rect1.x, rect1.y, rect2.XMost() - rect1.x, rect2.YMost() - rect1.y))) {
00413     printf("rect union test #4 failed!\n");
00414     return PR_FALSE;
00415   }
00416 
00417   // Test union case where the two rects overlap
00418   rect1.SetRect(30, 30, 50, 50);
00419   rect2.SetRect(10, 10, 50, 50);
00420   if (!dest.UnionRect(rect1, rect2) ||
00421       (dest != nsRect(rect2.x, rect2.y, rect1.XMost() - rect2.x, rect1.YMost() - rect2.y))) {
00422     printf("rect union test #5 failed!\n");
00423     return PR_FALSE;
00424   }
00425 
00426   return PR_TRUE;
00427 }
00428 
00429 int main(int argc, char** argv)
00430 {
00431   if (!TestConstructors())
00432     return -1;
00433 
00434   if (!TestEqualityOperator())
00435     return -1;
00436 
00437   if (!TestContainment())
00438     return -1;
00439 
00440   if (!TestIntersects())
00441     return -1;
00442 
00443   if (!TestIntersection())
00444     return -1;
00445 
00446   if (!TestUnion())
00447     return -1;
00448 
00449   return 0;
00450 }