diff options
Diffstat (limited to 'js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt')
21 files changed, 972 insertions, 0 deletions
diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/browser.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/browser.js diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-arguments-custom-accumulator.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-arguments-custom-accumulator.js new file mode 100644 index 0000000000..762f6896ce --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-arguments-custom-accumulator.js @@ -0,0 +1,60 @@ +// 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.reduce +description: > + callbackfn arguments using custom accumulator +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 8. Repeat, while k < len + ... + c. If kPresent is true, then + ... + i. Let accumulator be ? Call(callbackfn, undefined, « accumulator, kValue, + k, O »). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([42n, 43n, 44n]); + + var results = []; + + sample.reduce(function(accumulator) { + results.push(arguments); + return accumulator + 1; + }, 7); + + assert.sameValue(results.length, 3, "results.length"); + + assert.sameValue(results[0].length, 4, "results[0].length"); + assert.sameValue(results[0][0], 7, "results[0][0] - accumulator"); + assert.sameValue(results[0][1], 42n, "results[0][1] - kValue"); + assert.sameValue(results[0][2], 0, "results[0][2] - k"); + assert.sameValue(results[0][3], sample, "results[0][3] - this"); + + assert.sameValue(results[1].length, 4, "results[1].length"); + assert.sameValue(results[1][0], 8, "results[1][0] - accumulator"); + assert.sameValue(results[1][1], 43n, "results[1][1] - kValue"); + assert.sameValue(results[1][2], 1, "results[1][2] - k"); + assert.sameValue(results[1][3], sample, "results[1][3] - this"); + + assert.sameValue(results[2].length, 4, "results[2].length"); + assert.sameValue(results[2][0], 9, "results[2][0] - accumulator"); + assert.sameValue(results[2][1], 44n, "results[2][1] - kValue"); + assert.sameValue(results[2][2], 2, "results[2][2] - k"); + assert.sameValue(results[2][3], sample, "results[2][3] - this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-arguments-default-accumulator.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-arguments-default-accumulator.js new file mode 100644 index 0000000000..3f37559d74 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-arguments-default-accumulator.js @@ -0,0 +1,61 @@ +// 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.reduce +description: > + callbackfn arguments using default accumulator (value at index 0) +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 7. Else initialValue is not present, + a. Let kPresent be false. + b. Repeat, while kPresent is false and k < len + ... + iii. If kPresent is true, then + 1. Let accumulator be ? Get(O, Pk). + ... + 8. Repeat, while k < len + ... + c. If kPresent is true, then + ... + i. Let accumulator be ? Call(callbackfn, undefined, « accumulator, kValue, + k, O »). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([42n, 43n, 44n]); + + var results = []; + + sample.reduce(function(accumulator) { + results.push(arguments); + return accumulator - 1n; + }); + + assert.sameValue(results.length, 2, "results.length"); + + assert.sameValue(results[0].length, 4, "results[1].length"); + assert.sameValue(results[0][0], 42n, "results[1][0] - accumulator"); + assert.sameValue(results[0][1], 43n, "results[1][1] - kValue"); + assert.sameValue(results[0][2], 1, "results[1][2] - k"); + assert.sameValue(results[0][3], sample, "results[1][3] - this"); + + assert.sameValue(results[1].length, 4, "results[2].length"); + assert.sameValue(results[1][0], 41n, "results[2][0] - accumulator"); + assert.sameValue(results[1][1], 44n, "results[2][1] - kValue"); + assert.sameValue(results[1][2], 2, "results[2][2] - k"); + assert.sameValue(results[1][3], sample, "results[2][3] - this"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-detachbuffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-detachbuffer.js new file mode 100644 index 0000000000..344e7deaee --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-detachbuffer.js @@ -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.reduce +description: > + Instance buffer can be detached during loop +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 8. Repeat, while k < len + ... + c. If kPresent is true, then + ... + i. Let accumulator be ? Call(callbackfn, undefined, « accumulator, kValue, + k, O »). + ... +includes: [detachArrayBuffer.js, testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var loops = 0; + var sample = new TA(2); + + sample.reduce(function() { + if (loops === 0) { + $DETACHBUFFER(sample.buffer); + } + loops++; + return true; + }, 0); + + assert.sameValue(loops, 2); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-is-not-callable-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-is-not-callable-throws.js new file mode 100644 index 0000000000..bf9a51b527 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-is-not-callable-throws.js @@ -0,0 +1,69 @@ +// 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.reduce +description: > + Throws TypeError if callbackfn is not callable +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 3. If IsCallable(callbackfn) is false, throw a TypeError exception. + 4. If len is 0 and initialValue is not present, throw a TypeError exception. + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, Symbol, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + + assert.throws(TypeError, function() { + sample.reduce(); + }, "no arg"); + + assert.throws(TypeError, function() { + sample.reduce(undefined); + }, "undefined"); + + assert.throws(TypeError, function() { + sample.reduce(null); + }, "null"); + + assert.throws(TypeError, function() { + sample.reduce({}); + }, "{}"); + + assert.throws(TypeError, function() { + sample.reduce(1); + }, "1"); + + assert.throws(TypeError, function() { + sample.reduce(NaN); + }, "NaN"); + + assert.throws(TypeError, function() { + sample.reduce(""); + }, "string"); + + assert.throws(TypeError, function() { + sample.reduce(false); + }, "false"); + + assert.throws(TypeError, function() { + sample.reduce(true); + }, "true"); + + assert.throws(TypeError, function() { + sample.reduce(Symbol("")); + }, "symbol"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-no-iteration-over-non-integer-properties.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-no-iteration-over-non-integer-properties.js new file mode 100644 index 0000000000..7b23880d92 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-no-iteration-over-non-integer-properties.js @@ -0,0 +1,50 @@ +// 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.reduce +description: > + Does not iterate over non-integer properties +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 8. Repeat, while k < len + ... + c. If kPresent is true, then + ... + i. Let accumulator be ? Call(callbackfn, undefined, « accumulator, kValue, + k, O »). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, Symbol, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([7n, 8n]); + + var results = []; + + sample.foo = 42; + sample[Symbol("1")] = 43; + + sample.reduce(function() { + results.push(arguments); + }, 0); + + assert.sameValue(results.length, 2, "results.length"); + + assert.sameValue(results[0][2], 0, "results[0][2] - k"); + assert.sameValue(results[1][2], 1, "results[1][2] - k"); + + assert.sameValue(results[0][1], 7n, "results[0][1] - kValue"); + assert.sameValue(results[1][1], 8n, "results[1][1] - kValue"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-not-called-on-empty.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-not-called-on-empty.js new file mode 100644 index 0000000000..ffc057e473 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-not-called-on-empty.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.reduce +description: > + callbackfn is not called on empty instances +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 4. If len is 0 and initialValue is not present, throw a TypeError exception. + ... + 8. Repeat, while k < len + ... + c. If kPresent is true, then + ... + i. Let accumulator be ? Call(callbackfn, undefined, « accumulator, kValue, + k, O »). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var called = 0; + + new TA().reduce(function() { + called++; + }, undefined); + + assert.sameValue(called, 0); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-return-does-not-change-instance.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-return-does-not-change-instance.js new file mode 100644 index 0000000000..a59d946ee5 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-return-does-not-change-instance.js @@ -0,0 +1,23 @@ +// 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.reduce +description: > + The callbackfn return does not change the `this` instance +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([0n, 1n, 0n]); + + sample.reduce(function() { + return 42; + }, 7); + + assert.sameValue(sample[0], 0n, "[0] == 0"); + assert.sameValue(sample[1], 1n, "[1] == 1"); + assert.sameValue(sample[2], 0n, "[2] == 0"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-returns-abrupt.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-returns-abrupt.js new file mode 100644 index 0000000000..fc0f0856eb --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-returns-abrupt.js @@ -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.reduce +description: > + Returns abrupt from callbackfn +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 8. Repeat, while k < len + ... + c. If kPresent is true, then + ... + i. Let accumulator be ? Call(callbackfn, undefined, « accumulator, kValue, + k, O »). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(2); + + assert.throws(Test262Error, function() { + sample.reduce(function() { + throw new Test262Error(); + }); + }); + + assert.throws(Test262Error, function() { + sample.reduce(function() { + throw new Test262Error(); + }, 0); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-set-value-during-iteration.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-set-value-during-iteration.js new file mode 100644 index 0000000000..1d62357347 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-set-value-during-iteration.js @@ -0,0 +1,51 @@ +// 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.reduce +description: > + Integer indexed values changed during iteration +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) +includes: [testBigIntTypedArray.js] +features: [BigInt, Reflect.set, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([42n, 43n, 44n]); + var newVal = 0n; + + sample.reduce(function(acc, val, i) { + if (i > 0) { + assert.sameValue( + sample[i - 1], newVal - 1n, + "get the changed value during the loop" + ); + assert.sameValue( + Reflect.set(sample, 0, 7n), + true, + "re-set a value for sample[0]" + ); + } + assert.sameValue( + Reflect.set(sample, i, newVal), + true, + "set value during iteration" + ); + + newVal++; + }, 0); + + assert.sameValue(sample[0], 7n, "changed values after iteration [0] == 7"); + assert.sameValue(sample[1], 1n, "changed values after iteration [1] == 1"); + assert.sameValue(sample[2], 2n, "changed values after iteration [2] == 2"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-this.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-this.js new file mode 100644 index 0000000000..901b8ae0d7 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/callbackfn-this.js @@ -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.reduce +description: > + callbackfn `this` value +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 8. Repeat, while k < len + ... + c. If kPresent is true, then + ... + i. Let accumulator be ? Call(callbackfn, undefined, « accumulator, kValue, + k, O »). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +var expected = (function() { return this; })(); + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(3); + + var results = []; + + sample.reduce(function() { + results.push(this); + }, 0); + + assert.sameValue(results.length, 3, "results.length"); + assert.sameValue(results[0], expected, "[0]"); + assert.sameValue(results[1], expected, "[1]"); + assert.sameValue(results[2], expected, "[2]"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/detached-buffer.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/detached-buffer.js new file mode 100644 index 0000000000..2fda7511ab --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/detached-buffer.js @@ -0,0 +1,34 @@ +// 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.reduce +description: Throws a TypeError if this has a detached buffer +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + This function is not generic. ValidateTypedArray is applied to the this value + prior to evaluating the algorithm. If its result is an abrupt completion that + exception is thrown instead of evaluating the algorithm. + + 22.2.3.5.1 Runtime Semantics: ValidateTypedArray ( O ) + + ... + 5. If IsDetachedBuffer(buffer) is true, throw a TypeError exception. + ... +includes: [testBigIntTypedArray.js, detachArrayBuffer.js] +features: [BigInt, TypedArray] +---*/ + +var callbackfn = function() { + throw new Test262Error(); +}; + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA(1); + $DETACHBUFFER(sample.buffer); + assert.throws(TypeError, function() { + sample.reduce(callbackfn); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/empty-instance-return-initialvalue.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/empty-instance-return-initialvalue.js new file mode 100644 index 0000000000..e888cd589d --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/empty-instance-return-initialvalue.js @@ -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.reduce +description: > + Returns given initialValue on empty instances without calling callbackfn +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 7. Else initialValue is not present, + ... + b. Repeat, while kPresent is false and k < len + ... + iii. If kPresent is true, then + 1. Let accumulator be ? Get(O, Pk). + iv. Increase k by 1. + ... + 8. Repeat, while k < len + ... + 9. Return accumulator. +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var called = false; + var result = new TA().reduce(function() { + called = true; + }, 42); + + assert.sameValue(result, 42); + assert.sameValue(called, false); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/empty-instance-with-no-initialvalue-throws.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/empty-instance-with-no-initialvalue-throws.js new file mode 100644 index 0000000000..bbc77b69aa --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/empty-instance-with-no-initialvalue-throws.js @@ -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.reduce +description: > + If len is 0 and initialValue is not present, throw a TypeError exception. +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 4. If len is 0 and initialValue is not present, throw a TypeError exception. + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var called = 0; + + assert.throws(TypeError, function() { + new TA().reduce(function() { + called++; + }); + }); + + assert.sameValue(called, 0); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/get-length-uses-internal-arraylength.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/get-length-uses-internal-arraylength.js new file mode 100644 index 0000000000..d10d56a344 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/get-length-uses-internal-arraylength.js @@ -0,0 +1,48 @@ +// 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.reduce +description: Get "length" uses internal ArrayLength +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + 1. Let O be ? ToObject(this value). + 2. Let len be ? ToLength(? Get(O, "length")). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +var getCalls = 0; +var desc = { + get: function getLen() { + getCalls++; + return 0; + } +}; + +Object.defineProperty(TypedArray.prototype, "length", desc); + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([42n, 43n]); + var calls = 0; + + Object.defineProperty(TA.prototype, "length", desc); + Object.defineProperty(sample, "length", desc); + + sample.reduce(function() { + calls++; + }, 0); + + assert.sameValue(getCalls, 0, "ignores length properties"); + assert.sameValue(calls, 2, "iterations are not affected by custom length"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/result-is-last-callbackfn-return.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/result-is-last-callbackfn-return.js new file mode 100644 index 0000000000..efb3026781 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/result-is-last-callbackfn-return.js @@ -0,0 +1,61 @@ +// 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.reduce +description: > + Returns last accumulator value +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 7. Else initialValue is not present, + ... + b. Repeat, while kPresent is false and k < len + ... + iii. If kPresent is true, then + 1. Let accumulator be ? Get(O, Pk). + iv. Increase k by 1. + ... + 8. Repeat, while k < len + ... + c. If kPresent is true, then + i. Let kValue be ? Get(O, Pk). + ii. Let accumulator be ? Call(callbackfn, undefined, « accumulator, + kValue, k, O »). + 9. Return accumulator. +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var calls, result; + + calls = 0; + result = new TA([1n, 2n, 3n]).reduce(function() { + calls++; + + if (calls == 2) { + return 42; + } + }); + assert.sameValue(result, 42, "using default accumulator"); + + calls = 0; + result = new TA([1n, 2n, 3n]).reduce(function() { + calls++; + + if (calls == 3) { + return 7; + } + }, 0); + assert.sameValue(result, 7, "using custom accumulator"); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/result-of-any-type.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/result-of-any-type.js new file mode 100644 index 0000000000..911b5f2be8 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/result-of-any-type.js @@ -0,0 +1,69 @@ +// 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.reduce +description: > + Result can be of any type without any number conversions +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 7. Else initialValue is not present, + ... + b. Repeat, while kPresent is false and k < len + ... + iii. If kPresent is true, then + 1. Let accumulator be ? Get(O, Pk). + iv. Increase k by 1. + ... + 8. Repeat, while k < len + ... + c. If kPresent is true, then + i. Let kValue be ? Get(O, Pk). + ii. Let accumulator be ? Call(callbackfn, undefined, « accumulator, + kValue, k, O »). + 9. Return accumulator. +includes: [testBigIntTypedArray.js] +features: [BigInt, Symbol, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([42n, 43n, 44n]); + [ + ["test262", "string"], + ["", "empty string"], + [undefined, "undefined"], + [null, "null"], + [-0, "-0"], + [42, "integer"], + [NaN, "NaN"], + [Infinity, "Infinity"], + [0.6, "float number"], + [true, "true"], + [false, "false"], + [Symbol(""), "symbol"], + [{}, "object"] + ].forEach(function(item) { + var result; + + result = sample.reduce(function() { + return item[0]; + }); + assert.sameValue(result, item[0], item[1] + " - using default accumulator"); + + result = sample.reduce(function() { + return item[0]; + }, 0); + + assert.sameValue(result, item[0], item[1] + " - using custom accumulator"); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/return-abrupt-from-this-out-of-bounds.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/return-abrupt-from-this-out-of-bounds.js new file mode 100644 index 0000000000..0564ee92a9 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/return-abrupt-from-this-out-of-bounds.js @@ -0,0 +1,62 @@ +// |reftest| skip -- resizable-arraybuffer is not supported +// Copyright (C) 2021 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-%typedarray%.prototype.reduce +description: Return abrupt when "this" value fails buffer boundary checks +includes: [testBigIntTypedArray.js] +features: [ArrayBuffer, BigInt, TypedArray, arrow-function, resizable-arraybuffer] +---*/ + +assert.sameValue( + typeof TypedArray.prototype.reduce, + 'function', + 'implements TypedArray.prototype.reduce' +); + +assert.sameValue( + typeof ArrayBuffer.prototype.resize, + 'function', + 'implements ArrayBuffer.prototype.resize' +); + +testWithBigIntTypedArrayConstructors(TA => { + var BPE = TA.BYTES_PER_ELEMENT; + var ab = new ArrayBuffer(BPE * 4, {maxByteLength: BPE * 5}); + var array = new TA(ab, BPE, 2); + + try { + ab.resize(BPE * 5); + } catch (_) {} + + // no error following grow: + array.reduce(() => {}); + + try { + ab.resize(BPE * 3); + } catch (_) {} + + // no error following shrink (within bounds): + array.reduce(() => {}); + + var expectedError; + try { + ab.resize(BPE * 2); + // If the preceding "resize" operation is successful, the typed array will + // be out out of bounds, so the subsequent prototype method should produce + // a TypeError due to the semantics of ValidateTypedArray. + expectedError = TypeError; + } catch (_) { + // The host is permitted to fail any "resize" operation at its own + // discretion. If that occurs, the reduce operation should complete + // successfully. + expectedError = Test262Error; + } + + assert.throws(expectedError, () => { + array.reduce(() => {}); + throw new Test262Error('reduce completed successfully'); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/return-first-value-without-callbackfn.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/return-first-value-without-callbackfn.js new file mode 100644 index 0000000000..7fe9a17a29 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/return-first-value-without-callbackfn.js @@ -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.reduce +description: > + Returns [0] without calling callbackfn if length is 1 and initialValue is not + present. +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 7. Else initialValue is not present, + ... + b. Repeat, while kPresent is false and k < len + ... + iii. If kPresent is true, then + 1. Let accumulator be ? Get(O, Pk). + iv. Increase k by 1. + ... + 8. Repeat, while k < len + ... + 9. Return accumulator. +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var called = false; + var result = new TA([42n]).reduce(function() { + called = true; + }); + + assert.sameValue(result, 42n); + assert.sameValue(called, false); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/shell.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/shell.js new file mode 100644 index 0000000000..90ee9c114d --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/shell.js @@ -0,0 +1,42 @@ +// GENERATED, DO NOT EDIT +// file: testBigIntTypedArray.js +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +description: | + Collection of functions used to assert the correctness of BigInt TypedArray objects. +defines: + - TypedArray + - testWithBigIntTypedArrayConstructors +---*/ + +/** + * The %TypedArray% intrinsic constructor function. + */ +var TypedArray = Object.getPrototypeOf(Int8Array); + +/** + * Calls the provided function for every typed array constructor. + * + * @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor. + * @param {Array} selected - An optional Array with filtered typed arrays + */ +function testWithBigIntTypedArrayConstructors(f, selected) { + /** + * Array containing every BigInt typed array constructor. + */ + var constructors = selected || [ + BigInt64Array, + BigUint64Array + ]; + + for (var i = 0; i < constructors.length; ++i) { + var constructor = constructors[i]; + try { + f(constructor); + } catch (e) { + e.message += " (Testing with " + constructor.name + ".)"; + throw e; + } + } +} diff --git a/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/values-are-not-cached.js b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/values-are-not-cached.js new file mode 100644 index 0000000000..919b728856 --- /dev/null +++ b/js/src/tests/test262/built-ins/TypedArray/prototype/reduce/BigInt/values-are-not-cached.js @@ -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.reduce +description: > + Integer indexed values are not cached before iteration +info: | + 22.2.3.20 %TypedArray%.prototype.reduce ( callbackfn [ , initialValue ] ) + + %TypedArray%.prototype.reduce is a distinct function that implements the same + algorithm as Array.prototype.reduce as defined in 22.1.3.19 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.19 Array.prototype.reduce ( callbackfn [ , initialValue ] ) + + ... + 8. Repeat, while k < len + ... + c. If kPresent is true, then + i. Let kValue be ? Get(O, Pk). + ii. Let accumulator be ? Call(callbackfn, undefined, « accumulator, kValue, + k, O »). + ... +includes: [testBigIntTypedArray.js] +features: [BigInt, TypedArray] +---*/ + +testWithBigIntTypedArrayConstructors(function(TA) { + var sample = new TA([42n, 43n, 44n]); + + sample.reduce(function(a, v, i) { + if (i < sample.length - 1) { + sample[i+1] = 42n; + } + + assert.sameValue( + v, 42n, "method does not cache values before callbackfn calls" + ); + }, 0); +}); + +reportCompare(0, 0); |