Add tests: resizing ArrayBuffer during iteration

The Resizable ArrayBuffer proposal allows implementations to reject any
resize operation, so the tests must accommodate that possibility.

Mitigate the complexity this entails by minimizing branches and by
deferring assertions to locations with shallow call stacks.
This commit is contained in:
Mike Pennisi 2021-10-21 22:14:41 -04:00 committed by Rick Waldron
parent 19da3ca075
commit eb153de85b
22 changed files with 1626 additions and 0 deletions

View File

@ -0,0 +1,73 @@
// 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-array.prototype.every
description: TypedArray instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = Array.prototype.every.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return true;
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, true, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = Array.prototype.every.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return true;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, true, 'result (grow)');
});

View File

@ -0,0 +1,76 @@
// 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-array.prototype.filter
description: TypedArray instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var NaNvalue = (TA === Float32Array || TA === Float64Array) ? NaN : 0;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, finalResult, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = Array.prototype.filter.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
finalResult = NaNvalue;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
finalResult = 0;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return true;
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.compareArray(result, [0, 0, finalResult], 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = Array.prototype.filter.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return true;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.compareArray(result, expectedElements, 'result (grow)');
});

View File

@ -0,0 +1,73 @@
// 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-array.prototype.find
description: TypedArray instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = Array.prototype.find.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, undefined, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = Array.prototype.find.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, undefined, 'result (grow)');
});

View File

@ -0,0 +1,73 @@
// 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-array.prototype.findindex
description: TypedArray instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = Array.prototype.findIndex.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, -1, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = Array.prototype.findIndex.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, -1, 'result (grow)');
});

View File

@ -0,0 +1,73 @@
// 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-array.prototype.findlast
description: TypedArray instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var secondElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = Array.prototype.findLast.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(BPE);
secondElement = undefined;
expectedElements = [0];
expectedIndices = [0];
expectedArrays = [sample];
} catch (_) {
secondElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [2, 1, 0];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, [0, secondElement, 0], 'elements (shrink)');
assert.compareArray(indices, [2, 1, 0], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, undefined, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = Array.prototype.findLast.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, undefined, 'result (grow)');
});

View File

@ -0,0 +1,73 @@
// 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-array.prototype.findlastindex
description: TypedArray instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var secondElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = Array.prototype.findLastIndex.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(BPE);
secondElement = undefined;
expectedElements = [0];
expectedIndices = [0];
expectedArrays = [sample];
} catch (_) {
secondElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [2, 1, 0];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, [0, secondElement, 0], 'elements (shrink)');
assert.compareArray(indices, [2, 1, 0], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, -1, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = Array.prototype.findLastIndex.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, -1, 'result (grow)');
});

View File

@ -0,0 +1,71 @@
// 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-array.prototype.forEach
description: TypedArray instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = Array.prototype.forEach.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, undefined, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = Array.prototype.forEach.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, undefined, 'result (grow)');
});

View File

@ -0,0 +1,78 @@
// 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-array.prototype.map
description: TypedArray instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var NaNvalue = (TA === Float32Array || TA === Float64Array) ? NaN : 0;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, finalResult, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = Array.prototype.map.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
finalResult = NaNvalue;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
finalResult = 2;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return index;
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.compareArray(result, [0, 1, finalResult], 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = Array.prototype.map.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return index;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.compareArray(result, expectedIndices, 'result (grow)');
});

View File

