Array.prototype-generic-functions.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. load("test-common.js");
  2. try {
  3. [undefined, "foo", -42, 0].forEach(length => {
  4. const o = { length };
  5. assert(Array.prototype.push.call(o, "foo") === 1);
  6. assert(o.length === 1);
  7. assert(o[0] === "foo");
  8. assert(Array.prototype.push.call(o, "bar", "baz") === 3);
  9. assert(o.length === 3);
  10. assert(o[0] === "foo");
  11. assert(o[1] === "bar");
  12. assert(o[2] === "baz");
  13. assert(Array.prototype.pop.call(o) === "baz");
  14. assert(o.length === 2);
  15. assert(Array.prototype.pop.call(o) === "bar");
  16. assert(o.length === 1);
  17. assert(Array.prototype.pop.call(o) === "foo");
  18. assert(o.length === 0);
  19. assert(Array.prototype.pop.call(o) === undefined);
  20. assert(o.length === 0);
  21. o.length = length;
  22. assert(Array.prototype.pop.call(o) === undefined);
  23. assert(o.length === 0);
  24. });
  25. {
  26. assert(Array.prototype.join.call({}) === "");
  27. assert(Array.prototype.join.call({ length: "foo" }) === "");
  28. assert(Array.prototype.join.call({ length: 3 }) === ",,");
  29. assert(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar" }) === "foo,bar");
  30. assert(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar", 2: "baz" }) === "foo,bar");
  31. assert(Array.prototype.join.call({ length: 3, 1: "bar" }, "~") === "~bar~");
  32. assert(Array.prototype.join.call({ length: 3, 0: "foo", 1: "bar", 2: "baz" }, "~") === "foo~bar~baz");
  33. }
  34. {
  35. assert(Array.prototype.toString.call({}) === "[object Object]");
  36. assert(Array.prototype.toString.call({ join: "foo" }) === "[object Object]");
  37. assert(Array.prototype.toString.call({ join: () => "foo" }) === "foo");
  38. }
  39. const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
  40. {
  41. const visited = [];
  42. Array.prototype.every.call(o, function (value) {
  43. visited.push(value);
  44. return true;
  45. });
  46. assert(visited.length === 3);
  47. assert(visited[0] === "foo");
  48. assert(visited[1] === "bar");
  49. assert(visited[2] === "baz");
  50. }
  51. ["find", "findIndex"].forEach(name => {
  52. const visited = [];
  53. Array.prototype[name].call(o, function (value) {
  54. visited.push(value);
  55. return false;
  56. });
  57. assert(visited.length === 5);
  58. assert(visited[0] === "foo");
  59. assert(visited[1] === "bar");
  60. assert(visited[2] === undefined);
  61. assert(visited[3] === "baz");
  62. assert(visited[4] === undefined);
  63. });
  64. ["filter", "forEach", "map", "some"].forEach(name => {
  65. const visited = [];
  66. Array.prototype[name].call(o, function (value) {
  67. visited.push(value);
  68. return false;
  69. });
  70. assert(visited.length === 3);
  71. assert(visited[0] === "foo");
  72. assert(visited[1] === "bar");
  73. assert(visited[2] === "baz");
  74. });
  75. console.log("PASS");
  76. } catch (e) {
  77. console.log("FAIL: " + e);
  78. }