Add tests for TypedArrays toLocaleString

This commit is contained in:
Leonardo Balter 2016-04-25 18:19:30 -04:00 committed by Mike Pennisi
parent 9c45e2ac68
commit 177b2f6018
12 changed files with 585 additions and 0 deletions

View File

@ -0,0 +1,52 @@
// 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.tolocalestring
description: Calls toLocaleString from each property's value
info: |
22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
%TypedArray%.prototype.toLocaleString is a distinct function that implements
the same algorithm as Array.prototype.toLocaleString as defined in 22.1.3.27
except that the this object's [[ArrayLength]] internal slot is accessed in
place of performing a [[Get]] of "length".
22.1.3.27 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
...
5. Let firstElement be ? Get(array, "0").
6. If firstElement is undefined or null, then
a. Let R be the empty String.
7. Else,
a. Let R be ? ToString(? Invoke(firstElement, "toLocaleString")).
8. Let k be 1.
9.Repeat, while k < len
a. Let S be a String value produced by concatenating R and separator.
b. Let nextElement be ? Get(array, ! ToString(k)).
c. If nextElement is undefined or null, then
i. Let R be the empty String.
d. Else,
i. Let R be ? ToString(? Invoke(nextElement, "toLocaleString")).
includes: [testTypedArray.js, compareArray.js]
---*/
var separator = ["", ""].toLocaleString();
var calls;
Number.prototype.toLocaleString = function() {
calls.push(this);
return "hacks" + calls.length;
};
var arr = [42, 0];
var expected = ["hacks1", "hacks2"].join(separator);
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(arr);
calls = [];
assert.sameValue(sample.toLocaleString(), expected, "returns expected value");
assert(
compareArray(new TA(calls), sample),
"toLocaleString called for each item"
);
});

View File

@ -0,0 +1,57 @@
// 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.tolocalestring
description: >
Calls toString from each property's value return from toLocaleString
info: |
22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
%TypedArray%.prototype.toLocaleString is a distinct function that implements
the same algorithm as Array.prototype.toLocaleString as defined in 22.1.3.27
except that the this object's [[ArrayLength]] internal slot is accessed in
place of performing a [[Get]] of "length".
22.1.3.27 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
...
5. Let firstElement be ? Get(array, "0").
6. If firstElement is undefined or null, then
a. Let R be the empty String.
7. Else,
a. Let R be ? ToString(? Invoke(firstElement, "toLocaleString")).
8. Let k be 1.
9.Repeat, while k < len
a. Let S be a String value produced by concatenating R and separator.
b. Let nextElement be ? Get(array, ! ToString(k)).
c. If nextElement is undefined or null, then
i. Let R be the empty String.
d. Else,
i. Let R be ? ToString(? Invoke(nextElement, "toLocaleString")).
includes: [testTypedArray.js]
---*/
var separator = ["", ""].toLocaleString();
var calls;
Number.prototype.toLocaleString = function() {
return {
toString: function() {
calls++;
return "hacks" + calls;
},
valueOf: function() {
throw new Test262Error("should not call valueOf if toString is present");
}
};
};
var arr = [42, 0];
var expected = ["hacks1", "hacks2"].join(separator);
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(arr);
calls = 0;
assert.sameValue(sample.toLocaleString(), expected, "returns expected value");
assert.sameValue(calls, 2, "toString called once for each item");
});

View File

