summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/cacheir/number-parseInt-int32.js
blob: 42def800de7a59b274dca139baa98b00b184704b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Test inlining parseInt with an Int32 input.

const int32Values = [
  // Values around INT32_MIN.
  -2147483648,
  -2147483647,
  -2147483646,

  // Negative values.
  -65536, -65535, -256, -255, -100, -50, -10,

  // Values around zero.
  -2, -1, 0, 1, 2,

  // Positive values.
  10, 50, 100, 255, 256, 65535, 65536,

  // Values around INT32_MAX.
  2147483645,
  2147483646,
  2147483647,
];

// Test int32 input without an explicit radix.
function testRadixAbsent() {
  for (let i = 0; i < 200; ++i) {
    let x = int32Values[i % int32Values.length];
    assertEq(x, x|0, "x is an int32 value");

    let r = Number.parseInt(x);
    assertEq(r, x);
  }
}
for (let i = 0; i < 2; ++i) testRadixAbsent();

// Test int32 input with radix=10.
function testRadixTen() {
  for (let i = 0; i < 200; ++i) {
    let x = int32Values[i % int32Values.length];
    assertEq(x, x|0, "x is an int32 value");

    let r = Number.parseInt(x, 10);
    assertEq(r, x);
  }
}
for (let i = 0; i < 2; ++i) testRadixTen();

// Test int32 input with radix=16. (This case isn't currently inlined.)
function testRadixSixteen() {
  for (let i = 0; i < 200; ++i) {
    let x = int32Values[i % int32Values.length];
    assertEq(x, x|0, "x is an int32 value");

    let expected = Math.sign(x) * Number("0x" + Math.abs(x).toString(10));

    let r = Number.parseInt(x, 16);
    assertEq(r, expected);
  }
}
for (let i = 0; i < 2; ++i) testRadixSixteen();

// Test with variable radix.
function testRadixVariable() {
  for (let i = 0; i < 200; ++i) {
    let x = int32Values[i % int32Values.length];
    assertEq(x, x|0, "x is an int32 value");

    let radix = [10, 16][(i > 100)|0];

    let expected = x;
    if (radix === 16) {
      expected = Math.sign(x) * Number("0x" + Math.abs(x).toString(10));
    }

    let r = Number.parseInt(x, radix);
    assertEq(r, expected);
  }
}
for (let i = 0; i < 2; ++i) testRadixVariable();

// Test with int32 and double inputs.
function testBadInput() {
  for (let i = 0; i < 200; ++i) {
    let x = int32Values[i % int32Values.length];
    assertEq(x, x|0, "x is an int32 value");

    let y = [x, NaN][(i > 150)|0];

    let r = Number.parseInt(y, 10);
    assertEq(r, y);
  }
}
for (let i = 0; i < 2; ++i) testBadInput();