'use strict'; var common = require('../common'); var assert = require('assert'); var zero = []; var one = [ new Buffer('asdf') ]; var long = []; for (var i = 0; i < 10; i++) long.push(new Buffer('asdf')); var flatZero = Buffer.concat(zero); var flatOne = Buffer.concat(one); var flatLong = Buffer.concat(long); var flatLongLen = Buffer.concat(long, 40); assert(flatZero.length === 0); assert(flatOne.toString() === 'asdf'); // A special case where concat used to return the first item, // if the length is one. This check is to make sure that we don't do that. assert(flatOne !== one[0]); assert(flatLong.toString() === (new Array(10 + 1).join('asdf'))); assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf'))); assert.throws(function() { Buffer.concat([42]); }, TypeError); const random10 = common.hasCrypto ? require('crypto').randomBytes(10) : Buffer.alloc(10, 1); const empty = Buffer.alloc(0); assert.notDeepStrictEqual(random10, empty); assert.notDeepStrictEqual(random10, Buffer.alloc(10)); assert.deepStrictEqual(Buffer.concat([], 100), empty); assert.deepStrictEqual(Buffer.concat([random10], 0), empty); assert.deepStrictEqual(Buffer.concat([random10], 10), random10); assert.deepStrictEqual(Buffer.concat([random10, random10], 10), random10); assert.deepStrictEqual(Buffer.concat([empty, random10]), random10); assert.deepStrictEqual(Buffer.concat([random10, empty, empty]), random10); // The tail should be zero-filled assert.deepStrictEqual(Buffer.concat([empty], 100), Buffer.alloc(100)); assert.deepStrictEqual(Buffer.concat([empty], 4096), Buffer.alloc(4096)); assert.deepStrictEqual( Buffer.concat([random10], 40), Buffer.concat([random10, Buffer.alloc(30)])); console.log('ok');