summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/warp/number-tostring-with-base-uppercase.js
blob: 4dd6b182a8f549ce4cab2f84012adfbc20a9cdca (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
// Copy of "warp/number-tostring-with-base.js" with `toUpperCase()` conversion
// after `Number.prototype.toString(base)`.

function testConstantBaseFastPathTemplate(xs) {
  assertEq(xs.length, $BASE * $BASE);
  for (let j = 0; j < 200;) {
    // The fast path can be used for all integers below |base * base|.
    for (let i = 0; i < $BASE * $BASE; ++i, ++j) {
      assertEq(i.toString($BASE).toUpperCase(), xs[i]);
    }
  }
}

// Test when Number.prototype.toString is called with a constant base argument
// and the fast path for static strings can be used.
for (let base = 2; base <= 36; ++base) {
  let fn = Function(`return ${testConstantBaseFastPathTemplate}`.replaceAll("$BASE", base))();
  let xs = Array.from({length: base * base}, (_, i) => i.toString(base).toUpperCase());
  for (let i = 0; i < 2; ++i) {
    fn(xs);
  }
}

function testConstantBaseTemplate(xs) {
  assertEq(xs.length, $BASE * $BASE * 2);
  for (let j = 0; j < 200;) {
    // The fast path can only be used for integers below |base * base|.
    for (let i = 0; i < $BASE * $BASE * 2; ++i, ++j) {
      assertEq(i.toString($BASE).toUpperCase(), xs[i]);
    }
  }
}

// Test when Number.prototype.toString is called with a constant base argument
// and the fast path for static strings can't always be used.
for (let base = 2; base <= 36; ++base) {
  let fn = Function(`return ${testConstantBaseTemplate}`.replaceAll("$BASE", base))();
  let xs = Array.from({length: base * base * 2}, (_, i) => i.toString(base).toUpperCase());
  for (let i = 0; i < 2; ++i) {
    fn(xs);
  }
}

function testVariableBaseFastPathTemplate(xs, ys) {
  assertEq(ys.length, 2);
  assertEq(ys[0], ys[1]);
  let base = ys[0];

  assertEq(xs.length, base * base);

  for (let j = 0; j < 200;) {
    // The fast path can be used for all integers below |base * base|.
    for (let i = 0; i < base * base; ++i, ++j) {
      assertEq(i.toString(ys[i & 1]).toUpperCase(), xs[i]);
    }
  }
}

// Test when Number.prototype.toString is called with a non-constant base argument
// and the fast path for static strings can be used.
for (let base = 2; base <= 36; ++base) {
  let fn = Function(`return ${testVariableBaseFastPathTemplate}`)();
  let xs = Array.from({length: base * base}, (_, i) => i.toString(base).toUpperCase());
  for (let i = 0; i < 2; ++i) {
    fn(xs, [base, base]);
  }
}

function testVariableBaseTemplate(xs, ys) {
  assertEq(ys.length, 2);
  assertEq(ys[0], ys[1]);
  let base = ys[0];

  assertEq(xs.length, base * base * 2);

  for (let j = 0; j < 200;) {
    // The fast path can only be used for integers below |base * base|.
    for (let i = 0; i < base * base * 2; ++i, ++j) {
      assertEq(i.toString(ys[i & 1]).toUpperCase(), xs[i]);
    }
  }
}

// Test when Number.prototype.toString is called with a non-constant base argument
// and the fast path for static strings can't always be used.
for (let base = 2; base <= 36; ++base) {
  let fn = Function(`return ${testVariableBaseTemplate}`)();
  let xs = Array.from({length: base * base * 2}, (_, i) => i.toString(base).toUpperCase());
  for (let i = 0; i < 2; ++i) {
    fn(xs, [base, base]);
  }
}