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:
parent
d1bc1f5783
commit
199a6b40b3
4 changed files with 88 additions and 0 deletions
24
Libraries/LibJS/Tests/Array.prototype.fill.js
Normal file
24
Libraries/LibJS/Tests/Array.prototype.fill.js
Normal 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);
|
||||
}
|
||||
|
|
@ -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));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue