Skip to content

Commit

Permalink
test: add tests for TypedArray
Browse files Browse the repository at this point in the history
Add tests for TypedArray#ElementSize
Add tests for TypedArray#BufferLength
Add tests for TypedArray#ByteOffset

PR-URL: nodejs/node-addon-api#1179
Reviewed-By: Kevin Eady <[email protected]>
Reviewed-By: Michael Dawson <[email protected]
  • Loading branch information
kevindavies8 committed May 27, 2022
1 parent fd36b2e commit 0d08be2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
20 changes: 20 additions & 0 deletions test/typedarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ Value GetTypedArrayLength(const CallbackInfo& info) {
return Number::New(info.Env(), static_cast<double>(array.ElementLength()));
}

Value GetTypedArraySize(const CallbackInfo& info) {
TypedArray array = info[0].As<TypedArray>();
return Number::New(info.Env(), static_cast<double>(array.ElementSize()));
}

Value GetTypedArrayByteOffset(const CallbackInfo& info) {
TypedArray array = info[0].As<TypedArray>();
return Number::New(info.Env(), static_cast<double>(array.ByteOffset()));
}

Value GetTypedArrayByteLength(const CallbackInfo& info) {
TypedArray array = info[0].As<TypedArray>();
return Number::New(info.Env(), static_cast<double>(array.ByteLength()));
}

Value GetTypedArrayBuffer(const CallbackInfo& info) {
TypedArray array = info[0].As<TypedArray>();
return array.ArrayBuffer();
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 15 additions & 9 deletions test/typedarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 0d08be2

Please sign in to comment.