@ -0,0 +1,75 @@
// 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-array.prototype.reduce
description: TypedArray instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 3});
var sample = new TA(buffer);
var finalNext, expectedPrevs, expectedNexts, expectedIndices, expectedArrays;
var prevs, nexts, indices, arrays, result;
prevs = [];
nexts = [];
indices = [];
arrays = [];
result = Array.prototype.reduce.call(sample, function(prev, next, index, array) {
if (prevs.length === 0) {
try {
buffer.resize(2 * BPE);
finalNext = undefined;
expectedPrevs = [262, 0];
expectedNexts = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalNext = 0;
expectedPrevs = [262, 0, 1];
expectedNexts = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
prevs.push(prev);
nexts.push(next);
indices.push(index);
arrays.push(array);
return index;
}, 262);
assert.compareArray(prevs, [262, 0, 1], 'prevs (shrink)');
assert.compareArray(nexts, [0, 0, finalNext], 'nexts (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, 2, 'result (shrink)');
prevs = [];
nexts = [];
indices = [];
arrays = [];
result = Array.prototype.reduce.call(sample, function(prev, next, index, array) {
if (prevs.length === 0) {
try {
buffer.resize(3 * BPE);
} catch (_) {}
}
prevs.push(prev);
nexts.push(next);
indices.push(index);
arrays.push(array);
return index;
}, 262);
assert.compareArray(prevs, expectedPrevs, 'prevs (grow)');
assert.compareArray(nexts, expectedNexts, 'nexts (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, expectedIndices[expectedIndices.length - 1], 'result (grow)');
});

View File

@ -0,0 +1,75 @@
// 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-array.prototype.reduceright
description: TypedArray instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 3});
var sample = new TA(buffer);
var secondNext, expectedPrevs, expectedNexts, expectedIndices, expectedArrays;
var prevs, nexts, indices, arrays, result;
prevs = [];
nexts = [];
indices = [];
arrays = [];
result = Array.prototype.reduceRight.call(sample, function(prev, next, index, array) {
if (prevs.length === 0) {
try {
buffer.resize(BPE);
secondNext = undefined;
expectedPrevs = [262];
expectedNexts = [0];
expectedIndices = [0];
expectedArrays = [sample];
} catch (_) {
secondNext = 0;
expectedPrevs = [262, 2, 1];
expectedNexts = [0, 0, 0];
expectedIndices = [2, 1, 0];
expectedArrays = [sample, sample, sample];
}
}
prevs.push(prev);
nexts.push(next);
indices.push(index);
arrays.push(array);
return index;
}, 262);
assert.compareArray(prevs, [262, 2, 1], 'prevs (shrink)');
assert.compareArray(nexts, [0, secondNext, 0], 'nexts (shrink)');
assert.compareArray(indices, [2, 1, 0], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, 0, 'result (shrink)');
prevs = [];
nexts = [];
indices = [];
arrays = [];
result = Array.prototype.reduceRight.call(sample, function(prev, next, index, array) {
if (prevs.length === 0) {
try {
buffer.resize(3 * BPE);
} catch (_) {}
}
prevs.push(prev);
nexts.push(next);
indices.push(index);
arrays.push(array);
return index;
}, 262);
assert.compareArray(prevs, expectedPrevs, 'prevs (grow)');
assert.compareArray(nexts, expectedNexts, 'nexts (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, expectedIndices[expectedIndices.length - 1], 'result (grow)');
});

View File

@ -0,0 +1,73 @@
// 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-array.prototype.some
description: TypedArray instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = Array.prototype.some.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, false, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = Array.prototype.some.call(sample, function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, false, 'result (grow)');
});

View File

@ -0,0 +1,73 @@
// 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.every
description: Instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = sample.every(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return true;
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, true, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = sample.every(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return true;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, true, 'result (grow)');
});

View File

@ -0,0 +1,76 @@
// 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.filter
description: Instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var NaNvalue = (TA === Float32Array || TA === Float64Array) ? NaN : 0;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, finalResult, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = sample.filter(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
finalResult = NaNvalue;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
finalResult = 0;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return true;
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.compareArray(result, [0, 0, finalResult], 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = sample.filter(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return true;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.compareArray(result, expectedElements, 'result (grow)');
});

View File

@ -0,0 +1,73 @@
// 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.find
description: Instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = sample.find(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, undefined, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = sample.find(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, undefined, 'result (grow)');
});

View File

@ -0,0 +1,73 @@
// 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.findindex
description: Instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = sample.findIndex(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, -1, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = sample.findIndex(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, -1, 'result (grow)');
});

View File

@ -0,0 +1,73 @@
// 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.findlast
description: Instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var secondElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = sample.findLast(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(BPE);
secondElement = undefined;
expectedElements = [0];
expectedIndices = [0];
expectedArrays = [sample];
} catch (_) {
secondElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [2, 1, 0];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, [0, secondElement, 0], 'elements (shrink)');
assert.compareArray(indices, [2, 1, 0], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, undefined, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = sample.findLast(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, undefined, 'result (grow)');
});

View File

@ -0,0 +1,73 @@
// 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.findlastindex
description: Instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var secondElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = sample.findLastIndex(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(BPE);
secondElement = undefined;
expectedElements = [0];
expectedIndices = [0];
expectedArrays = [sample];
} catch (_) {
secondElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [2, 1, 0];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, [0, secondElement, 0], 'elements (shrink)');
assert.compareArray(indices, [2, 1, 0], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, -1, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = sample.findLastIndex(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, -1, 'result (grow)');
});

View File

@ -0,0 +1,71 @@
// 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.forEach
description: Instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = sample.forEach(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, undefined, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = sample.forEach(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, undefined, 'result (grow)');
});

View File

