Home | History | Annotate | Download | only in ecma_2
      1 /*
      2 * The contents of this file are subject to the Netscape Public
      3 * License Version 1.1 (the "License"); you may not use this file
      4 * except in compliance with the License. You may obtain a copy of
      5 * the License at http://www.mozilla.org/NPL/
      6 *
      7 * Software distributed under the License is distributed on an "AS
      8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      9 * implied. See the License for the specific language governing
     10 * rights and limitations under the License.
     11 *
     12 * The Original Code is mozilla.org code.
     13 *
     14 * The Initial Developer of the Original Code is Netscape
     15 * Communications 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 /*
     24  * JavaScript shared functions file for running the tests in either
     25  * stand-alone JavaScript engine.  To run a test, first load this file,
     26  * then load the test script.
     27  */
     28 
     29 var completed = false;
     30 var testcases;
     31 var tc = 0;
     32 
     33 SECTION = "";
     34 VERSION = "";
     35 BUGNUMBER="";
     36 
     37 var TZ_DIFF = getTimeZoneDiff();
     38 
     39 var DEBUG = false;
     40 
     41 var	GLOBAL = "[object global]";
     42 var PASSED = " PASSED!"
     43 var FAILED = " FAILED! expected: ";
     44 
     45 function test() {
     46     for ( tc=0; tc < testcases.length; tc++ ) {
     47         testcases[tc].passed = writeTestCaseResult(
     48                             testcases[tc].expect,
     49                             testcases[tc].actual,
     50                             testcases[tc].description +" = "+
     51                             testcases[tc].actual );
     52 
     53         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
     54     }
     55     stopTest();
     56     return ( testcases );
     57 }
     58 
     59 function TestCase( n, d, e, a ) {
     60     this.name        = n;
     61     this.description = d;
     62     this.expect      = e;
     63     this.actual      = a;
     64     this.passed      = true;
     65     this.reason      = "";
     66     this.bugnumber   = BUGNUMBER;
     67 
     68     this.passed = getTestCaseResult( this.expect, this.actual );
     69     if ( DEBUG ) {
     70         writeLineToLog( "added " + this.description );
     71     }
     72 }
     73 function startTest() {
     74     if ( version ) {
     75         //  JavaScript 1.3 is supposed to be compliant ecma version 1.0
     76         if ( VERSION == "ECMA_1" ) {
     77             version ( 130 );
     78         }
     79         if ( VERSION == "JS_13" ) {
     80             version ( 130 );
     81         }
     82         if ( VERSION == "JS_12" ) {
     83             version ( 120 );
     84         }
     85         if ( VERSION  == "JS_11" ) {
     86             version ( 110 );
     87         }
     88     }
     89 
     90 
     91     // for ecma version 2.0, we will leave the javascript version to
     92     // the default ( for now ).
     93 
     94     writeHeaderToLog( SECTION + " "+ TITLE);
     95     if ( BUGNUMBER ) {
     96         writeLineToLog ("BUGNUMBER: " + BUGNUMBER );
     97     }
     98 
     99     testcases = new Array();
    100     tc = 0;
    101 }
    102 
    103 function getTestCaseResult( expect, actual ) {
    104     //  because ( NaN == NaN ) always returns false, need to do
    105     //  a special compare to see if we got the right result.
    106         if ( actual != actual ) {
    107             if ( typeof actual == "object" ) {
    108                 actual = "NaN object";
    109             } else {
    110                 actual = "NaN number";
    111             }
    112         }
    113         if ( expect != expect ) {
    114             if ( typeof expect == "object" ) {
    115                 expect = "NaN object";
    116             } else {
    117                 expect = "NaN number";
    118             }
    119         }
    120 
    121         var passed = ( expect == actual ) ? true : false;
    122 
    123     //  if both objects are numbers
    124     // need to replace w/ IEEE standard for rounding
    125         if (    !passed
    126                 && typeof(actual) == "number"
    127                 && typeof(expect) == "number"
    128             ) {
    129                 if ( Math.abs(actual-expect) < 0.0000001 ) {
    130                     passed = true;
    131                 }
    132         }
    133 
    134     //  verify type is the same
    135         if ( typeof(expect) != typeof(actual) ) {
    136             passed = false;
    137         }
    138 
    139         return passed;
    140 }
    141 
    142 function writeTestCaseResult( expect, actual, string ) {
    143         var passed = getTestCaseResult( expect, actual );
    144         writeFormattedResult( expect, actual, string, passed );
    145         return passed;
    146 }
    147 
    148 function writeFormattedResult( expect, actual, string, passed ) {
    149         var s = string ;
    150         s += ( passed ) ? PASSED : FAILED + expect;
    151         writeLineToLog( s);
    152         return passed;
    153 }
    154 
    155 function writeLineToLog( string ) {
    156     print( string  );
    157 }
    158 function writeHeaderToLog( string ) {
    159     print( string );
    160 }
    161 function stopTest() {
    162     var gc;
    163     if ( gc != undefined ) {
    164         gc();
    165     }
    166 }
    167 function getFailedCases() {
    168   for ( var i = 0; i < testcases.length; i++ ) {
    169      if ( ! testcases[i].passed ) {
    170         print( testcases[i].description +" = " +testcases[i].actual +" expected: "+ testcases[i].expect );
    171      }
    172   }
    173 }
    174 function err( msg, page, line ) {
    175     writeLineToLog( page + " failed with error: " + msg + " on line " + line );
    176     testcases[tc].actual = "error";
    177     testcases[tc].reason = msg;
    178     writeTestCaseResult( testcases[tc].expect,
    179                          testcases[tc].actual,
    180                          testcases[tc].description +" = "+ testcases[tc].actual +
    181                          ": " + testcases[tc].reason );
    182     stopTest();
    183     return true;
    184 }
    185 
    186 function Enumerate ( o ) {
    187     var properties = new Array();
    188     for ( p in o ) {
    189        properties[ properties.length ] = new Array( p, o[p] );
    190     }
    191     return properties;
    192 }
    193 
    194 function getFailedCases() {
    195   for (	var	i =	0; i < testcases.length; i++ ) {
    196 	 if	( !	testcases[i].passed	) {
    197 		writeLineToLog( testcases[i].description	+" = " +testcases[i].actual	+
    198 		    " expected: "+	testcases[i].expect	);
    199 	 }
    200   }
    201 }
    202 function AddTestCase( description, expect, actual ) {
    203     testcases[tc++] = new TestCase( SECTION, description, expect, actual );
    204 }
    205 
    206 
    207 /*
    208  * Originally, the test suite used a hard-coded value TZ_DIFF = -8.
    209  * But that was only valid for testers in the Pacific Standard Time Zone!
    210  * We calculate the proper number dynamically for any tester. We just
    211  * have to be careful to use a date not subject to Daylight Savings Time...
    212 */
    213 function getTimeZoneDiff()
    214 {
    215   return -((new Date(2000, 1, 1)).getTimezoneOffset())/60;
    216 }
    217