tor-browser

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

number-parseInt-string.js (3765B)


      1 // Test inlining parseInt with a String input.
      2 
      3 const stringInt32Values = [
      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 const stringInt32HexValues = [
     25  // Values around INT32_MIN.
     26  "-0x80000000",
     27  "-0x7fffffff",
     28  "-0x7ffffffe",
     29 
     30  // Negative values.
     31  "-0x10000", "-0xffff", "-0x100", "-0xff", "-0x64", "-0x32", "-0xa",
     32 
     33  // Values around zero.
     34  "-0x2", "-0x1", "0x0", "0x1", "0x2",
     35 
     36  // Positive values.
     37  "0xa", "0x32", "0x64", "0xff", "0x100", "0xffff", "0x10000",
     38 
     39  // Values around INT32_MAX.
     40  "0x7ffffffd",
     41  "0x7ffffffe",
     42  "0x7fffffff",
     43 ];
     44 
     45 // Test string-int32 input without an explicit radix.
     46 function testRadixAbsent() {
     47  for (let i = 0; i < 200; ++i) {
     48    let x = stringInt32Values[i % stringInt32Values.length];
     49    assertEq(+x, x|0, "x is an int32 value");
     50 
     51    let r = Number.parseInt(x);
     52    assertEq(r, +x);
     53  }
     54 }
     55 for (let i = 0; i < 2; ++i) testRadixAbsent();
     56 
     57 // Test string-int32 hex input without an explicit radix.
     58 function testRadixAbsentHex() {
     59  for (let i = 0; i < 200; ++i) {
     60    let x = stringInt32HexValues[i % stringInt32HexValues.length];
     61 
     62    // String to number conversion doesn't support negative hex-strings, so we
     63    // have to chop off the leading minus sign manually.
     64    let y = x;
     65    let sign = 1;
     66    if (x.startsWith("-")) {
     67      y = x.slice(1);
     68      sign = -1;
     69    }
     70 
     71    assertEq((+y) * sign, ((+y) * sign)|0, "x is an int32 hex value");
     72 
     73    let r = Number.parseInt(x);
     74    assertEq(r, (+y) * sign);
     75  }
     76 }
     77 for (let i = 0; i < 2; ++i) testRadixAbsentHex();
     78 
     79 // Test string-int32 input with radix=10.
     80 function testRadixTen() {
     81  for (let i = 0; i < 200; ++i) {
     82    let x = stringInt32Values[i % stringInt32Values.length];
     83    assertEq(+x, x|0, "x is an int32 value");
     84 
     85    let r = Number.parseInt(x, 10);
     86    assertEq(r, +x);
     87  }
     88 }
     89 for (let i = 0; i < 2; ++i) testRadixTen();
     90 
     91 // Test string-int32 input with radix=16. (This case isn't currently inlined.)
     92 function testRadixSixteen() {
     93  for (let i = 0; i < 200; ++i) {
     94    let x = stringInt32Values[i % stringInt32Values.length];
     95    assertEq(+x, x|0, "x is an int32 value");
     96 
     97    let expected = Math.sign(x) * Number("0x" + Math.abs(x).toString(10));
     98 
     99    let r = Number.parseInt(x, 16);
    100    assertEq(r, expected);
    101  }
    102 }
    103 for (let i = 0; i < 2; ++i) testRadixSixteen();
    104 
    105 // Test string-int32 hex input with radix=16. (This case isn't currently inlined.)
    106 function testRadixSixteenHex() {
    107  for (let i = 0; i < 200; ++i) {
    108    let x = stringInt32HexValues[i % stringInt32HexValues.length];
    109 
    110    // String to number conversion doesn't support negative hex-strings, so we
    111    // have to chop off the leading minus sign manually.
    112    let y = x;
    113    let sign = 1;
    114    if (x.startsWith("-")) {
    115      y = x.slice(1);
    116      sign = -1;
    117    }
    118 
    119    assertEq((+y) * sign, ((+y) * sign)|0, "x is an int32 hex value");
    120 
    121    let r = Number.parseInt(x, 16);
    122    assertEq(r, (+y) * sign);
    123  }
    124 }
    125 for (let i = 0; i < 2; ++i) testRadixSixteenHex();
    126 
    127 // Test with variable radix.
    128 function testRadixVariable() {
    129  for (let i = 0; i < 200; ++i) {
    130    let x = stringInt32Values[i % stringInt32Values.length];
    131    assertEq(+x, x|0, "x is an int32 value");
    132 
    133    let radix = [10, 16][(i > 100)|0];
    134 
    135    let expected = +x;
    136    if (radix === 16) {
    137      expected = Math.sign(+x) * Number("0x" + Math.abs(+x).toString(10));
    138    }
    139 
    140    let r = Number.parseInt(x, radix);
    141    assertEq(r, expected);
    142  }
    143 }
    144 for (let i = 0; i < 2; ++i) testRadixVariable();