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 @@
assertEquals(Array.prototype.flat.length, 0);
assertEquals(Array.prototype.flat.name, 'flat');
const input = [1, [2], [[3]]];
assertEquals(input.flat(), [1, 2, [3]]);
assertEquals(input.flat(1), [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(-0), [1, [2], [[3]]]);
assertEquals(input.flat(0), [1, [2], [[3]]]);
assertEquals(input.flat(false), [1, [2], [[3]]]);
assertEquals(input.flat(null), [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(new Proxy({}, {})), [1, [2], [[3]]]);
assertEquals(input.flat((x) => x), [1, [2], [[3]]]);
assertEquals(
input.flat(String), [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);
{
const input = [1, [2], [[3]]];
assertEquals(input.flat(), [1, 2, [3]]);
assertEquals(input.flat(1), [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(-0), [1, [2], [[3]]]);
assertEquals(input.flat(0), [1, [2], [[3]]]);
assertEquals(input.flat(false), [1, [2], [[3]]]);
assertEquals(input.flat(null), [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(new Proxy({}, {})), [1, [2], [[3]]]);
assertEquals(input.flat((x) => x), [1, [2], [[3]]]);
assertEquals(
input.flat(String), [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);
}
{
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(
[[1, 1], [2, 4], [3, 9], [4, 16]]
);
const elements = new Set([
-Infinity,
-1,
-0,
+0,
+1,
Infinity,
null,
undefined,
true,
false,
'',
'foo',
/./,
[],
{},
Object.create(null),
new Proxy({}, {}),
Symbol(),
x => x ** 2,
String
]);
{
const elements = new Set([
-Infinity,
-1,
-0,
+0,
+1,
Infinity,
null,
undefined,
true,
false,
'',
'foo',
/./,
[],
{},
Object.create(null),
new Proxy({}, {}),
Symbol(),
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(
[value].flatMap((element) => [element, element]),
[value, value]
[array].flatMap((element) => [element, element]),
[array, array]
);
}
const array = [42];
assertEquals(
[array].flatMap((element) => [element, element]),
[array, array]
);
const nonCallables = new Set([
-Infinity,
-1,
-0,
+0,
+1,
Infinity,
null,
undefined,
true,
false,
'',
'foo',
/./,
[],
{},
Object.create(null),
new Proxy({}, {}),
Symbol(),
]);
for (const nonCallable of nonCallables) {
assertThrows(() => {
[].flatMap(nonCallable);
}, TypeError);
{
const nonCallables = new Set([
-Infinity,
-1,
-0,
+0,
+1,
Infinity,
null,
undefined,
true,
false,
'',
'foo',
/./,
[],
{},
Object.create(null),
new Proxy({}, {}),
Symbol(),
]);
for (const nonCallable of nonCallables) {
assertThrows(() => {
[].flatMap(nonCallable);
}, TypeError);
}
}
const object = {
foo: 42,
get length() {
object.foo = 0;
}
};
const result = [object].flatMap((element) => [element, element]);
%HeapObjectVerify(result);
assertEquals(result, [object, object]);
assertEquals(result[0].foo, 42);
{
const object = {
foo: 42,
get length() {
object.foo = 0;
}
};
const result = [object].flatMap((element) => [element, element]);
%HeapObjectVerify(result);
assertEquals(result, [object, object]);
assertEquals(result[0].foo, 42);
}
assertThrows(() => {
Array.prototype.flatMap.call(null, (element) => element);
......@@ -118,3 +126,37 @@ assertEquals(
),
['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