summaryrefslogtreecommitdiff
path: root/test/parallel/test-buffer-backing-arraybuffer.js
blob: e7e15c079e633203c4690c9fc52d834fb334e5d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// 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;
    assert(arrayBufferViewHasBuffer(array));
  }
}