diff --git a/test/built-ins/Array/prototype/groupByToMap/callback-arg.js b/test/built-ins/Array/prototype/groupByToMap/callback-arg.js new file mode 100644 index 0000000000..d66d21e9e8 --- /dev/null +++ b/test/built-ins/Array/prototype/groupByToMap/callback-arg.js @@ -0,0 +1,35 @@ +// Copyright (c) 2021 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.groupByToMap +description: Array.prototype.groupByToMap populates Map with correct keys and values +info: | + 22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] ) + + ... + + 6. Repeat, while k < len + + a. Let Pk be ! ToString(๐”ฝ(k)). + b. Let kValue be ? Get(O, Pk). + c. Let key be ? Call(callbackfn, thisArg, ยซ kValue, ๐”ฝ(k), O ยป). + d. If key is -0๐”ฝ, set key to +0๐”ฝ. + e. Perform ! AddValueToKeyedGroup(groups, key, kValue). + f. Set k to k + 1. + + ... + +---*/ + + +const arr = [-0,0,1,2,3]; + +const sentinel = {}; + +const map = arr.groupByToMap.call(sentinel, (n,i,testArr) => { + assert.sameValue(this, sentinel, "this value is correctly bound"); + assert.sameValue(n, arr[i], "selected element aligns with index"); + assert.sameValue(testArr, arr, "original array is passed as final argument"); + return null; +}); diff --git a/test/built-ins/Array/prototype/groupByToMap/emptyList.js b/test/built-ins/Array/prototype/groupByToMap/emptyList.js new file mode 100644 index 0000000000..01a7580d60 --- /dev/null +++ b/test/built-ins/Array/prototype/groupByToMap/emptyList.js @@ -0,0 +1,25 @@ +// Copyright (c) 2021 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.groupByToMap +description: Array.prototype.groupByToMap populates Map with correct keys and values +info: | + 22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] ) + + ... + + 8. For each Record { [[Key]], [[Elements]] } g of groups, do + + a. Let elements be ! CreateArrayFromList(g.[[Elements]]). + b. Let entry be the Record { [[Key]]: g.[[Key]], [[Value]]: elements }. + c. Append entry as the last element of map.[[MapData]]. + + ... + +---*/ + +const map = [].groupByToMap(i=>i); + +assert.sameValue(map.size, 0); +assert.sameValue(typeof map, typeof new Map()); diff --git a/test/built-ins/Array/prototype/groupByToMap/evenOdd.js b/test/built-ins/Array/prototype/groupByToMap/evenOdd.js new file mode 100644 index 0000000000..278b6854cb --- /dev/null +++ b/test/built-ins/Array/prototype/groupByToMap/evenOdd.js @@ -0,0 +1,27 @@ +// Copyright (c) 2021 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.groupByToMap +description: Array.prototype.groupByToMap populates Map with correct keys and values +info: | + 22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] ) + + ... + + 8. For each Record { [[Key]], [[Elements]] } g of groups, do + + a. Let elements be ! CreateArrayFromList(g.[[Elements]]). + b. Let entry be the Record { [[Key]]: g.[[Key]], [[Value]]: elements }. + c. Append entry as the last element of map.[[MapData]]. + + ... +---*/ + +const array = [1,2,3]; + +const map = array.groupByToMap(i=>{return i%2===0?'even':'odd';}); + +assert.sameValue(map.get('even')[0], 2); +assert.sameValue(map.get('odd')[0], 1); +assert.sameValue(map.get('odd')[1], 3); diff --git a/test/built-ins/Array/prototype/groupByToMap/groupByLength.js b/test/built-ins/Array/prototype/groupByToMap/groupByLength.js new file mode 100644 index 0000000000..c50751242d --- /dev/null +++ b/test/built-ins/Array/prototype/groupByToMap/groupByLength.js @@ -0,0 +1,28 @@ +// Copyright (c) 2021 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.groupByToMap +description: Array.prototype.groupByToMap populates Map with correct keys and values +info: | + 22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] ) + + ... + + 8. For each Record { [[Key]], [[Elements]] } g of groups, do + + a. Let elements be ! CreateArrayFromList(g.[[Elements]]). + b. Let entry be the Record { [[Key]]: g.[[Key]], [[Value]]: elements }. + c. Append entry as the last element of map.[[MapData]]. + + ... + +---*/ + +const arr = ['test', 'hello', 'world']; + +const map = arr.groupByToMap(i=>i.length); + +assert.sameValue(map.get(4)[0], 'test'); +assert.sameValue(map.get(5)[0], 'hello'); +assert.sameValue(map.get(5)[1], 'world') diff --git a/test/built-ins/Array/prototype/groupByToMap/length.js b/test/built-ins/Array/prototype/groupByToMap/length.js new file mode 100644 index 0000000000..876af00d40 --- /dev/null +++ b/test/built-ins/Array/prototype/groupByToMap/length.js @@ -0,0 +1,29 @@ + +// Copyright (c) 2021 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.groupByToMap +description: Array.prototype.groupByToMap property length descriptor +info: | + 22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] ) + + ... + + 17 ECMAScript Standard Built-in Objects + + ... + +includes: [propertyHelper.js] +---*/ + +assert.sameValue( + Array.prototype.groupByToMap.length, 1, + 'The value of `Array.prototype.groupByMap.length` is `1`' +); + +verifyProperty(Array.prototype.groupByToMap, "length", { + enumerable: false, + writable: false, + configurable: true +}); diff --git a/test/built-ins/Array/prototype/groupByToMap/name.js b/test/built-ins/Array/prototype/groupByToMap/name.js new file mode 100644 index 0000000000..b91ea4e130 --- /dev/null +++ b/test/built-ins/Array/prototype/groupByToMap/name.js @@ -0,0 +1,28 @@ +// Copyright (c) 2021 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.groupByToMap +description: Array.prototype.groupByToMap property name descriptor +info: | + 22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] ) + + ... + + 17 ECMAScript Standard Built-in Objects + + ... + +includes: [propertyHelper.js] +---*/ + +assert.sameValue( + Array.prototype.groupByToMap.name, 'groupByToMap', + 'The value of `Array.prototype.groupByMap.name` is `"groupByMap"`' +); + +verifyProperty(Array.prototype.groupByToMap, "name", { + enumerable: false, + writable: false, + configurable: true +}); diff --git a/test/built-ins/Array/prototype/groupByToMap/negativeZero.js b/test/built-ins/Array/prototype/groupByToMap/negativeZero.js new file mode 100644 index 0000000000..37a00843a4 --- /dev/null +++ b/test/built-ins/Array/prototype/groupByToMap/negativeZero.js @@ -0,0 +1,25 @@ +// Copyright (c) 2021 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.groupByToMap +description: Array.prototype.groupByToMap normalize 0 for Map key +info: | + 22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] ) + + ... + + 6.d. If key is -0๐”ฝ, set key to +0๐”ฝ. + + ... +---*/ + + +const arr = [-0, +0]; + +const map = arr.groupByToMap(i=>i); + + +assert.sameValue(map.size, 1); +assert.sameValue(map.has(0), true); +assert.sameValue(map.get(0).length, 2); diff --git a/test/built-ins/Array/prototype/groupByToMap/null.js b/test/built-ins/Array/prototype/groupByToMap/null.js new file mode 100644 index 0000000000..f0ae7855dd --- /dev/null +++ b/test/built-ins/Array/prototype/groupByToMap/null.js @@ -0,0 +1,21 @@ +// Copyright (c) 2021 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.groupByToMap +description: Array.prototype.groupByToMap applied to null throws a TypeError +info: | + 22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] ) + + ... + + 3. If IsCallable(callbackfn) is false, throw a TypeError exception. + + ... + +---*/ + + +assert.throws(TypeError, function() { + Array.prototype.groupByToMap.call(null,null); +}); diff --git a/test/built-ins/Array/prototype/groupByToMap/undefined.js b/test/built-ins/Array/prototype/groupByToMap/undefined.js new file mode 100644 index 0000000000..0ef5ab313c --- /dev/null +++ b/test/built-ins/Array/prototype/groupByToMap/undefined.js @@ -0,0 +1,21 @@ +// Copyright (c) 2021 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-array.prototype.groupByToMap +description: Array.prototype.groupByToMap applied to undefined throws a TypeError +info: | + 22.1.3.15 Array.prototype.groupByToMap ( callbackfn [ , thisArg ] ) + + ... + + 3. If IsCallable(callbackfn) is false, throw a TypeError exception. + + ... + +---*/ + + +assert.throws(TypeError, function() { + Array.prototype.groupByToMap.call(undefined, undefined); +});