| 1 |
// Copy of "warp/number-tostring-with-base.js" with `toUpperCase()` conversion |
| 2 |
// after `Number.prototype.toString(base)`. |
| 3 |
|
| 4 |
function testConstantBaseFastPathTemplate(xs) { |
| 5 |
assertEq(xs.length, $BASE * $BASE); |
| 6 |
for (let j = 0; j < 200;) { |
| 7 |
// The fast path can be used for all integers below |base * base|. |
| 8 |
for (let i = 0; i < $BASE * $BASE; ++i, ++j) { |
| 9 |
assertEq(i.toString($BASE).toUpperCase(), xs[i]); |
| 10 |
} |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
// Test when Number.prototype.toString is called with a constant base argument |
| 15 |
// and the fast path for static strings can be used. |
| 16 |
for (let base = 2; base <= 36; ++base) { |
| 17 |
let fn = Function(`return ${testConstantBaseFastPathTemplate}`.replaceAll("$BASE", base))(); |
| 18 |
let xs = Array.from({length: base * base}, (_, i) => i.toString(base).toUpperCase()); |
| 19 |
for (let i = 0; i < 2; ++i) { |
| 20 |
fn(xs); |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
function testConstantBaseTemplate(xs) { |
| 25 |
assertEq(xs.length, $BASE * $BASE * 2); |
| 26 |
for (let j = 0; j < 200;) { |
| 27 |
// The fast path can only be used for integers below |base * base|. |
| 28 |
for (let i = 0; i < $BASE * $BASE * 2; ++i, ++j) { |
| 29 |
assertEq(i.toString($BASE).toUpperCase(), xs[i]); |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
// Test when Number.prototype.toString is called with a constant base argument |
| 35 |
// and the fast path for static strings can't always be used. |
| 36 |
for (let base = 2; base <= 36; ++base) { |
| 37 |
let fn = Function(`return ${testConstantBaseTemplate}`.replaceAll("$BASE", base))(); |
| 38 |
let xs = Array.from({length: base * base * 2}, (_, i) => i.toString(base).toUpperCase()); |
| 39 |
for (let i = 0; i < 2; ++i) { |
| 40 |
fn(xs); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
function testVariableBaseFastPathTemplate(xs, ys) { |
| 45 |
assertEq(ys.length, 2); |
| 46 |
assertEq(ys[0], ys[1]); |
| 47 |
let base = ys[0]; |
| 48 |
|
| 49 |
assertEq(xs.length, base * base); |
| 50 |
|
| 51 |
for (let j = 0; j < 200;) { |
| 52 |
// The fast path can be used for all integers below |base * base|. |
| 53 |
for (let i = 0; i < base * base; ++i, ++j) { |
| 54 |
assertEq(i.toString(ys[i & 1]).toUpperCase(), xs[i]); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// Test when Number.prototype.toString is called with a non-constant base argument |
| 60 |
// and the fast path for static strings can be used. |
| 61 |
for (let base = 2; base <= 36; ++base) { |
| 62 |
let fn = Function(`return ${testVariableBaseFastPathTemplate}`)(); |
| 63 |
let xs = Array.from({length: base * base}, (_, i) => i.toString(base).toUpperCase()); |
| 64 |
for (let i = 0; i < 2; ++i) { |
| 65 |
fn(xs, [base, base]); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
function testVariableBaseTemplate(xs, ys) { |
| 70 |
assertEq(ys.length, 2); |
| 71 |
assertEq(ys[0], ys[1]); |
| 72 |
let base = ys[0]; |
| 73 |
|
| 74 |
assertEq(xs.length, base * base * 2); |
| 75 |
|
| 76 |
for (let j = 0; j < 200;) { |
| 77 |
// The fast path can only be used for integers below |base * base|. |
| 78 |
for (let i = 0; i < base * base * 2; ++i, ++j) { |
| 79 |
assertEq(i.toString(ys[i & 1]).toUpperCase(), xs[i]); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// Test when Number.prototype.toString is called with a non-constant base argument |
| 85 |
// and the fast path for static strings can't always be used. |
| 86 |
for (let base = 2; base <= 36; ++base) { |
| 87 |
let fn = Function(`return ${testVariableBaseTemplate}`)(); |
| 88 |
let xs = Array.from({length: base * base * 2}, (_, i) => i.toString(base).toUpperCase()); |
| 89 |
for (let i = 0; i < 2; ++i) { |
| 90 |
fn(xs, [base, base]); |
| 91 |
} |
| 92 |
} |