@ -0,0 +1,78 @@
// 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.map
description: Instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var NaNvalue = (TA === Float32Array || TA === Float64Array) ? NaN : 0;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, finalResult, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = sample.map(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
finalResult = NaNvalue;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
finalResult = 2;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return index;
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.compareArray(result, [0, 1, finalResult], 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = sample.map(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return index;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.compareArray(result, expectedIndices, 'result (grow)');
});

View File

@ -0,0 +1,75 @@
// 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: Instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 3});
var sample = new TA(buffer);
var finalNext, expectedPrevs, expectedNexts, expectedIndices, expectedArrays;
var prevs, nexts, indices, arrays, result;
prevs = [];
nexts = [];
indices = [];
arrays = [];
result = sample.reduce(function(prev, next, index, array) {
if (prevs.length === 0) {
try {
buffer.resize(2 * BPE);
finalNext = undefined;
expectedPrevs = [262, 0];
expectedNexts = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalNext = 0;
expectedPrevs = [262, 0, 1];
expectedNexts = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
prevs.push(prev);
nexts.push(next);
indices.push(index);
arrays.push(array);
return index;
}, 262);
assert.compareArray(prevs, [262, 0, 1], 'prevs (shrink)');
assert.compareArray(nexts, [0, 0, finalNext], 'nexts (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, 2, 'result (shrink)');
prevs = [];
nexts = [];
indices = [];
arrays = [];
result = sample.reduce(function(prev, next, index, array) {
if (prevs.length === 0) {
try {
buffer.resize(3 * BPE);
} catch (_) {}
}
prevs.push(prev);
nexts.push(next);
indices.push(index);
arrays.push(array);
return index;
}, 262);
assert.compareArray(prevs, expectedPrevs, 'prevs (grow)');
assert.compareArray(nexts, expectedNexts, 'nexts (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, expectedIndices[expectedIndices.length - 1], 'result (grow)');
});

View File

@ -0,0 +1,75 @@
// 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.reduceright
description: Instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 3});
var sample = new TA(buffer);
var secondNext, expectedPrevs, expectedNexts, expectedIndices, expectedArrays;
var prevs, nexts, indices, arrays, result;
prevs = [];
nexts = [];
indices = [];
arrays = [];
result = sample.reduceRight(function(prev, next, index, array) {
if (prevs.length === 0) {
try {
buffer.resize(BPE);
secondNext = undefined;
expectedPrevs = [262];
expectedNexts = [0];
expectedIndices = [0];
expectedArrays = [sample];
} catch (_) {
secondNext = 0;
expectedPrevs = [262, 2, 1];
expectedNexts = [0, 0, 0];
expectedIndices = [2, 1, 0];
expectedArrays = [sample, sample, sample];
}
}
prevs.push(prev);
nexts.push(next);
indices.push(index);
arrays.push(array);
return index;
}, 262);
assert.compareArray(prevs, [262, 2, 1], 'prevs (shrink)');
assert.compareArray(nexts, [0, secondNext, 0], 'nexts (shrink)');
assert.compareArray(indices, [2, 1, 0], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, 0, 'result (shrink)');
prevs = [];
nexts = [];
indices = [];
arrays = [];
result = sample.reduceRight(function(prev, next, index, array) {
if (prevs.length === 0) {
try {
buffer.resize(3 * BPE);
} catch (_) {}
}
prevs.push(prev);
nexts.push(next);
indices.push(index);
arrays.push(array);
return index;
}, 262);
assert.compareArray(prevs, expectedPrevs, 'prevs (grow)');
assert.compareArray(nexts, expectedNexts, 'nexts (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, expectedIndices[expectedIndices.length - 1], 'result (grow)');
});

View File

@ -0,0 +1,73 @@
// 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.some
description: Instance buffer can be resized during iteration
includes: [testTypedArray.js, compareArray.js]
features: [TypedArray, resizable-arraybuffer]
---*/
// If the host chooses to throw as allowed by the specification, the observed
// behavior will be identical to the case where `ArrayBuffer.prototype.resize`
// has not been implemented. The following assertion prevents this test from
// passing in runtimes which have not implemented the method.
assert.sameValue(typeof ArrayBuffer.prototype.resize, 'function');
testWithTypedArrayConstructors(function(TA) {
var BPE = TA.BYTES_PER_ELEMENT;
var buffer = new ArrayBuffer(BPE * 3, {maxByteLength: BPE * 4});
var sample = new TA(buffer);
var finalElement, expectedElements, expectedIndices, expectedArrays;
var elements, indices, arrays, result;
elements = [];
indices = [];
arrays = [];
result = sample.some(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(2 * BPE);
finalElement = undefined;
expectedElements = [0, 0];
expectedIndices = [0, 1];
expectedArrays = [sample, sample];
} catch (_) {
finalElement = 0;
expectedElements = [0, 0, 0];
expectedIndices = [0, 1, 2];
expectedArrays = [sample, sample, sample];
}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, [0, 0, finalElement], 'elements (shrink)');
assert.compareArray(indices, [0, 1, 2], 'indices (shrink)');
assert.compareArray(arrays, [sample, sample, sample], 'arrays (shrink)');
assert.sameValue(result, false, 'result (shrink)');
elements = [];
indices = [];
arrays = [];
result = sample.some(function(element, index, array) {
if (elements.length === 0) {
try {
buffer.resize(4 * BPE);
} catch (_) {}
}
elements.push(element);
indices.push(index);
arrays.push(array);
return false;
});
assert.compareArray(elements, expectedElements, 'elements (grow)');
assert.compareArray(indices, expectedIndices, 'indices (grow)');
assert.compareArray(arrays, expectedArrays, 'arrays (grow)');
assert.sameValue(result, false, 'result (grow)');
});