Follow-on fix for bug 457825. Use sheet principal for agent and user sheets. r=dbaron...
[wine-gecko.git] / js / tests / shell.js
blob40af0f3799ecf7f0fc130c8f7e8fdf833e66bc31
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
14 * License.
16 * The Original Code is Mozilla Communicator client code, released
17 * March 31, 1998.
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.
24 * Contributor(s):
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')
47 version(150);
50 var STATUS = "STATUS: ";
51 var VERBOSE = false;
52 var SECT_PREFIX = 'Section ';
53 var SECT_SUFFIX = ' of test - ';
54 var callStack = new Array();
56 var gTestfile;
57 var gTestPath;
58 var gTestsuite;
59 var gTestsubsuite;
60 var gDelayTestDriverEnd = false;
62 var gTestcases = new Array();
63 var gTc = gTestcases.length;
64 var BUGNUMBER = '';
65 var summary = '';
66 var description = '';
67 var expected = '';
68 var actual = '';
69 var msg = '';
71 var SECTION = "";
72 var VERSION = "";
73 var BUGNUMBER = "";
76 * constant strings
78 var GLOBAL = this + '';
79 var PASSED = " PASSED! ";
80 var FAILED = " FAILED! ";
82 var DEBUG = false;
84 var DESCRIPTION;
85 var EXPECTED;
88 * wrapper for test case constructor that doesn't require the SECTION
89 * argument.
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
103 if ( BUGNUMBER ) {
104 print ("BUGNUMBER: " + BUGNUMBER );
106 if ( typeof version != 'function') {
107 return;
110 // JavaScript 1.3 is supposed to be compliant ecma version 1.0
111 if ( VERSION == "ECMA_1" ) {
112 version ( "130" );
114 else if ( VERSION == "JS_1.8" || gTestsuite == 'js1_8') {
115 version ( "180" );
117 else if ( VERSION == "JS_1.7" || gTestsuite == 'js1_7') {
118 version ( "170" );
120 else if ( VERSION == "JS_1.6" || gTestsuite == 'js1_6') {
121 version ( "160" );
123 else if ( VERSION == "JS_1.5" || gTestsuite == 'js1_5') {
124 version ( "150" );
126 else if ( VERSION == "JS_1.4" || gTestsuite == 'js1_4') {
127 version ( "140" );
129 else if ( VERSION == "JS_1.3" || gTestsuite == 'js1_3') {
130 version ( "130" );
132 else if ( VERSION == "JS_1.2" || gTestsuite == 'js1_2') {
133 version ( "120" );
135 else if ( VERSION == "JS_1.1" || gTestsuite == 'js1_1') {
136 version ( "110" );
140 function TestCase(n, d, e, a)
142 this.path = (typeof gTestPath == 'undefined') ?
143 (gTestsuite + '/' + gTestsubsuite + '/' + gTestfile) :
144 gTestPath;
145 this.file = gTestfile;
146 this.name = n;
147 this.description = d;
148 this.expect = e;
149 this.actual = a;
150 this.passed = getTestCaseResult(e, a);
151 this.reason = '';
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");
191 var l;
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)
204 /* js1_6 had...
205 msg = String(msg);
206 msg = msg.toString();
208 msg = msg.toString();
209 var lines = msg.split ("\n");
210 var l;
212 for (var i=0; i<lines.length; i++)
213 print (STATUS + lines[i]);
217 * Print a bugnumber message.
219 function printBugNumber (num)
221 BUGNUMBER = num;
222 print ('BUGNUMBER: ' + num);
225 function toPrinted(value)
227 if (typeof value == "xml")
229 value = value.toXMLString();
231 else
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);
239 return value;
242 function escapeString (str)
244 var a, b, c, d;
245 var len = str.length;
246 var result = "";
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];
255 ch >>= 4;
256 b = digits[ch & 0xf];
257 ch >>= 4;
259 if (ch)
261 c = digits[ch & 0xf];
262 ch >>= 4;
263 d = digits[ch & 0xf];
265 result += "\\u" + d + c + b + a;
267 else
269 result += "\\x" + b + a;
273 return result;
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
279 * failure report.
281 function reportCompare (expected, actual, description) {
282 var expected_t = typeof expected;
283 var actual_t = typeof actual;
284 var output = "";
286 if (typeof description == "undefined")
288 description = '';
290 else if (VERBOSE)
292 printStatus ("Comparing '" + description + "'");
295 if (expected_t != actual_t)
297 output += "Type mismatch, expected type " + expected_t +
298 ", actual type " + actual_t + " ";
300 else if (VERBOSE)
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) + "' ";
311 else if (VERBOSE)
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;
320 if (testcase.passed)
322 print(PASSED + description);
324 else
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
336 * failure report.
338 function reportMatch (expectedRegExp, actual, description) {
339 var expected_t = "string";
340 var actual_t = typeof actual;
341 var output = "";
343 if (typeof description == "undefined")
345 description = '';
347 else if (VERBOSE)
349 printStatus ("Comparing '" + description + "'");
352 if (expected_t != actual_t)
354 output += "Type mismatch, expected type " + expected_t +
355 ", actual type " + actual_t + " ";
357 else if (VERBOSE)
359 printStatus ("Expected type '" + expected_t + "' matched actual " +
360 "type '" + actual_t + "'");
363 var matches = expectedRegExp.test(actual);
364 if (!matches)
366 output += "Expected match to '" + toPrinted(expectedRegExp) +
367 "', Actual value '" + toPrinted(actual) + "' ";
369 else if (VERBOSE)
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;
378 if (testcase.passed)
380 print(PASSED + description);
382 else
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(/\(\)$/))
397 funcName += "()";
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();
410 if (funcName)
412 if (!funcName.match(/\(\)$/))
413 funcName += "()";
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.
433 function BigO(data)
435 var order = 0;
436 var origLength = data.X.length;
438 while (data.X.length > 2)
440 var lr = new LinearRegression(data);
441 if (lr.b > 1e-6)
443 // only increase the order if the slope
444 // is "great" enough
445 order++;
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
452 // small slope lr.b
453 break;
455 data = dataDeriv(data);
458 if (2 == origLength - order)
460 order = Number.POSITIVE_INFINITY;
462 return order;
464 function LinearRegression(data)
467 y = a + bx
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
473 var i;
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;
484 var X = data.X;
485 var Y = data.Y;
487 this.Xavg = 0;
488 this.Yavg = 0;
490 var SUM_X = 0;
491 var SUM_XY = 0;
492 var SUM_XX = 0;
493 var SUM_Y = 0;
494 var SUM_YY = 0;
496 for (i = 0; i < n; i++)
498 SUM_X += X[i];
499 SUM_XY += X[i]*Y[i];
500 SUM_XX += X[i]*X[i];
501 SUM_Y += Y[i];
502 SUM_YY += Y[i]*Y[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;
508 this.Xavg = SUM_X/n;
509 this.Yavg = SUM_Y/n;
511 var SUM_Ydiff2 = 0;
512 var SUM_Xdiff2 = 0;
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;
542 if (length < 2)
544 throw 'length ' + length + ' must be >= 2';
546 var X = data.X;
547 var Y = data.Y;
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]);
556 return deriv;
559 return 0;
562 function compareSource(expect, actual, summary)
564 // compare source
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';
588 var actualCompile;
590 eval(expect);
593 eval(actual);
594 actualCompile = 'No Error';
596 catch(ex1)
598 actualCompile = ex1 + '';
600 reportCompare(expectCompile, actualCompile,
601 summary + ': compile actual');
603 catch(ex)
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];
623 if (optionName)
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];
637 if (optionName)
639 options(optionName);
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];
655 if (optionName)
657 optionsframe[optionName] = '';
661 optionsClear();
664 function optionsPop()
666 var optionsframe = options.stackvalues.pop();
668 optionsClear();
670 for (optionName in optionsframe)
672 options(optionName);
677 function optionsReset() {
679 optionsClear();
681 // turn on initial settings
682 for (optionName in options.initvalues)
684 options(optionName);
688 if (typeof options == 'function')
690 optionsInit();
691 optionsClear();
694 function getTestCaseResult(expected, actual)
696 var expected_t = typeof expected;
697 var actual_t = typeof actual;
698 var passed = true;
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";
708 else
710 actual = "NaN number";
713 if ( expected != expected )
715 if ( expected_t == "object" )
717 expected = "NaN object";
719 else
721 expected = "NaN number";
725 if (expected_t != actual_t)
727 passed = false;
729 else if (expected != actual)
731 if (expected_t != 'number' || (Math.abs(actual - expected) > 1E-10))
733 passed = false;
737 return passed;
740 if (typeof dump == 'undefined')
742 if (typeof window == 'undefined' &&
743 typeof print == 'function')
745 dump = print;
747 else
749 dump = (function () {});
753 function test() {
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 ";
764 catch(e)
766 print('test(): empty testcase for gTc = ' + gTc + ' ' + e);
769 stopTest();
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
777 * document.write.
780 function writeTestCaseResult( expect, actual, string ) {
781 var passed = getTestCaseResult( expect, actual );
782 writeFormattedResult( expect, actual, string, passed );
783 return passed;
785 function writeFormattedResult( expect, actual, string, passed ) {
786 var s = ( passed ? PASSED : FAILED ) + string + ' expected: ' + expect;
787 print(s);
788 return passed;
791 function writeHeaderToLog( string ) {
792 print( 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() {
803 var gc;
804 if ( gc != undefined ) {
805 gc();
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)
835 return;
840 optionsReset();
842 catch(ex)
844 dump('jsTestDriverEnd ' + ex);
847 for (var i = 0; i < gTestcases.length; i++)
849 gTestcases[i].dump();
854 function jit(on)
856 if (on && !options().match(/jit/))
858 options('jit');
860 else if (!on && options().match(/jit/))
862 options('jit');
867 * Some tests need to know if we are in Rhino as opposed to SpiderMonkey
869 function inRhino()
871 return (typeof defineClass == "function");
874 /* these functions are useful for running tests manually in Rhino */
876 function GetContext() {
877 return Packages.com.netscape.javascript.Context.getCurrentContext();
879 function OptLevel( i ) {
880 i = Number(i);
881 var cx = GetContext();
882 cx.setOptimizationLevel(i);
884 /* end of Rhino functions */