js/src/tests/ecma_6/Math/fround.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.

     1 /*
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/licenses/publicdomain/
     4  */
     6 // Some tests regarding conversion to Float32
     7 assertEq(Math.fround(), NaN);
     9 // Special values
    10 assertEq(Math.fround(NaN), NaN);
    11 assertEq(Math.fround(-Infinity), -Infinity);
    12 assertEq(Math.fround(Infinity), Infinity);
    13 assertEq(Math.fround(-0), -0);
    14 assertEq(Math.fround(+0), +0);
    16 // Polyfill function for Float32 conversion
    17 var toFloat32 = (function() {
    18     var f32 = new Float32Array(1);
    19     function f(x) {
    20         f32[0] = x;
    21         return f32[0];
    22     }
    23     return f;
    24 })();
    26 // A test on a certain range of numbers, including big numbers, so that
    27 // we get a loss in precision for some of them.
    28 for (var i = 0; i < 64; ++i) {
    29     var p = Math.pow(2, i) + 1;
    30     assertEq(Math.fround(p), toFloat32(p));
    31     assertEq(Math.fround(-p), toFloat32(-p));
    32 }
    34 /********************************************
    35 /* Tests on maximal Float32 / Double values *
    36 /*******************************************/
    37 function maxValue(exponentWidth, significandWidth) {
    38     var n = 0;
    39     var maxExp = Math.pow(2, exponentWidth - 1) - 1;
    40     for (var i = significandWidth; i >= 0; i--)
    41         n += Math.pow(2, maxExp - i);
    42     return n;
    43 }
    45 var DBL_MAX = maxValue(11, 52);
    46 assertEq(DBL_MAX, Number.MAX_VALUE); // sanity check
    48 // Finite as a double, too big for a float
    49 assertEq(Math.fround(DBL_MAX), Infinity);
    51 var FLT_MAX = maxValue(8, 23);
    52 assertEq(Math.fround(FLT_MAX), FLT_MAX);
    53 assertEq(Math.fround(FLT_MAX + Math.pow(2, Math.pow(2, 8 - 1) - 1 - 23 - 2)), FLT_MAX); // round-nearest rounds down to FLT_MAX
    54 assertEq(Math.fround(FLT_MAX + Math.pow(2, Math.pow(2, 8 - 1) - 1 - 23 - 1)), Infinity); // no longer rounds down to FLT_MAX
    56 /*********************************************************
    57 /******* Tests on denormalizations and roundings *********
    58 /********************************************************/
    60 function minValue(exponentWidth, significandWidth) {
    61     return Math.pow(2, -(Math.pow(2, exponentWidth - 1) - 2) - significandWidth);
    62 }
    64 var DBL_MIN = Math.pow(2, -1074);
    65 assertEq(DBL_MIN, Number.MIN_VALUE); // sanity check
    67 // Too small for a float
    68 assertEq(Math.fround(DBL_MIN), 0);
    70 var FLT_MIN = minValue(8, 23);
    71 assertEq(Math.fround(FLT_MIN), FLT_MIN);
    73 assertEq(Math.fround(FLT_MIN / 2), 0); // halfway, round-nearest rounds down to 0 (even)
    74 assertEq(Math.fround(FLT_MIN / 2 + Math.pow(2, -202)), FLT_MIN); // first double > FLT_MIN / 2, rounds up to FLT_MIN
    76 assertEq(Math.fround(-FLT_MIN), -FLT_MIN);
    78 assertEq(Math.fround(-FLT_MIN / 2), -0); // halfway, round-nearest rounds up to -0 (even)
    79 assertEq(Math.fround(-FLT_MIN / 2 - Math.pow(2, -202)), -FLT_MIN); // first double < -FLT_MIN / 2, rounds down to -FLT_MIN
    81 reportCompare(0, 0, "ok");

mercurial