mirror of https://github.com/tc39/test262.git
Add tests for TypedArrays sort
This commit is contained in:
parent
2c7c989439
commit
a0cd3b07fc
|
@ -0,0 +1,38 @@
|
|||
// 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.sort
|
||||
description: Use internal ArrayLength instead of getting a length property
|
||||
info: >
|
||||
22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
|
||||
|
||||
...
|
||||
3. Let len be the value of obj's [[ArrayLength]] internal slot.
|
||||
includes: [testTypedArray.js, compareArray.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, 42, 42]);
|
||||
getCalls = 0;
|
||||
|
||||
Object.defineProperty(TA.prototype, "length", desc);
|
||||
Object.defineProperty(sample, "length", desc);
|
||||
|
||||
var result = sample.sort();
|
||||
|
||||
assert.sameValue(getCalls, 0, "ignores length properties");
|
||||
assert(
|
||||
compareArray(result, sample),
|
||||
"result is not affected by custom length"
|
||||
);
|
||||
});
|
|
@ -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.sort
|
||||
description: Returns abrupt from comparefn
|
||||
info: >
|
||||
22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
|
||||
|
||||
When the TypedArray SortCompare abstract operation is called with two
|
||||
arguments x and y, the following steps are taken:
|
||||
|
||||
...
|
||||
2. If the argument comparefn is not undefined, then
|
||||
a. Let v be ? Call(comparefn, undefined, « x, y »).
|
||||
...
|
||||
...
|
||||
|
||||
22.1.3.25 Array.prototype.sort (comparefn)
|
||||
|
||||
The following steps are taken:
|
||||
|
||||
- If an abrupt completion is returned from any of these operations, it is
|
||||
immediately returned as the value of this function.
|
||||
includes: [testTypedArray.js]
|
||||
---*/
|
||||
|
||||
testWithTypedArrayConstructors(function(TA) {
|
||||
var sample = new TA([42, 43, 44, 45, 46]);
|
||||
var calls = 0;
|
||||
|
||||
var comparefn = function() {
|
||||
calls += 1;
|
||||
throw new Test262Error();
|
||||
};
|
||||
|
||||
assert.throws(Test262Error, function() {
|
||||
sample.sort(comparefn);
|
||||
});
|
||||
|
||||
assert.sameValue(calls, 1, "immediately returned");
|
||||
});
|
|
@ -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.sort
|
||||
description: comparefn is called if not undefined
|
||||
info: >
|
||||
22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
|
||||
|
||||
When the TypedArray SortCompare abstract operation is called with two
|
||||
arguments x and y, the following steps are taken:
|
||||
|
||||
...
|
||||
2. If the argument comparefn is not undefined, then
|
||||
a. Let v be ? Call(comparefn, undefined, « x, y »).
|
||||
...
|
||||
...
|
||||
includes: [testTypedArray.js]
|
||||
---*/
|
||||
|
||||
var expectedThis = (function() {
|
||||
return this;
|
||||
})();
|
||||
|
||||
testWithTypedArrayConstructors(function(TA) {
|
||||
var sample = new TA([42, 42, 42, 42, 42]);
|
||||
var calls = [];
|
||||
|
||||
var comparefn = function() {
|
||||
calls.push([this, arguments]);
|
||||
};
|
||||
|
||||
sample.sort(comparefn);
|
||||
|
||||
assert(calls.length > 0, "calls comparefn");
|
||||
calls.forEach(function(args) {
|
||||
assert.sameValue(args[0], expectedThis, "comparefn is called no specific this");
|
||||
assert.sameValue(args[1].length, 2, "comparefn is always called with 2 args");
|
||||
assert.sameValue(args[1][0], 42, "x is a listed value");
|
||||
assert.sameValue(args[1][0], 42, "y is a listed value");
|
||||
});
|
||||
});
|
|
@ -0,0 +1,37 @@
|
|||
// 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.sort
|
||||
description: Throws a TypeError if comparefn detaches the object buffer
|
||||
info: >
|
||||
22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
|
||||
|
||||
When the TypedArray SortCompare abstract operation is called with two
|
||||
arguments x and y, the following steps are taken:
|
||||
|
||||
...
|
||||
2. If the argument comparefn is not undefined, then
|
||||
a. Let v be ? Call(comparefn, undefined, « x, y »).
|
||||
b. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
|
||||
...
|
||||
...
|
||||
includes: [testTypedArray.js, detachArrayBuffer.js]
|
||||
---*/
|
||||
|
||||
testWithTypedArrayConstructors(function(TA) {
|
||||
var sample = new TA(4);
|
||||
var calls = 0;
|
||||
var comparefn = function() {
|
||||
if (calls > 0) {
|
||||
throw new Test262Error();
|
||||
}
|
||||
calls++;
|
||||
$DETACHBUFFER(sample.buffer);
|
||||
};
|
||||
|
||||
assert.throws(TypeError, function() {
|
||||
sample.sort(comparefn);
|
||||
});
|
||||
|
||||
assert.sameValue(calls, 1);
|
||||
});
|
|
@ -0,0 +1,24 @@
|
|||
// 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.sort
|
||||
description: Returns the same instance
|
||||
info: >
|
||||
22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
|
||||
|
||||
When the TypedArray SortCompare abstract operation is called with two
|
||||
arguments x and y, the following steps are taken:
|
||||
|
||||
...
|
||||
includes: [testTypedArray.js]
|
||||
---*/
|
||||
|
||||
testWithTypedArrayConstructors(function(TA) {
|
||||
var sample = new TA([2, 1]);
|
||||
var result = sample.sort();
|
||||
|
||||
assert.sameValue(sample, result, "without comparefn");
|
||||
|
||||
result = sample.sort(function() { return 0; });
|
||||
assert.sameValue(sample, result, "with comparefn");
|
||||
});
|
|
@ -0,0 +1,26 @@
|
|||
// 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.sort
|
||||
description: TypedArrays sort does not cast values to String
|
||||
info: >
|
||||
22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
|
||||
|
||||
When the TypedArray SortCompare abstract operation is called with two
|
||||
arguments x and y, the following steps are taken:
|
||||
|
||||
...
|
||||
2. If the argument comparefn is not undefined, then
|
||||
a. Let v be ? Call(comparefn, undefined, « x, y »).
|
||||
...
|
||||
...
|
||||
includes: [testTypedArray.js, compareArray.js]
|
||||
---*/
|
||||
|
||||
var origToString = Number.prototype.toString;
|
||||
|
||||
testWithTypedArrayConstructors(function(TA) {
|
||||
var sample = new TA([20, 100, 3]);
|
||||
var result = sample.sort();
|
||||
assert(compareArray(result, [3, 20, 100]));
|
||||
});
|
|
@ -0,0 +1,37 @@
|
|||
// 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.sort
|
||||
description: Sort values to numeric ascending order
|
||||
info: >
|
||||
22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
|
||||
|
||||
When the TypedArray SortCompare abstract operation is called with two
|
||||
arguments x and y, the following steps are taken:
|
||||
|
||||
...
|
||||
|
||||
NOTE: Because NaN always compares greater than any other value, NaN property
|
||||
values always sort to the end of the result when comparefn is not provided.
|
||||
includes: [testTypedArray.js, compareArray.js]
|
||||
---*/
|
||||
|
||||
testWithTypedArrayConstructors(function(TA) {
|
||||
var sample;
|
||||
|
||||
sample = new TA([2, NaN, NaN, 0, 1]).sort();
|
||||
assert.sameValue(sample[0], 0, "#1 [0]");
|
||||
assert.sameValue(sample[1], 1, "#1 [1]");
|
||||
assert.sameValue(sample[2], 2, "#1 [2]");
|
||||
assert(Number.isNaN(sample[3]), "#1 [3]");
|
||||
assert(Number.isNaN(sample[4]), "#1 [4]");
|
||||
|
||||
sample = new TA([3, NaN, NaN, Infinity, 0, -Infinity, 2]).sort();
|
||||
assert.sameValue(sample[0], -Infinity, "#2 [0]");
|
||||
assert.sameValue(sample[1], 0, "#2 [1]");
|
||||
assert.sameValue(sample[2], 2, "#2 [2]");
|
||||
assert.sameValue(sample[3], 3, "#2 [3]");
|
||||
assert.sameValue(sample[4], Infinity, "#2 [4]");
|
||||
assert(Number.isNaN(sample[5]), "#2 [5]");
|
||||
assert(Number.isNaN(sample[6]), "#2 [6]");
|
||||
}, [Float64Array, Float32Array]);
|
|
@ -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.sort
|
||||
description: Sort values to numeric ascending order
|
||||
info: >
|
||||
22.2.3.26 %TypedArray%.prototype.sort ( comparefn )
|
||||
|
||||
When the TypedArray SortCompare abstract operation is called with two
|
||||
arguments x and y, the following steps are taken:
|
||||
|
||||
...
|
||||
includes: [testTypedArray.js, compareArray.js]
|
||||
---*/
|
||||
|
||||
testWithTypedArrayConstructors(function(TA) {
|
||||
var sample;
|
||||
|
||||
sample = new TA([4, 3, 2, 1]).sort();
|
||||
assert(compareArray(sample, [1, 2, 3, 4]), "descending values");
|
||||
|
||||
sample = new TA([3, 4, 1, 2]).sort();
|
||||
assert(compareArray(sample, [1, 2, 3, 4]), "mixed numbers");
|
||||
|
||||
sample = new TA([3, 4, 3, 1, 0, 1, 2]).sort();
|
||||
assert(compareArray(sample, [0, 1, 1, 2, 3, 3, 4]), "repeating numbers");
|
||||
|
||||
sample = new TA([1, 0, -0, 2]).sort();
|
||||
assert(compareArray(sample, [0, 0, 1, 2]), "0s");
|
||||
});
|
||||
|
||||
testWithTypedArrayConstructors(function(TA) {
|
||||
var sample = new TA([-4, 3, 4, -3, 2, -2, 1, 0]).sort();
|
||||
assert(compareArray(sample, [-4, -3, -2, 0, 1, 2, 3, 4]), "negative values");
|
||||
}, [Float64Array, Float32Array, Int8Array, Int16Array, Int32Array]);
|
||||
|
||||
testWithTypedArrayConstructors(function(TA) {
|
||||
var sample;
|
||||
|
||||
sample = new TA([0.5, 0, 1.5, 1]).sort();
|
||||
assert(compareArray(sample, [0, 0.5, 1, 1.5]), "non integers");
|
||||
|
||||
sample = new TA([0.5, 0, 1.5, -0.5, -1, -1.5, 1]).sort();
|
||||
assert(compareArray(sample, [-1.5, -1, -0.5, 0, 0.5, 1, 1.5]), "non integers + negatives");
|
||||
|
||||
sample = new TA([1, 0, -0, 2]).sort();
|
||||
assert(compareArray(sample, [0, 0, 1, 2]), "0 and -0");
|
||||
|
||||
sample = new TA([3, 4, Infinity, -Infinity, 1, 2]).sort();
|
||||
assert(compareArray(sample, [-Infinity, 1, 2, 3, 4, Infinity]), "infinities");
|
||||
|
||||
}, [Float64Array, Float32Array]);
|
Loading…
Reference in New Issue