tor-browser

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

number-parseInt-int32.js (2243B)


      1 // Test inlining parseInt with an Int32 input.
      2 
      3 const int32Values = [
      4  // Values around INT32_MIN.
      5  -2147483648,
      6  -2147483647,
      7  -2147483646,
      8 
      9  // Negative values.
     10  -65536, -65535, -256, -255, -100, -50, -10,
     11 
     12  // Values around zero.
     13  -2, -1, 0, 1, 2,
     14 
     15  // Positive values.
     16  10, 50, 100, 255, 256, 65535, 65536,
     17 
     18  // Values around INT32_MAX.
     19  2147483645,
     20  2147483646,
     21  2147483647,
     22 ];
     23 
     24 // Test int32 input without an explicit radix.
     25 function testRadixAbsent() {
     26  for (let i = 0; i < 200; ++i) {
     27    let x = int32Values[i % int32Values.length];
     28    assertEq(x, x|0, "x is an int32 value");
     29 
     30    let r = Number.parseInt(x);
     31    assertEq(r, x);
     32  }
     33 }
     34 for (let i = 0; i < 2; ++i) testRadixAbsent();
     35 
     36 // Test int32 input with radix=10.
     37 function testRadixTen() {
     38  for (let i = 0; i < 200; ++i) {
     39    let x = int32Values[i % int32Values.length];
     40    assertEq(x, x|0, "x is an int32 value");
     41 
     42    let r = Number.parseInt(x, 10);
     43    assertEq(r, x);
     44  }
     45 }
     46 for (let i = 0; i < 2; ++i) testRadixTen();
     47 
     48 // Test int32 input with radix=16. (This case isn't currently inlined.)
     49 function testRadixSixteen() {
     50  for (let i = 0; i < 200; ++i) {
     51    let x = int32Values[i % int32Values.length];
     52    assertEq(x, x|0, "x is an int32 value");
     53 
     54    let expected = Math.sign(x) * Number("0x" + Math.abs(x).toString(10));
     55 
     56    let r = Number.parseInt(x, 16);
     57    assertEq(r, expected);
     58  }
     59 }
     60 for (let i = 0; i < 2; ++i) testRadixSixteen();
     61 
     62 // Test with variable radix.
     63 function testRadixVariable() {
     64  for (let i = 0; i < 200; ++i) {
     65    let x = int32Values[i % int32Values.length];
     66    assertEq(x, x|0, "x is an int32 value");
     67 
     68    let radix = [10, 16][(i > 100)|0];
     69 
     70    let expected = x;
     71    if (radix === 16) {
     72      expected = Math.sign(x) * Number("0x" + Math.abs(x).toString(10));
     73    }
     74 
     75    let r = Number.parseInt(x, radix);
     76    assertEq(r, expected);
     77  }
     78 }
     79 for (let i = 0; i < 2; ++i) testRadixVariable();
     80 
     81 // Test with int32 and double inputs.
     82 function testBadInput() {
     83  for (let i = 0; i < 200; ++i) {
     84    let x = int32Values[i % int32Values.length];
     85    assertEq(x, x|0, "x is an int32 value");
     86 
     87    let y = [x, NaN][(i > 150)|0];
     88 
     89    let r = Number.parseInt(y, 10);
     90    assertEq(r, y);
     91  }
     92 }
     93 for (let i = 0; i < 2; ++i) testBadInput();