From 800300e12043ae549f26b823dc8565ce5c8243a3 Mon Sep 17 00:00:00 2001 From: Leonardo Balter Date: Wed, 27 Apr 2016 13:34:00 -0400 Subject: [PATCH] Add tests for TypedArrays reverse --- .../get-length-uses-internal-arraylength.js | 41 ++++++++++++++ .../preserves-non-numeric-properties.js | 35 ++++++++++++ .../reverse/returns-original-object.js | 38 +++++++++++++ .../TypedArray/prototype/reverse/reverts.js | 56 +++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 test/built-ins/TypedArray/prototype/reverse/get-length-uses-internal-arraylength.js create mode 100644 test/built-ins/TypedArray/prototype/reverse/preserves-non-numeric-properties.js create mode 100644 test/built-ins/TypedArray/prototype/reverse/returns-original-object.js create mode 100644 test/built-ins/TypedArray/prototype/reverse/reverts.js diff --git a/test/built-ins/TypedArray/prototype/reverse/get-length-uses-internal-arraylength.js b/test/built-ins/TypedArray/prototype/reverse/get-length-uses-internal-arraylength.js new file mode 100644 index 0000000000..63d793023b --- /dev/null +++ b/test/built-ins/TypedArray/prototype/reverse/get-length-uses-internal-arraylength.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.reverse +description: Get "length" uses internal ArrayLength +info: > + 22.2.3.22 %TypedArray%.prototype.reverse ( ) + + %TypedArray%.prototype.reverse is a distinct function that implements the same + algorithm as Array.prototype.reverse as defined in 22.1.3.21 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.21 Array.prototype.reverse ( ) + + 1. Let O be ? ToObject(this value). + 2. Let len be ? ToLength(? Get(O, "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.reverse(); + + assert.sameValue(getCalls, 0, "ignores length properties"); +}); diff --git a/test/built-ins/TypedArray/prototype/reverse/preserves-non-numeric-properties.js b/test/built-ins/TypedArray/prototype/reverse/preserves-non-numeric-properties.js new file mode 100644 index 0000000000..6097089f76 --- /dev/null +++ b/test/built-ins/TypedArray/prototype/reverse/preserves-non-numeric-properties.js @@ -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.reverse +description: Preserves non numeric properties +info: > + 22.2.3.22 %TypedArray%.prototype.reverse ( ) + + %TypedArray%.prototype.reverse is a distinct function that implements the same + algorithm as Array.prototype.reverse as defined in 22.1.3.21 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.21 Array.prototype.reverse ( ) + + ... + 6. Return O. +includes: [testTypedArray.js] +features: [Symbol] +---*/ + +var s = Symbol("1"); + +testWithTypedArrayConstructors(function(TA) { + var sample, result; + + sample = new TA(2); + sample.foo = 42; + sample.bar = "bar"; + sample[s] = 1; + result = sample.reverse(); + assert.sameValue(result.foo, 42, "sample.foo === 42"); + assert.sameValue(result.bar, "bar", "sample.bar === 'bar'"); + assert.sameValue(result[s], 1, "sample[s] === 1"); +}); diff --git a/test/built-ins/TypedArray/prototype/reverse/returns-original-object.js b/test/built-ins/TypedArray/prototype/reverse/returns-original-object.js new file mode 100644 index 0000000000..e432210dba --- /dev/null +++ b/test/built-ins/TypedArray/prototype/reverse/returns-original-object.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.reverse +description: Returns the same object +info: > + 22.2.3.22 %TypedArray%.prototype.reverse ( ) + + %TypedArray%.prototype.reverse is a distinct function that implements the same + algorithm as Array.prototype.reverse as defined in 22.1.3.21 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.21 Array.prototype.reverse ( ) + + ... + 6. Return O. +includes: [testTypedArray.js] +---*/ + +var buffer = new ArrayBuffer(64); + +testWithTypedArrayConstructors(function(TA) { + var sample, result, expectedLength; + + sample = new TA(buffer, 0); + expectedLength = sample.length; + result = sample.reverse(); + assert.sameValue(result, sample, "returns the same object"); + assert.sameValue(sample.buffer, buffer, "keeps the same buffer"); + assert.sameValue(sample.length, expectedLength, "length is preserved"); + + sample = new TA(buffer, 0, 0); + result = sample.reverse(); + assert.sameValue(result, sample, "returns the same object (empty instance)"); + assert.sameValue(sample.buffer, buffer, "keeps the same buffer (empty instance)"); + assert.sameValue(sample.length, 0, "length is preserved (empty instance)"); +}); diff --git a/test/built-ins/TypedArray/prototype/reverse/reverts.js b/test/built-ins/TypedArray/prototype/reverse/reverts.js new file mode 100644 index 0000000000..a34baabeaf --- /dev/null +++ b/test/built-ins/TypedArray/prototype/reverse/reverts.js @@ -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.reverse +description: Reverts values +info: > + 22.2.3.22 %TypedArray%.prototype.reverse ( ) + + %TypedArray%.prototype.reverse is a distinct function that implements the same + algorithm as Array.prototype.reverse as defined in 22.1.3.21 except that the + this object's [[ArrayLength]] internal slot is accessed in place of performing + a [[Get]] of "length". + + 22.1.3.21 Array.prototype.reverse ( ) + + ... + 6. Return O. +includes: [testTypedArray.js, compareArray.js] +---*/ + +var buffer = new ArrayBuffer(64); + +testWithTypedArrayConstructors(function(TA) { + var sample = new TA(buffer, 0, 4); + var other = new TA(buffer, 0, 5); + + sample[0] = 42; + sample[1] = 43; + sample[2] = 2; + sample[3] = 1; + other[4] = 7; + + sample.reverse(); + assert( + compareArray(sample, [1, 2, 43, 42]) + ); + + assert( + compareArray(other, [1, 2, 43, 42, 7]) + ); + + sample[0] = 7; + sample[1] = 17; + sample[2] = 1; + sample[3] = 0; + other[4] = 42; + + other.reverse(); + assert( + compareArray(other, [42, 0, 1, 17, 7]) + ); + + assert( + compareArray(sample, [42, 0, 1, 17]) + ); +});