follow up to bug 588735, missing sdk ifdefing. a=nobug.
[mozilla-central.git] / gfx / tests / TestRegion.cpp
blob14fadc7d009c248a19f514928a9a7baa00a8e4ac
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 * Rob Arnold <robarnold@cs.cmu.edu>
19 * Portions created by the Initial Developer are Copyright (C) 2010
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 "TestHarness.h"
39 #include "nsRegion.h"
41 class TestLargestRegion {
42 static PRBool TestSingleRect(nsRect r) {
43 nsRegion region(r);
44 if (region.GetLargestRectangle() != r) {
45 fail("largest rect of singleton %d %d %d %d", r.x, r.y, r.width, r.height);
46 return PR_FALSE;
48 return PR_TRUE;
50 // Construct a rectangle, remove part of it, then check the remainder
51 static PRBool TestNonRectangular() {
52 nsRegion r(nsRect(0, 0, 30, 30));
54 const int nTests = 19;
55 struct {
56 nsRect rect;
57 PRInt64 expectedArea;
58 } tests[nTests] = {
59 // Remove a 20x10 chunk from the square
60 { nsRect(0, 0, 20, 10), 600 },
61 { nsRect(10, 0, 20, 10), 600 },
62 { nsRect(10, 20, 20, 10), 600 },
63 { nsRect(0, 20, 20, 10), 600 },
64 // Remove a 10x20 chunk from the square
65 { nsRect(0, 0, 10, 20), 600 },
66 { nsRect(20, 0, 10, 20), 600 },
67 { nsRect(20, 10, 10, 20), 600 },
68 { nsRect(0, 10, 10, 20), 600 },
69 // Remove the center 10x10
70 { nsRect(10, 10, 10, 10), 300 },
71 // Remove the middle column
72 { nsRect(10, 0, 10, 30), 300 },
73 // Remove the middle row
74 { nsRect(0, 10, 30, 10), 300 },
75 // Remove the corners 10x10
76 { nsRect(0, 0, 10, 10), 600 },
77 { nsRect(20, 20, 10, 10), 600 },
78 { nsRect(20, 0, 10, 10), 600 },
79 { nsRect(0, 20, 10, 10), 600 },
80 // Remove the corners 20x20
81 { nsRect(0, 0, 20, 20), 300 },
82 { nsRect(10, 10, 20, 20), 300 },
83 { nsRect(10, 0, 20, 20), 300 },
84 { nsRect(0, 10, 20, 20), 300 }
87 PRBool success = PR_TRUE;
88 for (PRInt32 i = 0; i < nTests; i++) {
89 nsRegion r2;
90 r2.Sub(r, tests[i].rect);
92 if (!r2.IsComplex())
93 fail("nsRegion code got unexpectedly smarter!");
95 nsRect largest = r2.GetLargestRectangle();
96 if (largest.width * largest.height != tests[i].expectedArea) {
97 fail("Did not succesfully find largest rectangle in non-rectangular region on iteration %d", i);
98 success = PR_FALSE;
102 return success;
104 static PRBool TwoRectTest() {
105 nsRegion r(nsRect(0, 0, 100, 100));
106 const int nTests = 4;
107 struct {
108 nsRect rect1, rect2;
109 PRInt64 expectedArea;
110 } tests[nTests] = {
111 { nsRect(0, 0, 75, 40), nsRect(0, 60, 75, 40), 2500 },
112 { nsRect(25, 0, 75, 40), nsRect(25, 60, 75, 40), 2500 },
113 { nsRect(25, 0, 75, 40), nsRect(0, 60, 75, 40), 2000 },
114 { nsRect(0, 0, 75, 40), nsRect(25, 60, 75, 40), 2000 },
116 PRBool success = PR_TRUE;
117 for (PRInt32 i = 0; i < nTests; i++) {
118 nsRegion r2;
120 r2.Sub(r, tests[i].rect1);
121 r2.Sub(r2, tests[i].rect2);
123 if (!r2.IsComplex())
124 fail("nsRegion code got unexpectedly smarter!");
126 nsRect largest = r2.GetLargestRectangle();
127 if (largest.width * largest.height != tests[i].expectedArea) {
128 fail("Did not succesfully find largest rectangle in two-rect-subtract region on iteration %d", i);
129 success = PR_FALSE;
132 return success;
134 public:
135 static PRBool Test() {
136 if (!TestSingleRect(nsRect(0, 52, 720, 480)) ||
137 !TestSingleRect(nsRect(-20, 40, 50, 20)) ||
138 !TestSingleRect(nsRect(-20, 40, 10, 8)) ||
139 !TestSingleRect(nsRect(-20, -40, 10, 8)) ||
140 !TestSingleRect(nsRect(-10, -10, 20, 20)))
141 return PR_FALSE;
142 if (!TestNonRectangular())
143 return PR_FALSE;
144 if (!TwoRectTest())
145 return PR_FALSE;
146 passed("TestLargestRegion");
147 return PR_TRUE;
151 int main(int argc, char** argv) {
152 ScopedXPCOM xpcom("TestRegion");
153 if (xpcom.failed())
154 return -1;
155 if (!TestLargestRegion::Test())
156 return -1;
157 return 0;