Skip to content

Commit

Permalink
add Float16Array to all generic TypedArray tests (#3849)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot authored Jan 10, 2024
1 parent 211195b commit 67a5153
Show file tree
Hide file tree
Showing 60 changed files with 177 additions and 262 deletions.
61 changes: 48 additions & 13 deletions harness/testTypedArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@
description: |
Collection of functions used to assert the correctness of TypedArray objects.
defines:
- typedArrayConstructors
- floatArrayConstructors
- intArrayConstructors
- typedArrayConstructors
- TypedArray
- testWithTypedArrayConstructors
- nonAtomicsFriendlyTypedArrayConstructors
- testWithAtomicsFriendlyTypedArrayConstructors
- testWithNonAtomicsFriendlyTypedArrayConstructors
- testTypedArrayConversions
---*/

/**
* Array containing every typed array constructor.
*/
var typedArrayConstructors = [
var floatArrayConstructors = [
Float64Array,
Float32Array,
Float32Array
];

var intArrayConstructors = [
Int32Array,
Int16Array,
Int8Array,
Expand All @@ -29,8 +30,17 @@ var typedArrayConstructors = [
Uint8ClampedArray
];

var floatArrayConstructors = typedArrayConstructors.slice(0, 2);
var intArrayConstructors = typedArrayConstructors.slice(2, 7);
// Float16Array is a newer feature
// adding it to this list unconditionally would cause implementations lacking it to fail every test which uses it
if (typeof Float16Array !== 'undefined') {
floatArrayConstructors.push(Float16Array);
}

/**
* Array containing every non-bigint typed array constructor.
*/

var typedArrayConstructors = floatArrayConstructors.concat(intArrayConstructors);

/**
* The %TypedArray% intrinsic constructor function.
Expand Down Expand Up @@ -63,18 +73,15 @@ function testWithTypedArrayConstructors(f, selected) {
}
}

var nonAtomicsFriendlyTypedArrayConstructors = floatArrayConstructors.concat([Uint8ClampedArray]);
/**
* Calls the provided function for every non-"Atomics Friendly" 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 testWithNonAtomicsFriendlyTypedArrayConstructors(f) {
testWithTypedArrayConstructors(f, [
Float64Array,
Float32Array,
Uint8ClampedArray
]);
testWithTypedArrayConstructors(f, nonAtomicsFriendlyTypedArrayConstructors);
}

/**
Expand Down Expand Up @@ -120,3 +127,31 @@ function testTypedArrayConversions(byteConversionValues, fn) {
});
});
}

/**
* Checks if the given argument is one of the float-based TypedArray constructors.
*
* @param {constructor} ctor - the value to check
* @returns {boolean}
*/
function isFloatTypedArrayConstructor(arg) {
return floatArrayConstructors.indexOf(arg) !== -1;
}

/**
* Determines the precision of the given float-based TypedArray constructor.
*
* @param {constructor} ctor - the value to check
* @returns {string} "half", "single", or "double" for Float16Array, Float32Array, and Float64Array respectively.
*/
function floatTypedArrayConstructorPrecision(FA) {
if (typeof Float16Array !== "undefined" && FA === Float16Array) {
return "half";
} else if (FA === Float32Array) {
return "single";
} else if (FA === Float64Array) {
return "double";
} else {
throw new Error("Malformed test - floatTypedArrayConstructorPrecision called with non-float TypedArray");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function concatTypedArray(type, elems, modulo) {
assert.compareArray([].concat(ta), expected);
}

// Float16Array cannot be included in this because it cannot precisely represent integers above 2048
var max = [Math.pow(2, 8), Math.pow(2, 16), Math.pow(2, 32), false, false];
[
Uint8Array,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ function concatTypedArray(type, elems, modulo) {
assert.compareArray([].concat(ta), expected);
}
var max = [Math.pow(2, 8), Math.pow(2, 16), Math.pow(2, 32), false, false];
[
var TAs = [
Uint8Array,
Uint16Array,
Uint32Array,
Float32Array,
Float64Array
].forEach(function(ctor, i) {
];
if (typeof Float16Array !== 'undefined') {
max.push(false);
TAs.push(Float16Array);
}

TAs.forEach(function(ctor, i) {
concatTypedArray(ctor, 1, max[i]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ info: |
a. If typeName is not "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array",
or "Uint32Array", throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Atomics, TypedArray]
---*/

Expand All @@ -31,11 +32,7 @@ var index = {
}
};

var badArrayTypes = [
Uint8ClampedArray, Float32Array, Float64Array
];

for (var badArrayType of badArrayTypes) {
for (var badArrayType of nonAtomicsFriendlyTypedArrayConstructors) {
var typedArray = new badArrayType(new SharedArrayBuffer(8));
assert.throws(TypeError, function() {
Atomics.add(typedArray, index, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ info: |
a. If typeName is not "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array",
or "Uint32Array", throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Atomics, TypedArray]
---*/

Expand All @@ -31,11 +32,7 @@ var value = {
}
};

var badArrayTypes = [
Uint8ClampedArray, Float32Array, Float64Array
];

for (var badArrayType of badArrayTypes) {
for (var badArrayType of nonAtomicsFriendlyTypedArrayConstructors) {
var typedArray = new badArrayType(new SharedArrayBuffer(8));
assert.throws(TypeError, function() {
Atomics.add(typedArray, 0, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ info: |
a. If typeName is not "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array",
or "Uint32Array", throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Atomics, TypedArray]
---*/

Expand All @@ -31,11 +32,7 @@ var index = {
}
};

var badArrayTypes = [
Uint8ClampedArray, Float32Array, Float64Array
];

for (var badArrayType of badArrayTypes) {
for (var badArrayType of nonAtomicsFriendlyTypedArrayConstructors) {
var typedArray = new badArrayType(new SharedArrayBuffer(8));
assert.throws(TypeError, function() {
Atomics.and(typedArray, index, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ info: |
a. If typeName is not "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array",
or "Uint32Array", throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Atomics, TypedArray]
---*/

Expand All @@ -31,11 +32,7 @@ var value = {
}
};

var badArrayTypes = [
Uint8ClampedArray, Float32Array, Float64Array
];

for (var badArrayType of badArrayTypes) {
for (var badArrayType of nonAtomicsFriendlyTypedArrayConstructors) {
var typedArray = new badArrayType(new SharedArrayBuffer(8));
assert.throws(TypeError, function() {
Atomics.and(typedArray, 0, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ info: |
a. If typeName is not "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array",
or "Uint32Array", throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Atomics, TypedArray]
---*/

Expand All @@ -28,11 +29,7 @@ var expectedValue = {
}
};

var badArrayTypes = [
Uint8ClampedArray, Float32Array, Float64Array
];

for (var badArrayType of badArrayTypes) {
for (var badArrayType of nonAtomicsFriendlyTypedArrayConstructors) {
var typedArray = new badArrayType(new SharedArrayBuffer(8));
assert.throws(TypeError, function() {
Atomics.compareExchange(typedArray, 0, expectedValue, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ info: |
a. If typeName is not "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array",
or "Uint32Array", throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Atomics, TypedArray]
---*/

Expand All @@ -28,11 +29,7 @@ var index = {
}
};

var badArrayTypes = [
Uint8ClampedArray, Float32Array, Float64Array
];

for (var badArrayType of badArrayTypes) {
for (var badArrayType of nonAtomicsFriendlyTypedArrayConstructors) {
var typedArray = new badArrayType(new SharedArrayBuffer(8));
assert.throws(TypeError, function() {
Atomics.compareExchange(typedArray, index, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ info: |
a. If typeName is not "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array",
or "Uint32Array", throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Atomics, TypedArray]
---*/

Expand All @@ -28,11 +29,7 @@ var replacementValue = {
}
};

var badArrayTypes = [
Uint8ClampedArray, Float32Array, Float64Array
];

for (var badArrayType of badArrayTypes) {
for (var badArrayType of nonAtomicsFriendlyTypedArrayConstructors) {
var typedArray = new badArrayType(new SharedArrayBuffer(8));
assert.throws(TypeError, function() {
Atomics.compareExchange(typedArray, 0, 0, replacementValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ info: |
a. If typeName is not "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array",
or "Uint32Array", throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Atomics, TypedArray]
---*/

Expand All @@ -31,11 +32,7 @@ var index = {
}
};

var badArrayTypes = [
Uint8ClampedArray, Float32Array, Float64Array
];

for (var badArrayType of badArrayTypes) {
for (var badArrayType of nonAtomicsFriendlyTypedArrayConstructors) {
var typedArray = new badArrayType(new SharedArrayBuffer(8));
assert.throws(TypeError, function() {
Atomics.exchange(typedArray, index, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ info: |
a. If typeName is not "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array",
or "Uint32Array", throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Atomics, TypedArray]
---*/

Expand All @@ -31,11 +32,7 @@ var value = {
}
};

var badArrayTypes = [
Uint8ClampedArray, Float32Array, Float64Array
];

for (var badArrayType of badArrayTypes) {
for (var badArrayType of nonAtomicsFriendlyTypedArrayConstructors) {
var typedArray = new badArrayType(new SharedArrayBuffer(8));
assert.throws(TypeError, function() {
Atomics.exchange(typedArray, 0, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ info: |
a. If typeName is not "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array",
or "Uint32Array", throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Atomics, TypedArray]
---*/

Expand All @@ -31,11 +32,7 @@ var index = {
}
};

var badArrayTypes = [
Uint8ClampedArray, Float32Array, Float64Array
];

for (var badArrayType of badArrayTypes) {
for (var badArrayType of nonAtomicsFriendlyTypedArrayConstructors) {
var typedArray = new badArrayType(new SharedArrayBuffer(8));
assert.throws(TypeError, function() {
Atomics.load(typedArray, index);
Expand Down
9 changes: 9 additions & 0 deletions test/built-ins/Atomics/notify/non-int32-typedarray-throws.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ assert.throws(TypeError, function() {
Atomics.notify(view, poisoned, poisoned);
}, '`const view = new Float32Array( new SharedArrayBuffer(Float32Array.BYTES_PER_ELEMENT * 4) ); Atomics.notify(view, poisoned, poisoned)` throws TypeError');

if (typeof Float16Array !== 'undefined') {
assert.throws(TypeError, function() {
const view = new Float16Array(
new SharedArrayBuffer(Float16Array.BYTES_PER_ELEMENT * 2)
);
Atomics.notify(view, poisoned, poisoned);
}, '`const view = new Float16Array( new SharedArrayBuffer(Float16Array.BYTES_PER_ELEMENT * 2) ); Atomics.notify(view, poisoned, poisoned)` throws TypeError');
}

assert.throws(TypeError, function() {
const view = new Int16Array(
new SharedArrayBuffer(Int16Array.BYTES_PER_ELEMENT * 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ info: |
a. If typeName is not "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array",
or "Uint32Array", throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Atomics, TypedArray]
---*/

Expand All @@ -28,10 +29,7 @@ var count = {
}
};

var badArrayTypes = [
Int8Array, Uint8Array, Int16Array, Uint16Array, Uint32Array,
Uint8ClampedArray, Float32Array, Float64Array
];
var badArrayTypes = typedArrayConstructors.filter(function(TA) { return TA !== Int32Array; });

for (var badArrayType of badArrayTypes) {
var typedArray = new badArrayType(new SharedArrayBuffer(8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ info: |
a. If typeName is not "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array",
or "Uint32Array", throw a TypeError exception.
...
includes: [testTypedArray.js]
features: [Atomics, TypedArray]
---*/

Expand All @@ -28,10 +29,7 @@ var index = {
}
};

var badArrayTypes = [
Int8Array, Uint8Array, Int16Array, Uint16Array, Uint32Array,
Uint8ClampedArray, Float32Array, Float64Array
];
var badArrayTypes = typedArrayConstructors.filter(function(TA) { return TA !== Int32Array; });

for (var badArrayType of badArrayTypes) {
var typedArray = new badArrayType(new SharedArrayBuffer(8));
Expand Down
Loading

0 comments on commit 67a5153

Please sign in to comment.