From 0d08be2b42d3d36986df6cb2b9e208c9844db541 Mon Sep 17 00:00:00 2001 From: Kenvin Davies Date: Mon, 16 May 2022 21:01:28 -0500 Subject: [PATCH] test: add tests for TypedArray Add tests for TypedArray#ElementSize Add tests for TypedArray#BufferLength Add tests for TypedArray#ByteOffset PR-URL: https://github.com/nodejs/node-addon-api/pull/1179 Reviewed-By: Kevin Eady Reviewed-By: Michael Dawson (array.ElementLength())); } +Value GetTypedArraySize(const CallbackInfo& info) { + TypedArray array = info[0].As(); + return Number::New(info.Env(), static_cast(array.ElementSize())); +} + +Value GetTypedArrayByteOffset(const CallbackInfo& info) { + TypedArray array = info[0].As(); + return Number::New(info.Env(), static_cast(array.ByteOffset())); +} + +Value GetTypedArrayByteLength(const CallbackInfo& info) { + TypedArray array = info[0].As(); + return Number::New(info.Env(), static_cast(array.ByteLength())); +} + Value GetTypedArrayBuffer(const CallbackInfo& info) { TypedArray array = info[0].As(); return array.ArrayBuffer(); @@ -287,6 +302,11 @@ Object InitTypedArray(Env env) { Function::New(env, CreateInvalidTypedArray); exports["getTypedArrayType"] = Function::New(env, GetTypedArrayType); exports["getTypedArrayLength"] = Function::New(env, GetTypedArrayLength); + exports["getTypedArraySize"] = Function::New(env, GetTypedArraySize); + exports["getTypedArrayByteOffset"] = + Function::New(env, GetTypedArrayByteOffset); + exports["getTypedArrayByteLength"] = + Function::New(env, GetTypedArrayByteLength); exports["getTypedArrayBuffer"] = Function::New(env, GetTypedArrayBuffer); exports["getTypedArrayElement"] = Function::New(env, GetTypedArrayElement); exports["setTypedArrayElement"] = Function::New(env, SetTypedArrayElement); diff --git a/test/typedarray.js b/test/typedarray.js index f108881..e32ba22 100644 --- a/test/typedarray.js +++ b/test/typedarray.js @@ -6,15 +6,15 @@ module.exports = require('./common').runTest(test); function test (binding) { const testData = [ - ['int8', Int8Array], - ['uint8', Uint8Array], - ['uint8_clamped', Uint8ClampedArray], - ['int16', Int16Array], - ['uint16', Uint16Array], - ['int32', Int32Array], - ['uint32', Uint32Array], - ['float32', Float32Array], - ['float64', Float64Array] + ['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] ]; testData.forEach(data => { @@ -24,6 +24,9 @@ function test (binding) { 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] = 11; assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 11); @@ -49,6 +52,9 @@ function test (binding) { 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), offset); + assert.strictEqual(binding.typedarray.getTypedArrayByteLength(t), data[2] * length); t[3] = 11; assert.strictEqual(binding.typedarray.getTypedArrayElement(t, 3), 11);