Add tests for TypedArrays join

This commit is contained in:
Leonardo Balter 2016-04-27 12:08:09 -04:00 committed by Mike Pennisi
parent e6f4b20834
commit 19c09e95d9
8 changed files with 429 additions and 0 deletions

View File

@ -0,0 +1,77 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.join
description: >
Concatenates the result of toString for each value with custom separator
info: >
22.2.3.15 %TypedArray%.prototype.join ( separator )
%TypedArray%.prototype.join is a distinct function that implements the same
algorithm as Array.prototype.join as defined in 22.1.3.13 except that the this
object's [[ArrayLength]] internal slot is accessed in place of performing a
[[Get]] of "length".
22.1.3.13 Array.prototype.join (separator)
...
7. If element0 is undefined or null, let R be the empty String; otherwise, let
R be ? ToString(element0).
8. Let k be 1.
9. Repeat, while k < len
a. Let S be the String value produced by concatenating R and sep.
b. Let element be ? Get(O, ! ToString(k)).
c. If element is undefined or null, let next be the empty String; otherwise,
let next be ? ToString(element).
d. Let R be a String value produced by concatenating S and next.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([1, 0, 2, 3, 42, 127]);
var result;
result = sample.join(",");
assert.sameValue(result, "1,0,2,3,42,127");
result = sample.join(undefined);
assert.sameValue(result, "1,0,2,3,42,127");
result = sample.join(null);
assert.sameValue(result, "1null0null2null3null42null127");
result = sample.join(",,");
assert.sameValue(result, "1,,0,,2,,3,,42,,127");
result = sample.join(0);
assert.sameValue(result, "10002030420127");
result = sample.join("");
assert.sameValue(result, "102342127");
result = sample.join(" a b c ");
assert.sameValue(result, "1 a b c 0 a b c 2 a b c 3 a b c 42 a b c 127");
result = sample.join({});
assert.sameValue(result, "1[object Object]0[object Object]2[object Object]3[object Object]42[object Object]127");
result = sample.join(true);
assert.sameValue(result, "1true0true2true3true42true127");
result = sample.join({ toString: function() { return "foo"; }});
assert.sameValue(result, "1foo0foo2foo3foo42foo127");
result = sample.join({ toString: undefined, valueOf: function() { return "bar"; }});
assert.sameValue(result, "1bar0bar2bar3bar42bar127");
result = sample.join(false);
assert.sameValue(result, "1false0false2false3false42false127");
result = sample.join(-1);
assert.sameValue(result, "1-10-12-13-142-1127");
result = sample.join(-0);
assert.sameValue(result, "10002030420127");
});

View File

@ -0,0 +1,134 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.join
description: >
Concatenates the result of toString for each value with custom separator
info: >
22.2.3.15 %TypedArray%.prototype.join ( separator )
%TypedArray%.prototype.join is a distinct function that implements the same
algorithm as Array.prototype.join as defined in 22.1.3.13 except that the this
object's [[ArrayLength]] internal slot is accessed in place of performing a
[[Get]] of "length".
22.1.3.13 Array.prototype.join (separator)
...
7. If element0 is undefined or null, let R be the empty String; otherwise, let
R be ? ToString(element0).
8. Let k be 1.
9. Repeat, while k < len
a. Let S be the String value produced by concatenating R and sep.
b. Let element be ? Get(O, ! ToString(k)).
c. If element is undefined or null, let next be the empty String; otherwise,
let next be ? ToString(element).
d. Let R be a String value produced by concatenating S and next.
...
includes: [testTypedArray.js]
---*/
var arr = [-2, Infinity, NaN, -Infinity, 0.6, 9007199254740992];
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(arr);
var result, separator, expected;
separator = ",";
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected);
separator = undefined;
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
separator = null;
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
separator = ",,";
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
separator = 0;
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
separator = "";
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
separator = " a b c ";
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
separator = {};
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
separator = { toString: function() { return "foo"; }};
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
separator = { toString: undefined, valueOf: function() { return "bar"; }};
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
separator = true;
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
separator = false;
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
separator = 1;
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
separator = 0;
expected = arr.map(function(_, i) {
return sample[i].toString();
}).join(separator);
result = sample.join(separator);
assert.sameValue(result, expected, "using: " + separator);
});

View File

@ -0,0 +1,28 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.join
description: Return the empty String if length is 0
info: >
22.2.3.15 %TypedArray%.prototype.join ( separator )
%TypedArray%.prototype.join is a distinct function that implements the same
algorithm as Array.prototype.join as defined in 22.1.3.13 except that the this
object's [[ArrayLength]] internal slot is accessed in place of performing a
[[Get]] of "length".
22.1.3.13 Array.prototype.join (separator)
...
4. Let sep be ? ToString(separator).
5. If len is zero, return the empty String.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
assert.sameValue(sample.join(), "");
assert.sameValue(sample.join("test262"), "");
});

