Home | History | Annotate | Download | only in TypeConversion
      1 /* The contents of this file are subject to the Netscape Public
      2  * License Version 1.1 (the "License"); you may not use this file
      3  * except in compliance with the License. You may obtain a copy of
      4  * the License at http://www.mozilla.org/NPL/
      5  *
      6  * Software distributed under the License is distributed on an "AS
      7  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      8  * implied. See the License for the specific language governing
      9  * rights and limitations under the License.
     10  *
     11  * The Original Code is Mozilla Communicator client code, released March
     12  * 31, 1998.
     13  *
     14  * The Initial Developer of the Original Code is Netscape Communications
     15  * Corporation. Portions created by Netscape are
     16  * Copyright (C) 1998 Netscape Communications Corporation. All
     17  * Rights Reserved.
     18  *
     19  * Contributor(s):
     20  *
     21  */
     22 /**
     23     File Name:          9.3.js
     24     ECMA Section:       9.3  Type Conversion:  ToNumber
     25     Description:        rules for converting an argument to a number.
     26                         see 9.3.1 for cases for converting strings to numbers.
     27                         special cases:
     28                         undefined           NaN
     29                         Null                NaN
     30                         Boolean             1 if true; +0 if false
     31                         Number              the argument ( no conversion )
     32                         String              see test 9.3.1
     33                         Object              see test 9.3-1
     34 
     35                         For ToNumber applied to the String type, see test 9.3.1.
     36                         For ToNumber applied to the object type, see test 9.3-1.
     37 
     38     Author:             christine (at) netscape.com
     39     Date:               10 july 1997
     40 
     41 */
     42     var SECTION = "9.3";
     43     var VERSION = "ECMA_1";
     44     startTest();
     45     var TITLE   = "ToNumber";
     46 
     47     writeHeaderToLog( SECTION + " "+ TITLE);
     48 
     49     var testcases = getTestCases();
     50     test();
     51 
     52 
     53 function test() {
     54     for ( tc=0; tc < testcases.length; tc++ ) {
     55         testcases[tc].passed = writeTestCaseResult(
     56                             testcases[tc].expect,
     57                             testcases[tc].actual,
     58                             testcases[tc].description +" = "+
     59                             testcases[tc].actual );
     60         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     61     }
     62     stopTest();
     63     return ( testcases );
     64 }
     65 function getTestCases() {
     66     var array = new Array();
     67     var item = 0;
     68 
     69     // special cases here
     70 
     71     array[item++] = new TestCase( SECTION,   "Number()",                      0,              Number() );
     72     array[item++] = new TestCase( SECTION,   "Number(eval('var x'))",         Number.NaN,     Number(eval("var x")) );
     73     array[item++] = new TestCase( SECTION,   "Number(void 0)",                Number.NaN,     Number(void 0) );
     74     array[item++] = new TestCase( SECTION,   "Number(null)",                  0,              Number(null) );
     75     array[item++] = new TestCase( SECTION,   "Number(true)",                  1,              Number(true) );
     76     array[item++] = new TestCase( SECTION,   "Number(false)",                 0,              Number(false) );
     77     array[item++] = new TestCase( SECTION,   "Number(0)",                     0,              Number(0) );
     78     array[item++] = new TestCase( SECTION,   "Number(-0)",                    -0,             Number(-0) );
     79     array[item++] = new TestCase( SECTION,   "Number(1)",                     1,              Number(1) );
     80     array[item++] = new TestCase( SECTION,   "Number(-1)",                    -1,             Number(-1) );
     81     array[item++] = new TestCase( SECTION,   "Number(Number.MAX_VALUE)",      1.7976931348623157e308, Number(Number.MAX_VALUE) );
     82     array[item++] = new TestCase( SECTION,   "Number(Number.MIN_VALUE)",      5e-324,         Number(Number.MIN_VALUE) );
     83 
     84     array[item++] = new TestCase( SECTION,   "Number(Number.NaN)",                Number.NaN,                 Number(Number.NaN) );
     85     array[item++] = new TestCase( SECTION,   "Number(Number.POSITIVE_INFINITY)",  Number.POSITIVE_INFINITY,   Number(Number.POSITIVE_INFINITY) );
     86     array[item++] = new TestCase( SECTION,   "Number(Number.NEGATIVE_INFINITY)",  Number.NEGATIVE_INFINITY,   Number(Number.NEGATIVE_INFINITY) );
     87 
     88     return ( array );
     89 }
     90