2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
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.
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.
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
20 #include "gnashconfig.h"
30 using namespace gnash
;
31 using namespace gnash::geometry
;
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));
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();
93 Range2d
<unsigned>(0, 3, 1, 6).growBy(2),
94 Range2d
<unsigned>(worldRange
) );
97 Range2d
<unsigned>(3, 1, 7, 8).growBy(2),
98 Range2d
<unsigned int>(worldRange
) );
101 Range2d
<unsigned>(10, 10, uupbound
-1, 20).growBy(2),
102 Range2d
<unsigned int>(worldRange
) );
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
110 Range2d
<unsigned>(1, 1,
111 uupbound
-1, uupbound
-1).growBy(uupbound
),
112 Range2d
<unsigned int>(worldRange
) );
119 Range2d
<int>(0, 0, 10, 20).shrinkBy(2),
120 Range2d
<int>(2, 2, 8, 18) );
122 // Collapse horizontally
124 Range2d
<int>(0, 3, 1, 6).shrinkBy(2),
125 Range2d
<int>(nullRange
) );
126 // Collapse vertically
128 Range2d
<int>(0, 3, 10, 6).shrinkBy(8),
129 Range2d
<int>(nullRange
) );
136 Range2d
<int>(0, 3, 10, 6).scale(2),
137 Range2d
<int>(0, 6, 20, 12) );
140 Range2d
<int>(0, 3, 10, 6).scale(2, 3),
141 Range2d
<int>(0, 9, 20, 18) );
144 Range2d
<int>(-1, 3, 10, 5).scale(3, .5),
145 Range2d
<int>(-3, 1, 30, 3) );
148 Range2d
<unsigned int>(1, 3, 10, 5).scale(3, .5),
149 Range2d
<unsigned int>(3, 1, 30, 3) );
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,
160 Range2d
<float>(0, 0, 40, 80).scale(1.2, 1.0/20).height(),
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
);
195 Union( Range2d
<int>(0, 0, 10, 10),
196 Range2d
<int>(-10, -10, -5, -5)),
197 Range2d
<int>(-10, -10, 10, 10)
202 Union( Range2d
<int>(0, 0, 10, 10),
203 Range2d
<int>(-10, -10, 5, 5)),
204 Range2d
<int>(-10, -10, 10, 10)
209 Union( Range2d
<int>(0, 0, 10, 10),
210 Range2d
<int>(-10, -10, 5, 5)),
211 Range2d
<int>(-10, -10, 10, 10)
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)
221 Union( Range2d
<int>(0, 0, 10, 10),
222 Range2d
<int>(2, 2, 5, 5)),
223 Range2d
<int>(0, 0, 10, 10)
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 );
247 Intersection( Range2d
<int>(0, 0, 10, 10),
248 Range2d
<int>(-10, -10, -2, -3)),
249 Range2d
<int>() // NULL range !
252 Intersection( Range2d
<double>(-100.4, 50.5, -60.4, 60),
253 Range2d
<double>(-80, -10, -70, -9)),
254 Range2d
<double>() // NULL range !
257 Intersect( Range2d
<double>(-100.4, 50.5, -60.4, 60),
258 Range2d
<double>(-80, -10, -70, -9)),
262 // overlapping ranges
264 Intersection( Range2d
<int>(0, 0, 10, 10),
265 Range2d
<int>(-10, -10, 5, 5) ),
266 Range2d
<int>(0, 0, 5, 5)
269 Intersect( Range2d
<int>(0, 0, 10, 10),
270 Range2d
<int>(-10, -10, 5, 5)),
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)
281 Intersect( Range2d
<unsigned short>(0, 0, 10, 10),
282 Range2d
<unsigned short>(2, 2, 5, 5)),
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) );
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
) );
309 check( fIntRange1
.contains(fIntRange1
) );
311 cout
<< "fIntRange1 == " << fIntRange1
<< endl
;
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)) );