Skip to content
Snippets Groups Projects
Commit 6fcd24cd authored by André Bargull's avatar André Bargull
Browse files

Add tests for ArrayBuffer and ArrayBuffer.prototype.slice

parent fd44cd73
No related branches found
No related tags found
No related merge requests found
Showing
with 578 additions and 0 deletions
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.2.1
description: >
Throws a RangeError if requested Data Block is too large.
info: >
ArrayBuffer( length )
...
6. Return AllocateArrayBuffer(NewTarget, byteLength).
6.2.6.1 CreateByteDataBlock(size)
...
2. Let db be a new Data Block value consisting of size bytes. If it is
impossible to create such a Data Block, throw a RangeError exception.
...
---*/
assert.throws(RangeError, function() {
// Allocating 7 PiB should fail with a RangeError.
// Math.pow(1024, 5) = 1125899906842624
new ArrayBuffer(7 * 1125899906842624);
}, "`length` parameter is 7 PiB");
assert.throws(RangeError, function() {
// Allocating almost 8 PiB should fail with a RangeError.
// Math.pow(2, 53) = 9007199254740992
new ArrayBuffer(9007199254740992 - 1);
}, "`length` parameter is Math.pow(2, 53) - 1");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.2.1
description: >
The new ArrayBuffer instance is created prior to allocating the Data Block.
info: >
ArrayBuffer( length )
...
6. Return AllocateArrayBuffer(NewTarget, byteLength).
AllocateArrayBuffer( constructor, byteLength )
1. Let obj be OrdinaryCreateFromConstructor(constructor, "%ArrayBufferPrototype%",
«[[ArrayBufferData]], [[ArrayBufferByteLength]]» ).
2. ReturnIfAbrupt(obj).
...
4. Let block be CreateByteDataBlock(byteLength).
5. ReturnIfAbrupt(block).
...
features: [Reflect.construct]
---*/
function DummyError() { }
var newTarget = function(){}.bind(null);
Object.defineProperty(newTarget, "prototype", {
get: function() {
throw new DummyError();
}
});
assert.throws(DummyError, function() {
// Allocating 7 PiB should fail with a RangeError.
// Math.pow(1024, 5) = 1125899906842624
Reflect.construct(ArrayBuffer, [7 * 1125899906842624], newTarget);
});
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.2.1
description: >
The `length` parameter must be a positive, numeric integral value.
info: >
ArrayBuffer( length )
...
2. Let numberLength be ToNumber(length).
3. Let byteLength be ToLength(numberLength).
4. ReturnIfAbrupt(byteLength).
5. If SameValueZero(numberLength, byteLength) is false, throw a RangeError exception.
...
---*/
assert.throws(RangeError, function() {
new ArrayBuffer();
}, "`length` parameter absent");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.2.1
description: >
The `length` parameter is converted to a number value.
info: >
ArrayBuffer( length )
...
2. Let numberLength be ToNumber(length).
3. Let byteLength be ToLength(numberLength).
4. ReturnIfAbrupt(byteLength).
5. If SameValueZero(numberLength, byteLength) is false, throw a RangeError exception.
...
features: [Symbol]
---*/
assert.throws(RangeError, function() {
new ArrayBuffer(undefined);
}, "`length` parameter is undefined");
var result = new ArrayBuffer(null);
assert.sameValue(result.byteLength, 0, "`length` parameter is null");
var result = new ArrayBuffer(true);
assert.sameValue(result.byteLength, 1, "`length` parameter is a Boolean");
var result = new ArrayBuffer("");
assert.sameValue(result.byteLength, 0, "`length` parameter is a String");
assert.throws(TypeError, function() {
new ArrayBuffer(Symbol());
}, "`length` parameter is a Symbol");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.2.1
description: >
[[Prototype]] defaults to %ArrayBufferPrototype% if NewTarget.prototype is not an object.
info: >
ArrayBuffer( length )
ArrayBuffer called with argument length performs the following steps:
...
6. Return AllocateArrayBuffer(NewTarget, byteLength).
AllocateArrayBuffer( constructor, byteLength )
1. Let obj be OrdinaryCreateFromConstructor(constructor, "%ArrayBufferPrototype%",
«[[ArrayBufferData]], [[ArrayBufferByteLength]]» ).
2. ReturnIfAbrupt(obj).
...
features: [Reflect.construct, Symbol]
---*/
function newTarget() { }
newTarget.prototype = undefined;
var arrayBuffer = Reflect.construct(ArrayBuffer, [1], newTarget);
assert.sameValue(Object.getPrototypeOf(arrayBuffer), ArrayBuffer.prototype, "newTarget.prototype is undefined");
newTarget.prototype = null;
var arrayBuffer = Reflect.construct(ArrayBuffer, [2], newTarget);
assert.sameValue(Object.getPrototypeOf(arrayBuffer), ArrayBuffer.prototype, "newTarget.prototype is null");
newTarget.prototype = true;
var arrayBuffer = Reflect.construct(ArrayBuffer, [3], newTarget);
assert.sameValue(Object.getPrototypeOf(arrayBuffer), ArrayBuffer.prototype, "newTarget.prototype is a Boolean");
newTarget.prototype = "";
var arrayBuffer = Reflect.construct(ArrayBuffer, [4], newTarget);
assert.sameValue(Object.getPrototypeOf(arrayBuffer), ArrayBuffer.prototype, "newTarget.prototype is a String");
newTarget.prototype = Symbol();
var arrayBuffer = Reflect.construct(ArrayBuffer, [5], newTarget);
assert.sameValue(Object.getPrototypeOf(arrayBuffer), ArrayBuffer.prototype, "newTarget.prototype is a Symbol");
newTarget.prototype = 1;
var arrayBuffer = Reflect.construct(ArrayBuffer, [6], newTarget);
assert.sameValue(Object.getPrototypeOf(arrayBuffer), ArrayBuffer.prototype, "newTarget.prototype is a Number");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.2.1
description: >
The `length` parameter is converted to a number value.
info: >
ArrayBuffer( length )
...
2. Let numberLength be ToNumber(length).
...
---*/
var log = "";
var lengthValue = {
valueOf: function() {
log += "ok";
return 10;
}
};
var arrayBuffer = new ArrayBuffer(lengthValue);
assert.sameValue(log, "ok");
assert.sameValue(arrayBuffer.byteLength, 10);
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.2.1
description: >
The `length` parameter must be a positive, numeric integral value.
info: >
ArrayBuffer( length )
...
2. Let numberLength be ToNumber(length).
3. Let byteLength be ToLength(numberLength).
4. ReturnIfAbrupt(byteLength).
5. If SameValueZero(numberLength, byteLength) is false, throw a RangeError exception.
...
---*/
assert.throws(RangeError, function() {
new ArrayBuffer(-10);
}, "`length` parameter is negative");
assert.throws(RangeError, function() {
new ArrayBuffer(3.8);
}, "`length` parameter is not integral value");
assert.throws(RangeError, function() {
// Math.pow(2, 53) = 9007199254740992
new ArrayBuffer(9007199254740992);
}, "`length` parameter is too large");
assert.throws(RangeError, function() {
new ArrayBuffer(+Infinity);
}, "`length` parameter is positive Infinity");
assert.throws(RangeError, function() {
new ArrayBuffer(-Infinity);
}, "`length` parameter is negative Infinity");
assert.throws(RangeError, function() {
new ArrayBuffer(NaN);
}, "`length` parameter is NaN");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.2.1
description: >
The [[Prototype]] internal slot is computed from NewTarget.
info: >
ArrayBuffer( length )
ArrayBuffer called with argument length performs the following steps:
...
6. Return AllocateArrayBuffer(NewTarget, byteLength).
AllocateArrayBuffer( constructor, byteLength )
1. Let obj be OrdinaryCreateFromConstructor(constructor, "%ArrayBufferPrototype%",
«[[ArrayBufferData]], [[ArrayBufferByteLength]]» ).
2. ReturnIfAbrupt(obj).
...
features: [Reflect.construct]
---*/
var arrayBuffer = Reflect.construct(ArrayBuffer, [8], Object);
assert.sameValue(Object.getPrototypeOf(arrayBuffer), Object.prototype, "NewTarget is built-in Object constructor");
var newTarget = function(){}.bind(null);
Object.defineProperty(newTarget, "prototype", {
get: function() {
return Array.prototype;
}
});
var arrayBuffer = Reflect.construct(ArrayBuffer, [16], newTarget);
assert.sameValue(Object.getPrototypeOf(arrayBuffer), Array.prototype, "NewTarget is BoundFunction with accessor");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.4.2
description: >
The `ArrayBuffer.prototype.constructor` property descriptor.
info: >
The initial value of ArrayBuffer.prototype.constructor is the intrinsic
object %ArrayBuffer%.
17 ECMAScript Standard Built-in Objects:
Every other data property described in clauses 18 through 26 and in
Annex B.2 has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
---*/
assert.sameValue(ArrayBuffer.prototype.constructor, ArrayBuffer);
verifyNotEnumerable(ArrayBuffer.prototype, "constructor");
verifyWritable(ArrayBuffer.prototype, "constructor");
verifyConfigurable(ArrayBuffer.prototype, "constructor");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.4.3
description: >
Throws a TypeError if `this` does not have an [[ArrayBufferData]] internal slot.
info: >
ArrayBuffer.prototype.slice ( start, end )
1. Let O be the this value.
2. If Type(O) is not Object, throw a TypeError exception.
3. If O does not have an [[ArrayBufferData]] internal slot, throw a TypeError exception.
...
---*/
assert.throws(TypeError, function() {
ArrayBuffer.prototype.slice.call({});
}, "`this` value is Object");
assert.throws(TypeError, function() {
ArrayBuffer.prototype.slice.call([]);
}, "`this` value is Array");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.4.3
description: >
Throws a TypeError if `this` is not an Object.
info: >
ArrayBuffer.prototype.slice ( start, end )
1. Let O be the this value.
2. If Type(O) is not Object, throw a TypeError exception.
...
features: [Symbol]
---*/
assert.throws(TypeError, function() {
ArrayBuffer.prototype.slice.call(undefined);
}, "`this` value is undefined");
assert.throws(TypeError, function() {
ArrayBuffer.prototype.slice.call(null);
}, "`this` value is null");
assert.throws(TypeError, function() {
ArrayBuffer.prototype.slice.call(true);
}, "`this` value is Boolean");
assert.throws(TypeError, function() {
ArrayBuffer.prototype.slice.call("");
}, "`this` value is String");
assert.throws(TypeError, function() {
ArrayBuffer.prototype.slice.call(Symbol());
}, "`this` value is Symbol");
assert.throws(TypeError, function() {
ArrayBuffer.prototype.slice.call(1);
}, "`this` value is Number");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.4.3
description: >
ArrayBuffer.prototype.slice has default data property attributes.
info: >
ArrayBuffer.prototype.slice ( start, end )
17 ECMAScript Standard Built-in Objects:
Every other data property described in clauses 18 through 26 and in
Annex B.2 has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
---*/
verifyNotEnumerable(ArrayBuffer.prototype, "slice");
verifyWritable(ArrayBuffer.prototype, "slice");
verifyConfigurable(ArrayBuffer.prototype, "slice");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.4.3
description: >
The `end` index defaults to [[ArrayBufferByteLength]] if absent.
info: >
ArrayBuffer.prototype.slice ( start, end )
...
9. If end is undefined, let relativeEnd be len; else let relativeEnd be ToInteger(end).
10. ReturnIfAbrupt(relativeEnd).
...
---*/
var arrayBuffer = new ArrayBuffer(8);
var start = 6;
var result = arrayBuffer.slice(start);
assert.sameValue(result.byteLength, 2);
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.4.3
description: >
The `end` index defaults to [[ArrayBufferByteLength]] if undefined.
info: >
ArrayBuffer.prototype.slice ( start, end )
...
9. If end is undefined, let relativeEnd be len; else let relativeEnd be ToInteger(end).
10. ReturnIfAbrupt(relativeEnd).
...
---*/
var arrayBuffer = new ArrayBuffer(8);
var start = 6, end = undefined;
var result = arrayBuffer.slice(start, end);
assert.sameValue(result.byteLength, 2);
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.4.3
description: >
Large `end` index is clamped to [[ArrayBufferByteLength]].
info: >
ArrayBuffer.prototype.slice ( start, end )
...
8. If relativeEnd < 0, let final be max((len + relativeEnd),0); else let final be min(relativeEnd, len).
...
---*/
var arrayBuffer = new ArrayBuffer(8);
var start = 1, end = 12;
var result = arrayBuffer.slice(start, end);
assert.sameValue(result.byteLength, 7, "slice(1, 12)");
var start = 2, end = 0x100000000;
var result = arrayBuffer.slice(start, end);
assert.sameValue(result.byteLength, 6, "slice(2, 0x100000000)");
var start = 3, end = +Infinity;
var result = arrayBuffer.slice(start, end);
assert.sameValue(result.byteLength, 5, "slice(3, Infinity)");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.4.3
description: >
ArrayBuffer.prototype.slice is extensible.
info: >
ArrayBuffer.prototype.slice ( start, end )
17 ECMAScript Standard Built-in Objects:
Unless specified otherwise, the [[Extensible]] internal slot
of a built-in object initially has the value true.
---*/
assert(Object.isExtensible(ArrayBuffer.prototype.slice));
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.4.3
description: >
ArrayBuffer.prototype.slice.length is 2.
info: >
ArrayBuffer.prototype.slice ( start, end )
17 ECMAScript Standard Built-in Objects:
Every built-in Function object, including constructors, has a length
property whose value is an integer. Unless otherwise specified, this
value is equal to the largest number of named arguments shown in the
subclause headings for the function description, including optional
parameters. However, rest parameters shown using the form “...name”
are not included in the default argument count.
Unless otherwise specified, the length property of a built-in Function
object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
includes: [propertyHelper.js]
---*/
assert.sameValue(ArrayBuffer.prototype.slice.length, 2);
verifyNotEnumerable(ArrayBuffer.prototype.slice, "length");
verifyNotWritable(ArrayBuffer.prototype.slice, "length");
verifyConfigurable(ArrayBuffer.prototype.slice, "length");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.4.3
description: >
ArrayBuffer.prototype.slice.name is "slice".
info: >
ArrayBuffer.prototype.slice ( start, end )
17 ECMAScript Standard Built-in Objects:
Every built-in Function object, including constructors, that is not
identified as an anonymous function has a name property whose value
is a String.
Unless otherwise specified, the name property of a built-in Function
object, if it exists, has the attributes { [[Writable]]: false,
[[Enumerable]]: false, [[Configurable]]: true }.
includes: [propertyHelper.js]
---*/
assert.sameValue(ArrayBuffer.prototype.slice.name, "slice");
verifyNotEnumerable(ArrayBuffer.prototype.slice, "name");
verifyNotWritable(ArrayBuffer.prototype.slice, "name");
verifyConfigurable(ArrayBuffer.prototype.slice, "name");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.4.3
description: >
Negative `end` index is relative to [[ArrayBufferByteLength]].
info: >
ArrayBuffer.prototype.slice ( start, end )
...
8. If relativeEnd < 0, let final be max((len + relativeEnd),0); else let final be min(relativeEnd, len).
...
---*/
var arrayBuffer = new ArrayBuffer(8);
var start = 2, end = -4;
var result = arrayBuffer.slice(start, end);
assert.sameValue(result.byteLength, 2, "slice(2, -4)");
var start = 2, end = -10;
var result = arrayBuffer.slice(start, end);
assert.sameValue(result.byteLength, 0, "slice(2, -10)");
var start = 2, end = -Infinity;
var result = arrayBuffer.slice(start, end);
assert.sameValue(result.byteLength, 0, "slice(2, -Infinity)");
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
es6id: 24.1.4.3
description: >
Negative `start` index is relative to [[ArrayBufferByteLength]].
info: >
ArrayBuffer.prototype.slice ( start, end )
...
8. If relativeStart < 0, let first be max((len + relativeStart),0); else let first be min(relativeStart, len).
...
---*/
var arrayBuffer = new ArrayBuffer(8);
var start = -5, end = 6;
var result = arrayBuffer.slice(start, end);
assert.sameValue(result.byteLength, 3, "slice(-5, 6)");
var start = -12, end = 6;
var result = arrayBuffer.slice(start, end);
assert.sameValue(result.byteLength, 6, "slice(-12, 6)");
var start = -Infinity, end = 6;
var result = arrayBuffer.slice(start, end);
assert.sameValue(result.byteLength, 6, "slice(-Infinity, 6)");
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment