Add automated tests on function-type FSCommand parameter passing.
[gnash.git] / testsuite / actionscript.all / Point.as
bloba512329a0768b83e8a379430aa29369e54c2cbc0
1 //
2 // Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // 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 // Test case for Point ActionScript class
20 // compile this test case with Ming makeswf, and then
21 // execute it like this gnash -1 -r 0 -v out.swf
23 rcsid="Point.as";
25 #include "check.as"
27 #if OUTPUT_VERSION < 8
29 check_equals(typeof(flash), 'undefined');
31 check_totals(1);
33 #else
35 Point = flash.geom.Point;
36 check_equals(typeof(Point), 'function');
37 check_equals(typeof(Point.prototype), 'object');
38 check(Point.prototype.hasOwnProperty('length'));
39 check(!Point.prototype.hasOwnProperty('x'));
40 check(!Point.prototype.hasOwnProperty('y'));
41 check(Point.prototype.hasOwnProperty('add'));
42 check(Point.prototype.hasOwnProperty('clone'));
43 check(!Point.prototype.hasOwnProperty('distance'));
44 check(Point.hasOwnProperty('distance'));
45 check(Point.prototype.hasOwnProperty('equals'));
46 check(!Point.prototype.hasOwnProperty('interpolate'));
47 check(Point.hasOwnProperty('interpolate'));
48 check(Point.prototype.hasOwnProperty('normalize'));
49 check(Point.prototype.hasOwnProperty('offset'));
50 check(!Point.prototype.hasOwnProperty('polar'));
51 check(Point.hasOwnProperty('polar'));
52 check(Point.prototype.hasOwnProperty('subtract'));
53 check(Point.prototype.hasOwnProperty('toString'));
55 //-------------------------------------------------------------
56 // Test constructor (and x, y, length)
57 //-------------------------------------------------------------
59 p0 = new Point();
60 check_equals(typeof(p0), 'object');
61 check(p0 instanceof Point);
62 check(p0.hasOwnProperty('x'));
63 check(p0.hasOwnProperty('y'));
64 check_equals(''+p0, '(x=0, y=0)');
65 check_equals(typeof(p0.x), 'number');
66 check_equals(typeof(p0.y), 'number');
67 check_equals(typeof(p0.length), 'number');
68 check_equals(p0.length, 0);
70 a = []; for (var i in p0) a.push(i);
71 check_equals(a.length, 10); // most of them...
73 p0 = new Point('x', 'y');
74 check_equals(''+p0, '(x=x, y=y)');
75 check_equals(typeof(p0.x), 'string');
76 check_equals(typeof(p0.y), 'string');
77 check_equals(typeof(p0.length), 'number');
78 check(isNaN(p0.length));
79 p0.x = 1;
80 check(isNaN(p0.length));
81 p0.y = 0;
82 check_equals(p0.length, 1);
84 p0 = new Point(3, 4);
85 check_equals(p0.length, 5);
87 ASSetPropFlags(p0, "length", 0, 4); // clear read-only (if any)
88 p0.length = 10;
89 check_equals(p0.length, 5);
91 p0 = new Point(50, -Infinity);
92 check_equals(p0.length, Infinity);
94 p0 = new Point(0, 0);
95 check_equals(p0.length, 0);
97 p0 = new Point(undef, undef);
98 check_equals(''+p0, '(x=undefined, y=undefined)');
100 //-------------------------------------------------------------
101 // Test Point.add
102 //-------------------------------------------------------------
104 p0 = new Point('x', 'y');
105 ret = p0.add();
106 check(ret instanceof Point);
107 check_equals(p0.toString(), '(x=x, y=y)');
108 check_equals(ret.toString(), '(x=xundefined, y=yundefined)');
109 String.prototype.x = 3; // to test it's used
110 ret = p0.add('1');
111 delete String.prototype.x;
112 check(ret instanceof Point);
113 check_equals(ret.toString(), '(x=x3, y=yundefined)');
114 check_equals(p0.toString(), '(x=x, y=y)');
115 ret = p0.add(1, '2');
116 check(ret instanceof Point);
117 check_equals(ret.toString(), '(x=xundefined, y=yundefined)');
118 check_equals(p0.toString(), '(x=x, y=y)');
120 p0 = new Point('x', 'y');
121 p1 = new Point('x1', 'y1');
122 ret = p0.add(p1);
123 check(ret instanceof Point);
124 check_equals(ret.toString(), '(x=xx1, y=yy1)');
125 check_equals(p0.toString(), '(x=x, y=y)');
126 check_equals(p1.toString(), '(x=x1, y=y1)');
128 p0 = new Point(2, 3);
129 p1 = { x:1, y:1 };
130 ret = p0.add(p1);
131 check_equals(ret.toString(), '(x=3, y=4)');
133 ret = p0.add(p1, 4, 5, 6);
134 check_equals(ret.toString(), '(x=3, y=4)');
136 // A non-point with a point's add method.
137 fakepoint = {x:20, y:30};
138 fakepoint.add = Point.prototype.add;
139 ret = fakepoint.add(p0);
140 check_equals(ret.toString(), "(x=22, y=33)");
141 check(ret instanceof Point);
143 //-------------------------------------------------------------
144 // Test Point.clone
145 //-------------------------------------------------------------
147 p0 = new Point(3, 4);
148 p0.z = 5;
149 p2 = p0.clone();
150 check(p2 instanceof Point);
151 check_equals(p2.toString(), "(x=3, y=4)");
152 check_equals(typeof(p2.z), 'undefined');
153 p2 = p0.clone(1, 2, 3);
154 check(p2 instanceof Point);
155 check_equals(p2.toString(), "(x=3, y=4)");
157 // A non-point with a point's clone method.
158 fakepoint = {x:20, y:30};
159 fakepoint.clone = Point.prototype.clone;
160 ret = fakepoint.clone(p0);
161 check_equals(ret.toString(), "(x=20, y=30)");
162 check(ret instanceof Point);
164 //-------------------------------------------------------------
165 // Test Point.distance (static)
166 //-------------------------------------------------------------
168 dist = Point.distance();
169 check_equals(typeof(dist), 'undefined');
171 dist = Point.distance(undefined);
172 check_equals(typeof(dist), 'undefined');
174 o0 = {x:10, y:1};
175 o1 = {x:21, y:1};
176 dist = Point.distance(o0, o1);
177 check_equals(typeof(dist), 'undefined');
179 p0 = new Point('x', 'y');
180 p1 = new Point('a', 'b');
181 dist = Point.distance(p0, p1);
182 check_equals(typeof(dist), 'number');
183 check(isNaN(dist));
184 dist = p0.distance(p1);
185 check_equals(typeof(dist), 'undefined');
187 p0 = new Point('10', '20');
188 p1 = new Point('10', 'y');
189 dist = Point.distance(p0, p1);
190 check_equals(typeof(dist), 'number');
191 check(isNaN(dist));
192 dist = p0.distance(p1);
193 check_equals(typeof(dist), 'undefined');
195 p0 = new Point('10', 'y');
196 p1 = new Point('10', '20');
197 dist = Point.distance(p0, p1);
198 check_equals(typeof(dist), 'number');
199 check(isNaN(dist));
200 dist = p0.distance(p1);
201 check_equals(typeof(dist), 'undefined');
203 p0 = new Point('5', '4');
204 p1 = new Point('4', '7');
205 dist = Point.distance(p0, p1);
206 check_equals(typeof(dist), 'number');
207 check_equals(Math.round(dist*100), 316);
208 dist = p0.distance(p1);
209 check_equals(typeof(dist), 'undefined');
211 p0 = new Point('1', '1');
212 p1 = new Point('10', '1');
213 dist = Point.distance(p0, p1);
214 check_equals(typeof(dist), 'number');
215 check_equals(dist, 9);
217 // Doesn't matter if second arg is an instanceof Point
218 dist = Point.distance(p0, o1);
219 check_equals(typeof(dist), 'number');
220 check_equals(dist, 20);
222 // But first arg *must* be instanceof point !
223 dist = Point.distance(o1, p0);
224 check_equals(typeof(dist), 'undefined');
225 o1.__proto__ = Point.prototype;
226 dist = Point.distance(o1, p0);
227 check_equals(dist, 20);
229 //-------------------------------------------------------------
230 // Test Point.equals
231 //-------------------------------------------------------------
233 o0 = {};
234 o0.valueOf = function() { return 4; };
235 o1 = {};
236 o1.valueOf = function() { return 4; };
238 p0 = new Point(3, o0);
239 check(p0.equals(p0));
241 p1 = new Point(3, o1);
242 check(p1.equals(p1));
244 check(p0 != p1);
245 check_equals(p0.toString(), p1.toString());
247 check(!p0.equals(p1));
248 check(!p1.equals(p0));
250 ret = p0.equals();
251 check_equals(typeof(ret), 'boolean');
252 check(!ret);
254 p2 = new Point(3, o1);
255 check(p1.equals(p2));
256 // Equals doesn't return true if p2 isn't an point
257 p2 = {x:3, y:o1};
258 ret = p1.equals(p2);
259 check_equals(typeof(ret), 'boolean');
260 check(!ret);
261 // But we can cheat ...
262 p2.__proto__ = Point.prototype;
263 check(p1.equals(p2));
264 // ... even with double jump to get there ...
265 o3 = {}; o3.prototype = {}; o3.prototype.__proto__ = Point.prototype;
266 p2.__proto__ = o3.prototype;
267 check(p1.equals(p2));
268 // ... but not with syntetized objects ?
269 String.prototype.x = 3;
270 String.prototype.y = o1;
271 String.prototype.__proto__ = Point.prototype;
272 check(!p1.equals('string'));
274 // A non-point with a point's equals method.
275 fakepoint = {x:20, y:30};
276 fakepoint.equals = Point.prototype.equals;
277 ret = fakepoint.equals(new Point(20,30));
278 check_equals(ret.toString(), "true");
279 check_equals(typeof(ret), "boolean");
281 //-------------------------------------------------------------
282 // Test Point.interpolate (static)
283 //-------------------------------------------------------------
285 ret = Point.interpolate();
286 check(ret instanceof Point);
287 check_equals(ret.toString(), '(x=NaN, y=NaN)');
289 ret = Point.interpolate(1, 2, 3);
290 check(ret instanceof Point);
291 check_equals(ret.toString(), '(x=NaN, y=NaN)');
293 p0 = new Point('x0', 'y0');
294 p1 = new Point('x1', 'y1');
295 ret = Point.interpolate(p0, p1, 3);
296 check(ret instanceof Point);
297 check_equals(ret.toString(), '(x=x1NaN, y=y1NaN)');
299 p0 = new Point('0', '0');
300 p1 = new Point('10', '0');
301 ret = Point.interpolate(p0, p1, 3);
302 check(ret instanceof Point);
303 check_equals(ret.toString(), '(x=10-30, y=00)');
304 ret = Point.interpolate(p0, p1, 0);
305 check(ret instanceof Point);
306 check_equals(ret.toString(), '(x=100, y=00)');
307 ret = Point.interpolate(p0, p1, 0.5);
308 check(ret instanceof Point);
309 check_equals(ret.toString(), '(x=10-5, y=00)');
311 // second arg drives newAdd
312 p0 = new Point(0, 0);
313 p1 = new Point('10', '0');
314 ret = Point.interpolate(p0, p1, 3);
315 check(ret instanceof Point);
316 check_equals(ret.toString(), '(x=10-30, y=00)');
318 // second arg drives newAdd
319 p0 = new Point('0', '0');
320 p1 = new Point(10, 0);
321 ret = Point.interpolate(p0, p1, 3);
322 check(ret instanceof Point);
323 check_equals(ret.toString(), '(x=-20, y=0)');
325 p0 = new Point(0, 0);
326 p1 = new Point(10, 0);
327 ret = Point.interpolate(p0, p1, 0.5);
328 check(ret instanceof Point);
329 check_equals(ret.toString(), '(x=5, y=0)');
331 p0 = new Point(0, 0);
332 p1 = new Point(10, 0);
333 ret = Point.interpolate(p0, p1, 1, 'discarder arg');
334 check(ret.equals(p0));
335 ret = Point.interpolate(p0, p1, 0);
336 check(ret.equals(p1));
337 ret = Point.interpolate(p0, p1);
338 check_equals(ret.toString(), '(x=NaN, y=NaN)');
340 o0 = {x:0, y:10};
341 o1 = {x:10, y:0};
342 ret = Point.interpolate(o0, o1, 1);
343 check_equals(ret.toString(), '(x=0, y=10)');
344 ret = Point.interpolate(o0, o1, 0);
345 check_equals(ret.toString(), '(x=10, y=0)');
346 ret = Point.interpolate(o0, o1, 0.5);
347 check_equals(ret.toString(), '(x=5, y=5)');
350 //-------------------------------------------------------------
351 // Test Point.normalize
352 //-------------------------------------------------------------
354 p0 = new Point(0, 0);
355 p1 = p0.clone();
356 ret = p1.normalize();
357 check_equals(typeof(ret), 'undefined');
358 check(p1.equals(p0));
360 p0 = new Point(0, 0);
361 p1 = p0.clone();
362 ret = p1.normalize(10);
363 check_equals(typeof(ret), 'undefined');
364 check(p1.equals(p0));
366 p0 = new Point(10, 0);
367 p1 = p0.clone();
368 ret = p1.normalize(5);
369 check_equals(typeof(ret), 'undefined');
370 check_equals(p1.toString(), '(x=5, y=0)');
372 p0 = new Point(0, 10);
373 p1 = p0.clone();
374 ret = p1.normalize(-5);
375 check_equals(typeof(ret), 'undefined');
376 check_equals(p1.toString(), '(x=0, y=-5)');
378 p0 = new Point(3, -4);
379 p1 = p0.clone();
380 ret = p1.normalize(-10);
381 check_equals(typeof(ret), 'undefined');
382 check_equals(p1.toString(), '(x=-6, y=8)');
384 p0 = new Point(-10, 0);
385 p1 = p0.clone();
386 ret = p1.normalize(5);
387 check_equals(typeof(ret), 'undefined');
388 check_equals(p1.toString(), '(x=-5, y=0)');
390 p0 = new Point(-10, 0);
391 p1 = p0.clone();
392 ret = p1.normalize('r');
393 check_equals(typeof(ret), 'undefined');
394 check_equals(p1.toString(), '(x=NaN, y=NaN)');
396 p0 = new Point('x', 'y');
397 p1 = p0.clone();
398 ret = p1.normalize(5);
399 check_equals(typeof(ret), 'undefined');
400 check_equals(p1.toString(), '(x=x, y=y)');
402 //-------------------------------------------------------------
403 // Test Point.offset
404 //-------------------------------------------------------------
406 p0 = new Point('x', 'y');
407 ret = p0.offset();
408 check_equals(typeof(ret), 'undefined');
409 check_equals(p0.toString(), '(x=xundefined, y=yundefined)');
411 p0 = new Point('x', 'y');
412 ret = p0.offset('a');
413 check_equals(typeof(ret), 'undefined');
414 check_equals(p0.toString(), '(x=xa, y=yundefined)');
416 p0 = new Point('x', 'y');
417 ret = p0.offset('a', 'b', 3);
418 check_equals(typeof(ret), 'undefined');
419 check_equals(p0.toString(), '(x=xa, y=yb)');
421 p0 = new Point(4, 5);
422 ret = p0.offset('-6', -8);
423 check_equals(typeof(ret), 'undefined');
424 check_equals(p0.toString(), '(x=4-6, y=-3)');
426 // A non-point with a point's offset method (fails)
427 fakepoint = {x:20, y:30};
428 fakepoint.offset = Point.prototype.offset;
429 ret = fakepoint.offset(new Point(1, 3));
430 check_equals(ret.toString(), undefined);
431 check(! ret instanceof Point);
432 //-------------------------------------------------------------
433 // Test Point.polar (static)
434 //-------------------------------------------------------------
436 p0 = Point.polar();
437 check(p0 instanceof Point);
438 check_equals(p0.toString(), '(x=NaN, y=NaN)');
440 p0 = Point.polar(1);
441 check(p0 instanceof Point);
442 check_equals(p0.toString(), '(x=NaN, y=NaN)');
444 p0 = Point.polar(1, 0);
445 check(p0 instanceof Point);
446 check_equals(p0.toString(), '(x=1, y=0)');
448 p0 = Point.polar(1, Math.PI);
449 check(p0 instanceof Point);
450 check_equals(p0.x, -1);
451 check_equals(Math.round(p0.y*100), 0);
453 p0 = Point.polar(1, Math.PI/2);
454 check(p0 instanceof Point);
455 check_equals(Math.round(p0.x*100), 0);
456 check_equals(p0.y, 1);
458 p0 = Point.polar(1, Math.PI*2);
459 check(p0 instanceof Point);
460 check_equals(p0.x, 1);
461 check_equals(Math.round(p0.y*100), 0);
463 p0 = Point.polar(1, Math.PI*1.5);
464 check(p0 instanceof Point);
465 check_equals(Math.round(p0.x*100), 0);
466 check_equals(p0.y, -1);
468 p0 = Point.polar('5', '0');
469 check(p0 instanceof Point);
470 check_equals(p0.x, 5);
471 check_equals(p0.y, 0);
474 //-------------------------------------------------------------
475 // Test Point.subtract
476 //-------------------------------------------------------------
478 p0 = new Point('x', 'y');
479 ret = p0.subtract();
480 check(ret instanceof Point);
481 check_equals(p0.toString(), '(x=x, y=y)');
482 check_equals(ret.toString(), '(x=NaN, y=NaN)');
483 String.prototype.x = 3; // to test it's used
484 ret = p0.subtract('1');
485 delete String.prototype.x;
486 check(ret instanceof Point);
487 check_equals(ret.toString(), '(x=NaN, y=NaN)');
488 check_equals(p0.toString(), '(x=x, y=y)');
489 ret = p0.subtract(1, '2');
490 check(ret instanceof Point);
491 check_equals(ret.toString(), '(x=NaN, y=NaN)');
492 check_equals(p0.toString(), '(x=x, y=y)');
494 p0 = new Point('x', 'y');
495 p1 = new Point('x1', 'y1');
496 ret = p0.subtract(p1);
497 check(ret instanceof Point);
498 check_equals(ret.toString(), '(x=NaN, y=NaN)');
499 check_equals(p0.toString(), '(x=x, y=y)');
500 check_equals(p1.toString(), '(x=x1, y=y1)');
502 p0 = new Point(2, 3);
503 p1 = { x:1, y:1 };
504 ret = p0.subtract(p1);
505 check_equals(ret.toString(), '(x=1, y=2)');
507 ret = p0.subtract(p1, 4, 5, 6);
508 check_equals(ret.toString(), '(x=1, y=2)');
510 //-------------------------------------------------------------
511 // END OF TEST
512 //-------------------------------------------------------------
514 check_totals(187);
516 #endif // OUTPUT_VERSION >= 8