1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator client code, released
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
25 * Rob Ginda rginda@netscape.com
26 * Bob Clary bob@bclary.com
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
42 // Spidermonkey shell now defaults to 1.8, so set the basic version to
43 // 1.5 for backwards compatibility.
45 if (typeof version != 'undefined')
50 var STATUS = "STATUS: ";
52 var SECT_PREFIX = 'Section ';
53 var SECT_SUFFIX = ' of test - ';
54 var callStack = new Array();
60 var gDelayTestDriverEnd = false;
62 var gTestcases = new Array();
63 var gTc = gTestcases.length;
78 var GLOBAL = "[object global]";
79 var PASSED = " PASSED! ";
80 var FAILED = " FAILED! ";
88 * wrapper for test case constructor that doesn't require the SECTION
92 function AddTestCase( description, expect, actual ) {
93 new TestCase( SECTION, description, expect, actual );
97 * Set up test environment.
100 function startTest() {
101 // print out bugnumber
104 print ("BUGNUMBER: " + BUGNUMBER );
106 if ( typeof version != 'function') {
110 // JavaScript 1.3 is supposed to be compliant ecma version 1.0
111 if ( VERSION == "ECMA_1" ) {
114 else if ( VERSION == "JS_1.8" || gTestsuite == 'js1_8') {
117 else if ( VERSION == "JS_1.7" || gTestsuite == 'js1_7') {
120 else if ( VERSION == "JS_1.6" || gTestsuite == 'js1_6') {
123 else if ( VERSION == "JS_1.5" || gTestsuite == 'js1_5') {
126 else if ( VERSION == "JS_1.4" || gTestsuite == 'js1_4') {
129 else if ( VERSION == "JS_1.3" || gTestsuite == 'js1_3') {
132 else if ( VERSION == "JS_1.2" || gTestsuite == 'js1_2') {
135 else if ( VERSION == "JS_1.1" || gTestsuite == 'js1_1') {
140 function TestCase(n, d, e, a)
142 this.path = (typeof gTestPath == 'undefined') ?
143 (gTestsuite + '/' + gTestsubsuite + '/' + gTestfile) :
145 this.file = gTestfile;
147 this.description = d;
150 this.passed = getTestCaseResult(e, a);
152 this.bugnumber = typeof(BUGNUMER) != 'undefined' ? BUGNUMBER : '';
153 this.type = (typeof window == 'undefined' ? 'shell' : 'browser');
154 gTestcases[gTc++] = this;
157 TestCase.prototype.dump = function () {
158 dump('\njstest: ' + this.path + ' ' +
159 'bug: ' + this.bugnumber + ' ' +
160 'result: ' + (this.passed ? 'PASSED':'FAILED') + ' ' +
161 'type: ' + this.type + ' ' +
162 'description: ' + toPrinted(this.description) + ' ' +
163 // 'expected: ' + toPrinted(this.expect) + ' ' +
164 // 'actual: ' + toPrinted(this.actual) + ' ' +
165 'reason: ' + toPrinted(this.reason) + '\n');
169 * The test driver searches for such a phrase in the test output.
170 * If such phrase exists, it will set n as the expected exit code.
172 function expectExitCode(n)
174 print('--- NOTE: IN THIS TESTCASE, WE EXPECT EXIT CODE ' + n + ' ---');
178 * Statuses current section of a test
180 function inSection(x)
182 return SECT_PREFIX + x + SECT_SUFFIX;
186 * Report a failure in the 'accepted' manner
188 function reportFailure (msg)
190 var lines = msg.split ("\n");
192 var funcName = currentFunc();
193 var prefix = (funcName) ? "[reported from " + funcName + "] ": "";
195 for (var i=0; i<lines.length; i++)
196 print (FAILED + prefix + lines[i]);
200 * Print a non-failure message.
202 function printStatus (msg)
206 msg = msg.toString();
208 msg = msg.toString();
209 var lines = msg.split ("\n");
212 for (var i=0; i<lines.length; i++)
213 print (STATUS + lines[i]);
217 * Print a bugnumber message.
219 function printBugNumber (num)
222 print ('BUGNUMBER: ' + num);
225 function toPrinted(value)
227 if (typeof value == "xml")
229 value = value.toXMLString();
233 value = String(value);
235 value = value.replace(/\\n/g, 'NL')
236 .replace(/\n/g, 'NL')
237 .replace(/\\r/g, 'CR')
238 .replace(/[^\x20-\x7E]+/g, escapeString);
242 function escapeString (str)
245 var len = str.length;
247 var digits = ["0", "1", "2", "3", "4", "5", "6", "7",
248 "8", "9", "A", "B", "C", "D", "E", "F"];
250 for (var i=0; i<len; i++)
252 var ch = str.charCodeAt(i);
254 a = digits[ch & 0xf];
256 b = digits[ch & 0xf];
261 c = digits[ch & 0xf];
263 d = digits[ch & 0xf];
265 result += "\\u" + d + c + b + a;
269 result += "\\x" + b + a;
277 * Compare expected result to actual result, if they differ (in value and/or
278 * type) report a failure. If description is provided, include it in the
281 function reportCompare (expected, actual, description) {
282 var expected_t = typeof expected;
283 var actual_t = typeof actual;
286 if (typeof description == "undefined")
292 printStatus ("Comparing '" + description + "'");
295 if (expected_t != actual_t)
297 output += "Type mismatch, expected type " + expected_t +
298 ", actual type " + actual_t + " ";
302 printStatus ("Expected type '" + expected_t + "' matched actual " +
303 "type '" + actual_t + "'");
306 if (expected != actual)
308 output += "Expected value '" + toPrinted(expected) +
309 "', Actual value '" + toPrinted(actual) + "' ";
313 printStatus ("Expected value '" + toPrinted(expected) +
314 "' matched actual value '" + toPrinted(actual) + "'");
317 var testcase = new TestCase(gTestfile, description, expected, actual);
318 testcase.reason = output;
322 print(PASSED + description);
326 reportFailure (description + " : " + output);
329 return testcase.passed;
333 * Attempt to match a regular expression describing the result to
334 * the actual result, if they differ (in value and/or
335 * type) report a failure. If description is provided, include it in the
338 function reportMatch (expectedRegExp, actual, description) {
339 var expected_t = "string";
340 var actual_t = typeof actual;
343 if (typeof description == "undefined")
349 printStatus ("Comparing '" + description + "'");
352 if (expected_t != actual_t)
354 output += "Type mismatch, expected type " + expected_t +
355 ", actual type " + actual_t + " ";
359 printStatus ("Expected type '" + expected_t + "' matched actual " +
360 "type '" + actual_t + "'");
363 var matches = expectedRegExp.test(actual);
366 output += "Expected match to '" + toPrinted(expectedRegExp) +
367 "', Actual value '" + toPrinted(actual) + "' ";
371 printStatus ("Expected match to '" + toPrinted(expectedRegExp) +
372 "' matched actual value '" + toPrinted(actual) + "'");
375 var testcase = new TestCase(gTestfile, description, true, matches);
376 testcase.reason = output;
380 print(PASSED + description);
384 reportFailure (description + " : " + output);
387 return testcase.passed;
391 * Puts funcName at the top of the call stack. This stack is used to show
392 * a function-reported-from field when reporting failures.
394 function enterFunc (funcName)
396 if (!funcName.match(/\(\)$/))
399 callStack.push(funcName);
403 * Pops the top funcName off the call stack. funcName is optional, and can be
404 * used to check push-pop balance.
406 function exitFunc (funcName)
408 var lastFunc = callStack.pop();
412 if (!funcName.match(/\(\)$/))
415 if (lastFunc != funcName)
416 reportCompare(funcName, lastFunc, "Test driver failure wrong exit function ");
421 * Peeks at the top of the call stack.
423 function currentFunc()
425 return callStack[callStack.length - 1];
429 Calculate the "order" of a set of data points {X: [], Y: []}
430 by computing successive "derivatives" of the data until
431 the data is exhausted or the derivative is linear.
436 var origLength = data.X.length;
438 while (data.X.length > 2)
440 var lr = new LinearRegression(data);
443 // only increase the order if the slope
448 if (lr.r > 0.98 || lr.Syx < 1 || lr.b < 1e-6)
450 // terminate if close to a line lr.r
451 // small error lr.Syx
455 data = dataDeriv(data);
458 if (2 == origLength - order)
460 order = Number.POSITIVE_INFINITY;
464 function LinearRegression(data)
468 for data points (Xi, Yi); 0 <= i < n
470 b = (n*SUM(XiYi) - SUM(Xi)*SUM(Yi))/(n*SUM(Xi*Xi) - SUM(Xi)*SUM(Xi))
471 a = (SUM(Yi) - b*SUM(Xi))/n
475 if (data.X.length != data.Y.length)
477 throw 'LinearRegression: data point length mismatch';
479 if (data.X.length < 3)
481 throw 'LinearRegression: data point length < 2';
483 var n = data.X.length;
496 for (i = 0; i < n; i++)
505 this.b = (n * SUM_XY - SUM_X * SUM_Y)/(n * SUM_XX - SUM_X * SUM_X);
506 this.a = (SUM_Y - this.b * SUM_X)/n;
513 var SUM_XdiffYdiff = 0;
515 for (i = 0; i < n; i++)
517 var Ydiff = Y[i] - this.Yavg;
518 var Xdiff = X[i] - this.Xavg;
520 SUM_Ydiff2 += Ydiff * Ydiff;
521 SUM_Xdiff2 += Xdiff * Xdiff;
522 SUM_XdiffYdiff += Xdiff * Ydiff;
525 var Syx2 = (SUM_Ydiff2 - Math.pow(SUM_XdiffYdiff/SUM_Xdiff2, 2))/(n - 2);
526 var r2 = Math.pow((n*SUM_XY - SUM_X * SUM_Y), 2) /
527 ((n*SUM_XX - SUM_X*SUM_X)*(n*SUM_YY-SUM_Y*SUM_Y));
529 this.Syx = Math.sqrt(Syx2);
530 this.r = Math.sqrt(r2);
534 function dataDeriv(data)
536 if (data.X.length != data.Y.length)
538 throw 'length mismatch';
540 var length = data.X.length;
544 throw 'length ' + length + ' must be >= 2';
549 var deriv = {X: [], Y: [] };
551 for (var i = 0; i < length - 1; i++)
553 deriv.X[i] = (X[i] + X[i+1])/2;
554 deriv.Y[i] = (Y[i+1] - Y[i])/(X[i+1] - X[i]);
562 function compareSource(expect, actual, summary)
565 var expectP = expect.
566 replace(/([(){},.:\[\]])/mg, ' $1 ').
567 replace(/(\w+)/mg, ' $1 ').
568 replace(/<(\/)? (\w+) (\/)?>/mg, '<$1$2$3>').
569 replace(/\s+/mg, ' ').
570 replace(/new (\w+)\s*\(\s*\)/mg, 'new $1');
572 var actualP = actual.
573 replace(/([(){},.:\[\]])/mg, ' $1 ').
574 replace(/(\w+)/mg, ' $1 ').
575 replace(/<(\/)? (\w+) (\/)?>/mg, '<$1$2$3>').
576 replace(/\s+/mg, ' ').
577 replace(/new (\w+)\s*\(\s*\)/mg, 'new $1');
579 print('expect:\n' + expectP);
580 print('actual:\n' + actualP);
582 reportCompare(expectP, actualP, summary);
584 // actual must be compilable if expect is?
587 var expectCompile = 'No Error';
594 actualCompile = 'No Error';
598 actualCompile = ex1 + '';
600 reportCompare(expectCompile, actualCompile,
601 summary + ': compile actual');
608 function optionsInit() {
610 // record initial values to support resetting
611 // options to their initial values
612 options.initvalues = {};
614 // record values in a stack to support pushing
615 // and popping options
616 options.stackvalues = [];
618 var optionNames = options().split(',');
620 for (var i = 0; i < optionNames.length; i++)
622 var optionName = optionNames[i];
625 options.initvalues[optionName] = '';
630 function optionsClear() {
632 // turn off current settings
633 var optionNames = options().split(',');
634 for (var i = 0; i < optionNames.length; i++)
636 var optionName = optionNames[i];
644 function optionsPush()
646 var optionsframe = {};
648 options.stackvalues.push(optionsframe);
650 var optionNames = options().split(',');
652 for (var i = 0; i < optionNames.length; i++)
654 var optionName = optionNames[i];
657 optionsframe[optionName] = '';
664 function optionsPop()
666 var optionsframe = options.stackvalues.pop();
670 for (optionName in optionsframe)
677 function optionsReset() {
681 // turn on initial settings
682 for (optionName in options.initvalues)
688 if (typeof options == 'function')
694 function getTestCaseResult(expected, actual)
696 var expected_t = typeof expected;
697 var actual_t = typeof actual;
700 // because ( NaN == NaN ) always returns false, need to do
701 // a special compare to see if we got the right result.
702 if ( actual != actual )
704 if ( actual_t == "object" )
706 actual = "NaN object";
710 actual = "NaN number";
713 if ( expected != expected )
715 if ( expected_t == "object" )
717 expected = "NaN object";
721 expected = "NaN number";
725 if (expected_t != actual_t)
729 else if (expected != actual)
731 if (expected_t != 'number' || (Math.abs(actual - expected) > 1E-10))
740 if (typeof dump == 'undefined')
742 if (typeof window == 'undefined' &&
743 typeof print == 'function')
749 dump = (function () {});
754 for ( gTc=0; gTc < gTestcases.length; gTc++ ) {
755 // temporary hack to work around some unknown issue in 1.7
758 gTestcases[gTc].passed = writeTestCaseResult(
759 gTestcases[gTc].expect,
760 gTestcases[gTc].actual,
761 gTestcases[gTc].description +" = "+ gTestcases[gTc].actual );
762 gTestcases[gTc].reason += ( gTestcases[gTc].passed ) ? "" : "wrong value ";
766 print('test(): empty testcase for gTc = ' + gTc + ' ' + e);
770 return ( gTestcases );
774 * Begin printing functions. These functions use the shell's
775 * print function. When running tests in the browser, these
776 * functions, override these functions with functions that use
780 function writeTestCaseResult( expect, actual, string ) {
781 var passed = getTestCaseResult( expect, actual );
782 writeFormattedResult( expect, actual, string, passed );
785 function writeFormattedResult( expect, actual, string, passed ) {
786 var s = ( passed ? PASSED : FAILED ) + string + ' expected: ' + expect;
791 function writeHeaderToLog( string ) {
794 /* end of print functions */
798 * When running in the shell, run the garbage collector after the
799 * test has completed.
802 function stopTest() {
804 if ( gc != undefined ) {
810 * Convenience function for displaying failed test cases. Useful
811 * when running tests manually.
814 function getFailedCases() {
815 for ( var i = 0; i < gTestcases.length; i++ ) {
816 if ( ! gTestcases[i].passed ) {
817 print( gTestcases[i].description + " = " +gTestcases[i].actual +
818 " expected: " + gTestcases[i].expect );
823 function jsTestDriverEnd()
825 // gDelayTestDriverEnd is used to
826 // delay collection of the test result and
827 // signal to Spider so that tests can continue
828 // to run after page load has fired. They are
829 // responsible for setting gDelayTestDriverEnd = true
830 // then when completed, setting gDelayTestDriverEnd = false
831 // then calling jsTestDriverEnd()
833 if (gDelayTestDriverEnd)
844 dump('jsTestDriverEnd ' + ex);
847 for (var i = 0; i < gTestcases.length; i++)
849 gTestcases[i].dump();
855 * Some tests need to know if we are in Rhino as opposed to SpiderMonkey
859 return (typeof defineClass == "function");
862 /* these functions are useful for running tests manually in Rhino */
864 function GetContext() {
865 return Packages.com.netscape.javascript.Context.getCurrentContext();
867 function OptLevel( i ) {
869 var cx = GetContext();
870 cx.setOptimizationLevel(i);
872 /* end of Rhino functions */