array-core-test262.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Portions adapted from Test262 at revision 250f204f23a9249ff204be2baec29600faae7b75:
  3. * - test/built-ins/Array/prototype/includes/samevaluezero.js
  4. * - test/built-ins/Array/prototype/includes/using-fromindex.js
  5. * - test/built-ins/Array/prototype/join/S15.4.4.5_A3.1_T1.js
  6. * - test/built-ins/Array/prototype/join/S15.4.4.5_A3.2_T1.js
  7. * - test/built-ins/Array/prototype/slice/S15.4.4.10_A1.2_T2.js
  8. * - test/built-ins/Array/prototype/concat/S15.4.4.4_A1_T1.js
  9. * - test/built-ins/Array/prototype/concat/S15.4.4.4_A1_T2.js
  10. * - test/built-ins/Array/prototype/concat/S15.4.4.4_A1_T3.js
  11. * - test/built-ins/Array/prototype/indexOf/fromindex-zero-conversion.js
  12. * - test/built-ins/Array/prototype/indexOf/length-zero-returns-minus-one.js
  13. * - test/built-ins/Array/prototype/lastIndexOf/fromindex-zero-conversion.js
  14. * - test/built-ins/Array/prototype/lastIndexOf/length-zero-returns-minus-one.js
  15. * - test/built-ins/Array/prototype/at/returns-item.js
  16. * - test/built-ins/Array/prototype/at/returns-item-relative-index.js
  17. * - test/built-ins/Array/prototype/at/returns-undefined-for-out-of-range-index.js
  18. * - test/built-ins/Array/prototype/flat/null-undefined-elements.js
  19. * - test/built-ins/Array/prototype/flat/positive-infinity.js
  20. * - test/built-ins/Array/prototype/reverse/S15.4.4.8_A1_T1.js
  21. * - test/built-ins/Array/prototype/toReversed/immutable.js
  22. * - test/built-ins/Array/prototype/toReversed/zero-or-one-element.js
  23. * - test/built-ins/Array/prototype/with/immutable.js
  24. * - test/built-ins/Array/prototype/with/index-negative.js
  25. * - test/built-ins/Array/prototype/push/S15.4.4.7_A1_T1.js
  26. * - test/built-ins/Array/prototype/pop/S15.4.4.6_A1.1_T1.js
  27. * - test/built-ins/Array/prototype/shift/S15.4.4.9_A1.1_T1.js
  28. * - test/built-ins/Array/prototype/unshift/S15.4.4.13_A1_T1.js
  29. * - test/built-ins/Array/prototype/splice/S15.4.4.12_A1.1_T1.js
  30. * - test/built-ins/Array/prototype/splice/S15.4.4.12_A1.2_T1.js
  31. * - test/built-ins/Array/prototype/splice/called_with_one_argument.js
  32. * - test/built-ins/Array/prototype/fill/fill-values.js
  33. * - test/built-ins/Array/prototype/fill/fill-values-custom-start-and-end.js
  34. * - test/built-ins/Array/prototype/fill/return-this.js
  35. * - test/built-ins/Array/prototype/copyWithin/non-negative-target-start-and-end.js
  36. * - test/built-ins/Array/prototype/copyWithin/return-this.js
  37. * - test/built-ins/Array/prototype/keys/iteration.js
  38. * - test/built-ins/Array/prototype/values/iteration.js
  39. * - test/built-ins/Array/prototype/entries/iteration.js
  40. * - test/built-ins/Array/isArray/15.4.3.2-0-3.js
  41. * - test/built-ins/Array/isArray/15.4.3.2-0-4.js
  42. * - test/built-ins/Array/from/from-array.js
  43. * - test/built-ins/Array/from/from-string.js
  44. * - test/built-ins/Array/from/array-like-has-length-but-no-indexes-with-values.js
  45. * - test/built-ins/Array/of/creates-a-new-array-from-arguments.js
  46. *
  47. * Copyright (C) 2015 André Bargull. All rights reserved.
  48. * Copyright (C) 2015 the V8 project authors. All rights reserved.
  49. * Copyright (C) 2016 the V8 project authors. All rights reserved.
  50. * Copyright (C) 2018 Shilpi Jain and Michael Ficarra. All rights reserved.
  51. * Copyright (C) 2020 Alexey Shvayka. All rights reserved.
  52. * Copyright (C) 2020 Rick Waldron. All rights reserved.
  53. * Copyright (C) 2021 Igalia, S.L. All rights reserved.
  54. * Copyright (c) 2012 Ecma International. All rights reserved.
  55. * Copyright (c) 2014 Hank Yates. All rights reserved.
  56. * Copyright (c) 2015 the V8 project authors. All rights reserved.
  57. * Copyright (c) 2021 Rick Waldron. All rights reserved.
  58. * Copyright 2009 the Sputnik authors. All rights reserved.
  59. * Copyright 2015 Microsoft Corporation. All rights reserved.
  60. * Copyright 2016 The V8 project authors. All rights reserved.
  61. * Test262 portions are governed by the BSD license in LICENSE.test262.
  62. */
  63. import { describe, expect, test } from "bun:test"
  64. import { Effect } from "effect"
  65. import { CodeMode } from "../src/index.js"
  66. const value = async (code: string) => {
  67. const result = await Effect.runPromise(CodeMode.execute({ code, tools: {} }))
  68. if (!result.ok) throw new Error(`expected success, got ${result.error.kind}: ${result.error.message}`)
  69. return result.value
  70. }
  71. const cases = [
  72. {
  73. path: "test/built-ins/Array/prototype/includes/samevaluezero.js",
  74. code: `const input = [42, 0, 1, NaN]; return [input.includes(42), input.includes("42"), input.includes([42]), input.includes(true), input.includes(NaN), input.includes(0), input.includes(-0), input.includes(null), input.includes("")]`,
  75. expected: [true, false, false, false, true, true, true, false, false],
  76. },
  77. {
  78. path: "test/built-ins/Array/prototype/includes/using-fromindex.js",
  79. code: `const input = ["a", "b", "c"]; return [input.includes("a", 0), input.includes("a", 1), input.includes("a", -4), input.includes("a", -3), input.includes("a", -2), input.includes("b", 0), input.includes("b", 1), input.includes("b", 2), input.includes("b", -3), input.includes("b", -2), input.includes("b", -1), input.includes("c", 0), input.includes("c", 2), input.includes("c", 3), input.includes("c", -3), input.includes("c", -1)]`,
  80. expected: [true, false, true, true, false, true, true, false, true, true, false, true, true, false, true, true],
  81. },
  82. {
  83. path: "test/built-ins/Array/prototype/join/S15.4.4.5_A3.1_T1.js",
  84. code: `return [[0, 1, 2, 3].join("&"), [0, 1, 2, 3].join("")]`,
  85. expected: ["0&1&2&3", "0123"],
  86. },
  87. {
  88. path: "test/built-ins/Array/prototype/join/S15.4.4.5_A3.2_T1.js",
  89. code: `return [
  90. ["", "", ""].join(""),
  91. ["&", "&", "&"].join("&"),
  92. [true, true, true].join(),
  93. [null, null, null].join(),
  94. [undefined, undefined, undefined].join(),
  95. [Infinity, Infinity, Infinity].join(),
  96. [NaN, NaN, NaN].join(),
  97. ]`,
  98. expected: ["", "&&&&&", "true,true,true", ",,", ",,", "Infinity,Infinity,Infinity", "NaN,NaN,NaN"],
  99. },
  100. {
  101. path: "test/built-ins/Array/prototype/slice/S15.4.4.10_A1.2_T2.js",
  102. code: `return [0, 1, 2, 3, 4].slice(-1, 5)`,
  103. expected: [4],
  104. },
  105. {
  106. path: "test/built-ins/Array/prototype/concat/S15.4.4.4_A1_T1.js",
  107. code: `return [].concat([0, 1], [2, 3, 4])`,
  108. expected: [0, 1, 2, 3, 4],
  109. },
  110. {
  111. path: "test/built-ins/Array/prototype/concat/S15.4.4.4_A1_T2.js",
  112. code: `const object = { value: 1 }; const result = [0].concat(object, [1, 2], -1, true, "NaN"); return [result, result[1] === object]`,
  113. expected: [[0, { value: 1 }, 1, 2, -1, true, "NaN"], true],
  114. },
  115. {
  116. path: "test/built-ins/Array/prototype/concat/S15.4.4.4_A1_T3.js",
  117. code: `const input = [0, 1]; const result = input.concat(); return [result, result !== input]`,
  118. expected: [[0, 1], true],
  119. },
  120. {
  121. path: "test/built-ins/Array/prototype/indexOf/fromindex-zero-conversion.js",
  122. code: `const result = [true].indexOf(true, -0); return [result, 1 / result === Infinity]`,
  123. expected: [0, true],
  124. },
  125. {
  126. path: "test/built-ins/Array/prototype/indexOf/length-zero-returns-minus-one.js",
  127. code: `return [].indexOf(1)`,
  128. expected: -1,
  129. },
  130. {
  131. path: "test/built-ins/Array/prototype/lastIndexOf/fromindex-zero-conversion.js",
  132. code: `const result = [true].lastIndexOf(true, -0); return [result, 1 / result === Infinity]`,
  133. expected: [0, true],
  134. },
  135. {
  136. path: "test/built-ins/Array/prototype/lastIndexOf/length-zero-returns-minus-one.js",
  137. code: `return [].lastIndexOf(1)`,
  138. expected: -1,
  139. },
  140. {
  141. path: "test/built-ins/Array/prototype/at/returns-item.js",
  142. code: `const input = [1, 2, 3, 4, undefined, 5]; return [input.at(0), input.at(1), input.at(2), input.at(3), input.at(4) === undefined, input.at(5)]`,
  143. expected: [1, 2, 3, 4, true, 5],
  144. },
  145. {
  146. path: "test/built-ins/Array/prototype/at/returns-item-relative-index.js",
  147. code: `const input = [1, 2, 3, 4, undefined, 5]; return [input.at(0), input.at(-1), input.at(-2) === undefined, input.at(-3), input.at(-4)]`,
  148. expected: [1, 5, true, 4, 3],
  149. },
  150. {
  151. path: "test/built-ins/Array/prototype/at/returns-undefined-for-out-of-range-index.js",
  152. code: `const input = []; return [input.at(-2) === undefined, input.at(0) === undefined, input.at(1) === undefined]`,
  153. expected: [true, true, true],
  154. },
  155. {
  156. path: "test/built-ins/Array/prototype/flat/null-undefined-elements.js",
  157. code: `const result = [1, [null, [undefined]]].flat(2); return [result.length, result[0], result[1] === null, result[2] === undefined]`,
  158. expected: [3, 1, true, true],
  159. },
  160. {
  161. path: "test/built-ins/Array/prototype/flat/positive-infinity.js",
  162. code: `return [1, [2, [3, [4]]]].flat(Infinity)`,
  163. expected: [1, 2, 3, 4],
  164. },
  165. {
  166. path: "test/built-ins/Array/prototype/reverse/S15.4.4.8_A1_T1.js",
  167. code: `const empty = []; const one = [1]; const input = [1, 2]; const emptyResult = empty.reverse(); const oneResult = one.reverse(); const result = input.reverse(); return [emptyResult === empty, oneResult === one, result === input, input]`,
  168. expected: [true, true, true, [2, 1]],
  169. },
  170. {
  171. path: "test/built-ins/Array/prototype/toReversed/immutable.js",
  172. code: `const input = [0, 1, 2]; const result = input.toReversed(); return [input, result !== input]`,
  173. expected: [[0, 1, 2], true],
  174. },
  175. {
  176. path: "test/built-ins/Array/prototype/toReversed/zero-or-one-element.js",
  177. code: `const zero = []; const one = [1]; const zeroResult = zero.toReversed(); const oneResult = one.toReversed(); return [zeroResult, oneResult, zeroResult !== zero, oneResult !== one]`,
  178. expected: [[], [1], true, true],
  179. },
  180. {
  181. path: "test/built-ins/Array/prototype/with/immutable.js",
  182. code: `const input = [0, 1, 2]; const result = input.with(1, 3); return [input, result !== input, input.with(1, 1) !== input]`,
  183. expected: [[0, 1, 2], true, true],
  184. },
  185. {
  186. path: "test/built-ins/Array/prototype/with/index-negative.js",
  187. code: `const input = [0, 1, 2]; return [input.with(-1, 4), input.with(-3, 4)]`,
  188. expected: [
  189. [0, 1, 4],
  190. [4, 1, 2],
  191. ],
  192. },
  193. {
  194. path: "test/built-ins/Array/prototype/push/S15.4.4.7_A1_T1.js",
  195. code: `const input = []; return [input.push(1), input.push(), input.push(-1), input]`,
  196. expected: [1, 1, 2, [1, -1]],
  197. },
  198. {
  199. path: "test/built-ins/Array/prototype/pop/S15.4.4.6_A1.1_T1.js",
  200. code: `const input = []; return [input.pop() === undefined, input.length]`,
  201. expected: [true, 0],
  202. },
  203. {
  204. path: "test/built-ins/Array/prototype/shift/S15.4.4.9_A1.1_T1.js",
  205. code: `const input = []; return [input.shift() === undefined, input.length]`,
  206. expected: [true, 0],
  207. },
  208. {
  209. path: "test/built-ins/Array/prototype/unshift/S15.4.4.13_A1_T1.js",
  210. code: `const input = []; return [input.unshift(1), input[0], input.unshift(), input.unshift(-1), input]`,
  211. expected: [1, 1, 1, 2, [-1, 1]],
  212. },
  213. {
  214. path: "test/built-ins/Array/prototype/splice/S15.4.4.12_A1.1_T1.js",
  215. code: `const input = [0, 1, 2, 3]; const removed = input.splice(0, 3); return [input, removed]`,
  216. expected: [[3], [0, 1, 2]],
  217. },
  218. {
  219. path: "test/built-ins/Array/prototype/splice/S15.4.4.12_A1.2_T1.js",
  220. code: `const input = [0, 1]; const removed = input.splice(-2, -1); return [input, removed]`,
  221. expected: [[0, 1], []],
  222. },
  223. {
  224. path: "test/built-ins/Array/prototype/splice/called_with_one_argument.js",
  225. code: `const input = ["first", "second", "third"]; const removed = input.splice(1); return [input, removed]`,
  226. expected: [["first"], ["second", "third"]],
  227. },
  228. {
  229. path: "test/built-ins/Array/prototype/fill/fill-values-custom-start-and-end.js",
  230. code: `const input = [0, 0, 0, 0, 0]; input.fill(8, -3, 4); const sparse = []; sparse[4] = 0; sparse.fill(8, 1, 3); return [[0, 0, 0].fill(8, 1, 2), input, [0, 0, 0, 0, 0].fill(8, -2, -1), [0, 0, 0, 0, 0].fill(8, -1, -3), [0 in sparse, sparse[1], sparse[2], 3 in sparse, sparse[4]]]`,
  231. expected: [
  232. [0, 8, 0],
  233. [0, 0, 8, 8, 0],
  234. [0, 0, 0, 8, 0],
  235. [0, 0, 0, 0, 0],
  236. [false, 8, 8, false, 0],
  237. ],
  238. },
  239. {
  240. path: "test/built-ins/Array/prototype/fill/return-this.js",
  241. code: `const input = []; return input.fill(1) === input`,
  242. expected: true,
  243. },
  244. {
  245. path: "test/built-ins/Array/prototype/fill/fill-values.js",
  246. code: `const omitted = [0, 0].fill(); return [[].fill(8), omitted.map((value) => value === undefined), [0, 0, 0].fill(8)]`,
  247. expected: [[], [true, true], [8, 8, 8]],
  248. },
  249. {
  250. path: "test/built-ins/Array/prototype/copyWithin/non-negative-target-start-and-end.js",
  251. code: `return [[0, 1, 2, 3].copyWithin(0, 0, 0), [0, 1, 2, 3].copyWithin(0, 0, 2), [0, 1, 2, 3].copyWithin(0, 1, 2), [0, 1, 2, 3].copyWithin(1, 0, 2), [0, 1, 2, 3, 4, 5].copyWithin(1, 3, 5)]`,
  252. expected: [
  253. [0, 1, 2, 3],
  254. [0, 1, 2, 3],
  255. [1, 1, 2, 3],
  256. [0, 0, 1, 3],
  257. [0, 3, 4, 3, 4, 5],
  258. ],
  259. },
  260. {
  261. path: "test/built-ins/Array/prototype/copyWithin/return-this.js",
  262. code: `const input = [0, 1, 2, 3]; const result = input.copyWithin(1, 0, 2); return [input, result === input]`,
  263. expected: [[0, 0, 1, 3], true],
  264. },
  265. {
  266. path: "test/built-ins/Array/prototype/keys/iteration.js",
  267. code: `return ["a", "b", "c"].keys()`,
  268. expected: [0, 1, 2],
  269. },
  270. {
  271. path: "test/built-ins/Array/prototype/values/iteration.js",
  272. code: `return ["a", "b", "c"].values()`,
  273. expected: ["a", "b", "c"],
  274. },
  275. {
  276. path: "test/built-ins/Array/prototype/entries/iteration.js",
  277. code: `return ["a", "b"].entries()`,
  278. expected: [
  279. [0, "a"],
  280. [1, "b"],
  281. ],
  282. },
  283. {
  284. path: "test/built-ins/Array/isArray/15.4.3.2-0-3.js",
  285. code: `return [Array.isArray([]), Array.isArray([1]), Array.isArray(Array.of(1))]`,
  286. expected: [true, true, true],
  287. },
  288. {
  289. path: "test/built-ins/Array/isArray/15.4.3.2-0-4.js",
  290. code: `return [Array.isArray(42), Array.isArray({}), Array.isArray(null), Array.isArray("array")]`,
  291. expected: [false, false, false, false],
  292. },
  293. {
  294. path: "test/built-ins/Array/from/from-array.js",
  295. code: `const input = [0, "foo", undefined, Infinity]; const result = Array.from(input); return [result.length, result[0], result[1], result[2] === undefined, result[3] === Infinity, result !== input, result instanceof Array]`,
  296. expected: [4, 0, "foo", true, true, true, true],
  297. },
  298. {
  299. path: "test/built-ins/Array/from/from-string.js",
  300. code: `return Array.from("Test")`,
  301. expected: ["T", "e", "s", "t"],
  302. },
  303. {
  304. path: "test/built-ins/Array/from/array-like-has-length-but-no-indexes-with-values.js",
  305. code: `const result = Array.from({ length: 5 }); const mapped = result.map(() => 1); return [result.length, result.map((value) => value === undefined), mapped.length, mapped]`,
  306. expected: [5, [true, true, true, true, true], 5, [1, 1, 1, 1, 1]],
  307. },
  308. {
  309. path: "test/built-ins/Array/of/creates-a-new-array-from-arguments.js",
  310. code: `const mixed = Array.of(undefined, false, null, undefined); return [Array.of("Mike", "Rick", "Leo"), mixed.length, mixed[0] === undefined, mixed[1], mixed[2], mixed[3] === undefined, Array.of()]`,
  311. expected: [["Mike", "Rick", "Leo"], 4, true, false, null, true, []],
  312. },
  313. ] as const
  314. describe("Test262 Array core adaptations", () => {
  315. for (const item of cases) {
  316. test(item.path, async () => {
  317. expect(await value(item.code)).toEqual(item.expected)
  318. })
  319. }
  320. })