Array.prototype-generic-functions.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. ["reduce"].forEach(name => {
  76. const visited = [];
  77. Array.prototype[name].call(o, function (_, value) {
  78. visited.push(value);
  79. return false;
  80. }, "initial");
  81. assert(visited.length === 3);
  82. assert(visited[0] === "foo");
  83. assert(visited[1] === "bar");
  84. assert(visited[2] === "baz");
  85. });
  86. console.log("PASS");
  87. } catch (e) {
  88. console.log("FAIL: " + e);
  89. }