Array.prototype-generic-functions.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. assertVisitsAll(visit => {
  66. Array.prototype.every.call(o, function (value) {
  67. visit(value);
  68. return true;
  69. });
  70. }, ["foo", "bar", "baz"]);
  71. }
  72. ["find", "findIndex"].forEach(name => {
  73. assertVisitsAll(visit => {
  74. Array.prototype[name].call(o, function (value) {
  75. visit(value);
  76. return false;
  77. });
  78. }, ["foo", "bar", undefined, "baz", undefined]);
  79. });
  80. ["filter", "forEach", "map", "some"].forEach(name => {
  81. assertVisitsAll(visit => {
  82. Array.prototype[name].call(o, function (value) {
  83. visit(value);
  84. return false;
  85. });
  86. }, ["foo", "bar", "baz"]);
  87. });
  88. {
  89. assertVisitsAll(visit => {
  90. Array.prototype.reduce.call(o, function (_, value) {
  91. visit(value);
  92. return false;
  93. }, "initial");
  94. }, ["foo", "bar", "baz"]);
  95. }
  96. {
  97. assertVisitsAll(visit => {
  98. Array.prototype.reduceRight.call(o, function (_, value) {
  99. visit(value);
  100. return false;
  101. }, "initial");
  102. }, ["baz", "bar", "foo"]);
  103. }
  104. console.log("PASS");
  105. } catch (e) {
  106. console.log("FAIL: " + e);
  107. }