|
| 1 | +function shouldBe(actual, expected) |
| 2 | +{ |
| 3 | + if (actual !== expected) |
| 4 | + throw new Error(`bad value: ${String(actual)}, expected: ${String(expected)}`); |
| 5 | +} |
| 6 | + |
| 7 | +function shouldThrow(func, errorConstructor) |
| 8 | +{ |
| 9 | + let error; |
| 10 | + try { |
| 11 | + func(); |
| 12 | + } catch (e) { |
| 13 | + error = e; |
| 14 | + } |
| 15 | + if (!(error instanceof errorConstructor)) |
| 16 | + throw new Error(`bad error: ${String(error)}`); |
| 17 | +} |
| 18 | + |
| 19 | +function direct(length) |
| 20 | +{ |
| 21 | + if (length !== undefined) |
| 22 | + arguments.length = length; |
| 23 | + return arguments; |
| 24 | +} |
| 25 | + |
| 26 | +function scoped(length) |
| 27 | +{ |
| 28 | + (function () { return length; })(); |
| 29 | + if (length !== undefined) |
| 30 | + arguments.length = length; |
| 31 | + return arguments; |
| 32 | +} |
| 33 | + |
| 34 | +function count() |
| 35 | +{ |
| 36 | + return arguments.length; |
| 37 | +} |
| 38 | + |
| 39 | +function apply(args) |
| 40 | +{ |
| 41 | + return count.apply(null, args); |
| 42 | +} |
| 43 | +noInline(apply); |
| 44 | + |
| 45 | +function reflectApply(args) |
| 46 | +{ |
| 47 | + return Reflect.apply(count, null, args); |
| 48 | +} |
| 49 | +noInline(reflectApply); |
| 50 | + |
| 51 | +function testNegative(create, length) |
| 52 | +{ |
| 53 | + let pushed = create(length, 2, 3); |
| 54 | + shouldBe(Array.prototype.push.call(pushed, 4), 1); |
| 55 | + shouldBe(pushed[0], 4); |
| 56 | + shouldBe(pushed.length, 1); |
| 57 | + |
| 58 | + let popped = create(length, 2, 3); |
| 59 | + shouldBe(Array.prototype.pop.call(popped), undefined); |
| 60 | + shouldBe(popped.length, 0); |
| 61 | + |
| 62 | + let args = create(length, 2, 3); |
| 63 | + shouldBe(Array.prototype.slice.call(args).length, 0); |
| 64 | + shouldBe(Array.prototype.slice.call(args, 0, 2).length, 0); |
| 65 | + shouldBe(Array.prototype.indexOf.call(args, 2), -1); |
| 66 | + shouldBe(Array.prototype.indexOf.call(args, 4), -1); |
| 67 | + shouldBe(Array.prototype.lastIndexOf.call(args, 2), -1); |
| 68 | + shouldBe(Array.prototype.includes.call(args, 3), false); |
| 69 | + shouldBe(Array.prototype.join.call(args), ""); |
| 70 | + shouldBe(Array.prototype.reverse.call(args), args); |
| 71 | + shouldBe(Array.prototype.fill.call(args, 0), args); |
| 72 | + shouldBe(Array.prototype.toReversed.call(args).length, 0); |
| 73 | + shouldBe(apply(args), 0); |
| 74 | + shouldBe(reflectApply(args), 0); |
| 75 | + shouldBe(args[1], 2); |
| 76 | + shouldBe(args[2], 3); |
| 77 | + shouldBe(args.length, length); |
| 78 | +} |
| 79 | + |
| 80 | +function testLarge(create, length, expected) |
| 81 | +{ |
| 82 | + let args = create(length, 2, 3); |
| 83 | + shouldBe(Array.prototype.indexOf.call(args, 3), 2); |
| 84 | + shouldBe(Array.prototype.includes.call(args, undefined), true); |
| 85 | + shouldBe(Array.prototype.slice.call(args, 1, 3).join(), "2,3"); |
| 86 | + shouldThrow(() => Array.prototype.slice.call(args), RangeError); |
| 87 | + shouldThrow(() => apply(args), RangeError); |
| 88 | + shouldThrow(() => reflectApply(args), RangeError); |
| 89 | + shouldBe(args.length, length); |
| 90 | + |
| 91 | + shouldBe(Array.prototype.pop.call(args), undefined); |
| 92 | + shouldBe(args.length, expected - 1); |
| 93 | + shouldBe(Array.prototype.push.call(args, 4), expected); |
| 94 | + shouldBe(args[expected - 1], 4); |
| 95 | + shouldBe(args.length, expected); |
| 96 | +} |
| 97 | + |
| 98 | +for (let i = 0; i < testLoopCount; ++i) { |
| 99 | + shouldBe(apply(direct(undefined, 2, 3)), 3); |
| 100 | + shouldBe(apply(scoped(undefined, 2, 3)), 3); |
| 101 | + shouldBe(reflectApply(direct(undefined, 2, 3)), 3); |
| 102 | + shouldBe(reflectApply(scoped(undefined, 2, 3)), 3); |
| 103 | +} |
| 104 | + |
| 105 | +for (let i = 0; i < 10; ++i) { |
| 106 | + for (let create of [direct, scoped]) { |
| 107 | + testNegative(create, -1); |
| 108 | + testNegative(create, -1.5); |
| 109 | + testNegative(create, -(2 ** 31)); |
| 110 | + testNegative(create, -(2 ** 32) + 2); |
| 111 | + testNegative(create, -(2 ** 32) - 1); |
| 112 | + testNegative(create, -Infinity); |
| 113 | + testNegative(create, "-1"); |
| 114 | + |
| 115 | + testLarge(create, 2 ** 32, 2 ** 32); |
| 116 | + testLarge(create, 2 ** 32 + 1, 2 ** 32 + 1); |
| 117 | + testLarge(create, 2 ** 32 + 2.5, 2 ** 32 + 2); |
| 118 | + testLarge(create, 2 ** 53 - 1, 2 ** 53 - 1); |
| 119 | + testLarge(create, 2 ** 53 + 2, 2 ** 53 - 1); |
| 120 | + testLarge(create, Infinity, 2 ** 53 - 1); |
| 121 | + |
| 122 | + shouldThrow(() => Array.prototype.push.call(create(Infinity, 2, 3), 4), TypeError); |
| 123 | + } |
| 124 | +} |
0 commit comments