|
| 1 | +function shouldBe(actual, expected) { |
| 2 | + if (actual !== expected) |
| 3 | + throw new Error(`bad value: ${actual}, expected: ${expected}`); |
| 4 | +} |
| 5 | + |
| 6 | +function shouldThrow(func, errorType) { |
| 7 | + let error; |
| 8 | + try { |
| 9 | + func(); |
| 10 | + } catch (e) { |
| 11 | + error = e; |
| 12 | + } |
| 13 | + if (!(error instanceof errorType)) |
| 14 | + throw new Error(`bad error: ${error}`); |
| 15 | +} |
| 16 | + |
| 17 | +function rope(a, b) { |
| 18 | + return a + b; |
| 19 | +} |
| 20 | +noInline(rope); |
| 21 | + |
| 22 | +function substring(string, start, end) { |
| 23 | + return string.slice(start, end); |
| 24 | +} |
| 25 | +noInline(substring); |
| 26 | + |
| 27 | +let base = "0123456789abcdefghijklmnopqrstuvwxyz"; |
| 28 | +let count = 0; |
| 29 | +let object = { |
| 30 | + toString() { |
| 31 | + ++count; |
| 32 | + return "xy"; |
| 33 | + } |
| 34 | +}; |
| 35 | +let thrower = { |
| 36 | + toString() { |
| 37 | + throw new RangeError("thrower"); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +function test(i) { |
| 42 | + shouldBe("7".padStart(3, rope("0", "1")), "017"); |
| 43 | + shouldBe("7".padEnd(6, rope("0", "1")), "701010"); |
| 44 | + shouldBe("7".padStart(4, substring(base, 10, 12)), "aba7"); |
| 45 | + shouldBe("7".padEnd(4, substring(base, 10, 12)), "7aba"); |
| 46 | + shouldBe("7".padStart(3, rope("\u3042", "\u3044")), "\u3042\u30447"); |
| 47 | + shouldBe("\u3042".padEnd(3, rope("a", "b")), "\u3042ab"); |
| 48 | + shouldBe("7".padStart(3, 0), "007"); |
| 49 | + shouldBe("7".padStart(5, 12 + (i & 1)), (i & 1) ? "13137" : "12127"); |
| 50 | + shouldBe("7".padEnd(5, 1.5), "71.51"); |
| 51 | + shouldBe("7".padStart(5, true), "true7"); |
| 52 | + shouldBe("7".padStart(5, null), "null7"); |
| 53 | + shouldBe("7".padStart(3, undefined), " 7"); |
| 54 | + shouldBe("7".padEnd(3), "7 "); |
| 55 | + shouldBe("7".padEnd(3, rope("", "")), "7"); |
| 56 | + |
| 57 | + let before = count; |
| 58 | + shouldBe("7".padStart(4, object), "xyx7"); |
| 59 | + shouldBe(count, before + 1); |
| 60 | + shouldBe("abc".padStart(2, object), "abc"); |
| 61 | + shouldBe(count, before + 1); |
| 62 | +} |
| 63 | +noInline(test); |
| 64 | + |
| 65 | +function testLong() { |
| 66 | + let long = "7".padStart(1030, rope("ab", "c")); |
| 67 | + shouldBe(long.length, 1030); |
| 68 | + shouldBe(long.slice(0, 5), "abcab"); |
| 69 | + shouldBe(long.slice(1024), "bcabc7"); |
| 70 | + |
| 71 | + long = "7".padEnd(1100, rope("0123456789", "abc")); |
| 72 | + shouldBe(long.length, 1100); |
| 73 | + shouldBe(long.slice(0, 15), "70123456789abc0"); |
| 74 | + shouldBe(long.slice(1090), "abc0123456"); |
| 75 | + |
| 76 | + long = "7".padStart(1030, substring(base, 10, 23)); |
| 77 | + shouldBe(long.length, 1030); |
| 78 | + shouldBe(long.slice(0, 14), "abcdefghijklma"); |
| 79 | + shouldBe(long.slice(1025), "lmab7"); |
| 80 | + |
| 81 | + long = "7".padStart(1030, 0); |
| 82 | + shouldBe(long.length, 1030); |
| 83 | + shouldBe(long.slice(1026), "0007"); |
| 84 | + |
| 85 | + long = "7".padEnd(1030); |
| 86 | + shouldBe(long.length, 1030); |
| 87 | + shouldBe(long.slice(0, 3), "7 "); |
| 88 | + |
| 89 | + let before = count; |
| 90 | + long = "7".padEnd(1030, object); |
| 91 | + shouldBe(long.length, 1030); |
| 92 | + shouldBe(long.slice(0, 4), "7xyx"); |
| 93 | + shouldBe(count, before + 1); |
| 94 | +} |
| 95 | +noInline(testLong); |
| 96 | + |
| 97 | +for (let i = 0; i < testLoopCount; ++i) |
| 98 | + test(i); |
| 99 | + |
| 100 | +for (let i = 0; i < 10; ++i) |
| 101 | + testLong(); |
| 102 | + |
| 103 | +shouldThrow(() => "7".padStart(3, Symbol()), TypeError); |
| 104 | +shouldThrow(() => "7".padEnd(3, thrower), RangeError); |
| 105 | +shouldThrow(() => "7".padEnd(2000, thrower), RangeError); |
0 commit comments