Debian Codegraph @

rhino/testsrc/tests/shell.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 // Spidermonkey shell now defaults to 1.8, so set the basic version to
8 // 1.5 for backwards compatibility.
9
10 if (typeof version != 'undefined')
11 {
12 version(150);
13 }
14
15 var STATUS = "STATUS: ";
16 var VERBOSE = false;
17 var SECT_PREFIX = 'Section ';
18 var SECT_SUFFIX = ' of test - ';
19 var callStack = new Array();
20
21 var gTestfile;
22 var gTestPath;
23 var gTestsuite;
24 var gTestsubsuite;
25 var gDelayTestDriverEnd = false;
26
27 var gTestcases = new Array();
28 var gTc = gTestcases.length;
29 var BUGNUMBER = '';
30 var summary = '';
31 var description = '';
32 var expected = '';
33 var actual = '';
34 var msg = '';
35
36 var SECTION = "";
37 var VERSION = "";
38 var BUGNUMBER = "";
39
40 /*
41 * constant strings
42 */
43 var GLOBAL = this + '';
44 var PASSED = " PASSED! ";
45 var FAILED = " FAILED! ";
46
47 var DEBUG = false;
48
49 var DESCRIPTION;
50 var EXPECTED;
51
52 /*
53 * wrapper for test case constructor that doesn't require the SECTION
54 * argument.
55 */
56
57 function AddTestCase( description, expect, actual ) {
58 new TestCase( SECTION, description, expect, actual );
59 }
60
61 /*
62 * Set up test environment.
63 *
64 */
65 function startTest() {
66 // print out bugnumber
67
68 if ( BUGNUMBER ) {
69 print ("BUGNUMBER: " + BUGNUMBER );
70 }
71 if ( typeof version != 'function') {
72 return;
73 }
74
75 // JavaScript 1.3 is supposed to be compliant ecma version 1.0
76 if ( VERSION == "ECMA_1" ) {
77 version ( "130" );
78 }
79 else if ( VERSION == "JS_1.8" || gTestsuite == 'js1_8') {
80 version ( "180" );
81 }
82 else if ( VERSION == "JS_1.7" || gTestsuite == 'js1_7') {
83 version ( "170" );
84 }
85 else if ( VERSION == "JS_1.6" || gTestsuite == 'js1_6') {
86 version ( "160" );
87 }
88 else if ( VERSION == "JS_1.5" || gTestsuite == 'js1_5') {
89 version ( "150" );
90 }
91 else if ( VERSION == "JS_1.4" || gTestsuite == 'js1_4') {
92 version ( "140" );
93 }
94 else if ( VERSION == "JS_1.3" || gTestsuite == 'js1_3') {
95 version ( "130" );
96 }
97 else if ( VERSION == "JS_1.2" || gTestsuite == 'js1_2') {
98 version ( "120" );
99 }
100 else if ( VERSION == "JS_1.1" || gTestsuite == 'js1_1') {
101 version ( "110" );
102 }
103 }
104
105 function TestCase(n, d, e, a)
106 {
107 this.path = (typeof gTestPath == 'undefined') ?
108 (gTestsuite + '/' + gTestsubsuite + '/' + gTestfile) :
109 gTestPath;
110 this.file = gTestfile;
111 this.name = n;
112 this.description = d;
113 this.expect = e;
114 this.actual = a;
115 this.passed = getTestCaseResult(e, a);
116 this.reason = '';
117 this.bugnumber = typeof(BUGNUMER) != 'undefined' ? BUGNUMBER : '';
118 this.type = (typeof window == 'undefined' ? 'shell' : 'browser');
119 gTestcases[gTc++] = this;
120 }
121
122 TestCase.prototype.dump = function () {
123 dump('\njstest: ' + this.path + ' ' +
124 'bug: ' + this.bugnumber + ' ' +
125 'result: ' + (this.passed ? 'PASSED':'FAILED') + ' ' +
126 'type: ' + this.type + ' ' +
127 'description: ' + toPrinted(this.description) + ' ' +
128 // 'expected: ' + toPrinted(this.expect) + ' ' +
129 // 'actual: ' + toPrinted(this.actual) + ' ' +
130 'reason: ' + toPrinted(this.reason) + '\n');
131 };
132
133 /*
134 * The test driver searches for such a phrase in the test output.
135 * If such phrase exists, it will set n as the expected exit code.
136 */
137 function expectExitCode(n)
138 {
139 print('--- NOTE: IN THIS TESTCASE, WE EXPECT EXIT CODE ' + n + ' ---');
140 }
141
142 /*
143 * Statuses current section of a test
144 */
145 function inSection(x)
146 {
147 return SECT_PREFIX + x + SECT_SUFFIX;
148 }
149
150 /*
151 * Report a failure in the 'accepted' manner
152 */
153 function reportFailure (msg)
154 {
155 var lines = msg.split ("\n");
156 var l;
157 var funcName = currentFunc();
158 var prefix = (funcName) ? "[reported from " + funcName + "] ": "";
159
160 for (var i=0; i<lines.length; i++)
161 print (FAILED + prefix + lines[i]);
162 }
163
164 /*
165 * Print a non-failure message.
166 */
167 function printStatus (msg)
168 {
169 /* js1_6 had...
170 msg = String(msg);
171 msg = msg.toString();
172 */
173 msg = msg.toString();
174 var lines = msg.split ("\n");
175 var l;
176
177 for (var i=0; i<lines.length; i++)
178 print (STATUS + lines[i]);
179 }
180
181 /*
182 * Print a bugnumber message.
183 */
184 function printBugNumber (num)
185 {
186 BUGNUMBER = num;
187 print ('BUGNUMBER: ' + num);
188 }
189
190 function toPrinted(value)
191 {
192 if (typeof value == "xml")
193 {
194 value = value.toXMLString();
195 }
196 else
197 {
198 value = String(value);
199 }
200 value = value.replace(/\\n/g, 'NL')
201 .replace(/\n/g, 'NL')
202 .replace(/\\r/g, 'CR')
203 .replace(/[^\x20-\x7E]+/g, escapeString);
204 return value;
205 }
206
207 function escapeString (str)
208 {
209 var a, b, c, d;
210 var len = str.length;
211 var result = "";
212 var digits = ["0", "1", "2", "3", "4", "5", "6", "7",
213 "8", "9", "A", "B", "C", "D", "E", "F"];
214
215 for (var i=0; i<len; i++)
216 {
217 var ch = str.charCodeAt(i);
218
219 a = digits[ch & 0xf];
220 ch >>= 4;
221 b = digits[ch & 0xf];
222 ch >>= 4;
223
224 if (ch)
225 {
226 c = digits[ch & 0xf];
227 ch >>= 4;
228 d = digits[ch & 0xf];
229
230 result += "\\u" + d + c + b + a;
231 }
232 else
233 {
234 result += "\\x" + b + a;
235 }
236 }
237
238 return result;
239 }
240
241 /*
242 * assertEq(actual, expected)
243 * Throw if the two arguments are not ===
244 * see https://bugzilla.mozilla.org/show_bug.cgi?id=480199
245 */
246 if (typeof assertEq == 'undefined')
247 {
248 var assertEq =
249 function (actual, expected)
250 {
251 if (actual !== expected)
252 {
253 throw new TypeError('Assertion failed: got "' + actual + '", expected "' + expected);
254 }
255 };
256 }
257
258 /*
259 * Compare expected result to actual result, if they differ (in value and/or
260 * type) report a failure. If description is provided, include it in the
261 * failure report.
262 */
263 function reportCompare (expected, actual, description) {
264 var expected_t = typeof expected;
265 var actual_t = typeof actual;
266 var output = "";
267
268 if (typeof description == "undefined")
269 {
270 description = '';
271 }
272 else if (VERBOSE)
273 {
274 printStatus ("Comparing '" + description + "'");
275 }
276
277 if (expected_t != actual_t)
278 {
279 output += "Type mismatch, expected type " + expected_t +
280 ", actual type " + actual_t + " ";
281 }
282 else if (VERBOSE)
283 {
284 printStatus ("Expected type '" + expected_t + "' matched actual " +
285 "type '" + actual_t + "'");
286 }
287
288 if (expected != actual)
289 {
290 output += "Expected value '" + toPrinted(expected) +
291 "', Actual value '" + toPrinted(actual) + "' ";
292 }
293 else if (VERBOSE)
294 {
295 printStatus ("Expected value '" + toPrinted(expected) +
296 "' matched actual value '" + toPrinted(actual) + "'");
297 }
298
299 var testcase = new TestCase(gTestfile, description, expected, actual);
300 testcase.reason = output;
301
302 if (testcase.passed)
303 {
304 print(PASSED + description);
305 }
306 else
307 {
308 reportFailure (description + " : " + output);
309 }
310
311 return testcase.passed;
312 }
313
314 /*
315 * Attempt to match a regular expression describing the result to
316 * the actual result, if they differ (in value and/or
317 * type) report a failure. If description is provided, include it in the
318 * failure report.
319 */
320 function reportMatch (expectedRegExp, actual, description) {
321 var expected_t = "string";
322 var actual_t = typeof actual;
323 var output = "";
324
325 if (typeof description == "undefined")
326 {
327 description = '';
328 }
329 else if (VERBOSE)
330 {
331 printStatus ("Comparing '" + description + "'");
332 }
333
334 if (expected_t != actual_t)
335 {
336 output += "Type mismatch, expected type " + expected_t +
337 ", actual type " + actual_t + " ";
338 }
339 else if (VERBOSE)
340 {
341 printStatus ("Expected type '" + expected_t + "' matched actual " +
342 "type '" + actual_t + "'");
343 }
344
345 var matches = expectedRegExp.test(actual);
346 if (!matches)
347 {
348 output += "Expected match to '" + toPrinted(expectedRegExp) +
349 "', Actual value '" + toPrinted(actual) + "' ";
350 }
351 else if (VERBOSE)
352 {
353 printStatus ("Expected match to '" + toPrinted(expectedRegExp) +
354 "' matched actual value '" + toPrinted(actual) + "'");
355 }
356
357 var testcase = new TestCase(gTestfile, description, true, matches);
358 testcase.reason = output;
359
360 if (testcase.passed)
361 {
362 print(PASSED + description);
363 }
364 else
365 {
366 reportFailure (description + " : " + output);
367 }
368
369 return testcase.passed;
370 }
371
372 /*
373 * Puts funcName at the top of the call stack. This stack is used to show
374 * a function-reported-from field when reporting failures.
375 */
376 function enterFunc (funcName)
377 {
378 if (!funcName.match(/\(\)$/))
379 funcName += "()";
380
381 callStack.push(funcName);
382 }
383
384 /*
385 * Pops the top funcName off the call stack. funcName is optional, and can be
386 * used to check push-pop balance.
387 */
388 function exitFunc (funcName)
389 {
390 var lastFunc = callStack.pop();
391
392 if (funcName)
393 {
394 if (!funcName.match(/\(\)$/))
395 funcName += "()";
396
397 if (lastFunc != funcName)
398 reportCompare(funcName, lastFunc, "Test driver failure wrong exit function ");
399 }
400 }
401
402 /*
403 * Peeks at the top of the call stack.
404 */
405 function currentFunc()
406 {
407 return callStack[callStack.length - 1];
408 }
409
410 /*
411 Calculate the "order" of a set of data points {X: [], Y: []}
412 by computing successive "derivatives" of the data until
413 the data is exhausted or the derivative is linear.
414 */
415 function BigO(data)
416 {
417 var order = 0;
418 var origLength = data.X.length;
419
420 while (data.X.length > 2)
421 {
422 var lr = new LinearRegression(data);
423 if (lr.b > 1e-6)
424 {
425 // only increase the order if the slope
426 // is "great" enough
427 order++;
428 }
429
430 if (lr.r > 0.98 || lr.Syx < 1 || lr.b < 1e-6)
431 {
432 // terminate if close to a line lr.r
433 // small error lr.Syx
434 // small slope lr.b
435 break;
436 }
437 data = dataDeriv(data);
438 }
439
440 if (2 == origLength - order)
441 {
442 order = Number.POSITIVE_INFINITY;
443 }
444 return order;
445
446 function LinearRegression(data)
447 {
448 /*
449 y = a + bx
450 for data points (Xi, Yi); 0 <= i < n
451
452 b = (n*SUM(XiYi) - SUM(Xi)*SUM(Yi))/(n*SUM(Xi*Xi) - SUM(Xi)*SUM(Xi))
453 a = (SUM(Yi) - b*SUM(Xi))/n
454 */
455 var i;
456
457 if (data.X.length != data.Y.length)
458 {
459 throw 'LinearRegression: data point length mismatch';
460 }
461 if (data.X.length < 3)
462 {
463 throw 'LinearRegression: data point length < 2';
464 }
465 var n = data.X.length;
466 var X = data.X;
467 var Y = data.Y;
468
469 this.Xavg = 0;
470 this.Yavg = 0;
471
472 var SUM_X = 0;
473 var SUM_XY = 0;
474 var SUM_XX = 0;
475 var SUM_Y = 0;
476 var SUM_YY = 0;
477
478 for (i = 0; i < n; i++)
479 {
480 SUM_X += X[i];
481 SUM_XY += X[i]*Y[i];
482 SUM_XX += X[i]*X[i];
483 SUM_Y += Y[i];
484 SUM_YY += Y[i]*Y[i];
485 }
486
487 this.b = (n * SUM_XY - SUM_X * SUM_Y)/(n * SUM_XX - SUM_X * SUM_X);
488 this.a = (SUM_Y - this.b * SUM_X)/n;
489
490 this.Xavg = SUM_X/n;
491 this.Yavg = SUM_Y/n;
492
493 var SUM_Ydiff2 = 0;
494 var SUM_Xdiff2 = 0;
495 var SUM_XdiffYdiff = 0;
496
497 for (i = 0; i < n; i++)
498 {
499 var Ydiff = Y[i] - this.Yavg;
500 var Xdiff = X[i] - this.Xavg;
501
502 SUM_Ydiff2 += Ydiff * Ydiff;
503 SUM_Xdiff2 += Xdiff * Xdiff;
504 SUM_XdiffYdiff += Xdiff * Ydiff;
505 }
506
507 var Syx2 = (SUM_Ydiff2 - Math.pow(SUM_XdiffYdiff/SUM_Xdiff2, 2))/(n - 2);
508 var r2 = Math.pow((n*SUM_XY - SUM_X * SUM_Y), 2) /
509 ((n*SUM_XX - SUM_X*SUM_X)*(n*SUM_YY-SUM_Y*SUM_Y));
510
511 this.Syx = Math.sqrt(Syx2);
512 this.r = Math.sqrt(r2);
513
514 }
515
516 function dataDeriv(data)
517 {
518 if (data.X.length != data.Y.length)
519 {
520 throw 'length mismatch';
521 }
522 var length = data.X.length;
523
524 if (length < 2)
525 {
526 throw 'length ' + length + ' must be >= 2';
527 }
528 var X = data.X;
529 var Y = data.Y;
530
531 var deriv = {X: [], Y: [] };
532
533 for (var i = 0; i < length - 1; i++)
534 {
535 deriv.X[i] = (X[i] + X[i+1])/2;
536 deriv.Y[i] = (Y[i+1] - Y[i])/(X[i+1] - X[i]);
537 }
538 return deriv;
539 }
540
541 return 0;
542 }
543
544 function compareSource(expect, actual, summary)
545 {
546 // compare source
547 var expectP = expect.
548 replace(/([(){},.:\[\]])/mg, ' $1 ').
549 replace(/(\w+)/mg, ' $1 ').
550 replace(/<(\/)? (\w+) (\/)?>/mg, '<$1$2$3>').
551 replace(/\s+/mg, ' ').
552 replace(/new (\w+)\s*\(\s*\)/mg, 'new $1');
553
554 var actualP = actual.
555 replace(/([(){},.:\[\]])/mg, ' $1 ').
556 replace(/(\w+)/mg, ' $1 ').
557 replace(/<(\/)? (\w+) (\/)?>/mg, '<$1$2$3>').
558 replace(/\s+/mg, ' ').
559 replace(/new (\w+)\s*\(\s*\)/mg, 'new $1');
560
561 print('expect:\n' + expectP);
562 print('actual:\n' + actualP);
563
564 reportCompare(expectP, actualP, summary);
565
566 // actual must be compilable if expect is?
567 try
568 {
569 var expectCompile = 'No Error';
570 var actualCompile;
571
572 eval(expect);
573 try
574 {
575 eval(actual);
576 actualCompile = 'No Error';
577 }
578 catch(ex1)
579 {
580 actualCompile = ex1 + '';
581 }
582 reportCompare(expectCompile, actualCompile,
583 summary + ': compile actual');
584 }
585 catch(ex)
586 {
587 }
588 }
589
590 function optionsInit() {
591
592 // record initial values to support resetting
593 // options to their initial values
594 options.initvalues = {};
595
596 // record values in a stack to support pushing
597 // and popping options
598 options.stackvalues = [];
599
600 var optionNames = options().split(',');
601
602 for (var i = 0; i < optionNames.length; i++)
603 {
604 var optionName = optionNames[i];
605 if (optionName)
606 {
607 options.initvalues[optionName] = '';
608 }
609 }
610 }
611
612 function optionsClear() {
613
614 // turn off current settings
615 var optionNames = options().split(',');
616 for (var i = 0; i < optionNames.length; i++)
617 {
618 var optionName = optionNames[i];
619 if (optionName)
620 {
621 options(optionName);
622 }
623 }
624 }
625
626 function optionsPush()
627 {
628 var optionsframe = {};
629
630 options.stackvalues.push(optionsframe);
631
632 var optionNames = options().split(',');
633
634 for (var i = 0; i < optionNames.length; i++)
635 {
636 var optionName = optionNames[i];
637 if (optionName)
638 {
639 optionsframe[optionName] = '';
640 }
641 }
642
643 optionsClear();
644 }
645
646 function optionsPop()
647 {
648 var optionsframe = options.stackvalues.pop();
649
650 optionsClear();
651
652 for (optionName in optionsframe)
653 {
654 options(optionName);
655 }
656
657 }
658
659 function optionsReset() {
660
661 try
662 {
663 optionsClear();
664
665 // turn on initial settings
666 for (optionName in options.initvalues)
667 {
668 options(optionName);
669 }
670 }
671 catch(ex)
672 {
673 print('optionsReset: caught ' + ex);
674 }
675
676 }
677
678 if (typeof options == 'function')
679 {
680 optionsInit();
681 optionsClear();
682 }
683
684 function getTestCaseResult(expected, actual)
685 {
686 if (typeof expected != typeof actual)
687 return false;
688 if (typeof expected != 'number')
689 // Note that many tests depend on the use of '==' here, not '==='.
690 return actual == expected;
691
692 // Distinguish NaN from other values. Using x != x comparisons here
693 // works even if tests redefine isNaN.
694 if (actual != actual)
695 return expected != expected;
696 if (expected != expected)
697 return false;
698
699 // Tolerate a certain degree of error.
700 if (actual != expected)
701 return Math.abs(actual - expected) <= 1E-10;
702
703 // Here would be a good place to distinguish 0 and -0, if we wanted
704 // to. However, doing so would introduce a number of failures in
705 // areas where they don't seem important. For example, the WeekDay
706 // function in ECMA-262 returns -0 for Sundays before the epoch, but
707 // the Date functions in SpiderMonkey specified in terms of WeekDay
708 // often don't. This seems unimportant.
709 return true;
710 }
711
712 if (typeof dump == 'undefined')
713 {
714 if (typeof window == 'undefined' &&
715 typeof print == 'function')
716 {
717 dump = print;
718 }
719 else
720 {
721 dump = (function () {});
722 }
723 }
724
725 function test() {
726 for ( gTc=0; gTc < gTestcases.length; gTc++ ) {
727 // temporary hack to work around some unknown issue in 1.7
728 try
729 {
730 gTestcases[gTc].passed = writeTestCaseResult(
731 gTestcases[gTc].expect,
732 gTestcases[gTc].actual,
733 gTestcases[gTc].description +" = "+ gTestcases[gTc].actual );
734 gTestcases[gTc].reason += ( gTestcases[gTc].passed ) ? "" : "wrong value ";
735 }
736 catch(e)
737 {
738 print('test(): empty testcase for gTc = ' + gTc + ' ' + e);
739 }
740 }
741 stopTest();
742 return ( gTestcases );
743 }
744
745 /*
746 * Begin printing functions. These functions use the shell's
747 * print function. When running tests in the browser, these
748 * functions, override these functions with functions that use
749 * document.write.
750 */
751
752 function writeTestCaseResult( expect, actual, string ) {
753 var passed = getTestCaseResult( expect, actual );
754 writeFormattedResult( expect, actual, string, passed );
755 return passed;
756 }
757 function writeFormattedResult( expect, actual, string, passed ) {
758 var s = ( passed ? PASSED : FAILED ) + string + ' expected: ' + expect;
759 print(s);
760 return passed;
761 }
762
763 function writeHeaderToLog( string ) {
764 print( string );
765 }
766 /* end of print functions */
767
768
769 /*
770 * When running in the shell, run the garbage collector after the
771 * test has completed.
772 */
773
774 function stopTest() {
775 var gc;
776 if ( gc != undefined ) {
777 gc();
778 }
779 }
780
781 /*
782 * Convenience function for displaying failed test cases. Useful
783 * when running tests manually.
784 *
785 */
786 function getFailedCases() {
787 for ( var i = 0; i < gTestcases.length; i++ ) {
788 if ( ! gTestcases[i].passed ) {
789 print( gTestcases[i].description + " = " +gTestcases[i].actual +
790 " expected: " + gTestcases[i].expect );
791 }
792 }
793 }
794
795 function jsTestDriverEnd()
796 {
797 // gDelayTestDriverEnd is used to
798 // delay collection of the test result and
799 // signal to Spider so that tests can continue
800 // to run after page load has fired. They are
801 // responsible for setting gDelayTestDriverEnd = true
802 // then when completed, setting gDelayTestDriverEnd = false
803 // then calling jsTestDriverEnd()
804
805 if (gDelayTestDriverEnd)
806 {
807 return;
808 }
809
810 try
811 {
812 optionsReset();
813 }
814 catch(ex)
815 {
816 dump('jsTestDriverEnd ' + ex);
817 }
818
819 for (var i = 0; i < gTestcases.length; i++)
820 {
821 gTestcases[i].dump();
822 }
823
824 }
825
826 function jit(on)
827 {
828 if (on && !options().match(/jit/))
829 {
830 options('jit');
831 }
832 else if (!on && options().match(/jit/))
833 {
834 options('jit');
835 }
836 }
837
838 /*
839 * Some tests need to know if we are in Rhino as opposed to SpiderMonkey
840 */
841 function inRhino()
842 {
843 return (typeof defineClass == "function");
844 }
845
846 /* these functions are useful for running tests manually in Rhino */
847
848 function GetContext() {
849 return Packages.com.netscape.javascript.Context.getCurrentContext();
850 }
851 function OptLevel( i ) {
852 i = Number(i);
853 var cx = GetContext();
854 cx.setOptimizationLevel(i);
855 }
856 /* end of Rhino functions */
857
858