]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/js1_6/shell.js
a1c08c00904f516e0b8d688b1abcf44bc4f63ddc
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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 var FAILED
= "FAILED!: ";
43 var STATUS
= "STATUS: ";
44 var BUGNUMBER
= "BUGNUMBER: ";
46 var SECT_PREFIX
= 'Section ';
47 var SECT_SUFFIX
= ' of test -';
48 var callStack
= new Array();
50 function writeLineToLog( string
) {
51 print( string
+ "\n");
54 * The test driver searches for such a phrase in the test output.
55 * If such phrase exists, it will set n as the expected exit code.
57 function expectExitCode(n
)
60 writeLineToLog('--- NOTE: IN THIS TESTCASE, WE EXPECT EXIT CODE ' + n
+ ' ---');
65 * Statuses current section of a test
70 return SECT_PREFIX
+ x
+ SECT_SUFFIX
;
75 * Some tests need to know if we are in Rhino as opposed to SpiderMonkey
79 return (typeof defineClass
== "function");
83 * Report a failure in the 'accepted' manner
85 function reportFailure (msg
)
87 var lines
= msg
.split ("\n");
89 var funcName
= currentFunc();
90 var prefix
= (funcName
) ? "[reported from " + funcName
+ "] ": "";
92 for (var i
=0; i
<lines
.length
; i
++)
93 writeLineToLog (FAILED
+ prefix
+ lines
[i
]);
98 * Print a non-failure message.
100 function printStatus (msg
)
103 msg
= msg
.toString();
104 var lines
= msg
.split ("\n");
107 for (var i
=0; i
<lines
.length
; i
++)
108 writeLineToLog (STATUS
+ lines
[i
]);
113 * Print a bugnumber message.
115 function printBugNumber (num
)
118 writeLineToLog (BUGNUMBER
+ num
);
123 * Compare expected result to actual result, if they differ (in value and/or
124 * type) report a failure. If description is provided, include it in the
127 function reportCompare (expected
, actual
, description
)
129 var expected_t
= typeof expected
;
130 var actual_t
= typeof actual
;
133 if ((VERBOSE
) && (typeof description
!= "undefined"))
134 printStatus ("Comparing '" + description
+ "'");
136 if (expected_t
!= actual_t
)
137 output
+= "Type mismatch, expected type " + expected_t
+
138 ", actual type " + actual_t
+ "\n";
140 printStatus ("Expected type '" + actual_t
+ "' matched actual " +
141 "type '" + expected_t
+ "'");
143 if (expected
!= actual
)
144 output
+= "Expected value '" + expected
+ "', Actual value '" + actual
+
147 printStatus ("Expected value '" + actual
+ "' matched actual " +
148 "value '" + expected
+ "'");
152 if (typeof description
!= "undefined")
153 reportFailure (description
);
154 reportFailure (output
);
156 return (output
== ""); // true if passed
160 * Puts funcName at the top of the call stack. This stack is used to show
161 * a function-reported-from field when reporting failures.
163 function enterFunc (funcName
)
166 if (!funcName
.match(/\(\)$/))
169 callStack
.push(funcName
);
174 * Pops the top funcName off the call stack. funcName is optional, and can be
175 * used to check push-pop balance.
177 function exitFunc (funcName
)
179 var lastFunc
= callStack
.pop();
183 if (!funcName
.match(/\(\)$/))
186 if (lastFunc
!= funcName
)
187 reportFailure ("Test driver failure, expected to exit function '" +
188 funcName
+ "' but '" + lastFunc
+ "' came off " +
195 * Peeks at the top of the call stack.
197 function currentFunc()
200 return callStack
[callStack
.length
- 1];
205 Calculate the "order" of a set of data points {X: [], Y: []}
206 by computing successive "derivatives" of the data until
207 the data is exhausted or the derivative is linear.
212 var origLength
= data
.X
.length
;
214 while (data
.X
.length
> 2)
216 var lr
= new LinearRegression(data
);
219 // only increase the order if the slope
224 if (lr
.r
> 0.98 || lr
.Syx
< 1 || lr
.b
< 1e-6)
226 // terminate if close to a line lr.r
227 // small error lr.Syx
231 data
= dataDeriv(data
);
234 if (2 == origLength
- order
)
236 order
= Number
.POSITIVE_INFINITY
;
240 function LinearRegression(data
)
244 for data points (Xi, Yi); 0 <= i < n
246 b = (n*SUM(XiYi) - SUM(Xi)*SUM(Yi))/(n*SUM(Xi*Xi) - SUM(Xi)*SUM(Xi))
247 a = (SUM(Yi) - b*SUM(Xi))/n
251 if (data
.X
.length
!= data
.Y
.length
)
253 throw 'LinearRegression: data point length mismatch';
255 if (data
.X
.length
< 3)
257 throw 'LinearRegression: data point length < 2';
259 var n
= data
.X
.length
;
272 for (i
= 0; i
< n
; i
++)
281 this.b
= (n
* SUM_XY
- SUM_X
* SUM_Y
)/(n
* SUM_XX
- SUM_X
* SUM_X
);
282 this.a
= (SUM_Y
- this.b
* SUM_X
)/n
;
289 var SUM_XdiffYdiff
= 0;
291 for (i
= 0; i
< n
; i
++)
293 var Ydiff
= Y
[i
] - this.Yavg
;
294 var Xdiff
= X
[i
] - this.Xavg
;
296 SUM_Ydiff2
+= Ydiff
* Ydiff
;
297 SUM_Xdiff2
+= Xdiff
* Xdiff
;
298 SUM_XdiffYdiff
+= Xdiff
* Ydiff
;
301 var Syx2
= (SUM_Ydiff2
- Math
.pow(SUM_XdiffYdiff
/SUM_Xdiff2
, 2))/(n
- 2);
302 var r2
= Math
.pow((n
*SUM_XY
- SUM_X
* SUM_Y
), 2) /
303 ((n
*SUM_XX
- SUM_X
*SUM_X
)*(n
*SUM_YY
-SUM_Y
*SUM_Y
));
305 this.Syx
= Math
.sqrt(Syx2
);
306 this.r
= Math
.sqrt(r2
);
310 function dataDeriv(data
)
312 if (data
.X
.length
!= data
.Y
.length
)
314 throw 'length mismatch';
316 var length
= data
.X
.length
;
320 throw 'length ' + length
+ ' must be >= 2';
325 var deriv
= {X: [], Y: [] };
327 for (var i
= 0; i
< length
- 1; i
++)
329 deriv
.X
[i
] = (X
[i
] + X
[i
+1])/2;
330 deriv
.Y
[i
] = (Y
[i
+1] - Y
[i
])/(X
[i
+1] - X
[i
]);
338 encapsulate the logic for setting and retrieving the values
339 of the javascript options.
341 Note: in shell, options() takes an optional comma delimited list
342 of option names, toggles the values for each option and returns the
343 list of option names which were set before the call.
344 If no argument is passed to options(), it returns the current
345 options with value true.
349 // create and initialize object.
350 jsOptions = new JavaScriptOptions();
352 // set a particular option
353 jsOptions.setOption(name, boolean);
355 // reset all options to their original values.
359 function JavaScriptOptions()
362 this.orig
.strict
= this.strict
= false;
363 this.orig
.werror
= this.werror
= false;
365 this.privileges
= 'UniversalXPConnect';
367 if (typeof options
== 'function')
370 var optString
= options();
373 var optList
= optString
.split(',');
374 for (iOpt
= 0; i
< optList
.length
; iOpt
++)
376 optName
= optList
[iOpt
];
377 this[optName
] = true;
381 else if (typeof document
!= 'undefined')
384 netscape
.security
.PrivilegeManager
.enablePrivilege(this.privileges
);
386 var preferences
= Components
.classes
['@mozilla.org/preferences;1'];
389 throw 'JavaScriptOptions: unable to get @mozilla.org/preference;1';
392 var prefService
= preferences
.
393 getService(Components
.interfaces
.nsIPrefService
);
397 throw 'JavaScriptOptions: unable to get nsIPrefService';
400 var pref
= prefService
.getBranch('');
404 throw 'JavaScriptOptions: unable to get prefService branch';
409 this.orig
.strict
= this.strict
=
410 pref
.getBoolPref('javascript.options.strict');
418 this.orig
.werror
= this.werror
=
419 pref
.getBoolPref('javascript.options.werror');
427 JavaScriptOptions
.prototype.setOption
=
428 function (optionName
, optionValue
)
430 if (typeof options
== 'function')
433 if (this[optionName
] != optionValue
)
438 else if (typeof document
!= 'undefined')
441 netscape
.security
.PrivilegeManager
.enablePrivilege(this.privileges
);
443 var preferences
= Components
.classes
['@mozilla.org/preferences;1'];
446 throw 'setOption: unable to get @mozilla.org/preference;1';
449 var prefService
= preferences
.
450 getService(Components
.interfaces
.nsIPrefService
);
454 throw 'setOption: unable to get nsIPrefService';
457 var pref
= prefService
.getBranch('');
461 throw 'setOption: unable to get prefService branch';
464 pref
.setBoolPref('javascript.options.' + optionName
, optionValue
);
467 this[optionName
] = optionValue
;
473 JavaScriptOptions
.prototype.reset = function ()
475 this.setOption('strict', this.orig
.strict
);
476 this.setOption('werror', this.orig
.werror
);