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