12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- load("test-common.js");
- try {
- [undefined, "foo", -42, 0].forEach(length => {
- const o = { length };
- assert(Array.prototype.push.call(o, "foo") === 1);
- assert(o.length === 1);
- assert(o[0] === "foo");
- assert(Array.prototype.push.call(o, "bar", "baz") === 3);
- assert(o.length === 3);
- assert(o[0] === "foo");
- assert(o[1] === "bar");
- assert(o[2] === "baz");
- assert(Array.prototype.pop.call(o) === "baz");
- assert(o.length === 2);
- assert(Array.prototype.pop.call(o) === "bar");
- assert(o.length === 1);
- assert(Array.prototype.pop.call(o) === "foo");
- assert(o.length === 0);
- assert(Array.prototype.pop.call(o) === undefined);
- assert(o.length === 0);
- o.length = length;
- assert(Array.prototype.pop.call(o) === undefined);
- assert(o.length === 0);
- });
- const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
- {
- const visited = [];
- Array.prototype.every.call(o, function (value) {
- visited.push(value);
- return true;
- });
- assert(visited.length === 3);
- assert(visited[0] === "foo");
- assert(visited[1] === "bar");
- assert(visited[2] === "baz");
- }
- ["find", "findIndex"].forEach(name => {
- const visited = [];
- Array.prototype[name].call(o, function (value) {
- visited.push(value);
- return false;
- });
- assert(visited.length === 5);
- assert(visited[0] === "foo");
- assert(visited[1] === "bar");
- assert(visited[2] === undefined);
- assert(visited[3] === "baz");
- assert(visited[4] === undefined);
- });
- ["filter", "forEach", "map", "some"].forEach(name => {
- const visited = [];
- Array.prototype[name].call(o, function (value) {
- visited.push(value);
- return false;
- });
- assert(visited.length === 3);
- assert(visited[0] === "foo");
- assert(visited[1] === "bar");
- assert(visited[2] === "baz");
- });
- console.log("PASS");
- } catch (e) {
- console.log("FAIL: " + e);
- }
|