tor-browser

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

bigint-compare-bigint-int32-constant.js (5629B)


      1 // Extensive test for (BigInt R BigInt(Int32)) comparison operations, testing the output
      2 // is correct and consistent with (BigInt(Int32) R⁻¹ BigInt).
      3 
      4 function LessThan(xs) {
      5  for (var i = 0; i < 100; ++i) {
      6    var x = xs[i % xs.length];
      7 
      8    assertEq(x == INT32, false);
      9    assertEq(INT32 == x, false);
     10 
     11    assertEq(x != INT32, true);
     12    assertEq(INT32 != x, true);
     13 
     14    assertEq(x < INT32, true);
     15    assertEq(INT32 < x, false);
     16 
     17    assertEq(x <= INT32, true);
     18    assertEq(INT32 <= x, false);
     19 
     20    assertEq(x > INT32, false);
     21    assertEq(INT32 > x, true);
     22 
     23    assertEq(x >= INT32, false);
     24    assertEq(INT32 >= x, true);
     25  }
     26 }
     27 
     28 function GreaterThan(xs) {
     29  for (var i = 0; i < 100; ++i) {
     30    var x = xs[i % xs.length];
     31 
     32    assertEq(x == INT32, false);
     33    assertEq(INT32 == x, false);
     34 
     35    assertEq(x != INT32, true);
     36    assertEq(INT32 != x, true);
     37 
     38    assertEq(x < INT32, false);
     39    assertEq(INT32 < x, true);
     40 
     41    assertEq(x <= INT32, false);
     42    assertEq(INT32 <= x, true);
     43 
     44    assertEq(x > INT32, true);
     45    assertEq(INT32 > x, false);
     46 
     47    assertEq(x >= INT32, true);
     48    assertEq(INT32 >= x, false);
     49  }
     50 }
     51 
     52 function Equal(xs) {
     53  for (var i = 0; i < 100; ++i) {
     54    var x = xs[i % xs.length];
     55 
     56    assertEq(x == INT32, true);
     57    assertEq(INT32 == x, true);
     58 
     59    assertEq(x != INT32, false);
     60    assertEq(INT32 != x, false);
     61 
     62    assertEq(x < INT32, false);
     63    assertEq(INT32 < x, false);
     64 
     65    assertEq(x <= INT32, true);
     66    assertEq(INT32 <= x, true);
     67 
     68    assertEq(x > INT32, false);
     69    assertEq(INT32 > x, false);
     70 
     71    assertEq(x >= INT32, true);
     72    assertEq(INT32 >= x, true);
     73  }
     74 }
     75 
     76 function test(fn, xs, ys) {
     77  // Clone the test function to ensure a new function is compiled each time.
     78  for (let y of ys) {
     79    let copy = Function(`return ${fn}`.replaceAll("INT32", `${(y|0)}n`))();
     80    copy(xs);
     81  }
     82 }
     83 
     84 const negativeInt32 = [-2147483648, -2147483647, -1];
     85 const zeroInt32 = [0];
     86 const positiveInt32 = [1, 2147483646, 2147483647];
     87 const zeroOrPositiveInt32 = [...zeroInt32, ...positiveInt32];
     88 const anyInt32 = [...negativeInt32, ...zeroInt32, ...positiveInt32];
     89 
     90 // Test when the BigInt is too large to be representable as a single BigInt digit.
     91 function testLarge() {
     92  var xs = [
     93    2n ** 32n, // exceeds single digit limit on 32-bit
     94    2n ** 64n, // exceeds single digit limit on 64-bit
     95    2n ** 96n, // not a single digit on either platform
     96  ];
     97  test(GreaterThan, xs, anyInt32);
     98 
     99  var xs = [
    100    -(2n ** 32n), // exceeds single digit limit on 32-bit
    101    -(2n ** 64n), // exceeds single digit limit on 64-bit
    102    -(2n ** 96n), // not a single digit on either platform
    103  ];
    104  test(LessThan, xs, anyInt32);
    105 }
    106 testLarge();
    107 
    108 // Test when the BigInt is 0n.
    109 function testZero() {
    110  var xs = [
    111    0n
    112  ];
    113 
    114  test(GreaterThan, xs, negativeInt32);
    115  test(Equal, xs, zeroInt32);
    116  test(LessThan, xs, positiveInt32);
    117 }
    118 testZero();
    119 
    120 // Test when both numbers are negative.
    121 function testNegative() {
    122  var xs = [
    123    -(2n ** 64n) - 2n,
    124    -(2n ** 64n) - 1n, // Max negative using a single BigInt digit on 64-bit.
    125    -(2n ** 64n),
    126 
    127    -(2n ** 32n) - 2n,
    128    -(2n ** 32n) - 1n, // Max negative using a single BigInt digit on 32-bit.
    129    -(2n ** 32n),
    130 
    131    -(2n ** 31n) - 1n, // One past max negative for Int32.
    132  ];
    133  test(LessThan, xs, negativeInt32);
    134 
    135  var xs = [
    136    -(2n ** 31n), // Max negative for Int32.
    137  ];
    138  test(Equal, xs, [-2147483648]);
    139  test(LessThan, xs, [-2147483647, -1]);
    140 
    141  var xs = [
    142    -(2n ** 31n) + 1n,
    143  ];
    144  test(GreaterThan, xs, [-2147483648]);
    145  test(Equal, xs, [-2147483647]);
    146  test(LessThan, xs, [-1]);
    147 
    148  var xs = [
    149    -1n,
    150  ];
    151  test(GreaterThan, xs, [-2147483648, -2147483647]);
    152  test(Equal, xs, [-1]);
    153 }
    154 testNegative();
    155 
    156 // Test when both numbers are positive (and BigInt strictly positive).
    157 function testPositive() {
    158  var xs = [
    159    1n,
    160  ];
    161  test(GreaterThan, xs, [0]);
    162  test(Equal, xs, [1]);
    163  test(LessThan, xs, [2147483646, 2147483647]);
    164 
    165  var xs = [
    166    2n ** 31n - 2n,
    167  ];
    168  test(GreaterThan, xs, [0, 1]);
    169  test(Equal, xs, [2147483646]);
    170  test(LessThan, xs, [2147483647]);
    171 
    172  var xs = [
    173    2n ** 31n - 1n, // Max positive for Int32.
    174  ];
    175  test(GreaterThan, xs, [0, 1, 2147483646]);
    176  test(Equal, xs, [2147483647]);
    177 
    178  var xs = [
    179    2n ** 31n, // One past max positive for Int32.
    180 
    181    2n ** 32n - 2n,
    182    2n ** 32n - 1n, // Max positive using a single BigInt digit on 32-bit.
    183    2n ** 32n,
    184 
    185    2n ** 64n - 2n,
    186    2n ** 64n - 1n, // Max positive using a single BigInt digit on 64-bit.
    187    2n ** 64n,
    188  ];
    189  test(GreaterThan, xs, zeroOrPositiveInt32);
    190 }
    191 testPositive();
    192 
    193 // Test negative BigInt and positive Int32.
    194 function testNegativePositive() {
    195  var xs = [
    196    -(2n ** 64n) - 2n,
    197    -(2n ** 64n) - 1n, // Max negative using a single BigInt digit on 64-bit.
    198    -(2n ** 64n),
    199 
    200    -(2n ** 32n) - 2n,
    201    -(2n ** 32n) - 1n, // Max negative using a single BigInt digit on 32-bit.
    202    -(2n ** 32n),
    203 
    204    -(2n ** 31n) - 1n,
    205    -(2n ** 31n), // Max negative for Int32.
    206    -(2n ** 31n) + 1n,
    207 
    208    -2n, // Extra entry to ensure assertAllCombinationsTested passes.
    209    -1n,
    210  ];
    211  test(LessThan, xs, zeroOrPositiveInt32);
    212 }
    213 testNegativePositive();
    214 
    215 // Test (strictly) positive BigInt and negative Int32.
    216 function testPositiveNegative() {
    217  var xs = [
    218    1n,
    219 
    220    2n ** 31n - 2n,
    221    2n ** 31n - 1n, // Max positive for Int32.
    222    2n ** 31n,
    223 
    224    2n ** 32n - 2n,
    225    2n ** 32n - 1n, // Max positive using a single BigInt digit on 32-bit.
    226    2n ** 32n,
    227 
    228    2n ** 64n - 2n,
    229    2n ** 64n - 1n, // Max positive using a single BigInt digit on 64-bit.
    230    2n ** 64n,
    231  ];
    232  test(GreaterThan, xs, negativeInt32);
    233 }
    234 testPositiveNegative();