Repository: nodejs/node
File: test/js-native-api/test_typedarray/test.js (JavaScript)

Code before:
const byteResult = test_typedarray.Multiply(byteArray, 3);
assert.ok(byteResult instanceof Uint8Array);
assert.strictEqual(byteResult.length, 3);
assert.strictEqual(byteResult[0], 0);
assert.strictEqual(byteResult[1], 3);
assert.strictEqual(byteResult[2], 6);

const doubleResult = test_typedarray.Multiply(doubleArray, -3);
assert.ok(doubleResult instanceof Float64Array);
assert.strictEqual(doubleResult.length, 3);
assert.strictEqual(doubleResult[0], -0);
assert.strictEqual(Math.round(10 * doubleResult[1]) / 10, -3.3);
assert.strictEqual(Math.round(10 * doubleResult[2]) / 10, -6.6);

const externalResult = test_typedarray.External();
assert.ok(externalResult instanceof Int8Array);
assert.strictEqual(externalResult.length, 3);
assert.strictEqual(externalResult[0], 0);
assert.strictEqual(externalResult[1], 1);
assert.strictEqual(externalResult[2], 2);

// Validate creation of all kinds of TypedArrays
const buffer = new ArrayBuffer(128);
const arrayTypes = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array,
                     Uint16Array, Int32Array, Uint32Array, Float32Array,
                     Float64Array, BigInt64Array, BigUint64Array, Float16Array ];

arrayTypes.forEach((currentType) => {
  const template = Reflect.construct(currentType, buffer);
  const theArray = test_typedarray.CreateTypedArray(template, buffer);

  assert.ok(theArray instanceof currentType,
            'Type of new array should match that of the template. ' +
            `Expected type: ${currentType.name}, ` +
            `actual type: ${template.constructor.name}`);
  assert.notStrictEqual(theArray, template);
  assert.strictEqual(theArray.buffer, buffer);
});

arrayTypes.forEach((currentType) => {
  const template = Reflect.construct(currentType, buffer);
  assert.throws(() => {
    test_typedarray.CreateTypedArray(template, buffer, 0, 136);
  }, RangeError);
});

const nonByteArrayTypes = [ Int16Array, Uint16Array, Int32Array, Uint32Array,
                            Float32Array, Float64Array,
                            BigInt64Array, BigUint64Array, Float16Array ];
nonByteArrayTypes.forEach((currentType) => {
  const template = Reflect.construct(currentType, buffer);

Review comment (suggestion):
Nit:
I think it would be better to add it before `Float32Array` for consistency.

Code after:
const byteResult = test_typedarray.Multiply(byteArray, 3);
assert.ok(byteResult instanceof Uint8Array);
assert.strictEqual(byteResult.length, 3);
assert.strictEqual(byteResult[0], 0);
assert.strictEqual(byteResult[1], 3);
assert.strictEqual(byteResult[2], 6);

const doubleResult = test_typedarray.Multiply(doubleArray, -3);
assert.ok(doubleResult instanceof Float64Array);
assert.strictEqual(doubleResult.length, 3);
assert.strictEqual(doubleResult[0], -0);
assert.strictEqual(Math.round(10 * doubleResult[1]) / 10, -3.3);
assert.strictEqual(Math.round(10 * doubleResult[2]) / 10, -6.6);

const externalResult = test_typedarray.External();
assert.ok(externalResult instanceof Int8Array);
assert.strictEqual(externalResult.length, 3);
assert.strictEqual(externalResult[0], 0);
assert.strictEqual(externalResult[1], 1);
assert.strictEqual(externalResult[2], 2);

// Validate creation of all kinds of TypedArrays
const buffer = new ArrayBuffer(128);
const arrayTypes = [ Int8Array, Uint8Array, Uint8ClampedArray, Int16Array,
                     Uint16Array, Int32Array, Uint32Array, Float16Array,
                     Float32Array, Float64Array, BigInt64Array, BigUint64Array ];

arrayTypes.forEach((currentType) => {
  const template = Reflect.construct(currentType, buffer);
  const theArray = test_typedarray.CreateTypedArray(template, buffer);

  assert.ok(theArray instanceof currentType,
            'Type of new array should match that of the template. ' +
            `Expected type: ${currentType.name}, ` +
            `actual type: ${template.constructor.name}`);
  assert.notStrictEqual(theArray, template);
  assert.strictEqual(theArray.buffer, buffer);
});

arrayTypes.forEach((currentType) => {
  const template = Reflect.construct(currentType, buffer);
  assert.throws(() => {
    test_typedarray.CreateTypedArray(template, buffer, 0, 136);
  }, RangeError);
});

const nonByteArrayTypes = [ Int16Array, Uint16Array, Int32Array, Uint32Array,
                            Float16Array, Float32Array, Float64Array,
                            BigInt64Array, BigUint64Array ];
nonByteArrayTypes.forEach((currentType) => {
  const template = Reflect.construct(currentType, buffer);