summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/cacheir/number-parseInt-string.js
blob: 7206c14db21e29f3bbecf4d5e1edcce19f4720e8 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Test inlining parseInt with a String input.

const stringInt32Values = [
  // 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",
];

const stringInt32HexValues = [
  // Values around INT32_MIN.
  "-0x80000000",
  "-0x7fffffff",
  "-0x7ffffffe",

  // Negative values.
  "-0x10000", "-0xffff", "-0x100", "-0xff", "-0x64", "-0x32", "-0xa",

  // Values around zero.
  "-0x2", "-0x1", "0x0", "0x1", "0x2",

  // Positive values.
  "0xa", "0x32", "0x64", "0xff", "0x100", "0xffff", "0x10000",

  // Values around INT32_MAX.
  "0x7ffffffd",
  "0x7ffffffe",
  "0x7fffffff",
];

// Test string-int32 input without an explicit radix.
function testRadixAbsent() {
  for (let i = 0; i < 200; ++i) {
    let x = stringInt32Values[i % stringInt32Values.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 string-int32 hex input without an explicit radix.
function testRadixAbsentHex() {
  for (let i = 0; i < 200; ++i) {
    let x = stringInt32HexValues[i % stringInt32HexValues.length];

    // String to number conversion doesn't support negative hex-strings, so we
    // have to chop off the leading minus sign manually.
    let y = x;
    let sign = 1;
    if (x.startsWith("-")) {
      y = x.slice(1);
      sign = -1;
    }

    assertEq((+y) * sign, ((+y) * sign)|0, "x is an int32 hex value");

    let r = Number.parseInt(x);
    assertEq(r, (+y) * sign);
  }
}
for (let i = 0; i < 2; ++i) testRadixAbsentHex();

// Test string-int32 input with radix=10.
function testRadixTen() {
  for (let i = 0; i < 200; ++i) {
    let x = stringInt32Values[i % stringInt32Values.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 string-int32 input with radix=16. (This case isn't currently inlined.)
function testRadixSixteen() {
  for (let i = 0; i < 200; ++i) {
    let x = stringInt32Values[i % stringInt32Values.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 string-int32 hex input with radix=16. (This case isn't currently inlined.)
function testRadixSixteenHex() {
  for (let i = 0; i < 200; ++i) {
    let x = stringInt32HexValues[i % stringInt32HexValues.length];

    // String to number conversion doesn't support negative hex-strings, so we
    // have to chop off the leading minus sign manually.
    let y = x;
    let sign = 1;
    if (x.startsWith("-")) {
      y = x.slice(1);
      sign = -1;
    }

    assertEq((+y) * sign, ((+y) * sign)|0, "x is an int32 hex value");

    let r = Number.parseInt(x, 16);
    assertEq(r, (+y) * sign);
  }
}
for (let i = 0; i < 2; ++i) testRadixSixteenHex();

// Test with variable radix.
function testRadixVariable() {
  for (let i = 0; i < 200; ++i) {
    let x = stringInt32Values[i % stringInt32Values.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();