diff --git a/test/built-ins/TypedArray/prototype/sort/arraylength-internal.js b/test/built-ins/TypedArray/prototype/sort/arraylength-internal.js new file mode 100644 index 0000000000000000000000000000000000000000..bf81e9580908dbd5194030f31d4c1e6b6e9d983a --- /dev/null +++ b/test/built-ins/TypedArray/prototype/sort/arraylength-internal.js @@ -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" + ); +}); diff --git a/test/built-ins/TypedArray/prototype/sort/comparefn-call-throws.js b/test/built-ins/TypedArray/prototype/sort/comparefn-call-throws.js new file mode 100644 index 0000000000000000000000000000000000000000..a995fecea21e5a0f34400fabbf353290d917ac54 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/sort/comparefn-call-throws.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/sort/comparefn-calls.js b/test/built-ins/TypedArray/prototype/sort/comparefn-calls.js new file mode 100644 index 0000000000000000000000000000000000000000..17b1f06d0ef4951c2270f67f341258bd9b76b46a --- /dev/null +++ b/test/built-ins/TypedArray/prototype/sort/comparefn-calls.js @@ -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"); + }); +}); diff --git a/test/built-ins/TypedArray/prototype/sort/detached-buffer-comparefn.js b/test/built-ins/TypedArray/prototype/sort/detached-buffer-comparefn.js new file mode 100644 index 0000000000000000000000000000000000000000..1de98debc8adaae3df5bb1041dfa11b8e66a39f8 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/sort/detached-buffer-comparefn.js @@ -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); +}); diff --git a/test/built-ins/TypedArray/prototype/sort/return-same-instance.js b/test/built-ins/TypedArray/prototype/sort/return-same-instance.js new file mode 100644 index 0000000000000000000000000000000000000000..d5ebc3816e25d4c9313c2ac9d36449ff003bf01c --- /dev/null +++ b/test/built-ins/TypedArray/prototype/sort/return-same-instance.js @@ -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"); +}); diff --git a/test/built-ins/TypedArray/prototype/sort/sortcompare-with-no-tostring.js b/test/built-ins/TypedArray/prototype/sort/sortcompare-with-no-tostring.js new file mode 100644 index 0000000000000000000000000000000000000000..f7943cf2a79eb91b35286cbe4cb5faf8eb2729dc --- /dev/null +++ b/test/built-ins/TypedArray/prototype/sort/sortcompare-with-no-tostring.js @@ -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])); +}); diff --git a/test/built-ins/TypedArray/prototype/sort/sorted-values-nan.js b/test/built-ins/TypedArray/prototype/sort/sorted-values-nan.js new file mode 100644 index 0000000000000000000000000000000000000000..884654a0b3d0de83dfde63095c34830dc01d5b5b --- /dev/null +++ b/test/built-ins/TypedArray/prototype/sort/sorted-values-nan.js @@ -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]); diff --git a/test/built-ins/TypedArray/prototype/sort/sorted-values.js b/test/built-ins/TypedArray/prototype/sort/sorted-values.js new file mode 100644 index 0000000000000000000000000000000000000000..bd1846a1dd70077706a02990683d2a4f41c803d7 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/sort/sorted-values.js @@ -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]);