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
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 ECMA Section: 9.3 Type Conversion: ToNumber
43 Description: rules for converting an argument to a number.
44 see 9.3.1 for cases for converting strings to numbers.
48 Boolean 1 if true; +0 if false
49 Number the argument ( no conversion )
54 Test cases provided by waldemar.
57 Author: christine@netscape.com
62 var SECTION = "9.3.1-3";
63 var VERSION = "ECMA_1";
64 var BUGNUMBER="129087";
66 var TITLE = "Number To String, String To Number";
70 writeHeaderToLog( SECTION + " "+ TITLE);
72 // test case from http://scopus.mcom.com/bugsplat/show_bug.cgi?id=312954
77 "var z = 0; print(1/-z)",
85 // test cases from bug http://scopus.mcom.com/bugsplat/show_bug.cgi?id=122882
89 new TestCase( SECTION,
94 new TestCase( SECTION,
99 new TestCase( SECTION,
100 '- "-0x123456789abcde8"',
102 - "-0x123456789abcde8" );
104 new TestCase( SECTION,
105 '- "+0x123456789abcde8"',
107 - "+0x123456789abcde8" );
109 // Convert some large numbers to string
112 new TestCase( SECTION,
117 new TestCase( SECTION,
122 new TestCase( SECTION,
127 new TestCase( SECTION,
132 new TestCase( SECTION,
137 new TestCase( SECTION,
142 new TestCase( SECTION,
147 // convert some strings to large numbers
149 new TestCase( SECTION,
154 new TestCase( SECTION,
159 new TestCase( SECTION,
164 new TestCase( SECTION,
165 "parseFloat(\"1e2000\")",
167 parseFloat("1e2000") );
169 new TestCase( SECTION,
170 "parseFloat(\"1e-2000\")",
172 parseFloat("1e-2000") );
174 new TestCase( SECTION,
175 "1.7976931348623157E+308",
176 1.7976931348623157e+308,
177 1.7976931348623157E+308 );
179 new TestCase( SECTION,
180 "1.7976931348623158e+308",
181 1.7976931348623157e+308,
182 1.7976931348623158e+308 );
184 new TestCase( SECTION,
185 "1.7976931348623159e+308",
187 1.7976931348623159e+308 );
190 "17976931348623158079372897140530341507993413271003782693617377898044496829276475094664901797758720709633028641669288791094655554785194040263065748867150582068";
193 print("-s = " + (-s));
195 new TestCase( SECTION,
196 "s = " + s +"; s +="+
197 "\"190890200070838367627385484581771153176447573027006985557136695962284291481986083493647529271907416844436551070434271155969950809304288017790417449779\""+
200 "17976931348623158079372897140530341507993413271003782693617377898044496829276475094664901797758720709633028641669288791094655554785194040263065748867150582068190890200070838367627385484581771153176447573027006985557136695962284291481986083493647529271907416844436551070434271155969950809304288017790417449779",
202 "190890200070838367627385484581771153176447573027006985557136695962284291481986083493647529271907416844436551070434271155969950809304288017790417449779"
208 print("-s1 = " + (-s1));
210 new TestCase( SECTION,
212 "179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791",
215 /***** This answer is preferred but -Infinity is also acceptable here *****/
217 new TestCase( SECTION,
218 "-s1 == Infinity || s1 == 1.7976931348623157e+308",
220 -s1 == Infinity || s1 == 1.7976931348623157e+308 );
225 print("-s2 = " + (-s2));
227 new TestCase( SECTION,
229 "179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497792",
232 // ***** This answer is preferred but -1.7976931348623157e+308 is also acceptable here *****
233 new TestCase( SECTION,
234 "-s2 == -Infinity || -s2 == -1.7976931348623157e+308 ",
236 -s2 == -Infinity || -s2 == -1.7976931348623157e+308 );
241 print("-s3 = " + (-s3));
243 new TestCase( SECTION,
245 "179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497793",
248 //***** This answer is preferred but -1.7976931348623157e+308 is also acceptable here *****
250 new TestCase( SECTION,
251 "-s3 == -Infinity || -s3 == -1.7976931348623157e+308",
253 -s3 == -Infinity || -s3 == -1.7976931348623157e+308 );
256 //***** This answer is preferred but Infinity is also acceptable here *****
258 new TestCase( SECTION,
259 "parseInt(s1,10) == 1.7976931348623157e+308 || parseInt(s1,10) == Infinity",
261 parseInt(s1,10) == 1.7976931348623157e+308 || parseInt(s1,10) == Infinity );
263 //***** This answer is preferred but 1.7976931348623157e+308 is also acceptable here *****
264 new TestCase( SECTION,
265 "parseInt(s2,10) == Infinity || parseInt(s2,10) == 1.7976931348623157e+308",
267 parseInt(s2,10) == Infinity || parseInt(s2,10) == 1.7976931348623157e+308 );
269 //***** This answer is preferred but Infinity is also acceptable here *****
271 new TestCase( SECTION,
272 "parseInt(s1) == 1.7976931348623157e+308 || parseInt(s1) == Infinity",
274 parseInt(s1) == 1.7976931348623157e+308 || parseInt(s1) == Infinity);
276 //***** This answer is preferred but 1.7976931348623157e+308 is also acceptable here *****
277 new TestCase( SECTION,
278 "parseInt(s2) == Infinity || parseInt(s2) == 1.7976931348623157e+308",
280 parseInt(s2) == Infinity || parseInt(s2) == 1.7976931348623157e+308 );
282 new TestCase( SECTION,
287 new TestCase( SECTION,
292 new TestCase( SECTION,
297 new TestCase( SECTION,
302 new TestCase( SECTION,
303 "077777777777777777",
305 077777777777777777 );
307 new TestCase( SECTION,
308 "077777777777777776",
310 077777777777777776 );
312 new TestCase( SECTION,
317 new TestCase( SECTION,
322 new TestCase( SECTION,
327 new TestCase( SECTION,
332 new TestCase( SECTION,
337 new TestCase( SECTION,
342 /***** These test the round-to-nearest-or-even-if-equally-close rule *****/
344 new TestCase( SECTION,
345 "0x1000000000000080",
347 0x1000000000000080 );
349 new TestCase( SECTION,
350 "0x1000000000000081",
352 0x1000000000000081 );
354 new TestCase( SECTION,
355 "0x1000000000000100",
357 0x1000000000000100 );
358 new TestCase( SECTION,
359 "0x100000000000017f",
361 0x100000000000017f );
363 new TestCase( SECTION,
364 "0x1000000000000180",
366 0x1000000000000180 );
368 new TestCase( SECTION,
369 "0x1000000000000181",
371 0x1000000000000181 );
373 new TestCase( SECTION,
374 "0x10000000000001f0",
376 0x10000000000001f0 );
378 new TestCase( SECTION,
379 "0x1000000000000200",
381 0x1000000000000200 );
383 new TestCase( SECTION,
384 "0x100000000000027f",
386 0x100000000000027f );
388 new TestCase( SECTION,
389 "0x1000000000000280",
391 0x1000000000000280 );
393 new TestCase( SECTION,
394 "0x1000000000000281",
396 0x1000000000000281 );
398 new TestCase( SECTION,
399 "0x10000000000002ff",
401 0x10000000000002ff );
403 new TestCase( SECTION,
404 "0x1000000000000300",
406 0x1000000000000300 );
408 new TestCase( SECTION,
409 "0x10000000000000000",
410 18446744073709552000,
411 0x10000000000000000 );
413 new TestCase( SECTION,
414 "parseInt(\"000000100000000100100011010001010110011110001001101010111100\",2)",
416 parseInt("000000100000000100100011010001010110011110001001101010111100",2) );
418 new TestCase( SECTION,
419 "parseInt(\"000000100000000100100011010001010110011110001001101010111101\",2)",
421 parseInt("000000100000000100100011010001010110011110001001101010111101",2) );
423 new TestCase( SECTION,
424 "parseInt(\"000000100000000100100011010001010110011110001001101010111111\",2)",
426 parseInt("000000100000000100100011010001010110011110001001101010111111",2) );
428 new TestCase( SECTION,
429 "parseInt(\"0000001000000001001000110100010101100111100010011010101111010\",2)",
431 parseInt("0000001000000001001000110100010101100111100010011010101111010",2));
433 new TestCase( SECTION,
434 "parseInt(\"0000001000000001001000110100010101100111100010011010101111011\",2)",
436 parseInt("0000001000000001001000110100010101100111100010011010101111011",2) );
438 new TestCase( SECTION,
439 "parseInt(\"0000001000000001001000110100010101100111100010011010101111100\",2)",
441 parseInt("0000001000000001001000110100010101100111100010011010101111100",2));
443 new TestCase( SECTION,
444 "parseInt(\"0000001000000001001000110100010101100111100010011010101111110\",2)",
446 parseInt("0000001000000001001000110100010101100111100010011010101111110",2));
448 new TestCase( SECTION,
449 "parseInt(\"yz\",35)",
453 new TestCase( SECTION,
454 "parseInt(\"yz\",36)",
458 new TestCase( SECTION,
459 "parseInt(\"yz\",37)",
463 new TestCase( SECTION,
468 new TestCase( SECTION,
469 "parseInt(\"-77\",9)",
473 new TestCase( SECTION,
474 "parseInt(\"\\u20001234\\u2000\")",
476 parseInt("\u20001234\u2000") );
478 new TestCase( SECTION,
479 "parseInt(\"123456789012345678\")",
481 parseInt("123456789012345678") );
483 new TestCase( SECTION,
488 new TestCase( SECTION,
493 new TestCase( SECTION,
494 "parseInt(\"1.9999999999999999999\")",
496 parseInt("1.9999999999999999999") );
498 new TestCase( SECTION,
499 "parseInt(\"0x10\")",
503 new TestCase( SECTION,
504 "parseInt(\"0x10\",10)",
506 parseInt("0x10",10) );
508 new TestCase( SECTION,
509 "parseInt(\"0022\")",
513 new TestCase( SECTION,
514 "parseInt(\"0022\",10)",
516 parseInt("0022",10) );
518 new TestCase( SECTION,
519 "parseInt(\"0x1000000000000080\")",
521 parseInt("0x1000000000000080") );
523 new TestCase( SECTION,
524 "parseInt(\"0x1000000000000081\")",
526 parseInt("0x1000000000000081") );
529 "0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
531 new TestCase( SECTION, "s = "+
532 "\"0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\";"+
534 "0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
538 new TestCase( SECTION, "s +="+
539 "\"0000000000000000000000000000000000000\"; s",
540 "0xFFFFFFFFFFFFF800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
541 s += "0000000000000000000000000000000000000" );
543 new TestCase( SECTION, "-s",
544 -1.7976931348623157e+308,
548 "0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
550 new TestCase( SECTION, "s ="+
551 "\"0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\";"+
553 "0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
556 new TestCase( SECTION,
557 "s += \"0000000000000000000000000000000000001\"",
558 "0xFFFFFFFFFFFFF800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
559 s += "0000000000000000000000000000000000001" );
561 new TestCase( SECTION,
563 -1.7976931348623157e+308,
567 "0xFFFFFFFFFFFFFC0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
569 new TestCase( SECTION,
571 "\"0xFFFFFFFFFFFFFC0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\";"+
573 "0xFFFFFFFFFFFFFC0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
577 new TestCase( SECTION,
578 "s += \"0000000000000000000000000000000000000\"",
579 "0xFFFFFFFFFFFFFC00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
580 s += "0000000000000000000000000000000000000");
583 new TestCase( SECTION,
589 "0xFFFFFFFFFFFFFB0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
591 new TestCase( SECTION,
593 "\"0xFFFFFFFFFFFFFB0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\";s",
594 "0xFFFFFFFFFFFFFB0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
597 new TestCase( SECTION,
598 "s += \"0000000000000000000000000000000000001\"",
599 "0xFFFFFFFFFFFFFB00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
600 s += "0000000000000000000000000000000000001" );
602 new TestCase( SECTION,
604 -1.7976931348623157e+308,
607 new TestCase( SECTION,
609 "0xFFFFFFFFFFFFFB000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010",
612 new TestCase( SECTION,
617 new TestCase( SECTION,
622 new TestCase( SECTION,
627 new TestCase( SECTION,
632 new TestCase( SECTION,
637 new TestCase( SECTION,
642 new TestCase( SECTION,
647 new TestCase( SECTION,
652 new TestCase( SECTION,
657 new TestCase( SECTION,
662 new TestCase( SECTION,
667 new TestCase( SECTION,
672 new TestCase( SECTION,
677 new TestCase( SECTION,
682 new TestCase( SECTION,
687 new TestCase( SECTION,
688 "- -\"0x100000000\"",
692 new TestCase( SECTION,
693 "- \"-0x123456789abcde8\"",
695 - "-0x123456789abcde8" );
697 new TestCase( SECTION,
698 "- \"+0x123456789abcde8\"",
700 - "+0x123456789abcde8" );
702 // the following two tests are not strictly ECMA 1.0
704 new TestCase( SECTION,
705 "-\"\\u20001234\\u2001\"",
707 -"\u20001234\u2001" );
709 new TestCase( SECTION,
710 "-\"\\u20001234\\0\"",
714 new TestCase( SECTION,
719 new TestCase( SECTION,
724 new TestCase( SECTION,
729 new TestCase( SECTION,
734 new TestCase( SECTION,
739 new TestCase( SECTION,