js/src/tests/ecma_6/Math/shell.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 // The nearest representable values to +1.0.
michael@0 2 const ONE_PLUS_EPSILON = 1 + Math.pow(2, -52); // 0.9999999999999999
michael@0 3 const ONE_MINUS_EPSILON = 1 - Math.pow(2, -53); // 1.0000000000000002
michael@0 4
michael@0 5 {
michael@0 6 var fail = function (msg) {
michael@0 7 var exc = new Error(msg);
michael@0 8 try {
michael@0 9 // Try to improve on exc.fileName and .lineNumber; leave exc.stack
michael@0 10 // alone. We skip two frames: fail() and its caller, an assertX()
michael@0 11 // function.
michael@0 12 var frames = exc.stack.trim().split("\n");
michael@0 13 if (frames.length > 2) {
michael@0 14 var m = /@([^@:]*):([0-9]+)$/.exec(frames[2]);
michael@0 15 if (m) {
michael@0 16 exc.fileName = m[1];
michael@0 17 exc.lineNumber = +m[2];
michael@0 18 }
michael@0 19 }
michael@0 20 } catch (ignore) { throw ignore;}
michael@0 21 throw exc;
michael@0 22 };
michael@0 23
michael@0 24 var ENDIAN; // 0 for little-endian, 1 for big-endian.
michael@0 25
michael@0 26 // Return the difference between the IEEE 754 bit-patterns for a and b.
michael@0 27 //
michael@0 28 // This is meaningful when a and b are both finite and have the same
michael@0 29 // sign. Then the following hold:
michael@0 30 //
michael@0 31 // * If a === b, then diff(a, b) === 0.
michael@0 32 //
michael@0 33 // * If a !== b, then diff(a, b) === 1 + the number of representable values
michael@0 34 // between a and b.
michael@0 35 //
michael@0 36 var f = new Float64Array([0, 0]);
michael@0 37 var u = new Uint32Array(f.buffer);
michael@0 38 var diff = function (a, b) {
michael@0 39 f[0] = a;
michael@0 40 f[1] = b;
michael@0 41 //print(u[1].toString(16) + u[0].toString(16) + " " + u[3].toString(16) + u[2].toString(16));
michael@0 42 return Math.abs((u[3-ENDIAN] - u[1-ENDIAN]) * 0x100000000 + u[2+ENDIAN] - u[0+ENDIAN]);
michael@0 43 };
michael@0 44
michael@0 45 // Set ENDIAN to the platform's endianness.
michael@0 46 ENDIAN = 0; // try little-endian first
michael@0 47 if (diff(2, 4) === 0x100000) // exact wrong answer we'll get on a big-endian platform
michael@0 48 ENDIAN = 1;
michael@0 49 assertEq(diff(2,4), 0x10000000000000);
michael@0 50 assertEq(diff(0, Number.MIN_VALUE), 1);
michael@0 51 assertEq(diff(1, ONE_PLUS_EPSILON), 1);
michael@0 52 assertEq(diff(1, ONE_MINUS_EPSILON), 1);
michael@0 53
michael@0 54 var assertNear = function assertNear(a, b, tolerance=1) {
michael@0 55 if (!Number.isFinite(b)) {
michael@0 56 fail("second argument to assertNear (expected value) must be a finite number");
michael@0 57 } else if (Number.isNaN(a)) {
michael@0 58 fail("got NaN, expected a number near " + b);
michael@0 59 } else if (!Number.isFinite(a)) {
michael@0 60 if (b * Math.sign(a) < Number.MAX_VALUE)
michael@0 61 fail("got " + a + ", expected a number near " + b);
michael@0 62 } else {
michael@0 63 // When the two arguments do not have the same sign bit, diff()
michael@0 64 // returns some huge number. So if b is positive or negative 0,
michael@0 65 // make target the zero that has the same sign bit as a.
michael@0 66 var target = b === 0 ? a * 0 : b;
michael@0 67 var err = diff(a, target);
michael@0 68 if (err > tolerance) {
michael@0 69 fail("got " + a + ", expected a number near " + b +
michael@0 70 " (relative error: " + err + ")");
michael@0 71 }
michael@0 72 }
michael@0 73 };
michael@0 74 }

mercurial