Array.prototype-generic-functions.js 4.7 KB

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