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:
|
|
|
|
- typedArrayConstructors
|
|
|
|
- floatArrayConstructors
|
|
|
|
- intArrayConstructors
|
|
|
|
- TypedArray
|
|
|
|
- testWithTypedArrayConstructors
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Array containing every typed array constructor.
|
|
|
|
*/
|
|
|
|
var typedArrayConstructors = [
|
|
|
|
Float64Array,
|
|
|
|
Float32Array,
|
|
|
|
Int32Array,
|
|
|
|
Int16Array,
|
|
|
|
Int8Array,
|
|
|
|
Uint32Array,
|
|
|
|
Uint16Array,
|
|
|
|
Uint8Array,
|
2016-02-03 23:03:21 +01:00
|
|
|
Uint8ClampedArray
|
2015-12-15 17:33:49 +01:00
|
|
|
];
|
|
|
|
|
2018-04-19 16:11:50 +02:00
|
|
|
var floatArrayConstructors = typedArrayConstructors.slice(0, 2);
|
|
|
|
var intArrayConstructors = typedArrayConstructors.slice(2, 7);
|
|
|
|
|
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
|
|
|
|
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) {
|
2020-05-06 21:26:38 +02:00
|
|
|
testWithTypedArrayConstructors(f, [
|
|
|
|
Float64Array,
|
|
|
|
Float32Array,
|
2020-05-27 21:00:02 +02:00
|
|
|
Uint8ClampedArray
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|