1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 07:38:10 +00:00

LibJS: Add Array.prototype.fill

This commit is contained in:
Angel 2020-05-26 20:31:22 +02:00 committed by Andreas Kling
parent d1bc1f5783
commit 199a6b40b3
4 changed files with 88 additions and 0 deletions

View file

@ -0,0 +1,24 @@
load("test-common.js");
try {
assert(Array.prototype.fill.length === 1);
var array = [1, 2, 3, 4];
assertArrayEquals(array.fill(0, 2, 4), [1, 2, 0, 0]);
assertArrayEquals(array.fill(5, 1), [1, 5, 5, 5]);
assertArrayEquals(array.fill(6), [6, 6, 6, 6]);
assertArrayEquals([1, 2, 3].fill(4), [4, 4, 4]);
assertArrayEquals([1, 2, 3].fill(4, 1), [1, 4, 4]);
assertArrayEquals([1, 2, 3].fill(4, 1, 2), [1, 4, 3]);
assertArrayEquals([1, 2, 3].fill(4, 3, 3), [1, 2, 3]);
assertArrayEquals([1, 2, 3].fill(4, -3, -2), [4, 2, 3]);
assertArrayEquals([1, 2, 3].fill(4, NaN, NaN), [1, 2, 3]);
assertArrayEquals([1, 2, 3].fill(4, 3, 5), [1, 2, 3]);
assertArrayEquals(Array(3).fill(4), [4, 4, 4]);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);
}

View file

@ -50,6 +50,21 @@ function assertThrowsError(testFunction, options) {
}
}
/**
* Ensures the provided arrays contain exactly the same items.
* @param {Array} a First array
* @param {Array} b Second array
*/
function assertArrayEquals(a, b) {
if (a.length != b.length)
throw new AssertionError("Array lengths do not match");
for (var i = 0; i < a.length; i++) {
if (a[i] !== b[i])
throw new AssertionError("Elements do not match");
}
}
const assertVisitsAll = (testFunction, expectedOutput) => {
const visited = [];
testFunction(value => visited.push(value));