Commit f338188a authored by Mathias Bynens's avatar Mathias Bynens Committed by Commit Bot

[test] Expand Array#{flat,flatMap} test coverage

Bug: v8:7220
Change-Id: I9fef685f19cadbe87cd6451fe887f4c9c7d23b19
Reviewed-on: https://chromium-review.googlesource.com/1070337
Commit-Queue: Mathias Bynens <mathias@chromium.org>
Reviewed-by: 's avatarSathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53315}
parent 3e1126bf
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-array-flat
{
class MyArray extends Array {
static get [Symbol.species]() {
return Array;
}
}
const wannabe = new MyArray();
const flattened = wannabe.flat(Infinity);
assertEquals(false, flattened instanceof MyArray);
assertEquals(true, flattened instanceof Array);
}
{
class MyArray extends Array {
static get [Symbol.species]() {
return this;
}
}
const wannabe = new MyArray();
const flattened = wannabe.flat(Infinity);
assertEquals(true, flattened instanceof MyArray);
assertEquals(true, flattened instanceof Array);
}
...@@ -7,32 +7,59 @@ ...@@ -7,32 +7,59 @@
assertEquals(Array.prototype.flat.length, 0); assertEquals(Array.prototype.flat.length, 0);
assertEquals(Array.prototype.flat.name, 'flat'); assertEquals(Array.prototype.flat.name, 'flat');
const input = [1, [2], [[3]]]; {
const input = [1, [2], [[3]]];
assertEquals(input.flat(), [1, 2, [3]]);
assertEquals(input.flat(1), [1, 2, [3]]); assertEquals(input.flat(), [1, 2, [3]]);
assertEquals(input.flat(true), [1, 2, [3]]); assertEquals(input.flat(1), [1, 2, [3]]);
assertEquals(input.flat(undefined), [1, 2, [3]]); assertEquals(input.flat(true), [1, 2, [3]]);
assertEquals(input.flat(undefined), [1, 2, [3]]);
assertEquals(input.flat(-Infinity), [1, [2], [[3]]]);
assertEquals(input.flat(-1), [1, [2], [[3]]]); assertEquals(input.flat(-Infinity), [1, [2], [[3]]]);
assertEquals(input.flat(-0), [1, [2], [[3]]]); assertEquals(input.flat(-1), [1, [2], [[3]]]);
assertEquals(input.flat(0), [1, [2], [[3]]]); assertEquals(input.flat(-0), [1, [2], [[3]]]);
assertEquals(input.flat(false), [1, [2], [[3]]]); assertEquals(input.flat(0), [1, [2], [[3]]]);
assertEquals(input.flat(null), [1, [2], [[3]]]); assertEquals(input.flat(false), [1, [2], [[3]]]);
assertEquals(input.flat(''), [1, [2], [[3]]]); assertEquals(input.flat(null), [1, [2], [[3]]]);
assertEquals(input.flat('foo'), [1, [2], [[3]]]); assertEquals(input.flat(''), [1, [2], [[3]]]);
assertEquals(input.flat(/./), [1, [2], [[3]]]); assertEquals(input.flat('foo'), [1, [2], [[3]]]);
assertEquals(input.flat([]), [1, [2], [[3]]]); assertEquals(input.flat(/./), [1, [2], [[3]]]);
assertEquals(input.flat({}), [1, [2], [[3]]]); assertEquals(input.flat([]), [1, [2], [[3]]]);
assertEquals( assertEquals(input.flat({}), [1, [2], [[3]]]);
input.flat(new Proxy({}, {})), [1, [2], [[3]]]); assertEquals(
assertEquals(input.flat((x) => x), [1, [2], [[3]]]); input.flat(new Proxy({}, {})), [1, [2], [[3]]]);
assertEquals( assertEquals(input.flat((x) => x), [1, [2], [[3]]]);
input.flat(String), [1, [2], [[3]]]); assertEquals(
input.flat(String), [1, [2], [[3]]]);
assertEquals(input.flat(2), [1, 2, 3]);
assertEquals(input.flat(Infinity), [1, 2, 3]); assertEquals(input.flat(2), [1, 2, 3]);
assertEquals(input.flat(Infinity), [1, 2, 3]);
assertThrows(() => { input.flat(Symbol()); }, TypeError);
assertThrows(() => { input.flat(Object.create(null)); }, TypeError); assertThrows(() => { input.flat(Symbol()); }, TypeError);
assertThrows(() => { input.flat(Object.create(null)); }, TypeError);
}
{
const input = { 0: 'a', 1: 'b', 2: 'c', length: 'wat' };
assertEquals(Array.prototype.flat.call(input, Infinity), []);
}
{
let count = 0;
const input = {
get length() { ++count; return 0; }
};
const result = Array.prototype.flat.call(input, Infinity);
assertEquals(count, 1);
}
{
const descriptor = Object.getOwnPropertyDescriptor(
Array.prototype,
'flat'
);
assertEquals(descriptor.value, Array.prototype.flat);
assertEquals(descriptor.writable, true);
assertEquals(descriptor.enumerable, false);
assertEquals(descriptor.configurable, true);
}
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-array-flat
{
class MyArray extends Array {
static get [Symbol.species]() {
return Array;
}
}
const wannabe = new MyArray();
const result = wannabe.flatMap(x => [x, x]);
assertEquals(false, result instanceof MyArray);
assertEquals(true, result instanceof Array);
}
{
class MyArray extends Array {
static get [Symbol.species]() {
return this;
}
}
const wannabe = new MyArray();
const result = wannabe.flatMap(x => [x, x]);
assertEquals(true, result instanceof MyArray);
assertEquals(true, result instanceof Array);
}
...@@ -16,78 +16,86 @@ assertEquals( ...@@ -16,78 +16,86 @@ assertEquals(
[[1, 1], [2, 4], [3, 9], [4, 16]] [[1, 1], [2, 4], [3, 9], [4, 16]]
); );
const elements = new Set([ {
-Infinity, const elements = new Set([
-1, -Infinity,
-0, -1,
+0, -0,
+1, +0,
Infinity, +1,
null, Infinity,
undefined, null,
true, undefined,
false, true,
'', false,
'foo', '',
/./, 'foo',
[], /./,
{}, [],
Object.create(null), {},
new Proxy({}, {}), Object.create(null),
Symbol(), new Proxy({}, {}),
x => x ** 2, Symbol(),
String x => x ** 2,
]); String
]);
for (const value of elements) { for (const value of elements) {
assertEquals(
[value].flatMap((element) => [element, element]),
[value, value]
);
}
}
{
const array = [42];
assertEquals( assertEquals(
[value].flatMap((element) => [element, element]), [array].flatMap((element) => [element, element]),
[value, value] [array, array]
); );
} }
const array = [42]; {
assertEquals( const nonCallables = new Set([
[array].flatMap((element) => [element, element]), -Infinity,
[array, array] -1,
); -0,
+0,
const nonCallables = new Set([ +1,
-Infinity, Infinity,
-1, null,
-0, undefined,
+0, true,
+1, false,
Infinity, '',
null, 'foo',
undefined, /./,
true, [],
false, {},
'', Object.create(null),
'foo', new Proxy({}, {}),
/./, Symbol(),
[], ]);
{}, for (const nonCallable of nonCallables) {
Object.create(null), assertThrows(() => {
new Proxy({}, {}), [].flatMap(nonCallable);
Symbol(), }, TypeError);
]); }
for (const nonCallable of nonCallables) {
assertThrows(() => {
[].flatMap(nonCallable);
}, TypeError);
} }
const object = { {
foo: 42, const object = {
get length() { foo: 42,
object.foo = 0; get length() {
} object.foo = 0;
}; }
const result = [object].flatMap((element) => [element, element]); };
%HeapObjectVerify(result); const result = [object].flatMap((element) => [element, element]);
assertEquals(result, [object, object]); %HeapObjectVerify(result);
assertEquals(result[0].foo, 42); assertEquals(result, [object, object]);
assertEquals(result[0].foo, 42);
}
assertThrows(() => { assertThrows(() => {
Array.prototype.flatMap.call(null, (element) => element); Array.prototype.flatMap.call(null, (element) => element);
...@@ -118,3 +126,37 @@ assertEquals( ...@@ -118,3 +126,37 @@ assertEquals(
), ),
['a', 'b'] ['a', 'b']
); );
{
const result = [1, 2, 3].flatMap(function() {
return [this];
}, 'abc');
assertEquals(true, result[0] == 'abc');
assertEquals(true, result[1] == 'abc');
assertEquals(true, result[2] == 'abc');
}
{
const input = { 0: 'a', 1: 'b', 2: 'c', length: 'wat' };
assertEquals(Array.prototype.flatMap.call(input, x => [x]), []);
}
{
let count = 0;
const input = {
get length() { ++count; return 0; }
};
const result = Array.prototype.flatMap.call(input, x => [x]);
assertEquals(count, 1);
}
{
const descriptor = Object.getOwnPropertyDescriptor(
Array.prototype,
'flatMap'
);
assertEquals(descriptor.value, Array.prototype.flatMap);
assertEquals(descriptor.writable, true);
assertEquals(descriptor.enumerable, false);
assertEquals(descriptor.configurable, true);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment