Array.prototype-generic-functions.js 4.9 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. const o = { length: 3, 0: "hello", 2: "serenity" };
  27. const removed = Array.prototype.splice.call(o, 0, 2, "hello", "friends");
  28. assert(o.length === 3);
  29. assert(o[0] === "hello");
  30. assert(o[1] === "friends");
  31. assert(o[2] === "serenity");
  32. assert(removed.length === 2);
  33. assert(removed[0] === "hello");
  34. assert(removed[1] === undefined);
  35. }
  36. {
  37. assert(Array.prototype.join.call({}) === "");
  38. assert(Array.prototype.join.call({ length: "foo" }) === "");
  39. assert(Array.prototype.join.call({ length: 3 }) === ",,");
  40. assert(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar" }) === "foo,bar");
  41. assert(Array.prototype.join.call({ length: 2, 0: "foo", 1: "bar", 2: "baz" }) === "foo,bar");
  42. assert(Array.prototype.join.call({ length: 3, 1: "bar" }, "~") === "~bar~");
  43. assert(Array.prototype.join.call({ length: 3, 0: "foo", 1: "bar", 2: "baz" }, "~") === "foo~bar~baz");
  44. }
  45. {
  46. assert(Array.prototype.toString.call({}) === "[object Object]");
  47. assert(Array.prototype.toString.call({ join: "foo" }) === "[object Object]");
  48. assert(Array.prototype.toString.call({ join: () => "foo" }) === "foo");
  49. }
  50. {
  51. assert(Array.prototype.indexOf.call({}) === -1);
  52. assert(Array.prototype.indexOf.call({ 0: undefined }) === -1);
  53. assert(Array.prototype.indexOf.call({ length: 1, 0: undefined }) === 0);
  54. assert(Array.prototype.indexOf.call({ length: 1, 2: "foo" }, "foo") === -1);
  55. assert(Array.prototype.indexOf.call({ length: 5, 2: "foo" }, "foo") === 2);
  56. assert(Array.prototype.indexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", 3) === 4);
  57. }
  58. {
  59. assert(Array.prototype.lastIndexOf.call({}) === -1);
  60. assert(Array.prototype.lastIndexOf.call({ 0: undefined }) === -1);
  61. assert(Array.prototype.lastIndexOf.call({ length: 1, 0: undefined }) === 0);
  62. assert(Array.prototype.lastIndexOf.call({ length: 1, 2: "foo" }, "foo") === -1);
  63. assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo" }, "foo") === 2);
  64. assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo") === 4);
  65. assert(Array.prototype.lastIndexOf.call({ length: 5, 2: "foo", 4: "foo" }, "foo", -2) === 2);
  66. }
  67. {
  68. assert(Array.prototype.includes.call({}) === false);
  69. assert(Array.prototype.includes.call({ 0: undefined }) === false);
  70. assert(Array.prototype.includes.call({ length: 1, 0: undefined }) === true);
  71. assert(Array.prototype.includes.call({ length: 1, 2: "foo" }, "foo") === false);
  72. assert(Array.prototype.includes.call({ length: 5, 2: "foo" }, "foo") === true);
  73. }
  74. const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
  75. {
  76. assertVisitsAll(visit => {
  77. Array.prototype.every.call(o, function (value) {
  78. visit(value);
  79. return true;
  80. });
  81. }, ["foo", "bar", "baz"]);
  82. }
  83. ["find", "findIndex"].forEach(name => {
  84. assertVisitsAll(visit => {
  85. Array.prototype[name].call(o, function (value) {
  86. visit(value);
  87. return false;
  88. });
  89. }, ["foo", "bar", undefined, "baz", undefined]);
  90. });
  91. ["filter", "forEach", "map", "some"].forEach(name => {
  92. assertVisitsAll(visit => {
  93. Array.prototype[name].call(o, function (value) {
  94. visit(value);
  95. return false;
  96. });
  97. }, ["foo", "bar", "baz"]);
  98. });
  99. {
  100. assertVisitsAll(visit => {
  101. Array.prototype.reduce.call(o, function (_, value) {
  102. visit(value);
  103. return false;
  104. }, "initial");
  105. }, ["foo", "bar", "baz"]);
  106. }
  107. {
  108. assertVisitsAll(visit => {
  109. Array.prototype.reduceRight.call(o, function (_, value) {
  110. visit(value);
  111. return false;
  112. }, "initial");
  113. }, ["baz", "bar", "foo"]);
  114. }
  115. console.log("PASS");
  116. } catch (e) {
  117. console.log("FAIL: " + e);
  118. }