View File

@ -0,0 +1,44 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.join
description: Get "length" uses internal ArrayLength
info: >
22.2.3.15 %TypedArray%.prototype.join ( separator )
%TypedArray%.prototype.join is a distinct function that implements the same
algorithm as Array.prototype.join as defined in 22.1.3.13 except that the this
object's [[ArrayLength]] internal slot is accessed in place of performing a
[[Get]] of "length".
22.1.3.13 Array.prototype.join (separator)
1. Let O be ? ToObject(this value).
2. Let len be ? ToLength(? Get(O, "length")).
...
5. If len is zero, return the empty String.
...
includes: [testTypedArray.js]
---*/
var getCalls = 0;
var desc = {
get: function getLen() {
getCalls++;
return 0;
}
};
Object.defineProperty(TypedArray.prototype, "length", desc);
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([42, 43]);
Object.defineProperty(TA.prototype, "length", desc);
Object.defineProperty(sample, "length", desc);
var result = sample.join();
assert.sameValue(getCalls, 0, "ignores length properties");
assert.notSameValue(result, "", "result is not affected but custom length 0");
});

View File

@ -0,0 +1,36 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.join
description: Concatenates the result of toString for each simple value
info: >
22.2.3.15 %TypedArray%.prototype.join ( separator )
%TypedArray%.prototype.join is a distinct function that implements the same
algorithm as Array.prototype.join as defined in 22.1.3.13 except that the this
object's [[ArrayLength]] internal slot is accessed in place of performing a
[[Get]] of "length".
22.1.3.13 Array.prototype.join (separator)
...
7. If element0 is undefined or null, let R be the empty String; otherwise, let
R be ? ToString(element0).
8. Let k be 1.
9. Repeat, while k < len
a. Let S be the String value produced by concatenating R and sep.
b. Let element be ? Get(O, ! ToString(k)).
c. If element is undefined or null, let next be the empty String; otherwise,
let next be ? ToString(element).
d. Let R be a String value produced by concatenating S and next.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA([1, 0, 2, 3, 42, 127]);
var result = sample.join();
assert.sameValue(result, "1,0,2,3,42,127");
});

View File

@ -0,0 +1,43 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.join
description: Concatenates the result of toString for each value
info: >
22.2.3.15 %TypedArray%.prototype.join ( separator )
%TypedArray%.prototype.join is a distinct function that implements the same
algorithm as Array.prototype.join as defined in 22.1.3.13 except that the this
object's [[ArrayLength]] internal slot is accessed in place of performing a
[[Get]] of "length".
22.1.3.13 Array.prototype.join (separator)
...
7. If element0 is undefined or null, let R be the empty String; otherwise, let
R be ? ToString(element0).
8. Let k be 1.
9. Repeat, while k < len
a. Let S be the String value produced by concatenating R and sep.
b. Let element be ? Get(O, ! ToString(k)).
c. If element is undefined or null, let next be the empty String; otherwise,
let next be ? ToString(element).
d. Let R be a String value produced by concatenating S and next.
...
includes: [testTypedArray.js]
---*/
var arr = [-2, Infinity, NaN, -Infinity, 0.6, 9007199254740992];
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(arr);
// Use converted values using Array methods as helpers
var expected = arr.map(function(_, i) {
return sample[i].toString();
}).join();
var result = sample.join();
assert.sameValue(result, expected);
});

View File

@ -0,0 +1,32 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.join
description: Return abrupt from ToString(Symbol separator)
info: >
22.2.3.15 %TypedArray%.prototype.join ( separator )
%TypedArray%.prototype.join is a distinct function that implements the same
algorithm as Array.prototype.join as defined in 22.1.3.13 except that the this
object's [[ArrayLength]] internal slot is accessed in place of performing a
[[Get]] of "length".
22.1.3.13 Array.prototype.join (separator)
...
4. Let sep be ? ToString(separator).
5. If len is zero, return the empty String.
...
includes: [testTypedArray.js]
features: [Symbol]
---*/
var s = Symbol("");
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
assert.throws(TypeError, function() {
sample.join(s);
});
});

View File

@ -0,0 +1,35 @@
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%typedarray%.prototype.join
description: Return abrupt from ToString(separator)
info: >
22.2.3.15 %TypedArray%.prototype.join ( separator )
%TypedArray%.prototype.join is a distinct function that implements the same
algorithm as Array.prototype.join as defined in 22.1.3.13 except that the this
object's [[ArrayLength]] internal slot is accessed in place of performing a
[[Get]] of "length".
22.1.3.13 Array.prototype.join (separator)
...
4. Let sep be ? ToString(separator).
5. If len is zero, return the empty String.
...
includes: [testTypedArray.js]
---*/
var obj = {
toString: function() {
throw new Test262Error();
}
};
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
assert.throws(Test262Error, function() {
sample.join(obj);
});
});