0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-buffer-backing-arraybuffer.js
Rich Trott 330f25ef82 test: prepare for consistent comma-dangle lint rule
Make changes so that tests will pass when the comma-dangle settings
applied to the rest of the code base are also applied to tests.

PR-URL: https://github.com/nodejs/node/pull/37930
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Derek Lewis <DerekNonGeneric@inf.is>
2021-04-01 23:14:29 -07:00

38 lines
1.2 KiB
JavaScript

// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const { internalBinding } = require('internal/test/binding');
const { arrayBufferViewHasBuffer } = internalBinding('util');
const tests = [
{ length: 0, expectOnHeap: true },
{ length: 48, expectOnHeap: true },
{ length: 96, expectOnHeap: false },
{ length: 1024, expectOnHeap: false },
];
for (const { length, expectOnHeap } of tests) {
const arrays = [
new Uint8Array(length),
new Uint16Array(length / 2),
new Uint32Array(length / 4),
new Float32Array(length / 4),
new Float64Array(length / 8),
Buffer.alloc(length),
Buffer.allocUnsafeSlow(length),
// Buffer.allocUnsafe() is missing because it may use pooled allocations.
];
for (const array of arrays) {
const isOnHeap = !arrayBufferViewHasBuffer(array);
assert.strictEqual(isOnHeap, expectOnHeap,
`mismatch: ${isOnHeap} vs ${expectOnHeap} ` +
`for ${array.constructor.name}, length = ${length}`);
// Consistency check: Accessing .buffer should create it.
array.buffer; // eslint-disable-line no-unused-expressions
assert(arrayBufferViewHasBuffer(array));
}
}