Add file under gui/pythonmod to .gitignore
[gnash.git] / testsuite / libbase.all / Range2dTest.cpp
blob8be0bb8f29ce6c2e1407e1c0e720054e566f51ac
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifdef HAVE_CONFIG_H
20 #include "gnashconfig.h"
21 #endif
23 #include "check.h"
24 #include "Range2d.h"
25 #include <iostream>
26 #include <sstream>
27 #include <cassert>
29 using namespace std;
30 using namespace gnash;
31 using namespace gnash::geometry;
33 int
34 main(int /*argc*/, char** /*argv*/)
38 // Test NULL range construction
41 Range2d<int> nullIntRange1;
42 check( nullIntRange1.isNull() );
43 check( ! nullIntRange1.isWorld() );
44 check( ! nullIntRange1.isFinite() );
46 Range2d<int> nullIntRange2(nullRange);
47 check_equals( nullIntRange1, nullIntRange2 );
48 check( nullIntRange1 == nullIntRange2 );
51 // Test WORLD range construction
54 Range2d<int> worldIntRange1(worldRange);
55 check( ! worldIntRange1.isNull() );
56 check( worldIntRange1.isWorld() );
57 check( ! worldIntRange1.isFinite() );
58 check( nullIntRange1 != worldIntRange1 );
61 // Test FINITE range construction
64 Range2d<int> fIntRange1(0, 0, 10, 10);
66 check( ! fIntRange1.isNull() );
67 check( ! fIntRange1.isWorld() );
68 check( fIntRange1.isFinite() );
69 check( fIntRange1 != worldIntRange1 );
70 check( fIntRange1 != nullIntRange1 );
73 // Test expandToCircle()
76 Range2d<float> circleRange;
77 circleRange.expandToCircle(50.0, 100.0, 10.0);
78 check_equals( circleRange, Range2d<float>(40.0, 90.0, 60.0, 110.0));
82 // Test growBy()
85 check_equals(
86 Range2d<int>(0, 0, 1, 2).growBy(2),
87 Range2d<int>(-2, -2, 3, 4) );
89 // hit the numeric limits on each side
90 unsigned uupbound = std::numeric_limits<unsigned>::max();
91 // overflow xmin
92 check_equals(
93 Range2d<unsigned>(0, 3, 1, 6).growBy(2),
94 Range2d<unsigned>(worldRange) );
95 // overflow ymin
96 check_equals(
97 Range2d<unsigned>(3, 1, 7, 8).growBy(2),
98 Range2d<unsigned int>(worldRange) );
99 // overflow xmax
100 check_equals(
101 Range2d<unsigned>(10, 10, uupbound-1, 20).growBy(2),
102 Range2d<unsigned int>(worldRange) );
103 // overflow ymax
104 check_equals(
105 Range2d<unsigned>(10, 10, 20, uupbound-1).growBy(2),
106 Range2d<unsigned int>(worldRange) );
107 // overflow both direction so that min is still < max as a result
108 // (this is tricky)
109 check_equals(
110 Range2d<unsigned>(1, 1,
111 uupbound-1, uupbound-1).growBy(uupbound),
112 Range2d<unsigned int>(worldRange) );
115 // Test shinkBy()
118 check_equals(
119 Range2d<int>(0, 0, 10, 20).shrinkBy(2),
120 Range2d<int>(2, 2, 8, 18) );
122 // Collapse horizontally
123 check_equals(
124 Range2d<int>(0, 3, 1, 6).shrinkBy(2),
125 Range2d<int>(nullRange) );
126 // Collapse vertically
127 check_equals(
128 Range2d<int>(0, 3, 10, 6).shrinkBy(8),
129 Range2d<int>(nullRange) );
132 // Test scale()
135 check_equals(
136 Range2d<int>(0, 3, 10, 6).scale(2),
137 Range2d<int>(0, 6, 20, 12) );
139 check_equals(
140 Range2d<int>(0, 3, 10, 6).scale(2, 3),
141 Range2d<int>(0, 9, 20, 18) );
143 check_equals(
144 Range2d<int>(-1, 3, 10, 5).scale(3, .5),
145 Range2d<int>(-3, 1, 30, 3) );
147 check_equals(
148 Range2d<unsigned int>(1, 3, 10, 5).scale(3, .5),
149 Range2d<unsigned int>(3, 1, 30, 3) );
151 check_equals(
152 Range2d<float>(-1, 3, 10, 5).scale(3, .5),
153 Range2d<float>(-3, 1.5, 30, 2.5) );
155 check_equals( // tolerate 1/1000 floating point drift
156 round(Range2d<float>(0, 0, 40, 80).scale(1.2, 1.0/20).width()*1000)/1000,
157 48);
159 check_equals(
160 Range2d<float>(0, 0, 40, 80).scale(1.2, 1.0/20).height(),
164 // Test range Union
167 Range2d<int> uIntNW1 = Union(nullIntRange1, worldIntRange1);
168 Range2d<int> uIntNW2 = Union(worldIntRange1, nullIntRange1);
170 // union is transitive
171 check_equals( uIntNW1, uIntNW2 );
173 // union of anything with world is world
174 check_equals( uIntNW1, worldIntRange1 );
176 Range2d<int> uIntNF1 = Union(nullIntRange1, fIntRange1);
177 Range2d<int> uIntNF2 = Union(fIntRange1, nullIntRange1);
179 // union is transitive
180 check_equals( uIntNF1, uIntNF2 );
182 // union of anything with null is no-op
183 check_equals( uIntNF1, fIntRange1 );
185 Range2d<int> uIntWF1 = Union(worldIntRange1, fIntRange1);
186 Range2d<int> uIntWF2 = Union(fIntRange1, worldIntRange1);
188 // union is transitive
189 check_equals( uIntWF1, uIntWF2 );
191 // union of anything with world is world
192 check_equals( uIntWF1, worldIntRange1 );
194 check_equals(
195 Union( Range2d<int>(0, 0, 10, 10),
196 Range2d<int>(-10, -10, -5, -5)),
197 Range2d<int>(-10, -10, 10, 10)
200 // disjoint
201 check_equals(
202 Union( Range2d<int>(0, 0, 10, 10),
203 Range2d<int>(-10, -10, 5, 5)),
204 Range2d<int>(-10, -10, 10, 10)
207 // overlapping
208 check_equals(
209 Union( Range2d<int>(0, 0, 10, 10),
210 Range2d<int>(-10, -10, 5, 5)),
211 Range2d<int>(-10, -10, 10, 10)
213 check_equals(
214 Union( Range2d<float>(-0.2, -0.3, 0.7, 0.8),
215 Range2d<float>(-0.1, -0.1, 0.8, 0.9)),
216 Range2d<float>(-0.2, -0.3, 0.8, 0.9)
219 // inscribed
220 check_equals(
221 Union( Range2d<int>(0, 0, 10, 10),
222 Range2d<int>(2, 2, 5, 5)),
223 Range2d<int>(0, 0, 10, 10)
225 check_equals(
226 Union( Range2d<unsigned short>(2, 2, 8, 9),
227 Range2d<unsigned short>(0, 1, 9, 10)),
228 Range2d<unsigned short>(0, 1, 9, 10)
232 // Test Intersection / Intersects
235 Range2d<int> iIntNW1 = Intersection(nullIntRange1, worldIntRange1);
236 Range2d<int> iIntNW2 = Intersection(worldIntRange1, nullIntRange1);
238 // intersection is transitive
239 check_equals( iIntNW1, iIntNW2 );
241 // intersection of anything with null is null
242 check_equals( iIntNW1, nullIntRange1 );
243 check_equals( Intersect(nullIntRange1, worldIntRange1), false );
245 // disjoint ranges
246 check_equals(
247 Intersection( Range2d<int>(0, 0, 10, 10),
248 Range2d<int>(-10, -10, -2, -3)),
249 Range2d<int>() // NULL range !
251 check_equals(
252 Intersection( Range2d<double>(-100.4, 50.5, -60.4, 60),
253 Range2d<double>(-80, -10, -70, -9)),
254 Range2d<double>() // NULL range !
256 check_equals(
257 Intersect( Range2d<double>(-100.4, 50.5, -60.4, 60),
258 Range2d<double>(-80, -10, -70, -9)),
259 false
262 // overlapping ranges
263 check_equals(
264 Intersection( Range2d<int>(0, 0, 10, 10),
265 Range2d<int>(-10, -10, 5, 5) ),
266 Range2d<int>(0, 0, 5, 5)
268 check_equals(
269 Intersect( Range2d<int>(0, 0, 10, 10),
270 Range2d<int>(-10, -10, 5, 5)),
271 true
274 // inscribed ranges
275 check_equals(
276 Intersection( Range2d<unsigned short>(0, 0, 10, 10),
277 Range2d<unsigned short>(2, 2, 5, 5)),
278 Range2d<unsigned short>(2, 2, 5, 5)
280 check_equals(
281 Intersect( Range2d<unsigned short>(0, 0, 10, 10),
282 Range2d<unsigned short>(2, 2, 5, 5)),
283 true
287 // Test Range2d<float> to Range2d<int> cast
290 check_equals( (Range2d<int>)Range2d<float>(-0.1, 0.1, 10.1, 10.2),
291 Range2d<int>(-1, 0, 11, 11) );
295 // Test Contains
298 // Null ranges don't contain anything, not even themselves
299 check( ! nullIntRange1.contains(10, 10) );
300 check( ! nullIntRange1.contains(nullIntRange1) );
301 check( ! nullIntRange1.contains(worldIntRange1) );
303 // World ranges contain everything except null ranges
304 check( worldIntRange1.contains(10, -190) );
305 check( worldIntRange1.contains(worldIntRange1) );
306 check( ! worldIntRange1.contains(nullIntRange1) );
308 // Self-containment
309 check( fIntRange1.contains(fIntRange1) );
311 cout << "fIntRange1 == " << fIntRange1 << endl;
313 // Boundary overlaps
314 check( fIntRange1.contains(0, 0) );
315 check( fIntRange1.contains(10, 5) );
316 check( fIntRange1.contains(5, 10) );
317 check( fIntRange1.contains(5, 0) );
318 check( fIntRange1.contains(Range2d<int>(0, 0, 2, 2)) );
319 check( fIntRange1.contains(Range2d<int>(0, 4, 2, 6)) );
320 check( fIntRange1.contains(Range2d<int>(0, 8, 2, 10)) );
321 check( fIntRange1.contains(Range2d<int>(4, 8, 6, 10)) );
322 check( fIntRange1.contains(Range2d<int>(8, 8, 10, 10)) );
323 check( fIntRange1.contains(Range2d<int>(8, 4, 10, 6)) );
324 check( fIntRange1.contains(Range2d<int>(8, 0, 10, 2)) );
325 check( fIntRange1.contains(Range2d<int>(4, 0, 6, 2)) );
327 // Strict containment
328 check( fIntRange1.contains(Range2d<int>(2, 2, 4, 4)) );
329 check( fIntRange1.contains(5, 5) );
331 // Intersection (partial overlap)
332 check( ! fIntRange1.contains(Range2d<int>(-2, 0, 2, 2)) );
333 check( ! fIntRange1.contains(Range2d<int>(8, 8, 10, 11)) );
334 check( ! fIntRange1.contains(Range2d<int>(8, 8, 11, 11)) );