@ -0,0 +1,55 @@
// 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.tolocalestring
description: >
Calls valueOf from each property's value return from toLocaleString
info: |
22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
%TypedArray%.prototype.toLocaleString is a distinct function that implements
the same algorithm as Array.prototype.toLocaleString as defined in 22.1.3.27
except that the this object's [[ArrayLength]] internal slot is accessed in
place of performing a [[Get]] of "length".
22.1.3.27 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
...
5. Let firstElement be ? Get(array, "0").
6. If firstElement is undefined or null, then
a. Let R be the empty String.
7. Else,
a. Let R be ? ToString(? Invoke(firstElement, "toLocaleString")).
8. Let k be 1.
9.Repeat, while k < len
a. Let S be a String value produced by concatenating R and separator.
b. Let nextElement be ? Get(array, ! ToString(k)).
c. If nextElement is undefined or null, then
i. Let R be the empty String.
d. Else,
i. Let R be ? ToString(? Invoke(nextElement, "toLocaleString")).
includes: [testTypedArray.js]
---*/
var separator = ["", ""].toLocaleString();
var calls;
Number.prototype.toLocaleString = function() {
return {
toString: undefined,
valueOf: function() {
calls++;
return "hacks" + calls;
}
};
};
var arr = [42, 0];
var expected = ["hacks1", "hacks2"].join(separator);
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(arr);
calls = 0;
assert.sameValue(sample.toLocaleString(), expected, "returns expected value");
assert.sameValue(calls, 2, "valueOf called once for each item");
});

View File

@ -0,0 +1,25 @@
// 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.tolocalestring
description: Returns an empty string if called on an empty instance
info: |
22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
%TypedArray%.prototype.toLocaleString is a distinct function that implements
the same algorithm as Array.prototype.toLocaleString as defined in 22.1.3.27
except that the this object's [[ArrayLength]] internal slot is accessed in
place of performing a [[Get]] of "length".
22.1.3.27 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
...
4. If len is zero, return the empty String.
...
includes: [testTypedArray.js]
---*/
testWithTypedArrayConstructors(function(TA) {
var sample = new TA();
assert.sameValue(sample.toLocaleString(), "");
});

View File

@ -0,0 +1,41 @@
// 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.tolocalestring
description: Get "length" uses internal ArrayLength
info: >
22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
%TypedArray%.prototype.toLocaleString is a distinct function that implements
the same algorithm as Array.prototype.toLocaleString as defined in 22.1.3.27
except that the this object's [[ArrayLength]] internal slot is accessed in
place of performing a [[Get]] of "length".
22.1.3.27 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
1. Let array be ? ToObject(this value).
2.Let len be ? ToLength(? Get(array, "length")).
...
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);
sample.toLocaleString();
assert.sameValue(getCalls, 0, "ignores length properties");
});

View File

@ -0,0 +1,42 @@
// 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.tolocalestring
description: Returns abrupt from firstElement's toLocaleString
info: |
22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
%TypedArray%.prototype.toLocaleString is a distinct function that implements
the same algorithm as Array.prototype.toLocaleString as defined in 22.1.3.27
except that the this object's [[ArrayLength]] internal slot is accessed in
place of performing a [[Get]] of "length".
22.1.3.27 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
...
4. If len is zero, return the empty String.
5. Let firstElement be ? Get(array, "0").
6. If firstElement is undefined or null, then
a. Let R be the empty String.
7. Else,
a. Let R be ? ToString(? Invoke(firstElement, "toLocaleString")).
includes: [testTypedArray.js]
---*/
var calls;
Number.prototype.toLocaleString = function() {
calls++;
throw new Test262Error();
};
var arr = [42, 0];
testWithTypedArrayConstructors(function(TA) {
calls = 0;
var sample = new TA(arr);
assert.throws(Test262Error, function() {
sample.toLocaleString();
});
assert.sameValue(calls, 1, "abrupt from first element");
});

View File

