diff --git a/test/typedarray.cc b/test/typedarray.cc index ae27ae317..3ee9f3f2a 100644 --- a/test/typedarray.cc +++ b/test/typedarray.cc @@ -1,5 +1,5 @@ +#include #include "napi.h" - using namespace Napi; #if defined(NAPI_HAS_CONSTEXPR) @@ -179,6 +179,99 @@ Value GetTypedArrayType(const CallbackInfo& info) { } } +template +bool TypedArrayDataIsEquivalent(TypedArrayOf arr, + TypedArrayOf inputArr) { + if (arr.ElementLength() != inputArr.ElementLength()) { + return false; + } + std::vector bufferContent(arr.Data(), arr.Data() + arr.ElementLength()); + std::vector inputContent(inputArr.Data(), + inputArr.Data() + inputArr.ElementLength()); + if (bufferContent != inputContent) { + return false; + } + return true; +} + +Value CheckBufferContent(const CallbackInfo& info) { + TypedArray array = info[0].As(); + + switch (array.TypedArrayType()) { + case napi_int8_array: + return Boolean::New( + info.Env(), + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); + + break; + case napi_uint8_array: + return Boolean::New( + info.Env(), + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); + + case napi_uint8_clamped_array: + return Boolean::New( + info.Env(), + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); + + case napi_int16_array: + return Boolean::New( + info.Env(), + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); + + case napi_uint16_array: + return Boolean::New( + info.Env(), + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); + + case napi_int32_array: + return Boolean::New( + info.Env(), + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); + + case napi_uint32_array: + return Boolean::New( + info.Env(), + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); + + case napi_float32_array: + return Boolean::New( + info.Env(), + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); + + case napi_float64_array: + return Boolean::New( + info.Env(), + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); + +#if (NAPI_VERSION > 5) + case napi_bigint64_array: + return Boolean::New( + info.Env(), + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); + + case napi_biguint64_array: + return Boolean::New( + info.Env(), + TypedArrayDataIsEquivalent(info[0].As(), + info[1].As())); + +#endif + default: + return Boolean::New(info.Env(), false); + } +} + Value GetTypedArrayLength(const CallbackInfo& info) { TypedArray array = info[0].As(); return Number::New(info.Env(), static_cast(array.ElementLength())); @@ -310,6 +403,7 @@ Object InitTypedArray(Env env) { exports["getTypedArrayBuffer"] = Function::New(env, GetTypedArrayBuffer); exports["getTypedArrayElement"] = Function::New(env, GetTypedArrayElement); exports["setTypedArrayElement"] = Function::New(env, SetTypedArrayElement); + exports["checkBufferContent"] = Function::New(env, CheckBufferContent); return exports; } diff --git a/test/typedarray.js b/test/typedarray.js index e32ba2274..f7224efb7 100644 --- a/test/typedarray.js +++ b/test/typedarray.js @@ -6,17 +6,42 @@ module.exports = require('./common').runTest(test); function test (binding) { const testData = [ - ['int8', Int8Array, 1], - ['uint8', Uint8Array, 1], - ['uint8_clamped', Uint8ClampedArray, 1], - ['int16', Int16Array, 2], - ['uint16', Uint16Array, 2], - ['int32', Int32Array, 4], - ['uint32', Uint32Array, 4], - ['float32', Float32Array, 4], - ['float64', Float64Array, 8] + ['int8', Int8Array, 1, new Int8Array([0, 124, 24, 44])], + ['uint8', Uint8Array, 1, new Uint8Array([0, 255, 2, 14])], + ['uint8_clamped', Uint8ClampedArray, 1, new Uint8ClampedArray([0, 256, 0, 255])], + ['int16', Int16Array, 2, new Int16Array([-32768, 32767, 1234, 42])], + ['uint16', Uint16Array, 2, new Uint16Array([0, 65535, 4, 12])], + ['int32', Int32Array, 4, new Int32Array([Math.pow(2, 31), Math.pow(-2, 31), 255, 4])], + ['uint32', Uint32Array, 4, new Uint32Array([0, Math.pow(2, 32), 24, 125])], + ['float32', Float32Array, 4, new Float32Array([0, 21, 34, 45])], + ['float64', Float64Array, 8, new Float64Array([0, 4124, 45, 90])] ]; + const bigIntTests = [ + ['bigint64', BigInt64Array, 8, new BigInt64Array([9007199254740991n, 9007199254740991n, 24n, 125n])], + ['biguint64', BigUint64Array, 8, new BigUint64Array([9007199254740991n, 9007199254740991n, 2345n, 345n])] + ]; + + bigIntTests.forEach(data => { + const length = 4; + const t = binding.typedarray.createTypedArray(data[0], length); + assert.ok(t instanceof data[1]); + assert.strictEqual(binding.typedarray.getTypedArrayType(t), data[0]); + assert.strictEqual(binding.typedarray.getTypedArrayLength(t), length); + assert.strictEqual(binding.typedarray.getTypedArraySize(t), data[2]); + assert.strictEqual(binding.typedarray.getTypedArrayByteOffset(t), 0); + assert.strictEqual(binding.typedarray.getTypedArrayByteLength(t), data[2] * length); + + t[3] = 11n; + assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 11n); + binding.typedarray.setTypedArrayElement(t, 3, 22n); + assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 22n); + assert.strictEqual(t[3], 22n); + + const nonEmptyTypedArray = binding.typedarray.createTypedArray(data[0], length, data[3].buffer); + binding.typedarray.checkBufferContent(nonEmptyTypedArray, data[3]); + }); + testData.forEach(data => { try { const length = 4; @@ -63,6 +88,9 @@ function test (binding) { assert.strictEqual(t[3], 22); assert.strictEqual(binding.typedarray.getTypedArrayBuffer(t), b); + + const nonEmptyTypedArray = binding.typedarray.createTypedArray(data[0], length, data[3].buffer); + assert.strictEqual(binding.typedarray.checkBufferContent(nonEmptyTypedArray, data[3]), true); } catch (e) { console.log(data); throw e;