11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding');
61cb0ef41Sopenharmony_ciconst { arrayBufferViewHasBuffer } = internalBinding('util');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst tests = [
91cb0ef41Sopenharmony_ci  { length: 0, expectOnHeap: true },
101cb0ef41Sopenharmony_ci  { length: 48, expectOnHeap: true },
111cb0ef41Sopenharmony_ci  { length: 96, expectOnHeap: false },
121cb0ef41Sopenharmony_ci  { length: 1024, expectOnHeap: false },
131cb0ef41Sopenharmony_ci];
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_cifor (const { length, expectOnHeap } of tests) {
161cb0ef41Sopenharmony_ci  const arrays = [
171cb0ef41Sopenharmony_ci    new Uint8Array(length),
181cb0ef41Sopenharmony_ci    new Uint16Array(length / 2),
191cb0ef41Sopenharmony_ci    new Uint32Array(length / 4),
201cb0ef41Sopenharmony_ci    new Float32Array(length / 4),
211cb0ef41Sopenharmony_ci    new Float64Array(length / 8),
221cb0ef41Sopenharmony_ci    Buffer.alloc(length),
231cb0ef41Sopenharmony_ci    Buffer.allocUnsafeSlow(length),
241cb0ef41Sopenharmony_ci    // Buffer.allocUnsafe() is missing because it may use pooled allocations.
251cb0ef41Sopenharmony_ci  ];
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  for (const array of arrays) {
281cb0ef41Sopenharmony_ci    const isOnHeap = !arrayBufferViewHasBuffer(array);
291cb0ef41Sopenharmony_ci    assert.strictEqual(isOnHeap, expectOnHeap,
301cb0ef41Sopenharmony_ci                       `mismatch: ${isOnHeap} vs ${expectOnHeap} ` +
311cb0ef41Sopenharmony_ci                       `for ${array.constructor.name}, length = ${length}`);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    // Consistency check: Accessing .buffer should create it.
341cb0ef41Sopenharmony_ci    array.buffer; // eslint-disable-line no-unused-expressions
351cb0ef41Sopenharmony_ci    assert(arrayBufferViewHasBuffer(array));
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci}
38