@ -0,0 +1,54 @@
// 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.tolocalestring
description: >
Return abrupt from firstElement's toLocaleString => toString
info: |
22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
%TypedArray%.prototype.toLocaleString is a distinct function that implements
the same algorithm as Array.prototype.toLocaleString as defined in 22.1.3.27
except that the this object's [[ArrayLength]] internal slot is accessed in
place of performing a [[Get]] of "length".
22.1.3.27 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
...
5. Let firstElement be ? Get(array, "0").
6. If firstElement is undefined or null, then
a. Let R be the empty String.
7. Else,
a. Let R be ? ToString(? Invoke(firstElement, "toLocaleString")).
8. Let k be 1.
9.Repeat, while k < len
a. Let S be a String value produced by concatenating R and separator.
b. Let nextElement be ? Get(array, ! ToString(k)).
c. If nextElement is undefined or null, then
i. Let R be the empty String.
d. Else,
i. Let R be ? ToString(? Invoke(nextElement, "toLocaleString")).
includes: [testTypedArray.js]
---*/
var calls = 0;
Number.prototype.toLocaleString = function() {
return {
toString: function() {
calls++;
throw new Test262Error();
}
};
};
var arr = [42, 0];
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(arr);
calls = 0;
assert.throws(Test262Error, function() {
sample.toLocaleString();
});
assert.sameValue(calls, 1, "toString called once");
});

View File

@ -0,0 +1,55 @@
// 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.tolocalestring
description: >
Return abrupt from firstElement's toLocaleString => valueOf
info: |
22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
%TypedArray%.prototype.toLocaleString is a distinct function that implements
the same algorithm as Array.prototype.toLocaleString as defined in 22.1.3.27
except that the this object's [[ArrayLength]] internal slot is accessed in
place of performing a [[Get]] of "length".
22.1.3.27 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
...
5. Let firstElement be ? Get(array, "0").
6. If firstElement is undefined or null, then
a. Let R be the empty String.
7. Else,
a. Let R be ? ToString(? Invoke(firstElement, "toLocaleString")).
8. Let k be 1.
9.Repeat, while k < len
a. Let S be a String value produced by concatenating R and separator.
b. Let nextElement be ? Get(array, ! ToString(k)).
c. If nextElement is undefined or null, then
i. Let R be the empty String.
d. Else,
i. Let R be ? ToString(? Invoke(nextElement, "toLocaleString")).
includes: [testTypedArray.js]
---*/
var calls = 0;
Number.prototype.toLocaleString = function() {
return {
toString: undefined,
valueOf: function() {
calls++;
throw new Test262Error();
}
};
};
var arr = [42, 0];
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(arr);
calls = 0;
assert.throws(Test262Error, function() {
sample.toLocaleString();
});
assert.sameValue(calls, 1, "toString called once");
});

View File

@ -0,0 +1,45 @@
// 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.tolocalestring
description: Returns abrupt from a nextElement's toLocaleString
info: |
22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
%TypedArray%.prototype.toLocaleString is a distinct function that implements
the same algorithm as Array.prototype.toLocaleString as defined in 22.1.3.27
except that the this object's [[ArrayLength]] internal slot is accessed in
place of performing a [[Get]] of "length".
22.1.3.27 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
...
9.Repeat, while k < len
a. Let S be a String value produced by concatenating R and separator.
b. Let nextElement be ? Get(array, ! ToString(k)).
c. If nextElement is undefined or null, then
i. Let R be the empty String.
d. Else,
i. Let R be ? ToString(? Invoke(nextElement, "toLocaleString")).
includes: [testTypedArray.js]
---*/
var calls = 0;
Number.prototype.toLocaleString = function() {
calls++;
if (calls > 1) {
throw new Test262Error();
}
};
var arr = [42, 0];
testWithTypedArrayConstructors(function(TA) {
calls = 0;
var sample = new TA(arr);
assert.throws(Test262Error, function() {
sample.toLocaleString();
});
assert.sameValue(calls, 2, "abrupt from a next element");
});

View File

