Skip to content

Commit

Permalink
test: add tests for .Data method
Browse files Browse the repository at this point in the history
- add tests for .Data method to ensure the content is consistent

PR-URL: #1203
Reviewed-By: Michael Dawson <midawson@redhat.com
  • Loading branch information
JckXia authored and mhdawson committed Sep 20, 2022
1 parent 225ca35 commit 30cd4a3
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 10 deletions.
96 changes: 95 additions & 1 deletion test/typedarray.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <vector>
#include "napi.h"

using namespace Napi;

#if defined(NAPI_HAS_CONSTEXPR)
Expand Down Expand Up @@ -179,6 +179,99 @@ Value GetTypedArrayType(const CallbackInfo& info) {
}
}

template <typename type>
bool TypedArrayDataIsEquivalent(TypedArrayOf<type> arr,
TypedArrayOf<type> inputArr) {
if (arr.ElementLength() != inputArr.ElementLength()) {
return false;
}
std::vector<type> bufferContent(arr.Data(), arr.Data() + arr.ElementLength());
std::vector<type> inputContent(inputArr.Data(),
inputArr.Data() + inputArr.ElementLength());
if (bufferContent != inputContent) {
return false;
}
return true;
}

Value CheckBufferContent(const CallbackInfo& info) {
TypedArray array = info[0].As<TypedArray>();

switch (array.TypedArrayType()) {
case napi_int8_array:
return Boolean::New(
info.Env(),
TypedArrayDataIsEquivalent<int8_t>(info[0].As<Int8Array>(),
info[1].As<Int8Array>()));

break;
case napi_uint8_array:
return Boolean::New(
info.Env(),
TypedArrayDataIsEquivalent<int8_t>(info[0].As<Int8Array>(),
info[1].As<Int8Array>()));

case napi_uint8_clamped_array:
return Boolean::New(
info.Env(),
TypedArrayDataIsEquivalent<uint8_t>(info[0].As<Uint8Array>(),
info[1].As<Uint8Array>()));

case napi_int16_array:
return Boolean::New(
info.Env(),
TypedArrayDataIsEquivalent<int16_t>(info[0].As<Int16Array>(),
info[1].As<Int16Array>()));

case napi_uint16_array:
return Boolean::New(
info.Env(),
TypedArrayDataIsEquivalent<uint16_t>(info[0].As<Uint16Array>(),
info[1].As<Uint16Array>()));

case napi_int32_array:
return Boolean::New(
info.Env(),
TypedArrayDataIsEquivalent<int32_t>(info[0].As<Int32Array>(),
info[1].As<Int32Array>()));

case napi_uint32_array:
return Boolean::New(
info.Env(),
TypedArrayDataIsEquivalent<uint32_t>(info[0].As<Uint32Array>(),
info[1].As<Uint32Array>()));

case napi_float32_array:
return Boolean::New(
info.Env(),
TypedArrayDataIsEquivalent<float>(info[0].As<Float32Array>(),
info[1].As<Float32Array>()));

case napi_float64_array:
return Boolean::New(
info.Env(),
TypedArrayDataIsEquivalent<double>(info[0].As<Float64Array>(),
info[1].As<Float64Array>()));

#if (NAPI_VERSION > 5)
case napi_bigint64_array:
return Boolean::New(
info.Env(),
TypedArrayDataIsEquivalent<int64_t>(info[0].As<BigInt64Array>(),
info[1].As<BigInt64Array>()));

case napi_biguint64_array:
return Boolean::New(
info.Env(),
TypedArrayDataIsEquivalent<uint64_t>(info[0].As<BigUint64Array>(),
info[1].As<BigUint64Array>()));

#endif
default:
return Boolean::New(info.Env(), false);
}
}

Value GetTypedArrayLength(const CallbackInfo& info) {
TypedArray array = info[0].As<TypedArray>();
return Number::New(info.Env(), static_cast<double>(array.ElementLength()));
Expand Down Expand Up @@ -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;
}
46 changes: 37 additions & 9 deletions test/typedarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 30cd4a3

Please sign in to comment.