'use strict';
var Buffer = require('../../').Buffer;
-var common = require('./common');
+const common = require('./common');
+const assert = require('assert');
-var assert = require('assert');
+const LENGTH = 16;
-var Buffer = require('../../').Buffer;
-var LENGTH = 16;
-
-var ab = new ArrayBuffer(LENGTH);
-var dv = new DataView(ab);
-var ui = new Uint8Array(ab);
-var buf = Buffer.from(ab);
+const ab = new ArrayBuffer(LENGTH);
+const dv = new DataView(ab);
+const ui = new Uint8Array(ab);
+const buf = Buffer.from(ab);
assert.ok(buf instanceof Buffer);
-// For backwards compatibility of old .parent property test that if buf is not
-// a slice then .parent should be undefined.
-assert.equal(buf.parent, buf.buffer);
-assert.equal(buf.buffer, ab);
-assert.equal(buf.length, ab.byteLength);
+assert.strictEqual(buf.parent, buf.buffer);
+assert.strictEqual(buf.buffer, ab);
+assert.strictEqual(buf.length, ab.byteLength);
buf.fill(0xC);
-for (var i = 0; i < LENGTH; i++) {
- assert.equal(ui[i], 0xC);
+for (let i = 0; i < LENGTH; i++) {
+ assert.strictEqual(ui[i], 0xC);
ui[i] = 0xF;
- assert.equal(buf[i], 0xF);
+ assert.strictEqual(buf[i], 0xF);
}
buf.writeUInt32LE(0xF00, 0);
buf.writeUInt32BE(0xB47, 4);
buf.writeDoubleLE(3.1415, 8);
-assert.equal(dv.getUint32(0, true), 0xF00);
-assert.equal(dv.getUint32(4), 0xB47);
-assert.equal(dv.getFloat64(8, true), 3.1415);
+assert.strictEqual(dv.getUint32(0, true), 0xF00);
+assert.strictEqual(dv.getUint32(4), 0xB47);
+assert.strictEqual(dv.getFloat64(8, true), 3.1415);
// Now test protecting users from doing stupid things
}, TypeError);
// write{Double,Float}{LE,BE} with noAssert should not crash, cf. #3766
-var b = Buffer.allocUnsafe(1);
+const b = Buffer.allocUnsafe(1);
b.writeFloatLE(11.11, 0, true);
b.writeFloatBE(11.11, 0, true);
b.writeDoubleLE(11.11, 0, true);
// Test the byteOffset and length arguments
{
- var ab = new Uint8Array(5);
+ const ab = new Uint8Array(5);
ab[0] = 1;
ab[1] = 2;
ab[2] = 3;
ab[3] = 4;
ab[4] = 5;
- var buf = Buffer.from(ab.buffer, 1, 3);
- assert.equal(buf.length, 3);
- assert.equal(buf[0], 2);
- assert.equal(buf[1], 3);
- assert.equal(buf[2], 4);
+ const buf = Buffer.from(ab.buffer, 1, 3);
+ assert.strictEqual(buf.length, 3);
+ assert.strictEqual(buf[0], 2);
+ assert.strictEqual(buf[1], 3);
+ assert.strictEqual(buf[2], 4);
buf[0] = 9;
- assert.equal(ab[1], 9);
+ assert.strictEqual(ab[1], 9);
common.expectsError(() => Buffer.from(ab.buffer, 6), {
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
// Test the deprecated Buffer() version also
{
- var ab = new Uint8Array(5);
+ const ab = new Uint8Array(5);
ab[0] = 1;
ab[1] = 2;
ab[2] = 3;
ab[3] = 4;
ab[4] = 5;
- var buf = Buffer(ab.buffer, 1, 3);
- assert.equal(buf.length, 3);
- assert.equal(buf[0], 2);
- assert.equal(buf[1], 3);
- assert.equal(buf[2], 4);
+ const buf = Buffer(ab.buffer, 1, 3);
+ assert.strictEqual(buf.length, 3);
+ assert.strictEqual(buf[0], 2);
+ assert.strictEqual(buf[1], 3);
+ assert.strictEqual(buf[2], 4);
buf[0] = 9;
- assert.equal(ab[1], 9);
+ assert.strictEqual(ab[1], 9);
common.expectsError(() => Buffer(ab.buffer, 6), {
code: 'ERR_BUFFER_OUT_OF_BOUNDS',
});
}
+{
+ // If byteOffset is not numeric, it defaults to 0.
+ const ab = new ArrayBuffer(10);
+ const expected = Buffer.from(ab, 0);
+ assert.deepStrictEqual(Buffer.from(ab, 'fhqwhgads'), expected);
+ assert.deepStrictEqual(Buffer.from(ab, NaN), expected);
+ assert.deepStrictEqual(Buffer.from(ab, {}), expected);
+ assert.deepStrictEqual(Buffer.from(ab, []), expected);
+
+ // If byteOffset can be converted to a number, it will be.
+ assert.deepStrictEqual(Buffer.from(ab, [1]), Buffer.from(ab, 1));
+
+ // If byteOffset is Infinity, throw.
+ common.expectsError(() => {
+ Buffer.from(ab, Infinity);
+ }, {
+ code: 'ERR_BUFFER_OUT_OF_BOUNDS',
+ type: RangeError,
+ message: '"offset" is outside of buffer bounds'
+ });
+}
+
+{
+ // If length is not numeric, it defaults to 0.
+ const ab = new ArrayBuffer(10);
+ const expected = Buffer.from(ab, 0, 0);
+ assert.deepStrictEqual(Buffer.from(ab, 0, 'fhqwhgads'), expected);
+ assert.deepStrictEqual(Buffer.from(ab, 0, NaN), expected);
+ assert.deepStrictEqual(Buffer.from(ab, 0, {}), expected);
+ assert.deepStrictEqual(Buffer.from(ab, 0, []), expected);
+
+ // If length can be converted to a number, it will be.
+ assert.deepStrictEqual(Buffer.from(ab, 0, [1]), Buffer.from(ab, 0, 1));
+
+ //If length is Infinity, throw.
+ common.expectsError(() => {
+ Buffer.from(ab, 0, Infinity);
+ }, {
+ code: 'ERR_BUFFER_OUT_OF_BOUNDS',
+ type: RangeError,
+ message: '"length" is outside of buffer bounds'
+ });
+}
+
-'use strict';
-var Buffer = require('../../').Buffer;
-
+// Copyright Joyent, Inc. and other Node contributors.var Buffer = require('../../').Buffer;
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
-var assert = require('assert');
+'use strict';
+require('./common');
+const assert = require('assert');
// ASCII conversion in node.js simply masks off the high bits,
// it doesn't do transliteration.
-assert.equal(Buffer.from('hérité').toString('ascii'), 'hC)ritC)');
+assert.strictEqual(Buffer.from('hérité').toString('ascii'), 'hC)ritC)');
// 71 characters, 78 bytes. The ’ character is a triple-byte sequence.
-var input = 'C’est, graphiquement, la réunion d’un accent aigu ' +
- 'et d’un accent grave.';
+const input = 'C’est, graphiquement, la réunion d’un accent aigu ' +
+ 'et d’un accent grave.';
-var expected = 'Cb\u0000\u0019est, graphiquement, la rC)union ' +
- 'db\u0000\u0019un accent aigu et db\u0000\u0019un ' +
- 'accent grave.';
+const expected = 'Cb\u0000\u0019est, graphiquement, la rC)union ' +
+ 'db\u0000\u0019un accent aigu et db\u0000\u0019un ' +
+ 'accent grave.';
-var buf = Buffer.from(input);
+const buf = Buffer.from(input);
-for (var i = 0; i < expected.length; ++i) {
- assert.equal(buf.slice(i).toString('ascii'), expected.slice(i));
+for (let i = 0; i < expected.length; ++i) {
+ assert.strictEqual(buf.slice(i).toString('ascii'), expected.slice(i));
// Skip remainder of multi-byte sequence.
if (input.charCodeAt(i) > 65535) ++i;
'use strict';
var Buffer = require('../../').Buffer;
+require('./common');
+const assert = require('assert');
+// Test hex strings and bad hex strings
+{
+ const buf = Buffer.alloc(4);
+ assert.strictEqual(buf.length, 4);
+ assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0]));
+ assert.strictEqual(buf.write('abcdxx', 0, 'hex'), 2);
+ assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0x00, 0x00]));
+ assert.strictEqual(buf.toString('hex'), 'abcd0000');
+ assert.strictEqual(buf.write('abcdef01', 0, 'hex'), 4);
+ assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0xef, 0x01]));
+ assert.strictEqual(buf.toString('hex'), 'abcdef01');
+
+ const copy = Buffer.from(buf.toString('hex'), 'hex');
+ assert.strictEqual(buf.toString('hex'), copy.toString('hex'));
+}
-var assert = require('assert');
-var Buffer = require('../../').Buffer;
+{
+ const buf = Buffer.alloc(5);
+ assert.strictEqual(buf.write('abcdxx', 1, 'hex'), 2);
+ assert.strictEqual(buf.toString('hex'), '00abcd0000');
+}
+
+{
+ const buf = Buffer.alloc(4);
+ assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0]));
+ assert.strictEqual(buf.write('xxabcd', 0, 'hex'), 0);
+ assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0]));
+ assert.strictEqual(buf.write('xxab', 1, 'hex'), 0);
+ assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0]));
+ assert.strictEqual(buf.write('cdxxab', 0, 'hex'), 1);
+ assert.deepStrictEqual(buf, new Buffer([0xcd, 0, 0, 0]));
+}
-// Test hex strings and bad hex strings
{
- var buf1 = Buffer.alloc(4);
- assert.strictEqual(buf1.length, 4);
- assert.deepStrictEqual(buf1, new Buffer([0, 0, 0, 0]));
- assert.strictEqual(buf1.write('abcdxx', 0, 'hex'), 2);
- assert.deepStrictEqual(buf1, new Buffer([0xab, 0xcd, 0x00, 0x00]));
- assert.strictEqual(buf1.toString('hex'), 'abcd0000');
- assert.strictEqual(buf1.write('abcdef01', 0, 'hex'), 4);
- assert.deepStrictEqual(buf1, new Buffer([0xab, 0xcd, 0xef, 0x01]));
- assert.strictEqual(buf1.toString('hex'), 'abcdef01');
-
- var buf2 = Buffer.from(buf1.toString('hex'), 'hex');
- assert.strictEqual(buf1.toString('hex'), buf2.toString('hex'));
-
- var buf3 = Buffer.alloc(5);
- assert.strictEqual(buf3.write('abcdxx', 1, 'hex'), 2);
- assert.strictEqual(buf3.toString('hex'), '00abcd0000');
-
- var buf4 = Buffer.alloc(4);
- assert.deepStrictEqual(buf4, new Buffer([0, 0, 0, 0]));
- assert.strictEqual(buf4.write('xxabcd', 0, 'hex'), 0);
- assert.deepStrictEqual(buf4, new Buffer([0, 0, 0, 0]));
- assert.strictEqual(buf4.write('xxab', 1, 'hex'), 0);
- assert.deepStrictEqual(buf4, new Buffer([0, 0, 0, 0]));
- assert.strictEqual(buf4.write('cdxxab', 0, 'hex'), 1);
- assert.deepStrictEqual(buf4, new Buffer([0xcd, 0, 0, 0]));
-
- var buf5 = Buffer.alloc(256);
- for (var i = 0; i < 256; i++)
- buf5[i] = i;
-
- var hex = buf5.toString('hex');
- assert.deepStrictEqual(Buffer.from(hex, 'hex'), buf5);
-
- var badHex = hex.slice(0, 256) + 'xx' + hex.slice(256, 510);
- assert.deepStrictEqual(Buffer.from(badHex, 'hex'), buf5.slice(0, 128));
+ const buf = Buffer.alloc(256);
+ for (let i = 0; i < 256; i++)
+ buf[i] = i;
+
+ const hex = buf.toString('hex');
+ assert.deepStrictEqual(Buffer.from(hex, 'hex'), buf);
+
+ const badHex = `${hex.slice(0, 256)}xx${hex.slice(256, 510)}`;
+ assert.deepStrictEqual(Buffer.from(badHex, 'hex'), buf.slice(0, 128));
}
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+
+require('./common');
+const assert = require('assert');
+const SlowBuffer = require('../../').SlowBuffer;
+
+// Test failed or zero-sized Buffer allocations not affecting typed arrays.
+// This test exists because of a regression that occurred. Because Buffer
+// instances are allocated with the same underlying allocator as TypedArrays,
+// but Buffer's can optional be non-zero filled, there was a regression that
+// occurred when a Buffer allocated failed, the internal flag specifying
+// whether or not to zero-fill was not being reset, causing TypedArrays to
+// allocate incorrectly.
+const zeroArray = new Uint32Array(10).fill(0);
+const sizes = [1e10, 0, 0.1, -1, 'a', undefined, null, NaN];
+const allocators = [
+ Buffer,
+ SlowBuffer,
+ Buffer.alloc,
+ Buffer.allocUnsafe,
+ Buffer.allocUnsafeSlow
+];
+for (const allocator of allocators) {
+ for (const size of sizes) {
+ try {
+ // These allocations are known to fail. If they do,
+ // Uint32Array should still produce a zeroed out result.
+ allocator(size);
+ } catch (e) {
+ assert.deepStrictEqual(new Uint32Array(10), zeroArray);
+ }
+ }
+}
+
'use strict';
var Buffer = require('../../').Buffer;
-
-
-var assert = require('assert');
+require('./common');
+const assert = require('assert');
function T(n) {
- var ui8 = new Uint8Array(n);
+ const ui8 = new Uint8Array(n);
Object.setPrototypeOf(ui8, T.prototype);
return ui8;
}
Object.setPrototypeOf(T, Buffer);
T.prototype.sum = function sum() {
- var cntr = 0;
- for (var i = 0; i < this.length; i++)
+ let cntr = 0;
+ for (let i = 0; i < this.length; i++)
cntr += this[i];
return cntr;
};
-var vals = [new T(4), T(4)];
+const vals = [new T(4), T(4)];
vals.forEach(function(t) {
- assert.equal(t.constructor, T);
- assert.equal(Object.getPrototypeOf(t), T.prototype);
- assert.equal(Object.getPrototypeOf(Object.getPrototypeOf(t)),
- Buffer.prototype);
+ assert.strictEqual(t.constructor, T);
+ assert.strictEqual(Object.getPrototypeOf(t), T.prototype);
+ assert.strictEqual(Object.getPrototypeOf(Object.getPrototypeOf(t)),
+ Buffer.prototype);
t.fill(5);
- var cntr = 0;
- for (var i = 0; i < t.length; i++)
+ let cntr = 0;
+ for (let i = 0; i < t.length; i++)
cntr += t[i];
- assert.equal(t.length * 5, cntr);
+ assert.strictEqual(t.length * 5, cntr);
// Check this does not throw
t.toString();
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+
+require('./common');
+const assert = require('assert');
+
+[
+ 'hex',
+ 'utf8',
+ 'utf-8',
+ 'ascii',
+ 'latin1',
+ 'binary',
+ 'base64',
+ 'ucs2',
+ 'ucs-2',
+ 'utf16le',
+ 'utf-16le'
+].forEach((enc) => {
+ assert.strictEqual(Buffer.isEncoding(enc), true);
+});
+
+[
+ 'utf9',
+ 'utf-7',
+ 'Unicode-FTW',
+ 'new gnu gun',
+ false,
+ NaN,
+ {},
+ Infinity,
+ [],
+ 1,
+ 0,
+ -1
+].forEach((enc) => {
+ assert.strictEqual(Buffer.isEncoding(enc), false);
+});
+
'use strict';
var Buffer = require('../../').Buffer;
+require('./common');
+const assert = require('assert');
-
-var assert = require('assert');
-
-var buffer = Buffer.from([1, 2, 3, 4, 5]);
-var arr;
-var b;
+const buffer = Buffer.from([1, 2, 3, 4, 5]);
+let arr;
+let b;
// buffers should be iterable
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+
+/*
+ * Fix for https://github.com/nodejs/node/issues/8266
+ *
+ * Zero length Buffer objects should expose the `buffer` property of the
+ * TypedArrays, via the `parent` property.
+ */
+require('./common');
+const assert = require('assert');
+
+// If the length of the buffer object is zero
+assert((new Buffer(0)).parent instanceof ArrayBuffer);
+
+// If the length of the buffer object is equal to the underlying ArrayBuffer
+assert((new Buffer(Buffer.poolSize)).parent instanceof ArrayBuffer);
+
+// Same as the previous test, but with user created buffer
+const arrayBuffer = new ArrayBuffer(0);
+assert.strictEqual(new Buffer(arrayBuffer).parent, arrayBuffer);
+assert.strictEqual(new Buffer(arrayBuffer).buffer, arrayBuffer);
+assert.strictEqual(Buffer.from(arrayBuffer).parent, arrayBuffer);
+assert.strictEqual(Buffer.from(arrayBuffer).buffer, arrayBuffer);
+
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+require('./common');
+
+// lib/buffer.js defines Buffer.prototype.inspect() to override how buffers are
+// presented by util.inspect().
+
+const assert = require('assert');
+const util = require('util');
+
+{
+ const buf = Buffer.from('fhqwhgads');
+ assert.strictEqual(util.inspect(buf), '<Buffer 66 68 71 77 68 67 61 64 73>');
+}
+
+{
+ const buf = Buffer.from('');
+ assert.strictEqual(util.inspect(buf), '<Buffer >');
+}
+
+{
+ const buf = Buffer.from('x'.repeat(51));
+ assert.ok(/^<Buffer (?:78 ){50}\.\.\. >$/.test(util.inspect(buf)));
+}
+
'use strict';
var Buffer = require('../../').Buffer;
+require('./common');
+const assert = require('assert');
-
-var assert = require('assert');
-
-var safe = Buffer.alloc(10);
+const safe = Buffer.alloc(10);
function isZeroFilled(buf) {
- for (var n = 0; n < buf.length; n++)
+ for (let n = 0; n < buf.length; n++)
if (buf[n] !== 0) return false;
return true;
}
--- /dev/null
+// Copyright Joyent, Inc. and other Node contributors.var Buffer = require('../../').Buffer;
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+'use strict';
+
+require('./common');
+const assert = require('assert');
+
+assert.strictEqual(0, Buffer.from('hello', 'utf8').slice(0, 0).length);
+assert.strictEqual(0, Buffer('hello', 'utf8').slice(0, 0).length);
+
+const buf = Buffer.from('0123456789', 'utf8');
+const expectedSameBufs = [
+ [buf.slice(-10, 10), Buffer.from('0123456789', 'utf8')],
+ [buf.slice(-20, 10), Buffer.from('0123456789', 'utf8')],
+ [buf.slice(-20, -10), Buffer.from('', 'utf8')],
+ [buf.slice(), Buffer.from('0123456789', 'utf8')],
+ [buf.slice(0), Buffer.from('0123456789', 'utf8')],
+ [buf.slice(0, 0), Buffer.from('', 'utf8')],
+ [buf.slice(undefined), Buffer.from('0123456789', 'utf8')],
+ [buf.slice('foobar'), Buffer.from('0123456789', 'utf8')],
+ [buf.slice(undefined, undefined), Buffer.from('0123456789', 'utf8')],
+ [buf.slice(2), Buffer.from('23456789', 'utf8')],
+ [buf.slice(5), Buffer.from('56789', 'utf8')],
+ [buf.slice(10), Buffer.from('', 'utf8')],
+ [buf.slice(5, 8), Buffer.from('567', 'utf8')],
+ [buf.slice(8, -1), Buffer.from('8', 'utf8')],
+ [buf.slice(-10), Buffer.from('0123456789', 'utf8')],
+ [buf.slice(0, -9), Buffer.from('0', 'utf8')],
+ [buf.slice(0, -10), Buffer.from('', 'utf8')],
+ [buf.slice(0, -1), Buffer.from('012345678', 'utf8')],
+ [buf.slice(2, -2), Buffer.from('234567', 'utf8')],
+ [buf.slice(0, 65536), Buffer.from('0123456789', 'utf8')],
+ [buf.slice(65536, 0), Buffer.from('', 'utf8')],
+ [buf.slice(-5, -8), Buffer.from('', 'utf8')],
+ [buf.slice(-5, -3), Buffer.from('56', 'utf8')],
+ [buf.slice(-10, 10), Buffer.from('0123456789', 'utf8')],
+ [buf.slice('0', '1'), Buffer.from('0', 'utf8')],
+ [buf.slice('-5', '10'), Buffer.from('56789', 'utf8')],
+ [buf.slice('-10', '10'), Buffer.from('0123456789', 'utf8')],
+ [buf.slice('-10', '-5'), Buffer.from('01234', 'utf8')],
+ [buf.slice('-10', '-0'), Buffer.from('', 'utf8')],
+ [buf.slice('111'), Buffer.from('', 'utf8')],
+ [buf.slice('0', '-111'), Buffer.from('', 'utf8')]
+];
+
+for (let i = 0, s = buf.toString(); i < buf.length; ++i) {
+ expectedSameBufs.push(
+ [buf.slice(i), Buffer.from(s.slice(i))],
+ [buf.slice(0, i), Buffer.from(s.slice(0, i))],
+ [buf.slice(-i), Buffer.from(s.slice(-i))],
+ [buf.slice(0, -i), Buffer.from(s.slice(0, -i))]
+ );
+}
+
+expectedSameBufs.forEach(([buf1, buf2]) => {
+ assert.strictEqual(0, Buffer.compare(buf1, buf2));
+});
+
+const utf16Buf = Buffer.from('0123456789', 'utf16le');
+assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer.from('012', 'utf16le'));
+// try to slice a zero length Buffer
+// see https://github.com/joyent/node/issues/5881
+assert.doesNotThrow(() => Buffer.alloc(0).slice(0, 1));
+assert.strictEqual(Buffer.alloc(0).slice(0, 1).length, 0);
+
+{
+ // Single argument slice
+ assert.strictEqual('bcde',
+ Buffer.from('abcde', 'utf8').slice(1).toString('utf8'));
+}
+
+// slice(0,0).length === 0
+assert.strictEqual(0, Buffer.from('hello', 'utf8').slice(0, 0).length);
+
+{
+ // Regression tests for https://github.com/nodejs/node/issues/9096
+ const buf = Buffer.from('abcd', 'utf8');
+ assert.strictEqual(buf.slice(buf.length / 3).toString('utf8'), 'bcd');
+ assert.strictEqual(
+ buf.slice(buf.length / 3, buf.length).toString(),
+ 'bcd'
+ );
+}
+
+{
+ const buf = Buffer.from('abcdefg', 'utf8');
+ assert.strictEqual(buf.slice(-(-1 >>> 0) - 1).toString('utf8'),
+ buf.toString('utf8'));
+}
+
+{
+ const buf = Buffer.from('abc', 'utf8');
+ assert.strictEqual(buf.slice(-0.5).toString('utf8'), buf.toString('utf8'));
+}
+
+{
+ const buf = Buffer.from([
+ 1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0,
+ 0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0
+ ]);
+ const chunk1 = Buffer.from([
+ 1, 29, 0, 0, 1, 143, 216, 162, 92, 254, 248, 63, 0
+ ]);
+ const chunk2 = Buffer.from([
+ 0, 0, 18, 184, 6, 0, 175, 29, 0, 8, 11, 1, 0, 0
+ ]);
+ const middle = buf.length / 2;
+
+ assert.deepStrictEqual(buf.slice(0, middle), chunk1);
+ assert.deepStrictEqual(buf.slice(middle), chunk2);
+}
+
'use strict';
var Buffer = require('../../').Buffer;
-
-
-var assert = require('assert');
+require('./common');
+const assert = require('assert');
// Test buffers small enough to use the JS implementation
-var buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
- 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);
-
-assert.strictEqual(buf, buf.swap16());
-assert.deepStrictEqual(buf, Buffer.from([0x02, 0x01, 0x04, 0x03, 0x06, 0x05,
- 0x08, 0x07, 0x0a, 0x09, 0x0c, 0x0b,
- 0x0e, 0x0d, 0x10, 0x0f]));
-buf.swap16(); // restore
-
-assert.strictEqual(buf, buf.swap32());
-assert.deepStrictEqual(buf, Buffer.from([0x04, 0x03, 0x02, 0x01, 0x08, 0x07,
- 0x06, 0x05, 0x0c, 0x0b, 0x0a, 0x09,
- 0x10, 0x0f, 0x0e, 0x0d]));
-buf.swap32(); // restore
-
-assert.strictEqual(buf, buf.swap64());
-assert.deepStrictEqual(buf, Buffer.from([0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
- 0x02, 0x01, 0x10, 0x0f, 0x0e, 0x0d,
- 0x0c, 0x0b, 0x0a, 0x09]));
+{
+ const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
+ 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);
+
+ assert.strictEqual(buf, buf.swap16());
+ assert.deepStrictEqual(buf, Buffer.from([0x02, 0x01, 0x04, 0x03, 0x06, 0x05,
+ 0x08, 0x07, 0x0a, 0x09, 0x0c, 0x0b,
+ 0x0e, 0x0d, 0x10, 0x0f]));
+ buf.swap16(); // restore
+
+ assert.strictEqual(buf, buf.swap32());
+ assert.deepStrictEqual(buf, Buffer.from([0x04, 0x03, 0x02, 0x01, 0x08, 0x07,
+ 0x06, 0x05, 0x0c, 0x0b, 0x0a, 0x09,
+ 0x10, 0x0f, 0x0e, 0x0d]));
+ buf.swap32(); // restore
+
+ assert.strictEqual(buf, buf.swap64());
+ assert.deepStrictEqual(buf, Buffer.from([0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
+ 0x02, 0x01, 0x10, 0x0f, 0x0e, 0x0d,
+ 0x0c, 0x0b, 0x0a, 0x09]));
+}
// Operates in-place
-var buf3 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7]);
-buf3.slice(1, 5).swap32();
-assert.deepStrictEqual(buf3, Buffer.from([0x1, 0x5, 0x4, 0x3, 0x2, 0x6, 0x7]));
-
-buf3.slice(1, 5).swap16();
-assert.deepStrictEqual(buf3, Buffer.from([0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7]));
-
-var buf3_64 = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
- 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
- 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
- 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);
-buf3_64.slice(2, 18).swap64();
-assert.deepStrictEqual(buf3_64, Buffer.from([0x01, 0x02, 0x0a, 0x09, 0x08, 0x07,
- 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
- 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b,
- 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
- 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
- 0x0f, 0x10]));
-
-// Force use of native code (Buffer size above threshold limit for js impl)
-var buf4A = new Uint32Array(256).fill(0x04030201);
-var buf4 = Buffer.from(buf4A.buffer, buf4A.byteOffset);
-var buf5A = new Uint32Array(256).fill(0x03040102);
-var buf5 = Buffer.from(buf5A.buffer, buf5A.byteOffset);
-
-buf4.swap16();
-assert.deepStrictEqual(buf4, buf5);
-
-var buf6A = new Uint32Array(256).fill(0x04030201);
-var buf6 = Buffer.from(buf6A.buffer);
-var bu7A = new Uint32Array(256).fill(0x01020304);
-var buf7 = Buffer.from(bu7A.buffer, bu7A.byteOffset);
-
-buf6.swap32();
-assert.deepStrictEqual(buf6, buf7);
-
-var buf8A = new Uint8Array(256 * 8);
-var buf9A = new Uint8Array(256 * 8);
-for (var i = 0; i < buf8A.length; i++) {
- buf8A[i] = i % 8;
- buf9A[buf9A.length - i - 1] = i % 8;
+{
+ const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7]);
+ buf.slice(1, 5).swap32();
+ assert.deepStrictEqual(buf, Buffer.from([0x1, 0x5, 0x4, 0x3, 0x2, 0x6, 0x7]));
+ buf.slice(1, 5).swap16();
+ assert.deepStrictEqual(buf, Buffer.from([0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7]));
+
+ // Length assertions
+ const re16 = /Buffer size must be a multiple of 16-bits/;
+ const re32 = /Buffer size must be a multiple of 32-bits/;
+ const re64 = /Buffer size must be a multiple of 64-bits/;
+
+ assert.throws(() => Buffer.from(buf).swap16(), re16);
+ assert.throws(() => Buffer.alloc(1025).swap16(), re16);
+ assert.throws(() => Buffer.from(buf).swap32(), re32);
+ assert.throws(() => buf.slice(1, 3).swap32(), re32);
+ assert.throws(() => Buffer.alloc(1025).swap32(), re32);
+ assert.throws(() => buf.slice(1, 3).swap64(), re64);
+ assert.throws(() => Buffer.alloc(1025).swap64(), re64);
}
-var buf8 = Buffer.from(buf8A.buffer, buf8A.byteOffset);
-var buf9 = Buffer.from(buf9A.buffer, buf9A.byteOffset);
-
-buf8.swap64();
-assert.deepStrictEqual(buf8, buf9);
-// Test native code with buffers that are not memory-aligned
-var buf10A = new Uint8Array(256 * 8);
-var buf11A = new Uint8Array(256 * 8 - 2);
-for (var i = 0; i < buf10A.length; i++) {
- buf10A[i] = i % 2;
-}
-for (var i = 1; i < buf11A.length; i++) {
- buf11A[buf11A.length - i] = (i + 1) % 2;
-}
-var buf10 = Buffer.from(buf10A.buffer, buf10A.byteOffset);
-// 0|1 0|1 0|1...
-var buf11 = Buffer.from(buf11A.buffer, buf11A.byteOffset);
-// 0|0 1|0 1|0...
+{
+ const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+ 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
+ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+ 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);
-buf10.slice(1, buf10.length - 1).swap16();
-assert.deepStrictEqual(buf10.slice(0, buf11.length), buf11);
+ buf.slice(2, 18).swap64();
+ assert.deepStrictEqual(buf, Buffer.from([0x01, 0x02, 0x0a, 0x09, 0x08, 0x07,
+ 0x06, 0x05, 0x04, 0x03, 0x02, 0x01,
+ 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b,
+ 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+ 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
+ 0x0f, 0x10]));
+}
-var buf12A = new Uint8Array(256 * 8);
-var buf13A = new Uint8Array(256 * 8 - 4);
-for (var i = 0; i < buf12A.length; i++) {
- buf12A[i] = i % 4;
+// Force use of native code (Buffer size above threshold limit for js impl)
+{
+ const bufData = new Uint32Array(256).fill(0x04030201);
+ const buf = Buffer.from(bufData.buffer, bufData.byteOffset);
+ const otherBufData = new Uint32Array(256).fill(0x03040102);
+ const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);
+ buf.swap16();
+ assert.deepStrictEqual(buf, otherBuf);
}
-for (var i = 1; i < buf13A.length; i++) {
- buf13A[buf13A.length - i] = (i + 1) % 4;
+
+{
+ const bufData = new Uint32Array(256).fill(0x04030201);
+ const buf = Buffer.from(bufData.buffer);
+ const otherBufData = new Uint32Array(256).fill(0x01020304);
+ const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);
+ buf.swap32();
+ assert.deepStrictEqual(buf, otherBuf);
}
-var buf12 = Buffer.from(buf12A.buffer, buf12A.byteOffset);
-// 0|1 2 3 0|1 2 3...
-var buf13 = Buffer.from(buf13A.buffer, buf13A.byteOffset);
-// 0|0 3 2 1|0 3 2...
-buf12.slice(1, buf12.length - 3).swap32();
-assert.deepStrictEqual(buf12.slice(0, buf13.length), buf13);
+{
+ const bufData = new Uint8Array(256 * 8);
+ const otherBufData = new Uint8Array(256 * 8);
+ for (let i = 0; i < bufData.length; i++) {
+ bufData[i] = i % 8;
+ otherBufData[otherBufData.length - i - 1] = i % 8;
+ }
+ const buf = Buffer.from(bufData.buffer, bufData.byteOffset);
+ const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);
+ buf.swap64();
+ assert.deepStrictEqual(buf, otherBuf);
+}
+// Test native code with buffers that are not memory-aligned
+{
+ const bufData = new Uint8Array(256 * 8);
+ const otherBufData = new Uint8Array(256 * 8 - 2);
+ for (let i = 0; i < bufData.length; i++) {
+ bufData[i] = i % 2;
+ }
+ for (let i = 1; i < otherBufData.length; i++) {
+ otherBufData[otherBufData.length - i] = (i + 1) % 2;
+ }
+ const buf = Buffer.from(bufData.buffer, bufData.byteOffset);
+ // 0|1 0|1 0|1...
+ const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);
+ // 0|0 1|0 1|0...
+
+ buf.slice(1, buf.length - 1).swap16();
+ assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf);
+}
-var buf14A = new Uint8Array(256 * 8);
-var buf15A = new Uint8Array(256 * 8 - 8);
-for (var i = 0; i < buf14A.length; i++) {
- buf14A[i] = i % 8;
+{
+ const bufData = new Uint8Array(256 * 8);
+ const otherBufData = new Uint8Array(256 * 8 - 4);
+ for (let i = 0; i < bufData.length; i++) {
+ bufData[i] = i % 4;
+ }
+ for (let i = 1; i < otherBufData.length; i++) {
+ otherBufData[otherBufData.length - i] = (i + 1) % 4;
+ }
+ const buf = Buffer.from(bufData.buffer, bufData.byteOffset);
+ // 0|1 2 3 0|1 2 3...
+ const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);
+ // 0|0 3 2 1|0 3 2...
+
+ buf.slice(1, buf.length - 3).swap32();
+ assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf);
}
-for (var i = 1; i < buf15A.length; i++) {
- buf15A[buf15A.length - i] = (i + 1) % 8;
+
+{
+ const bufData = new Uint8Array(256 * 8);
+ const otherBufData = new Uint8Array(256 * 8 - 8);
+ for (let i = 0; i < bufData.length; i++) {
+ bufData[i] = i % 8;
+ }
+ for (let i = 1; i < otherBufData.length; i++) {
+ otherBufData[otherBufData.length - i] = (i + 1) % 8;
+ }
+ const buf = Buffer.from(bufData.buffer, bufData.byteOffset);
+ // 0|1 2 3 4 5 6 7 0|1 2 3 4...
+ const otherBuf = Buffer.from(otherBufData.buffer, otherBufData.byteOffset);
+ // 0|0 7 6 5 4 3 2 1|0 7 6 5...
+
+ buf.slice(1, buf.length - 7).swap64();
+ assert.deepStrictEqual(buf.slice(0, otherBuf.length), otherBuf);
}
-var buf14 = Buffer.from(buf14A.buffer, buf14A.byteOffset);
-// 0|1 2 3 4 5 6 7 0|1 2 3 4...
-var buf15 = Buffer.from(buf15A.buffer, buf15A.byteOffset);
-// 0|0 7 6 5 4 3 2 1|0 7 6 5...
-
-buf14.slice(1, buf14.length - 7).swap64();
-assert.deepStrictEqual(buf14.slice(0, buf15.length), buf15);
-
-// Length assertions
-var re16 = /Buffer size must be a multiple of 16-bits/;
-var re32 = /Buffer size must be a multiple of 32-bits/;
-var re64 = /Buffer size must be a multiple of 64-bits/;
-
-assert.throws(() => Buffer.from(buf3).swap16(), re16);
-assert.throws(() => Buffer.alloc(1025).swap16(), re16);
-assert.throws(() => Buffer.from(buf3).swap32(), re32);
-assert.throws(() => buf3.slice(1, 3).swap32(), re32);
-assert.throws(() => Buffer.alloc(1025).swap32(), re32);
-assert.throws(() => buf3.slice(1, 3).swap64(), re64);
-assert.throws(() => Buffer.alloc(1025).swap64(), re64);
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+
+require('./common');
+const assert = require('assert');
+
+{
+ assert.strictEqual(JSON.stringify(Buffer.alloc(0)),
+ '{"type":"Buffer","data":[]}');
+ assert.strictEqual(JSON.stringify(Buffer.from([1, 2, 3, 4])),
+ '{"type":"Buffer","data":[1,2,3,4]}');
+}
+
+// issue GH-7849
+{
+ const buf = Buffer.from('test');
+ const json = JSON.stringify(buf);
+ const obj = JSON.parse(json);
+ const copy = Buffer.from(obj);
+
+ assert.deepStrictEqual(buf, copy);
+}
+
+// GH-5110
+{
+ const buffer = Buffer.from('test');
+ const string = JSON.stringify(buffer);
+
+ assert.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}');
+
+ function receiver(key, value) {
+ return value && value.type === 'Buffer' ? Buffer.from(value.data) : value;
+ }
+
+ assert.deepStrictEqual(buffer, JSON.parse(string, receiver));
+}
+
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+
+const common = require('./common');
+const assert = require('assert');
+
+// utf8, ucs2, ascii, latin1, utf16le
+const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1',
+ 'binary', 'utf16le', 'utf-16le'];
+
+encodings
+ .reduce((es, e) => es.concat(e, e.toUpperCase()), [])
+ .forEach((encoding) => {
+ assert.strictEqual(Buffer.from('foo', encoding).toString(encoding), 'foo');
+ });
+
+// base64
+['base64', 'BASE64'].forEach((encoding) => {
+ assert.strictEqual(Buffer.from('Zm9v', encoding).toString(encoding), 'Zm9v');
+});
+
+// hex
+['hex', 'HEX'].forEach((encoding) => {
+ assert.strictEqual(Buffer.from('666f6f', encoding).toString(encoding),
+ '666f6f');
+});
+
+// Invalid encodings
+for (let i = 1; i < 10; i++) {
+ const encoding = String(i).repeat(i);
+ const error = common.expectsError({
+ code: 'ERR_UNKNOWN_ENCODING',
+ type: TypeError,
+ message: `Unknown encoding: ${encoding}`
+ });
+ assert.ok(!Buffer.isEncoding(encoding));
+ assert.throws(() => Buffer.from('foo').toString(encoding), error);
+}
+
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+
+const common = require('./common');
+const assert = require('assert');
+
+const outsideBounds = common.expectsError({
+ code: 'ERR_BUFFER_OUT_OF_BOUNDS',
+ type: RangeError,
+ message: 'Attempt to write outside buffer bounds'
+}, 2);
+
+assert.throws(() => Buffer.alloc(9).write('foo', -1), outsideBounds);
+assert.throws(() => Buffer.alloc(9).write('foo', 10), outsideBounds);
+
+const resultMap = new Map([
+ ['utf8', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
+ ['ucs2', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])],
+ ['ascii', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
+ ['latin1', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
+ ['binary', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
+ ['utf16le', Buffer.from([102, 0, 111, 0, 111, 0, 0, 0, 0])],
+ ['base64', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])],
+ ['hex', Buffer.from([102, 111, 111, 0, 0, 0, 0, 0, 0])]
+]);
+
+// utf8, ucs2, ascii, latin1, utf16le
+const encodings = ['utf8', 'utf-8', 'ucs2', 'ucs-2', 'ascii', 'latin1',
+ 'binary', 'utf16le', 'utf-16le'];
+
+encodings
+ .reduce((es, e) => es.concat(e, e.toUpperCase()), [])
+ .forEach((encoding) => {
+ const buf = Buffer.alloc(9);
+ const len = Buffer.byteLength('foo', encoding);
+ assert.strictEqual(buf.write('foo', 0, len, encoding), len);
+
+ if (encoding.includes('-'))
+ encoding = encoding.replace('-', '');
+
+ assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
+ });
+
+// base64
+['base64', 'BASE64'].forEach((encoding) => {
+ const buf = Buffer.alloc(9);
+ const len = Buffer.byteLength('Zm9v', encoding);
+
+ assert.strictEqual(buf.write('Zm9v', 0, len, encoding), len);
+ assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
+});
+
+// hex
+['hex', 'HEX'].forEach((encoding) => {
+ const buf = Buffer.alloc(9);
+ const len = Buffer.byteLength('666f6f', encoding);
+
+ assert.strictEqual(buf.write('666f6f', 0, len, encoding), len);
+ assert.deepStrictEqual(buf, resultMap.get(encoding.toLowerCase()));
+});
+
+// Invalid encodings
+for (let i = 1; i < 10; i++) {
+ const encoding = String(i).repeat(i);
+ const error = common.expectsError({
+ code: 'ERR_UNKNOWN_ENCODING',
+ type: TypeError,
+ message: `Unknown encoding: ${encoding}`
+ });
+
+ assert.ok(!Buffer.isEncoding(encoding));
+ assert.throws(() => Buffer.alloc(9).write('foo', encoding), error);
+}
+
'use strict';
var Buffer = require('../../').Buffer;
-
// Flags: --zero-fill-buffers
// when using --zero-fill-buffers, every Buffer and SlowBuffer
// instance must be zero filled upon creation
-
-var SlowBuffer = require('../../').SlowBuffer;
-var assert = require('assert');
+require('./common');
+const SlowBuffer = require('../../').SlowBuffer;
+const assert = require('assert');
function isZeroFilled(buf) {
- for (var n = 0; n < buf.length; n++)
+ for (let n = 0; n < buf.length; n++)
if (buf[n] > 0) return false;
return true;
}
// allocated memory might just already happen to
// contain all zeroes. The test is run multiple
// times to improve the reliability.
-for (var i = 0; i < 50; i++) {
- var bufs = [
+for (let i = 0; i < 50; i++) {
+ const bufs = [
Buffer.alloc(20),
Buffer.allocUnsafe(20),
SlowBuffer(20),
Buffer(20),
new SlowBuffer(20)
];
- for (var buf of bufs) {
+ for (const buf of bufs) {
assert(isZeroFilled(buf));
}
}
'use strict';
var Buffer = require('../../').Buffer;
-
-
-var assert = require('assert');
+require('./common');
+const assert = require('assert');
function testUint8Array(ui) {
- var length = ui.length;
- for (var i = 0; i < length; i++)
+ const length = ui.length;
+ for (let i = 0; i < length; i++)
if (ui[i] !== 0) return false;
return true;
}
-for (var i = 0; i < 100; i++) {
+for (let i = 0; i < 100; i++) {
Buffer.alloc(0);
- var ui = new Uint8Array(65);
- assert.ok(testUint8Array(ui), 'Uint8Array is not zero-filled');
+ const ui = new Uint8Array(65);
+ assert.ok(testUint8Array(ui), `Uint8Array is not zero-filled: ${ui}`);
}
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+
+require('./common');
+const assert = require('assert');
+
+const buf1 = Buffer(100);
+const buf2 = new Buffer(100);
+
+for (let n = 0; n < buf1.length; n++)
+ assert.strictEqual(buf1[n], 0);
+
+for (let n = 0; n < buf2.length; n++)
+ assert.strictEqual(buf2[n], 0);
+