@ -0,0 +1,56 @@
// 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.tolocalestring
description: >
Return abrupt from nextElement's toLocaleString => valueOf
info: |
22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
%TypedArray%.prototype.toLocaleString is a distinct function that implements
the same algorithm as Array.prototype.toLocaleString as defined in 22.1.3.27
except that the this object's [[ArrayLength]] internal slot is accessed in
place of performing a [[Get]] of "length".
22.1.3.27 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
...
5. Let firstElement be ? Get(array, "0").
6. If firstElement is undefined or null, then
a. Let R be the empty String.
7. Else,
a. Let R be ? ToString(? Invoke(firstElement, "toLocaleString")).
8. Let k be 1.
9.Repeat, while k < len
a. Let S be a String value produced by concatenating R and separator.
b. Let nextElement be ? Get(array, ! ToString(k)).
c. If nextElement is undefined or null, then
i. Let R be the empty String.
d. Else,
i. Let R be ? ToString(? Invoke(nextElement, "toLocaleString")).
includes: [testTypedArray.js]
---*/
var calls = 0;
Number.prototype.toLocaleString = function() {
return {
toString: function() {
calls++;
if (calls > 1) {
throw new Test262Error();
}
}
};
};
var arr = [42, 0];
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(arr);
calls = 0;
assert.throws(Test262Error, function() {
sample.toLocaleString();
});
assert.sameValue(calls, 2, "abrupt from a nextElement");
});

View File

@ -0,0 +1,57 @@
// 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.tolocalestring
description: >
Return abrupt from nextElement's toLocaleString => valueOf
info: |
22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
%TypedArray%.prototype.toLocaleString is a distinct function that implements
the same algorithm as Array.prototype.toLocaleString as defined in 22.1.3.27
except that the this object's [[ArrayLength]] internal slot is accessed in
place of performing a [[Get]] of "length".
22.1.3.27 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
...
5. Let firstElement be ? Get(array, "0").
6. If firstElement is undefined or null, then
a. Let R be the empty String.
7. Else,
a. Let R be ? ToString(? Invoke(firstElement, "toLocaleString")).
8. Let k be 1.
9.Repeat, while k < len
a. Let S be a String value produced by concatenating R and separator.
b. Let nextElement be ? Get(array, ! ToString(k)).
c. If nextElement is undefined or null, then
i. Let R be the empty String.
d. Else,
i. Let R be ? ToString(? Invoke(nextElement, "toLocaleString")).
includes: [testTypedArray.js]
---*/
var calls = 0;
Number.prototype.toLocaleString = function() {
return {
toString: undefined,
valueOf: function() {
calls++;
if (calls > 1) {
throw new Test262Error();
}
}
};
};
var arr = [42, 0];
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(arr);
calls = 0;
assert.throws(Test262Error, function() {
sample.toLocaleString();
});
assert.sameValue(calls, 2, "abrupt from a nextElement");
});

View File

@ -0,0 +1,46 @@
// 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.tolocalestring
description: Returns a string
info: |
22.2.3.28 %TypedArray%.prototype.toLocaleString ([ reserved1 [ , reserved2 ] ])
%TypedArray%.prototype.toLocaleString is a distinct function that implements
the same algorithm as Array.prototype.toLocaleString as defined in 22.1.3.27
except that the this object's [[ArrayLength]] internal slot is accessed in
place of performing a [[Get]] of "length".
22.1.3.27 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
...
5. Let firstElement be ? Get(array, "0").
6. If firstElement is undefined or null, then
a. Let R be the empty String.
7. Else,
a. Let R be ? ToString(? Invoke(firstElement, "toLocaleString")).
8. Let k be 1.
9.Repeat, while k < len
a. Let S be a String value produced by concatenating R and separator.
b. Let nextElement be ? Get(array, ! ToString(k)).
c. If nextElement is undefined or null, then
i. Let R be the empty String.
d. Else,
i. Let R be ? ToString(? Invoke(nextElement, "toLocaleString")).
includes: [testTypedArray.js]
---*/
var separator = ["", ""].toLocaleString();
var arr = [42, 0, 43];
testWithTypedArrayConstructors(function(TA) {
var sample = new TA(arr);
var expected =
sample[0].toLocaleString().toString() +
separator +
sample[1].toLocaleString().toString() +
separator +
sample[2].toLocaleString().toString();
assert.sameValue(sample.toLocaleString(), expected);
});