tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

bigint-compare-double.js (4363B)


      1 // Same test as bigint-compare-number, except that the inputs are always Double typed.
      2 
      3 // Test various combinations of (BigInt R Double) and ensures the result is consistent with
      4 // (Double R⁻¹ BigInt). This test doesn't aim to cover the overall correctness of (BigInt R Double),
      5 // but merely ensures the possible combinations are properly handled in CacheIR.
      6 
      7 var xs = [
      8  // Definitely heap digits.
      9  -(2n ** 2000n),
     10  -(2n ** 1000n),
     11 
     12  // -(2n**64n)
     13  -18446744073709551617n,
     14  -18446744073709551616n,
     15  -18446744073709551615n,
     16 
     17  // -(2n**63n)
     18  -9223372036854775809n,
     19  -9223372036854775808n,
     20  -9223372036854775807n,
     21 
     22  // -(2**32)
     23  -4294967297n,
     24  -4294967296n,
     25  -4294967295n,
     26 
     27  // -(2**31)
     28  -2147483649n,
     29  -2147483648n,
     30  -2147483647n,
     31 
     32  -1n,
     33  0n,
     34  1n,
     35 
     36  // 2**31
     37  2147483647n,
     38  2147483648n,
     39  2147483649n,
     40 
     41  // 2**32
     42  4294967295n,
     43  4294967296n,
     44  4294967297n,
     45 
     46  // 2n**63n
     47  9223372036854775807n,
     48  9223372036854775808n,
     49  9223372036854775809n,
     50 
     51  // 2n**64n
     52  18446744073709551615n,
     53  18446744073709551616n,
     54  18446744073709551617n,
     55 
     56  // Definitely heap digits.
     57  2n ** 1000n,
     58  2n ** 2000n,
     59 ];
     60 
     61 function Double(x) {
     62  // numberToDouble always returns a Double valued number.
     63  return numberToDouble(x);
     64 }
     65 
     66 // Compute the Double approximation of the BigInt values.
     67 var ys = xs.map(x => Double(Number(x)));
     68 
     69 // Compute if the Double approximation of the BigInt values is exact.
     70 // (The larger test values are all powers of two, so we can keep this function simple.)
     71 var zs = xs.map(x => {
     72  var isNegative = x < 0n;
     73  if (isNegative) {
     74    x = -x;
     75  }
     76  var s = x.toString(2);
     77  if (s.length <= 53 || (s.length <= 1024 && /^1+0+$/.test(s))) {
     78    return 0;
     79  }
     80  if (s.length <= 1024 && /^1+$/.test(s)) {
     81    return isNegative ? -1 : 1;
     82  }
     83  if (s.length <= 1024 && /^1+0+1$/.test(s)) {
     84    return isNegative ? 1 : -1;
     85  }
     86  return NaN;
     87 });
     88 
     89 function testLooseEqual() {
     90  for (var i = 0; i < 100; ++i) {
     91    var j = i % xs.length;
     92    var x = xs[j];
     93    var y = ys[j];
     94    var z = zs[j];
     95 
     96    assertEq(x == y, z === 0);
     97    assertEq(y == x, z === 0);
     98  }
     99 }
    100 testLooseEqual();
    101 
    102 function testLooseNotEqual() {
    103  for (var i = 0; i < 100; ++i) {
    104    var j = i % xs.length;
    105    var x = xs[j];
    106    var y = ys[j];
    107    var z = zs[j];
    108 
    109    assertEq(x != y, z !== 0);
    110    assertEq(y != x, z !== 0);
    111  }
    112 }
    113 testLooseNotEqual();
    114 
    115 function testLessThan() {
    116  for (var i = 0; i < 100; ++i) {
    117    var j = i % xs.length;
    118    var x = xs[j];
    119    var y = ys[j];
    120    var z = zs[j];
    121 
    122    if (z === 0) {
    123      assertEq(x < y, false);
    124      assertEq(y < x, false);
    125    } else if (z > 0) {
    126      assertEq(x < y, true);
    127      assertEq(y < x, false);
    128    } else if (z < 0) {
    129      assertEq(x < y, false);
    130      assertEq(y < x, true);
    131    } else {
    132      assertEq(x < y, y > 0);
    133      assertEq(y < x, y < 0);
    134    }
    135  }
    136 }
    137 testLessThan();
    138 
    139 function testLessThanEquals() {
    140  for (var i = 0; i < 100; ++i) {
    141    var j = i % xs.length;
    142    var x = xs[j];
    143    var y = ys[j];
    144    var z = zs[j];
    145 
    146    if (z === 0) {
    147      assertEq(x <= y, true);
    148      assertEq(y <= x, true);
    149    } else if (z > 0) {
    150      assertEq(x <= y, true);
    151      assertEq(y <= x, false);
    152    } else if (z < 0) {
    153      assertEq(x <= y, false);
    154      assertEq(y <= x, true);
    155    } else {
    156      assertEq(x <= y, y > 0);
    157      assertEq(y <= x, y < 0);
    158    }
    159  }
    160 }
    161 testLessThanEquals();
    162 
    163 function testGreaterThan() {
    164  for (var i = 0; i < 100; ++i) {
    165    var j = i % xs.length;
    166    var x = xs[j];
    167    var y = ys[j];
    168    var z = zs[j];
    169 
    170    if (z === 0) {
    171      assertEq(x > y, false);
    172      assertEq(y > x, false);
    173    } else if (z > 0) {
    174      assertEq(x > y, false);
    175      assertEq(y > x, true);
    176    } else if (z < 0) {
    177      assertEq(x > y, true);
    178      assertEq(y > x, false);
    179    } else {
    180      assertEq(x > y, y < 0);
    181      assertEq(y > x, y > 0);
    182    }
    183  }
    184 }
    185 testGreaterThan();
    186 
    187 function testGreaterThanEquals() {
    188  for (var i = 0; i < 100; ++i) {
    189    var j = i % xs.length;
    190    var x = xs[j];
    191    var y = ys[j];
    192    var z = zs[j];
    193 
    194    if (z === 0) {
    195      assertEq(x >= y, true);
    196      assertEq(y >= x, true);
    197    } else if (z > 0) {
    198      assertEq(x >= y, false);
    199      assertEq(y >= x, true);
    200    } else if (z < 0) {
    201      assertEq(x >= y, true);
    202      assertEq(y >= x, false);
    203    } else {
    204      assertEq(x >= y, y < 0);
    205      assertEq(y >= x, y > 0);
    206    }
    207  }
    208 }
    209 testGreaterThanEquals();