2015-09-01 10:51:17 +02:00
|
|
|
'use strict';
|
|
|
|
|
2025-05-09 20:41:31 +01:00
|
|
|
require('../common');
|
2015-09-01 10:51:17 +02:00
|
|
|
const assert = require('assert');
|
2025-05-09 20:41:31 +01:00
|
|
|
const { Buffer, kMaxLength } = require('buffer');
|
2015-09-01 10:51:17 +02:00
|
|
|
|
|
|
|
const ones = [1, 1, 1, 1];
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// Should create a Buffer
|
2025-05-09 20:41:31 +01:00
|
|
|
let sb = Buffer.allocUnsafeSlow(4);
|
2015-09-01 10:51:17 +02:00
|
|
|
assert(sb instanceof Buffer);
|
|
|
|
assert.strictEqual(sb.length, 4);
|
|
|
|
sb.fill(1);
|
2016-04-19 15:37:45 -07:00
|
|
|
for (const [key, value] of sb.entries()) {
|
|
|
|
assert.deepStrictEqual(value, ones[key]);
|
|
|
|
}
|
2015-09-01 10:51:17 +02:00
|
|
|
|
|
|
|
// underlying ArrayBuffer should have the same length
|
|
|
|
assert.strictEqual(sb.buffer.byteLength, 4);
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// Should work without new
|
2025-05-09 20:41:31 +01:00
|
|
|
sb = Buffer.allocUnsafeSlow(4);
|
2015-09-01 10:51:17 +02:00
|
|
|
assert(sb instanceof Buffer);
|
|
|
|
assert.strictEqual(sb.length, 4);
|
|
|
|
sb.fill(1);
|
2016-04-19 15:37:45 -07:00
|
|
|
for (const [key, value] of sb.entries()) {
|
|
|
|
assert.deepStrictEqual(value, ones[key]);
|
|
|
|
}
|
2015-09-01 10:51:17 +02:00
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// Should work with edge cases
|
2025-05-09 20:41:31 +01:00
|
|
|
assert.strictEqual(Buffer.allocUnsafeSlow(0).length, 0);
|
2015-09-01 10:51:17 +02:00
|
|
|
|
2019-03-04 11:02:53 +08:00
|
|
|
// Should throw with invalid length type
|
|
|
|
const bufferInvalidTypeMsg = {
|
|
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
2019-03-16 12:09:14 +01:00
|
|
|
name: 'TypeError',
|
2019-03-04 11:02:53 +08:00
|
|
|
message: /^The "size" argument must be of type number/,
|
|
|
|
};
|
2025-05-09 20:41:31 +01:00
|
|
|
assert.throws(() => Buffer.allocUnsafeSlow(), bufferInvalidTypeMsg);
|
|
|
|
assert.throws(() => Buffer.allocUnsafeSlow({}), bufferInvalidTypeMsg);
|
|
|
|
assert.throws(() => Buffer.allocUnsafeSlow('6'), bufferInvalidTypeMsg);
|
|
|
|
assert.throws(() => Buffer.allocUnsafeSlow(true), bufferInvalidTypeMsg);
|
2019-03-04 11:02:53 +08:00
|
|
|
|
|
|
|
// Should throw with invalid length value
|
|
|
|
const bufferMaxSizeMsg = {
|
2023-01-18 19:36:27 +09:00
|
|
|
code: 'ERR_OUT_OF_RANGE',
|
2019-03-16 12:09:14 +01:00
|
|
|
name: 'RangeError',
|
2019-03-04 11:02:53 +08:00
|
|
|
};
|
2025-05-09 20:41:31 +01:00
|
|
|
assert.throws(() => Buffer.allocUnsafeSlow(NaN), bufferMaxSizeMsg);
|
|
|
|
assert.throws(() => Buffer.allocUnsafeSlow(Infinity), bufferMaxSizeMsg);
|
|
|
|
assert.throws(() => Buffer.allocUnsafeSlow(-1), bufferMaxSizeMsg);
|
|
|
|
assert.throws(() => Buffer.allocUnsafeSlow(kMaxLength + 1), bufferMaxSizeMsg);
|