2 // Copyright (C) 2005, 2006, 2007, 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 // Test case for Number ActionScript class
21 // compile this test case with Ming makeswf, and then
22 // execute it like this gnash -1 -r 0 -v out.swf
25 // TODO: have ming output ACTION_EQUAL
26 // it seems it will only do it for SWF4 output
28 // TODO: test with SWF target != 6 (the only one tested so far)
32 rcsid
="$Id: Number.as,v 1.53 2008/04/02 10:55:32 strk Exp $";
35 Number.hasOwnProperty
= ASnative
(101, 5);
37 check
(Number.hasOwnProperty
('MAX_VALUE'));
38 check_equals
(typeof(Number.MAX_VALUE
), "number");
39 // gnash fails in rounding (ends in 2316, we round up to 232)
40 // TODO: check that we're close, if not extremely accurate
41 xcheck_equals
(Number.MAX_VALUE
.toString
(), "1.79769313486231e+308");
43 check
(Number.hasOwnProperty
('MIN_VALUE'));
44 check_equals
(typeof(Number.MIN_VALUE
), "number");
45 check_equals
(Number.MIN_VALUE
.toString
(), "4.94065645841247e-324"); // LNX 10,0,12,10 gives 0 here, 9.0.115.0 succeeds
47 check
(Number.hasOwnProperty
('NEGATIVE_INFINITY'));
48 check_equals
(Number.NEGATIVE_INFINITY
, -Infinity);
50 check
(Number.hasOwnProperty
('NaN'));
51 check_equals
(Number.NaN, NaN);
53 check
(Number.hasOwnProperty
('POSITIVE_INFINITY'));
54 check_equals
(Number.POSITIVE_INFINITY
, Infinity);
56 var n1
=new Number(268);
57 check_equals
(typeof(n1
), 'object');
58 var n1prim
= Number(268);
59 check_equals
(typeof(n1prim
), 'number');
60 // gnash used to fail below because it compares 2 objects
61 // rather then an object and a primitive
62 check_equals
(n1
, n1prim
);
64 // strict-equality operator was introduced in SWF6
65 #if OUTPUT_VERSION
> 5
66 check
( ! (n1
=== 268) );
67 // They are not the same object !
68 check
( ! (n1
=== Number(268)) );
71 // but they have the same numeric value
72 check_equals
(n1
, 268 );
73 check_equals
(268 , n1
);
75 //------------------------------------
76 // Test Number.toString
77 //------------------------------------
79 check_equals
(typeof(n1
.toString
), "function");
80 check_equals
(typeof(n1
.toString
()), "string");
81 check_equals
(n1
.toString
(), "268");
83 var backup
= Object.prototype
.toString
;
84 Object.prototype
.toString
= function() { return "fake_string"; };
85 check_equals
(n1
.toString
(), "268"); // doesn't inherit from Object
86 Object.prototype
.toString
= backup
;
88 #if OUTPUT_VERSION
>= 6
89 check
(Number.prototype
.hasOwnProperty
('toString'));
93 check_equals
(tmp
.toString
(2), '1010');
95 check_equals
(tmp
.toString
(2), '110');
96 check_equals
(tmp
.toString
(3), '20');
97 check_equals
(tmp
.toString
(8), '6');
98 check_equals
(tmp
.toString
(-2), '6'); // invalid, returns 10 ?
99 check_equals
(tmp
.toString
(0), '6'); // invalid, returns 10 ?
100 check_equals
(tmp
.toString
(5), '11');
102 check_equals
(tmp
.toString
(2), '-101');
103 check_equals
(tmp
.toString
(16), '-5');
105 check_equals
(tmp
.toString
(16), '-b');
107 //------------------------------------
108 // Test Number.valueOf
109 //------------------------------------
111 check_equals
(typeof(n1
.valueOf
), "function");
112 check_equals
(typeof(n1
.__proto__
.valueOf
), "function");
113 check_equals
(typeof(n1
.__proto__
.__proto__
.valueOf
), "function");
114 check_equals
(typeof(n1
.valueOf
()), "number");
115 check_equals
(n1
.valueOf
(), 268);
117 #if OUTPUT_VERSION
>= 6
118 check
(Number.prototype
.hasOwnProperty
('valueOf'));
119 check
(Object.prototype
.hasOwnProperty
('valueOf'));
122 var backup
= Object.prototype
.valueOf
;
123 Object.prototype
.valueOf
= function() { return "fake_value"; };
124 check_equals
(n1
.valueOf
(), 268); // doesn't inherit from Object
125 Object.prototype
.valueOf
= backup
;
127 backup
= Number.prototype
.valueOf
;
128 Number.prototype
.valueOf
= function() { return "fake_value"; };
129 check_equals
(n1
.valueOf
(), "fake_value"); // does inherit from Number
130 Number.prototype
.valueOf
= backup
;
132 // Check unary minus operator
134 check_equals
(-268 , n1
);
135 check_equals
(n1
.toString
(), "-268");
137 //---------------------------------------
139 //---------------------------------------
141 check_equals
( typeof(NaN), 'number' );
142 check_equals
( typeof(isNaN
), 'function' );
143 check_equals
( typeof(isNaN
(NaN)), 'boolean' );
145 check
((0/2) == (0/5));
146 check
(! (NaN != NaN) );
148 check_equals
( typeof(isNaN
(0/0)), 'boolean' );
152 #if OUTPUT_VERSION
>= 6
153 check_equals
( typeof(_global
.NaN), 'number' );
154 check_equals
( typeof(isNaN
(_global
.NaN)), 'boolean' );
155 check
( isNaN
(_global
.NaN) ); // NOTE: isNaN(undefined) is true for SWF7 up
156 #else // SWF5 or below
157 check_equals
( typeof(_global
), 'undefined' );
158 check_equals
( typeof(Object), 'function' );
159 check_equals
( typeof(Object.prototype
), 'object' );
160 check_equals
( typeof(Object.prototype
.NaN), 'undefined' );
163 #if OUTPUT_VERSION
>= 7
164 check
( isNaN
(undefined) );
165 check
( isNaN
(null) );
166 check
( isNaN
(Object.prototype
.NaN) );
167 #else // SWF6 or below
168 check
( ! isNaN
(undefined) );
169 check
( ! isNaN
(null) );
170 check
( ! isNaN
(Object.prototype
.NaN) );
173 check
(! Object.hasOwnProperty
('NaN'));
174 check
(! Object.prototype
.hasOwnProperty
('NaN'));
175 check
(! this.__proto__
.hasOwnProperty
('NaN'));
177 //---------------------------------------
179 //---------------------------------------
181 check_equals
( typeof(Infinity), 'number' );
182 check_equals
( typeof(isFinite
), 'function' );
183 check_equals
( typeof(isFinite
(Infinity)), 'boolean' );
184 check_equals
(Infinity, Infinity);
185 check
( ! isFinite
(Infinity) );
186 check_equals
( typeof(isFinite
(0/0)), 'boolean' );
187 check
( ! isFinite
(0/0) );
190 #if OUTPUT_VERSION
>= 6
191 check_equals
( typeof(_global
.Infinity), 'number' );
192 check_equals
( typeof(isFinite
(_global
.Infinity)), 'boolean' );
193 check
( ! isFinite
(_global
.Infinity) ); // NOTE: isFinite(undefined) is false for SWF7 up
194 #else // SWF5 or below
195 check_equals
( typeof(_global
), 'undefined' );
196 check_equals
( typeof(Object), 'function' );
197 check_equals
( typeof(Object.prototype
), 'object' );
198 check_equals
( typeof(Object.prototype
.Infinity), 'undefined' );
201 #if OUTPUT_VERSION
>= 7
202 check
( ! isFinite
(undefined) );
203 check
( ! isFinite
(null) );
204 check
( ! isFinite
(Object.prototype
.NaN) );
205 #else // SWF6 or below
206 check
( isFinite
(undefined) );
207 check
( isFinite
(null) );
208 check
( isFinite
(Object.prototype
.NaN) );
211 check
(! Object.hasOwnProperty
('Infinity'));
212 check
(! Object.prototype
.hasOwnProperty
('Infinity'));
213 check
(! this.__proto__
.hasOwnProperty
('Infinity'));
215 //--------------------------------------------------------
216 // Test automatic conversion to number
217 //--------------------------------------------------------
219 check
(isNaN
(0+this));
221 this.valueOf
= function() { return 5; };
226 o
.valueOf
= function() { return 3; };
227 check_equals
(0+o
, 3);
228 check_equals
(0+"string", "0string");
230 #if OUTPUT_VERSION
< 6
231 check
(!isNaN
(2+Number));
233 check
(isNaN
(2+Number));
236 #if OUTPUT_VERSION
>= 7
237 check
(isNaN
(2/undefined));
238 check
(isNaN
(undefined/2));
239 check
(!isFinite
(undefined/2));
240 check
(2/undefined != Infinity);
242 check
(!isNaN
(2/undefined));
243 check
(!isNaN
(undefined/2));
244 check
(isFinite
(undefined/2));
245 check_equals
(2/undefined, Infinity);
248 check
(!isFinite
(Infinity));
249 check
(!isFinite
(2/undefined));
251 check_equals
(typeof(("string"<7)), 'undefined');
252 check_equals
(typeof((7<"string")), 'undefined');
253 check_equals
(typeof(("18"<7)), 'boolean');
254 check_equals
(typeof((7<"18")), 'boolean');
255 check_equals
(("18"<"7"), true); // string comparison
256 check_equals
(("18"<7), false); // numeric comparison
257 check_equals
((7<"18"), true); // numeric comparison
258 check_equals
(typeof(_root
<"18"), 'undefined'); // _root is ensured to be NAN for SWF6 too
260 #if OUTPUT_VERSION
> 6
261 check_equals
(typeof(undefined<7), 'undefined');
262 check_equals
(typeof(undefined>7), 'undefined');
263 check_equals
(typeof(undefined<-7), 'undefined');
264 check_equals
(typeof(undefined>-7), 'undefined');
265 check_equals
(typeof(7<undefined), 'undefined');
266 check_equals
(typeof(7>undefined), 'undefined');
267 check_equals
(typeof(-7<undefined), 'undefined');
268 check_equals
(typeof(-7>undefined), 'undefined');
269 check_equals
(typeof(null<7), 'undefined');
270 check_equals
(typeof(null>7), 'undefined');
271 check_equals
(typeof(null<-7), 'undefined');
272 check_equals
(typeof(null>-7), 'undefined');
273 check_equals
(typeof(7<null), 'undefined');
274 check_equals
(typeof(7>null), 'undefined');
275 check_equals
(typeof(-7<null), 'undefined');
276 check_equals
(typeof(-7>null), 'undefined');
278 check_equals
(typeof(undefined<7), 'boolean');
279 check_equals
(typeof(undefined>7), 'boolean');
280 check_equals
(typeof(undefined<-7), 'boolean');
281 check_equals
(typeof(undefined>-7), 'boolean');
282 check_equals
(typeof(7<undefined), 'boolean');
283 check_equals
(typeof(7>undefined), 'boolean');
284 check_equals
(typeof(-7<undefined), 'boolean');
285 check_equals
(typeof(-7>undefined), 'boolean');
287 check_equals
((undefined<7), true);
288 check_equals
((undefined>7), false);
289 check_equals
((undefined<-7), false);
290 check_equals
((undefined>-7), true);
291 check_equals
((7<undefined), false);
292 check_equals
((7>undefined), true);
293 check_equals
((-7<undefined), true);
294 check_equals
((-7>undefined), false);
298 check_equals
('0' + -1, '0-1');
300 // string:00 number:0 equality
301 check_equals
('00', 0);
303 // string:0xFF0000 number:0xFF0000 equality
304 #if OUTPUT_VERSION
> 5
305 check_equals
("0xFF0000", 0xFF0000);
306 check_equals
("0XFF0000", 0xFF0000);
307 check_equals
("0Xff0000", 0xFF0000);
308 check
("0Xff000000" != 0xFF000000);
309 check_equals
("07700000000", 07700000000);
310 check
("077000000000" != 077000000000);
312 check
("0xFF0000" != 0xFF0000);
313 check
("0XFF0000" != 0xFF0000);
316 check
(isNaN
("0xff000z"));
318 check_equals
(typeof(Number.prototype
.valueOf
), 'function');
319 check_equals
(typeof(Number.prototype
.toString
), 'function');
320 #if OUTPUT_VERSION
> 5
321 check
(isNaN
(Number.valueOf
()));
323 check
(!isNaN
(Number.valueOf
()) );
326 check_equals
(typeof(Number), 'function');
327 check
(Number.hasOwnProperty
('prototype'));
328 check
(Number.hasOwnProperty
('__proto__'));
329 check
(Number.hasOwnProperty
('constructor'));
330 check_equals
(typeof(Number.prototype
), 'object');
331 #if OUTPUT_VERSION
== 5
332 check_equals
(Number.prototype
, Object);
333 check_equals
(Number.prototype
, Function);
334 check_equals
(Number.prototype
, Object.__proto__
);
335 check_equals
(Number.prototype
, null);
336 check_equals
(Number.prototype
, undefined);
337 check
(Number.prototype
!= 0);
338 check
(Number.prototype
!= "string");
340 check
(Number.prototype
!= Object);
343 #if OUTPUT_VERSION
== 5
344 // not visible in swf5 by default.
345 check_equals
(typeof(Number.valueOf
), 'undefined');
346 check_equals
(typeof(Number.toString
), 'undefined');
347 check_equals
(typeof(Number.__proto__
), 'undefined');
348 // make properties visible.
349 ASSetPropFlags
(Number, null, 0, 128 + 1);
350 ASSetPropFlags
(Object, null, 0, 128 + 1);
352 check_equals
(typeof(Number.__proto__
), 'object');
353 check_equals
(Number.prototype
.__proto__
, Object.prototype
);
355 check_equals
(typeof(Number.toString
), 'function');
356 check_equals
(typeof(Number.valueOf
), 'function');
357 check
(!Number.hasOwnProperty
('valueOf'));
358 check
(!Number.hasOwnProperty
('toString'));
359 check
(!Number.__proto__
.hasOwnProperty
('valueOf'));
360 check
(!Number.__proto__
.hasOwnProperty
('toString'));
361 check
(Number.__proto__
.__proto__
== Object.prototype
);
363 check_equals
(typeof(Number.valueOf
()), 'function'); // this is odd
366 check_equals
(typeof(a
.toString
), 'function');
367 check_equals
(typeof(a
.valueOf
), 'function');
368 check
(!a
.hasOwnProperty
('valueOf'));
369 #if OUTPUT_VERSION
== 5
370 // make properties visible.
371 ASSetPropFlags
(Number.prototype
, null, 0, 128 + 1);
372 ASSetPropFlags
(Object.prototype
, null, 0, 128 + 1);
374 check
(a
.__proto__
.hasOwnProperty
('valueOf'));
375 check
(!a
.hasOwnProperty
('toString'));
378 anum
= new Number(1);
379 check_equals
(typeof(anum
.toString
), 'function');
380 check_equals
(typeof(anum
.valueOf
), 'function');
381 check
(!anum
.hasOwnProperty
('valueOf'));
382 check
(anum
.__proto__
.hasOwnProperty
('valueOf'));
383 check
(!anum
.hasOwnProperty
('toString'));
386 //-----------------------------------------------------------
387 // Check conversion to number
388 //-----------------------------------------------------------
390 #ifdef MING_SUPPORTS_ASM
392 asm
{ push
'val',4 tonumber setvariable
};
393 check_equals
(val
, 4);
395 asm
{ push
'val',null tonumber setvariable
};
396 #if OUTPUT_VERSION
< 7
397 check_equals
(val
, 0);
403 asm
{ push
'val',undefined tonumber setvariable
};
404 #if OUTPUT_VERSION
< 7
405 check_equals
(val
, 0);
411 asm
{ push
'val','10' tonumber setvariable
};
412 check_equals
(val
, 10);
414 asm
{ push
'val','3e2' tonumber setvariable
};
415 check_equals
(val
, 300);
417 asm
{ push
'val','2E1' tonumber setvariable
};
418 check_equals
(val
, 20);
420 asm
{ push
'val','2p' tonumber setvariable
};
423 asm
{ push
'val','2.6' tonumber setvariable
};
424 check_equals
(val
, 2.6);
426 asm
{ push
'val','string' tonumber setvariable
};
429 asm
{ push
'val','NaN' tonumber setvariable
};
432 asm
{ push
'val','Infinity' tonumber setvariable
};
433 check
(val
!= Infinity);
436 asm
{ push
'val','-Infinity' tonumber setvariable
};
437 check
(val
!= Infinity);
441 asm
{ push
'val','obj' getvariable tonumber setvariable
};
444 obj
= new Object(); obj
.valueOf
= function() { return 7; };
445 asm
{ push
'val','obj' getvariable tonumber setvariable
};
446 check_equals
(val
, 7);
449 asm
{ push
'val','obj' getvariable tonumber setvariable
};
450 #if OUTPUT_VERSION
> 5
454 check_equals
(typeof(val
), 'number');
455 check_equals
(val
, 0);
456 check_equals
(0, val
);
459 obj
= function() {}; obj
.valueOf
= function() { return 9; };
460 asm
{ push
'val','obj' getvariable tonumber setvariable
};
461 check_equals
(val
, 9);
462 check_equals
(9, val
);
464 #endif
// defined(MING_SUPPORTS_ASM)
466 //-----------------------------------------------------------
467 // Check subtraction operator TODO: create an ops.as file
468 // for testing operators in general ?
469 //-----------------------------------------------------------
471 #if OUTPUT_VERSION
> 6
472 check
( isNaN
(450 - undefined) );
474 check_equals
(450 - undefined, 450);
477 //-----------------------------------------------------------
478 // Check number formatting as documented in as_value.cpp.
480 // Numbers should be rounded to 15 significant digits.
481 // Numbers above 10e+15 are expressed with exponent.
482 // Numbers below 0 with more than 4 leading zeros expressed
484 // Exponent has no leading zero.
485 // Trailing zeros are always trimmed.
486 //-----------------------------------------------------------
488 a
=new Number(11111111111111.11111111);
489 check_equals
(a
.toString
(), "11111111111111.1");
491 a
=new Number(111111111111111.1111111);
492 check_equals
(a
.toString
(), "111111111111111");
494 a
=new Number(1111111111111111.1111111);
495 check_equals
(a
.toString
(), "1.11111111111111e+15");
497 a
=new Number(0.000123456789012346);
498 check_equals
(a
.toString
(), "0.000123456789012346");
500 a
=new Number(0.0000123456789012346);
501 check_equals
(a
.toString
(), "0.0000123456789012346");
503 a
=new Number(0.00000123456789012346);
504 check_equals
(a
.toString
(), "1.23456789012346e-6");
506 a
=new Number(0.000000123456789012346);
507 check_equals
(a
.toString
(), "1.23456789012346e-7");
509 a
=new Number(0.0999999999999999);
510 check_equals
(a
.toString
(), "0.0999999999999999");
512 a
=new Number(0.99999999999999938);
513 check_equals
(a
.toString
(), "0.999999999999999");
515 a
=new Number(9.9999999999999939 / 10);
516 check_equals
(a
.toString
(), "0.999999999999999");
518 a
=new Number(5.4 / 100000);
519 check_equals
(a
.toString
(), "0.000054");
521 a
=new Number(5.4 / 1000000);
522 check_equals
(a
.toString
(), "5.4e-6");
523 check_equals
(a
.toString
(10), "5.4e-6");
525 a
=new Number(2.123456789123455);
526 check_equals
(a
.toString
(), "2.12345678912346"); // rounds abs up
528 a
=new Number(-2.123456789123455);
529 check_equals
(a
.toString
(), "-2.12345678912346"); // rounds abs up
531 a
=new Number(0.1234567891234565);
532 check_equals
(a
.toString
(), "0.123456789123457"); // rounds abs up
534 a
=new Number(-0.1234567891234565);
535 check_equals
(a
.toString
(), "-0.123456789123457"); // round abs up
537 a
=new Number(1.234567891234565e+308);
538 check_equals
(a
.toString
(), "1.23456789123456e+308"); // round abs down
540 a
=new Number(-1.234567891234565e+308);
541 check_equals
(a
.toString
(), "-1.23456789123456e+308"); // round abs down
543 a
=new Number(1.234567891234565e-308); // gnash rounds this down, pp up
544 xcheck_equals
(a
.toString
(), "1.23456789123457e-308"); // round abs up
546 a
=new Number(-1.234567891234565e-308); // gnash rounds this down, pp up
547 xcheck_equals
(a
.toString
(), "-1.23456789123457e-308"); // round abs up
549 a
=new Number(1.234567891234565e-6); // gnash rounds this down, pp up
550 xcheck_equals
(a
.toString
(), "1.23456789123457e-6"); // round abs up
552 a
=new Number(-1.234567891234565e-6); // gnash rounds this down, pp up
553 xcheck_equals
(a
.toString
(), "-1.23456789123457e-6"); // round abs up
555 a
=new Number(-1.244567891234565e-6); // gnash rounds this down, pp up
556 check_equals
(a
.toString
(), "-1.24456789123457e-6"); // round abs up
558 a
=new Number(-1.244667891234565e-6); // gnash rounds this down, pp up
559 check_equals
(a
.toString
(), "-1.24466789123457e-6"); // round abs up
561 a
=new Number(-0.001234567891234565); // gnash succeeds in rounding this up
562 check_equals
(a
.toString
(), "-0.00123456789123457"); // round abs up
564 a
=new Number(-0.001244567891234565); // gnash succeeds in rounding this down
565 check_equals
(a
.toString
(), "-0.00124456789123456");
567 a
=new Number(-0.001234567891234564);
568 check_equals
(a
.toString
(), "-0.00123456789123456"); // round abs down (obvious?)
570 a
=new Number(0.001234567891234565);
571 check_equals
(a
.toString
(), "0.00123456789123457"); // round abs up
573 a
=new Number(0.001234567891234564);
574 check_equals
(a
.toString
(), "0.00123456789123456"); // round abs down (obvious?)
594 a
=new Number(new String(" 2"));
597 a
=new Number(new String(" -2"));
600 a
=new Number(new String(" -2e-5"));
601 check_equals
(a
, -0.00002);
603 a
=new Number(new String(" -2e5"));
604 check_equals
(a
, -200000);
606 a
=new Number(new String("-56e"));
607 check_equals
(a
, -56);
609 a
=new Number(new String("-56eu"));
610 check_equals
(a
.toString
(), "NaN");
612 a
=new Number(new String("-56e-"));
613 check_equals
(a
, -56);
615 a
=new Number(new String("-56e+"));
616 check_equals
(a
, -56);
620 #if OUTPUT_VERSION
< 6
627 #if OUTPUT_VERSION
< 6
633 a
=new Number(" 0x2");
640 #if OUTPUT_VERSION
< 6
646 a
=Number("0x-ffffffff");
647 #if OUTPUT_VERSION
< 6
654 #if OUTPUT_VERSION
< 6
661 #if OUTPUT_VERSION
< 6
662 check_equals
(a
, -77);
664 check_equals
(a
, -63);
668 #if OUTPUT_VERSION
< 6
681 #if OUTPUT_VERSION
< 6
684 #if OUTPUT_VERSION
< 7