tor-browser

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

bigint-compare.js (3426B)


      1 // Test various combinations of (BigInt R BigInt) and ensures the result is consistent with
      2 // (BigInt R⁻¹ BigInt). This test doesn't aim to cover the overall correctness of (BigInt R BigInt),
      3 // but merely ensures the possible combinations are properly handled in CacheIR.
      4 
      5 var xs = [
      6  // Definitely heap digits.
      7  -(2n ** 1000n),
      8 
      9  // -(2n**64n)
     10  -18446744073709551617n,
     11  -18446744073709551616n,
     12  -18446744073709551615n,
     13 
     14  // -(2n**63n)
     15  -9223372036854775809n,
     16  -9223372036854775808n,
     17  -9223372036854775807n,
     18 
     19  // -(2**32)
     20  -4294967297n,
     21  -4294967296n,
     22  -4294967295n,
     23 
     24  // -(2**31)
     25  -2147483649n,
     26  -2147483648n,
     27  -2147483647n,
     28 
     29  -1n,
     30  0n,
     31  1n,
     32 
     33  // 2**31
     34  2147483647n,
     35  2147483648n,
     36  2147483649n,
     37 
     38  // 2**32
     39  4294967295n,
     40  4294967296n,
     41  4294967297n,
     42 
     43  // 2n**63n
     44  9223372036854775807n,
     45  9223372036854775808n,
     46  9223372036854775809n,
     47 
     48  // 2n**64n
     49  18446744073709551615n,
     50  18446744073709551616n,
     51  18446744073709551617n,
     52 
     53  // Definitely heap digits.
     54  2n ** 1000n,
     55 ];
     56 
     57 function testLooseEqual() {
     58  for (var i = 0; i < 100; ++i) {
     59    var j = i % xs.length;
     60    var x = xs[j];
     61    var isZero = j === xs.length >> 1;
     62 
     63    assertEq(x == x, true);
     64    assertEq(x == 0n, isZero);
     65    assertEq(0n == x, isZero);
     66  }
     67 }
     68 testLooseEqual();
     69 
     70 function testStrictEqual() {
     71  for (var i = 0; i < 100; ++i) {
     72    var j = i % xs.length;
     73    var x = xs[j];
     74    var isZero = j === xs.length >> 1;
     75 
     76    assertEq(x === x, true);
     77    assertEq(x === 0n, isZero);
     78    assertEq(0n === x, isZero);
     79  }
     80 }
     81 testStrictEqual();
     82 
     83 function testLooseNotEqual() {
     84  for (var i = 0; i < 100; ++i) {
     85    var j = i % xs.length;
     86    var x = xs[j];
     87    var isZero = j === xs.length >> 1;
     88 
     89    assertEq(x != x, false);
     90    assertEq(x != 0n, !isZero);
     91    assertEq(0n != x, !isZero);
     92  }
     93 }
     94 testLooseNotEqual();
     95 
     96 function testStrictNotEqual() {
     97  for (var i = 0; i < 100; ++i) {
     98    var j = i % xs.length;
     99    var x = xs[j];
    100    var isZero = j === xs.length >> 1;
    101 
    102    assertEq(x !== x, false);
    103    assertEq(x !== 0n, !isZero);
    104    assertEq(0n !== x, !isZero);
    105  }
    106 }
    107 testStrictNotEqual();
    108 
    109 function testLessThan() {
    110  for (var i = 0; i < 100; ++i) {
    111    var j = i % xs.length;
    112    var x = xs[j];
    113    var isNegative = j < (xs.length >> 1);
    114    var isPositive = j > (xs.length >> 1);
    115 
    116    assertEq(x < x, false);
    117    assertEq(x < 0n, isNegative);
    118    assertEq(0n < x, isPositive);
    119  }
    120 }
    121 testLessThan();
    122 
    123 function testLessThanEquals() {
    124  for (var i = 0; i < 100; ++i) {
    125    var j = i % xs.length;
    126    var x = xs[j];
    127    var isNegativeOrZero = j <= (xs.length >> 1);
    128    var isPositiveOrZero = j >= (xs.length >> 1);
    129 
    130    assertEq(x <= x, true);
    131    assertEq(x <= 0n, isNegativeOrZero);
    132    assertEq(0n <= x, isPositiveOrZero);
    133  }
    134 }
    135 testLessThanEquals();
    136 
    137 function testGreaterThan() {
    138  for (var i = 0; i < 100; ++i) {
    139    var j = i % xs.length;
    140    var x = xs[j];
    141    var isNegative = j < (xs.length >> 1);
    142    var isPositive = j > (xs.length >> 1);
    143 
    144    assertEq(x > x, false);
    145    assertEq(x > 0n, isPositive);
    146    assertEq(0n > x, isNegative);
    147  }
    148 }
    149 testGreaterThan();
    150 
    151 function testGreaterThanEquals() {
    152  for (var i = 0; i < 100; ++i) {
    153    var j = i % xs.length;
    154    var x = xs[j];
    155    var isNegativeOrZero = j <= (xs.length >> 1);
    156    var isPositiveOrZero = j >= (xs.length >> 1);
    157 
    158    assertEq(x >= x, true);
    159    assertEq(x >= 0n, isPositiveOrZero);
    160    assertEq(0n >= x, isNegativeOrZero);
    161  }
    162 }
    163 testGreaterThanEquals();