2015-12-15 17:33:49 +01:00
|
|
|
// Copyright (C) 2015 André Bargull. All rights reserved.
|
|
|
|
// This code is governed by the BSD license found in the LICENSE file.
|
2017-07-14 17:37:24 +02:00
|
|
|
/*---
|
|
|
|
description: |
|
|
|
|
Collection of functions used to assert the correctness of TypedArray objects.
|
2019-09-25 02:22:26 +02:00
|
|
|
defines:
|
|
|
|
- floatArrayConstructors
|
2024-01-11 11:35:28 +01:00
|
|
|
- nonClampedIntArrayConstructors
|
2019-09-25 02:22:26 +02:00
|
|
|
- intArrayConstructors
|
2024-01-10 14:07:57 +01:00
|
|
|
- typedArrayConstructors
|
2019-09-25 02:22:26 +02:00
|
|
|
- TypedArray
|
|
|
|
- testWithTypedArrayConstructors
|
2024-01-10 14:07:57 +01:00
|
|
|
- nonAtomicsFriendlyTypedArrayConstructors
|
2020-05-27 21:00:02 +02:00
|
|
|
- testWithAtomicsFriendlyTypedArrayConstructors
|
|
|
|
- testWithNonAtomicsFriendlyTypedArrayConstructors
|
2019-09-25 02:22:26 +02:00
|
|
|
- testTypedArrayConversions
|
2017-07-14 17:37:24 +02:00
|
|
|
---*/
|
2015-12-15 17:33:49 +01:00
|
|
|
|
2024-01-10 14:07:57 +01:00
|
|
|
var floatArrayConstructors = [
|
2015-12-15 17:33:49 +01:00
|
|
|
Float64Array,
|
2024-01-10 14:07:57 +01:00
|
|
|
Float32Array
|
|
|
|
];
|
|
|
|
|
2024-01-11 11:35:28 +01:00
|
|
|
var nonClampedIntArrayConstructors = [
|
2015-12-15 17:33:49 +01:00
|
|
|
Int32Array,
|
|
|
|
Int16Array,
|
|
|
|
Int8Array,
|
|
|
|
Uint32Array,
|
|
|
|
Uint16Array,
|
2024-01-11 11:35:28 +01:00
|
|
|
Uint8Array
|
2015-12-15 17:33:49 +01:00
|
|
|
];
|
|
|
|
|
2024-01-11 11:35:28 +01:00
|
|
|
var intArrayConstructors = nonClampedIntArrayConstructors.concat([Uint8ClampedArray]);
|
|
|
|
|
2024-01-10 14:07:57 +01:00
|
|
|
// 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);
|
2018-04-19 16:11:50 +02:00
|
|
|
|
2016-01-15 18:12:42 +01:00
|
|
|
/**
|
|
|
|
* The %TypedArray% intrinsic constructor function.
|
|
|
|
*/
|
|
|
|
var TypedArray = Object.getPrototypeOf(Int8Array);
|
|
|
|
|
2015-12-15 17:33:49 +01:00
|
|
|
/**
|
|
|
|
* Callback for testing a typed array constructor.
|
|
|
|
*
|
|
|
|
* @callback typedArrayConstructorCallback
|
|
|
|
* @param {Function} Constructor the constructor object to test with.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls the provided function for every typed array constructor.
|
|
|
|
*
|
|
|
|
* @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor.
|
2016-02-03 23:03:21 +01:00
|
|
|
* @param {Array} selected - An optional Array with filtered typed arrays
|
2015-12-15 17:33:49 +01:00
|
|
|
*/
|
2016-02-03 23:03:21 +01:00
|
|
|
function testWithTypedArrayConstructors(f, selected) {
|
|
|
|
var constructors = selected || typedArrayConstructors;
|
|
|
|
for (var i = 0; i < constructors.length; ++i) {
|
|
|
|
var constructor = constructors[i];
|
2015-12-15 17:33:49 +01:00
|
|
|
try {
|
|
|
|
f(constructor);
|
|
|
|
} catch (e) {
|
|
|
|
e.message += " (Testing with " + constructor.name + ".)";
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-11 18:51:11 +02:00
|
|
|
|
2024-01-10 14:07:57 +01:00
|
|
|
var nonAtomicsFriendlyTypedArrayConstructors = floatArrayConstructors.concat([Uint8ClampedArray]);
|
2020-05-06 21:26:38 +02:00
|
|
|
/**
|
2020-05-27 21:00:02 +02:00
|
|
|
* Calls the provided function for every non-"Atomics Friendly" typed array constructor.
|
2020-05-06 21:26:38 +02:00
|
|
|
*
|
|
|
|
* @param {typedArrayConstructorCallback} f - the function to call for each typed array constructor.
|
|
|
|
* @param {Array} selected - An optional Array with filtered typed arrays
|
|
|
|
*/
|
2020-05-27 21:00:02 +02:00
|
|
|
function testWithNonAtomicsFriendlyTypedArrayConstructors(f) {
|
2024-01-10 14:07:57 +01:00
|
|
|
testWithTypedArrayConstructors(f, nonAtomicsFriendlyTypedArrayConstructors);
|
2020-05-27 21:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls the provided function for every "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 testWithAtomicsFriendlyTypedArrayConstructors(f) {
|
|
|
|
testWithTypedArrayConstructors(f, [
|
|
|
|
Int32Array,
|
2020-05-06 21:26:38 +02:00
|
|
|
Int16Array,
|
|
|
|
Int8Array,
|
|
|
|
Uint32Array,
|
|
|
|
Uint16Array,
|
|
|
|
Uint8Array,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-11 18:51:11 +02:00
|
|
|
/**
|
|
|
|
* Helper for conversion operations on TypedArrays, the expected values
|
|
|
|
* properties are indexed in order to match the respective value for each
|
|
|
|
* TypedArray constructor
|
|
|
|
* @param {Function} fn - the function to call for each constructor and value.
|
|
|
|
* will be called with the constructor, value, expected
|
|
|
|
* value, and a initial value that can be used to avoid
|
|
|
|
* a false positive with an equivalent expected value.
|
|
|
|
*/
|
2016-05-13 17:47:26 +02:00
|
|
|
function testTypedArrayConversions(byteConversionValues, fn) {
|
|
|
|
var values = byteConversionValues.values;
|
|
|
|
var expected = byteConversionValues.expected;
|
2016-04-11 18:51:11 +02:00
|
|
|
|
|
|
|
testWithTypedArrayConstructors(function(TA) {
|
2016-05-13 17:47:26 +02:00
|
|
|
var name = TA.name.slice(0, -5);
|
|
|
|
|
2016-04-11 18:51:11 +02:00
|
|
|
return values.forEach(function(value, index) {
|
2016-05-13 17:47:26 +02:00
|
|
|
var exp = expected[name][index];
|
2016-04-11 18:51:11 +02:00
|
|
|
var initial = 0;
|
|
|
|
if (exp === 0) {
|
|
|
|
initial = 1;
|
|
|
|
}
|
|
|
|
fn(TA, value, exp, initial);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2024-01-10 14:07:57 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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");
|
|
|
|
}
|
|
|
|
}
|