1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/js1_2/jsref.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,195 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +var completed = false; 1.8 +var testcases; 1.9 +var tc = 0; 1.10 + 1.11 +SECTION = ""; 1.12 +VERSION = ""; 1.13 +BUGNUMBER = ""; 1.14 +EXCLUDE = ""; 1.15 + 1.16 +/* 1.17 + * constant strings 1.18 + */ 1.19 +var GLOBAL = "[object global]"; 1.20 +var PASSED = " PASSED!" 1.21 +var FAILED = " FAILED! expected: "; 1.22 + 1.23 +var DEBUG = false; 1.24 + 1.25 +version("120"); 1.26 +/* 1.27 + * change this for date tests if you're not in PST 1.28 + */ 1.29 + 1.30 +TZ_DIFF = -8; 1.31 +/* wrapper for test cas constructor that doesn't require the SECTION 1.32 + * argument. 1.33 + */ 1.34 + 1.35 +function AddTestCase( description, expect, actual ) { 1.36 + testcases[tc++] = new TestCase( SECTION, description, expect, actual ); 1.37 +} 1.38 +function TestCase( n, d, e, a ) { 1.39 + this.name = n; 1.40 + this.description = d; 1.41 + this.expect = e; 1.42 + this.actual = a; 1.43 + this.passed = true; 1.44 + this.reason = ""; 1.45 + this.bugnumber = BUGNUMBER; 1.46 + 1.47 + this.passed = getTestCaseResult( this.expect, this.actual ); 1.48 +} 1.49 +function startTest() { 1.50 + version(120); 1.51 + 1.52 + // for ecma version 2.0, we will leave the javascript version to 1.53 + // the default ( for now ). 1.54 + // print out bugnumber 1.55 + 1.56 + if ( BUGNUMBER ) { 1.57 + print ("BUGNUMBER: " + BUGNUMBER ); 1.58 + } 1.59 + 1.60 + testcases = new Array(); 1.61 + tc = 0; 1.62 + 1.63 +} 1.64 +function test() { 1.65 + for ( tc=0; tc < testcases.length; tc++ ) { 1.66 + testcases[tc].passed = writeTestCaseResult( 1.67 + testcases[tc].expect, 1.68 + testcases[tc].actual, 1.69 + testcases[tc].description +" = "+ 1.70 + testcases[tc].actual ); 1.71 + 1.72 + testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 1.73 + } 1.74 + stopTest(); 1.75 + return ( testcases ); 1.76 +} 1.77 +function getTestCaseResult( expect, actual ) { 1.78 + // because ( NaN == NaN ) always returns false, need to do 1.79 + // a special compare to see if we got the right result. 1.80 + if ( actual != actual ) { 1.81 + if ( typeof actual == "object" ) { 1.82 + actual = "NaN object"; 1.83 + } else { 1.84 + actual = "NaN number"; 1.85 + } 1.86 + } 1.87 + if ( expect != expect ) { 1.88 + if ( typeof expect == "object" ) { 1.89 + expect = "NaN object"; 1.90 + } else { 1.91 + expect = "NaN number"; 1.92 + } 1.93 + } 1.94 + 1.95 + var passed = ( expect == actual ) ? true : false; 1.96 + 1.97 + // if both objects are numbers, give a little leeway for rounding. 1.98 + if ( !passed 1.99 + && typeof(actual) == "number" 1.100 + && typeof(expect) == "number" 1.101 + ) { 1.102 + if ( Math.abs(actual-expect) < 0.0000001 ) { 1.103 + passed = true; 1.104 + } 1.105 + } 1.106 + 1.107 + // verify type is the same 1.108 + if ( typeof(expect) != typeof(actual) ) { 1.109 + passed = false; 1.110 + } 1.111 + 1.112 + return passed; 1.113 +} 1.114 +/* 1.115 + * Begin printing functions. These functions use the shell's 1.116 + * print function. When running tests in the browser, these 1.117 + * functions, override these functions with functions that use 1.118 + * document.write. 1.119 + */ 1.120 + 1.121 +function writeTestCaseResult( expect, actual, string ) { 1.122 + var passed = getTestCaseResult( expect, actual ); 1.123 + writeFormattedResult( expect, actual, string, passed ); 1.124 + return passed; 1.125 +} 1.126 +function writeFormattedResult( expect, actual, string, passed ) { 1.127 + var s = string ; 1.128 + s += ( passed ) ? PASSED : FAILED + expect; 1.129 + print( s); 1.130 + return passed; 1.131 +} 1.132 + 1.133 +function writeHeaderToLog( string ) { 1.134 + print( string ); 1.135 +} 1.136 +/* end of print functions */ 1.137 + 1.138 + 1.139 +function stopTest() { 1.140 + var sizeTag = "<#TEST CASES SIZE>"; 1.141 + var doneTag = "<#TEST CASES DONE>"; 1.142 + var beginTag = "<#TEST CASE "; 1.143 + var endTag = ">"; 1.144 + 1.145 + print(sizeTag); 1.146 + print(testcases.length); 1.147 + for (tc = 0; tc < testcases.length; tc++) 1.148 + { 1.149 + print(beginTag + 'PASSED' + endTag); 1.150 + print(testcases[tc].passed); 1.151 + print(beginTag + 'NAME' + endTag); 1.152 + print(testcases[tc].name); 1.153 + print(beginTag + 'EXPECTED' + endTag); 1.154 + print(testcases[tc].expect); 1.155 + print(beginTag + 'ACTUAL' + endTag); 1.156 + print(testcases[tc].actual); 1.157 + print(beginTag + 'DESCRIPTION' + endTag); 1.158 + print(testcases[tc].description); 1.159 + print(beginTag + 'REASON' + endTag); 1.160 + print(( testcases[tc].passed ) ? "" : "wrong value "); 1.161 + print(beginTag + 'BUGNUMBER' + endTag); 1.162 + print( BUGNUMBER ); 1.163 + } 1.164 + print(doneTag); 1.165 + gc(); 1.166 +} 1.167 + 1.168 +function getFailedCases() { 1.169 + for ( var i = 0; i < testcases.length; i++ ) { 1.170 + if ( ! testcases[i].passed ) { 1.171 + print( testcases[i].description +" = " +testcases[i].actual +" expected: "+ testcases[i].expect ); 1.172 + } 1.173 + } 1.174 +} 1.175 +function err( msg, page, line ) { 1.176 + testcases[tc].actual = "error"; 1.177 + testcases[tc].reason = msg; 1.178 + writeTestCaseResult( testcases[tc].expect, 1.179 + testcases[tc].actual, 1.180 + testcases[tc].description +" = "+ testcases[tc].actual + 1.181 + ": " + testcases[tc].reason ); 1.182 + stopTest(); 1.183 + return true; 1.184 +} 1.185 +function Enumerate ( o ) { 1.186 + var p; 1.187 + for ( p in o ) { 1.188 + print( p +": " + o[p] ); 1.189 + } 1.190 +} 1.191 +function GetContext() { 1.192 + return Packages.com.netscape.javascript.Context.getCurrentContext(); 1.193 +} 1.194 +function OptLevel( i ) { 1.195 + i = Number(i); 1.196 + var cx = GetContext(); 1.197 + cx.setOptimizationLevel(i); 1.198 +}