Array.prototype-generic-functions.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. {
  40. assert(Array.prototype.indexOf.call({}) === -1);
  41. assert(Array.prototype.indexOf.call({ 0: undefined }) === -1);
  42. assert(Array.prototype.indexOf.call({ length: 1, 0: undefined }) === 0);
  43. assert(Array.prototype.indexOf.call({ length: 1, 2: "foo" }, "foo") === -1);
  44. assert(Array.prototype.indexOf.call({ length: 5, 2: "foo" }, "foo") === 2);
  45. assert(Array.prototype.indexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", 3) === 4);
  46. }
  47. {
  48. assert(Array.prototype.lastIndexOf.call({}) === -1);
  49. assert(Array.prototype.lastIndexOf.call({ 0: undefined }) === -1);
  50. assert(Array.prototype.lastIndexOf.call({ length: 1, 0: undefined }) === 0);
  51. assert(Array.prototype.lastIndexOf.call({ length: 1, 2: "foo" }, "foo") === -1);
  52. assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo" }, "foo") === 2);
  53. assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo") === 4);
  54. assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", -2) === 2);
  55. }
  56. {
  57. assert(Array.prototype.includes.call({}) === false);
  58. assert(Array.prototype.includes.call({ 0: undefined }) === false);
  59. assert(Array.prototype.includes.call({ length: 1, 0: undefined }) === true);
  60. assert(Array.prototype.includes.call({ length: 1, 2: "foo" }, "foo") === false);
  61. assert(Array.prototype.includes.call({ length: 5, 2: "foo" }, "foo") === true);
  62. }
  63. const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
  64. {
  65. const visited = [];
  66. Array.prototype.every.call(o, function (value) {
  67. visited.push(value);
  68. return true;
  69. });
  70. assert(visited.length === 3);
  71. assert(visited[0] === "foo");
  72. assert(visited[1] === "bar");
  73. assert(visited[2] === "baz");
  74. }
  75. ["find", "findIndex"].forEach(name => {
  76. const visited = [];
  77. Array.prototype[name].call(o, function (value) {
  78. visited.push(value);
  79. return false;
  80. });
  81. assert(visited.length === 5);
  82. assert(visited[0] === "foo");
  83. assert(visited[1] === "bar");
  84. assert(visited[2] === undefined);
  85. assert(visited[3] === "baz");
  86. assert(visited[4] === undefined);
  87. });
  88. ["filter", "forEach", "map", "some"].forEach(name => {
  89. const visited = [];
  90. Array.prototype[name].call(o, function (value) {
  91. visited.push(value);
  92. return false;
  93. });
  94. assert(visited.length === 3);
  95. assert(visited[0] === "foo");
  96. assert(visited[1] === "bar");
  97. assert(visited[2] === "baz");
  98. });
  99. {
  100. const visited = [];
  101. Array.prototype.reduce.call(o, function (_, value) {
  102. visited.push(value);
  103. return false;
  104. }, "initial");
  105. assert(visited.length === 3);
  106. assert(visited[0] === "foo");
  107. assert(visited[1] === "bar");
  108. assert(visited[2] === "baz");
  109. }
  110. {
  111. const visited = [];
  112. Array.prototype.reduceRight.call(o, function (_, value) {
  113. visited.push(value);
  114. return false;
  115. }, "initial");
  116. assert(visited.length === 3);
  117. assert(visited[2] === "foo");
  118. assert(visited[1] === "bar");
  119. assert(visited[0] === "baz");
  120. }
  121. console.log("PASS");
  122. } catch (e) {
  123. console.log("FAIL: " + e);
  124. }