diff options
author | Anna Henningsen <anna@addaleax.net> | 2019-02-25 19:40:24 +0100 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2019-03-01 21:58:08 +0100 |
commit | adbe3b837e8a2285238ec0fcba89c20882eb4cdb (patch) | |
tree | e04c000251c6202d416effa85cdf505c0b0ed8c2 /test | |
parent | 6a0f4636d9f8e94ebe905f0807edd0f5d9fbdd51 (diff) | |
download | android-node-v8-adbe3b837e8a2285238ec0fcba89c20882eb4cdb.tar.gz android-node-v8-adbe3b837e8a2285238ec0fcba89c20882eb4cdb.tar.bz2 android-node-v8-adbe3b837e8a2285238ec0fcba89c20882eb4cdb.zip |
test: verify heap buffer allocations occur
Check that small typed arrays, including `Buffer`s (unless allocated
by `Buffer.allocUnsafe()`), are indeed heap-allocated.
PR-URL: https://github.com/nodejs/node/pull/26301
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-buffer-backing-arraybuffer.js | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/test/parallel/test-buffer-backing-arraybuffer.js b/test/parallel/test-buffer-backing-arraybuffer.js new file mode 100644 index 0000000000..e7e15c079e --- /dev/null +++ b/test/parallel/test-buffer-backing-arraybuffer.js @@ -0,0 +1,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)); + } +} |