mirror of https://github.com/tc39/test262.git
77 lines
2.3 KiB
JavaScript
77 lines
2.3 KiB
JavaScript
|
// Copyright (C) 2015 André Bargull. All rights reserved.
|
||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||
|
/*---
|
||
|
description: |
|
||
|
Collection of functions used to assert the correctness of BigInt TypedArray objects.
|
||
|
---*/
|
||
|
|
||
|
/**
|
||
|
* Array containing every typed array constructor.
|
||
|
*/
|
||
|
var BigIntTypedArrayConstructors = [
|
||
|
BigInt64Array,
|
||
|
BigUint64Array
|
||
|
];
|
||
|
|
||
|
/**
|
||
|
* The %TypedArray% intrinsic constructor function.
|
||
|
*/
|
||
|
var TypedArray = Object.getPrototypeOf(Int8Array);
|
||
|
|
||
|
function convertToBigInt(x) {
|
||
|
return (Array.isArray(x)) ? x.map(convertToBigInt) : BigInt(x);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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.
|
||
|
* @param {Array} selected - An optional Array with filtered typed arrays
|
||
|
*/
|
||
|
function testWithBigIntTypedArrayConstructors(f, selected) {
|
||
|
var constructors = selected || BigIntTypedArrayConstructors;
|
||
|
for (var i = 0; i < constructors.length; ++i) {
|
||
|
var constructor = constructors[i];
|
||
|
try {
|
||
|
f(constructor);
|
||
|
} catch (e) {
|
||
|
e.message += " (Testing with " + constructor.name + ".)";
|
||
|
throw e;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 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.
|
||
|
*/
|
||
|
function testBigIntTypedArrayConversions(byteConversionValues, fn) {
|
||
|
var values = byteConversionValues.values;
|
||
|
var expected = byteConversionValues.expected;
|
||
|
|
||
|
testWithBigIntTypedArrayConstructors(function(TA) {
|
||
|
var name = TA.name.slice(0, -5);
|
||
|
|
||
|
return values.forEach(function(value, index) {
|
||
|
var exp = expected[name][index];
|
||
|
var initial = 0;
|
||
|
if (exp === 0) {
|
||
|
initial = 1;
|
||
|
}
|
||
|
fn(TA, value, exp, initial);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|