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