var split = require('split')
var through = require('through2')
-var url = 'https://api.github.com/repos/nodejs/io.js/contents'
+var url = 'https://api.github.com/repos/nodejs/node/contents'
var dirs = [
'/test/parallel',
'/test/pummel'
]
cp.execSync('rm -rf node/*.js', { cwd: path.join(__dirname, '../test') })
-cp.execSync('rm -rf node-es6/*.js', { cwd: path.join(__dirname, '../test') })
var httpOpts = {
headers: {
files.forEach(function (file) {
if (!/test-buffer.*/.test(file.name)) return
- var out
- if (file.name === 'test-buffer-iterator.js' ||
- file.name === 'test-buffer-arraybuffer.js') {
- out = path.join(__dirname, '../test/node-es6', file.name)
- } else if (file.name === 'test-buffer-fakes.js') {
+ if (file.name === 'test-buffer-fakes.js') {
// These teses only apply to node, where they're calling into C++ and need to
// ensure the prototype can't be faked, or else there will be a segfault.
return
- } else {
- out = path.join(__dirname, '../test/node', file.name)
}
console.log(file.download_url)
+
+ var out = path.join(__dirname, '../test/node', file.name)
hyperquest(file.download_url, httpOpts)
.pipe(split())
.pipe(testfixer(file.name))
if (firstline) {
// require buffer explicitly
- var preamble = 'if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;\n' +
- 'var Buffer = require(\'../../\').Buffer;'
+ var preamble = 'var Buffer = require(\'../../\').Buffer;\n' +
+ 'if (!Buffer.TYPED_ARRAY_SUPPORT) return;'
if (/use strict/.test(line)) line += '\n' + preamble
else line + preamble + '\n' + line
firstline = false
// use `var` instead of `const`/`let`
line = line.replace(/(const|let) /g, 'var ')
- // make `require('common')` work
+ // make `var common = require('common')` work
line = line.replace(/(var common = require.*)/g, 'var common = {};')
- // use `Buffer.isBuffer` instead of `instanceof Buffer`
- line = line.replace(/buf instanceof Buffer/g, 'Buffer.isBuffer(buf)')
+ // make `require('../common')` work
+ line = line.replace(/require\('\.\.\/common'\);/g, '')
// require browser buffer
line = line.replace(/(.*)require\('buffer'\)(.*)/g, '$1require(\'../../\')$2')
- // smalloc is only used for kMaxLength
- line = line.replace(
- /require\('smalloc'\)/g,
- '{ kMaxLength: process.env.OBJECT_IMPL ? 0x3fffffff : 0x7fffffff }'
- )
-
// comment out console logs
line = line.replace(/(.*console\..*)/g, '// $1')
line = line.replace(/(.*new Float64Array.*RangeError.*)/, '// $1')
}
- // https://github.com/iojs/io.js/blob/v0.12/test/parallel/test-buffer.js#L38
- // we can't run this because we need to support
- // browsers that don't have typed arrays
- if (filename === 'test-buffer.js') {
- line = line.replace(/b\[0\] = -1;/, 'b[0] = 255;')
- }
-
- // https://github.com/iojs/io.js/blob/v0.12/test/parallel/test-buffer.js#L1138
- // unfortunately we can't run this as it touches
- // node streams which do an instanceof check
- // and crypto-browserify doesn't work in old
+ // https://github.com/nodejs/node/blob/v0.12/test/parallel/test-buffer.js#L1138
+ // unfortunately we can't run this because crypto-browserify doesn't work in old
// versions of ie
if (filename === 'test-buffer.js') {
line = line.replace(/^(\s*)(var crypto = require.*)/, '$1// $2')
exports.Buffer = Buffer
exports.SlowBuffer = SlowBuffer
exports.INSPECT_MAX_BYTES = 50
-Buffer.poolSize = 8192 // not used by this implementation
-
-var rootParent = {}
/**
* If `Buffer.TYPED_ARRAY_SUPPORT`:
? global.TYPED_ARRAY_SUPPORT
: typedArraySupport()
+/*
+ * Export kMaxLength after typed array support is determined.
+ */
+exports.kMaxLength = kMaxLength()
+
function typedArraySupport () {
try {
var arr = new Uint8Array(1)
}
function createBuffer (that, length) {
+ if (kMaxLength() < length) {
+ throw new RangeError('Invalid typed array length')
+ }
if (Buffer.TYPED_ARRAY_SUPPORT) {
// Return an augmented `Uint8Array` instance, for best performance
that = new Uint8Array(length)
that.length = length
}
- var fromPool = length !== 0 && length <= Buffer.poolSize >>> 1
- if (fromPool) that.parent = rootParent
-
return that
}
*/
function Buffer (arg, encodingOrOffset, length) {
- var that
- if (Buffer.TYPED_ARRAY_SUPPORT) {
- that = null
- } else {
- if (!(this instanceof Buffer)) {
- return new Buffer(arg, encodingOrOffset, length)
- }
- that = this
+ if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
+ return new Buffer(arg, encodingOrOffset, length)
}
// Common case.
'If encoding is specified then the first argument must be a string'
)
}
- return allocUnsafe(that, arg)
+ return allocUnsafe(this, arg)
}
- return from(that, arg, encodingOrOffset, length)
+ return from(this, arg, encodingOrOffset, length)
}
+Buffer.poolSize = 8192 // not used by this implementation
+
// TODO: Legacy, not needed anymore. Remove in next major version.
Buffer._augment = function (arr) {
arr.__proto__ = Buffer.prototype
configurable: true
})
}
-} else {
- Buffer.prototype.length = undefined
- Buffer.prototype.parent = undefined
}
function assertSize (size) {
return that
}
-function fromArrayBuffer (that, array) {
+function fromArrayBuffer (that, array, byteOffset, length) {
array.byteLength // this throws if `array` is not a valid ArrayBuffer
+ if (byteOffset < 0 || array.byteLength < byteOffset) {
+ throw new RangeError('\'offset\' is out of bounds')
+ }
+
+ if (array.byteLength < byteOffset + (length || 0)) {
+ throw new RangeError('\'length\' is out of bounds')
+ }
+
if (Buffer.TYPED_ARRAY_SUPPORT) {
// Return an augmented `Uint8Array` instance, for best performance
- that = new Uint8Array(array)
+ if (length === undefined) {
+ that = new Uint8Array(array, byteOffset)
+ } else {
+ that = new Uint8Array(array, byteOffset, length)
+ }
that.__proto__ = Buffer.prototype
} else {
// Fallback: Return an object instance of the Buffer class
- that = fromArrayLike(that, new Uint8Array(array))
+ that = fromArrayLike(that, new Uint8Array(array, byteOffset, length))
}
return that
}
}
}
- throw new TypeError('Must start with number, buffer, array or string')
+ throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
}
function checked (length) {
return length | 0
}
-function SlowBuffer (subject, encoding) {
- if (!(this instanceof SlowBuffer)) return new SlowBuffer(subject, encoding)
-
- var buf = new Buffer(subject, encoding)
- delete buf.parent
- return buf
+function SlowBuffer (length) {
+ if (+length != length) { // eslint-disable-line eqeqeq
+ length = 0
+ }
+ return Buffer.alloc(+length)
}
Buffer.isBuffer = function isBuffer (b) {
if (Buffer.isBuffer(string)) {
return string.length
}
+ if (typeof ArrayBuffer !== 'undefined' &&
+ (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
+ return string.byteLength
+ }
if (typeof string !== 'string') {
string = '' + string
}
return '<Buffer ' + str + '>'
}
-Buffer.prototype.compare = function compare (b) {
- if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
- return Buffer.compare(this, b)
+Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
+ if (!Buffer.isBuffer(target)) {
+ throw new TypeError('Argument must be a Buffer')
+ }
+
+ if (start === undefined) {
+ start = 0
+ }
+ if (end === undefined) {
+ end = target ? target.length : 0
+ }
+ if (thisStart === undefined) {
+ thisStart = 0
+ }
+ if (thisEnd === undefined) {
+ thisEnd = this.length
+ }
+
+ if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
+ throw new RangeError('out of range index')
+ }
+
+ if (thisStart >= thisEnd && start >= end) {
+ return 0
+ }
+ if (thisStart >= thisEnd) {
+ return -1
+ }
+ if (start >= end) {
+ return 1
+ }
+
+ start >>>= 0
+ end >>>= 0
+ thisStart >>>= 0
+ thisEnd >>>= 0
+
+ if (this === target) return 0
+
+ var x = thisEnd - thisStart
+ var y = end - start
+ var len = Math.min(x, y)
+
+ var thisCopy = this.slice(thisStart, thisEnd)
+ var targetCopy = target.slice(start, end)
+
+ for (var i = 0; i < len; ++i) {
+ if (thisCopy[i] !== targetCopy[i]) {
+ x = thisCopy[i]
+ y = targetCopy[i]
+ break
+ }
+ }
+
+ if (x < y) return -1
+ if (y < x) return 1
+ return 0
}
-function arrayIndexOf (arr, val, byteOffset) {
+function arrayIndexOf (arr, val, byteOffset, encoding) {
+ var indexSize = 1
+ var arrLength = arr.length
+ var valLength = val.length
+
+ if (encoding !== undefined) {
+ encoding = String(encoding).toLowerCase()
+ if (encoding === 'ucs2' || encoding === 'ucs-2' ||
+ encoding === 'utf16le' || encoding === 'utf-16le') {
+ if (arr.length < 2 || val.length < 2) {
+ return -1
+ }
+ indexSize = 2
+ arrLength /= 2
+ valLength /= 2
+ byteOffset /= 2
+ }
+ }
+
+ function read (buf, i) {
+ if (indexSize === 1) {
+ return buf[i]
+ } else {
+ return buf.readUInt16BE(i * indexSize)
+ }
+ }
+
var foundIndex = -1
- for (var i = 0; byteOffset + i < arr.length; i++) {
- if (arr[byteOffset + i] === val[foundIndex === -1 ? 0 : i - foundIndex]) {
+ for (var i = 0; byteOffset + i < arrLength; i++) {
+ if (read(arr, byteOffset + i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
if (foundIndex === -1) foundIndex = i
- if (i - foundIndex + 1 === val.length) return byteOffset + foundIndex
+ if (i - foundIndex + 1 === valLength) return (byteOffset + foundIndex) * indexSize
} else {
+ if (foundIndex !== -1) i -= i - foundIndex
foundIndex = -1
}
}
if (byteOffset < 0) byteOffset = Math.max(this.length + byteOffset, 0)
if (typeof val === 'string') {
- if (val.length === 0) return -1 // special case: looking for empty string always fails
- if (encoding !== undefined) {
- val = new Buffer(val, encoding).toString()
- }
- return String.prototype.indexOf.call(this, val, byteOffset)
+ val = Buffer.from(val, encoding)
}
+
if (Buffer.isBuffer(val)) {
- return arrayIndexOf(this, val, byteOffset)
+ // special case: looking for empty string/buffer always fails
+ if (val.length === 0) {
+ return -1
+ }
+ return arrayIndexOf(this, val, byteOffset, encoding)
}
if (typeof val === 'number') {
if (Buffer.TYPED_ARRAY_SUPPORT && Uint8Array.prototype.indexOf === 'function') {
return Uint8Array.prototype.indexOf.call(this, val, byteOffset)
}
- return arrayIndexOf(this, [ val ], byteOffset)
+ return arrayIndexOf(this, [ val ], byteOffset, encoding)
}
throw new TypeError('val must be string, number or Buffer')
}
for (var i = 0; i < length; i++) {
var parsed = parseInt(string.substr(i * 2, 2), 16)
- if (isNaN(parsed)) throw new Error('Invalid hex string')
+ if (isNaN(parsed)) return i
buf[offset + i] = parsed
}
return i
}
}
- if (newBuf.length) newBuf.parent = this.parent || this
-
return newBuf
}
}
// Invalid ranges are not set to a default, so can range check early.
- if (start < 0 || start >= this.length || end < 0 || end > this.length) {
+ if (start < 0 || this.length < start || this.length < end) {
throw new RangeError('Out of range index')
}
return this
}
+ start = start >>> 0
+ end = end === undefined ? this.length : end >>> 0
+
if (!val) val = 0
- if (!start) start = 0
- if (!end) end = this.length
var i
if (typeof val === 'number') {
? val
: utf8ToBytes(new Buffer(val, encoding).toString())
var len = bytes.length
- for (i = start; i < end; i++) {
- this[i] = bytes[i % len]
+ for (i = 0; i < end - start; i++) {
+ this[i + start] = bytes[i % len]
}
}
"test": "standard && node ./bin/test.js",
"test-browser": "zuul -- test/*.js test/node/*.js",
"test-browser-local": "zuul --local -- test/*.js test/node/*.js",
- "test-node": "tape test/*.js test/node/*.js test/node-es6/*.js && OBJECT_IMPL=true tape test/*.js test/node/*.js",
+ "test-node": "tape test/*.js test/node/*.js && OBJECT_IMPL=true tape test/*.js",
"perf": "browserify --debug perf/bracket-notation.js > perf/bundle.js && open perf/index.html",
"perf-node": "node perf/bracket-notation.js && node perf/concat.js && node perf/copy-big.js && node perf/copy.js && node perf/new-big.js && node perf/new.js && node perf/readDoubleBE.js && node perf/readFloatBE.js && node perf/readUInt32LE.js && node perf/slice.js && node perf/writeFloatBE.js",
"size": "browserify -r ./ | uglifyjs -c -m | gzip | wc -c"
"standard": {
"ignore": [
"test/node/*.js",
- "test/node-es6/*.js",
"test/_polyfill.js",
"perf/*.js"
]
+++ /dev/null
-node buffer tests that require ES6 (e.g. "for..of" construct)
+++ /dev/null
-'use strict';
-if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
-var Buffer = require('../../').Buffer;
-
-var common = {};
-var assert = require('assert');
-
-var Buffer = require('../../').Buffer;
-var LENGTH = 16;
-
-var ab = new ArrayBuffer(LENGTH);
-var dv = new DataView(ab);
-var ui = new Uint8Array(ab);
-var buf = new Buffer(ab);
-
-
-assert.ok(Buffer.isBuffer(buf));
-// For backwards compatibility of old .parent property test that if buf is not
-// a slice then .parent should be undefined.
-assert.equal(buf.parent, undefined);
-assert.equal(buf.buffer, ab);
-assert.equal(buf.length, ab.byteLength);
-
-
-buf.fill(0xC);
-for (var i = 0; i < LENGTH; i++) {
- assert.equal(ui[i], 0xC);
- ui[i] = 0xF;
- assert.equal(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);
-
-
-// Now test protecting users from doing stupid things
-
-assert.throws(function() {
- function AB() { }
- AB.__proto__ = ArrayBuffer;
- AB.prototype.__proto__ = ArrayBuffer.prototype;
- new Buffer(new AB());
-}, TypeError);
-
+++ /dev/null
-node core buffer tests
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+var common = {};
+var assert = require('assert');
+
+var Buffer = require('../../').Buffer;
+
+// counter to ensure unique value is always copied
+var cntr = 0;
+
+var b = Buffer.allocUnsafe(1024);
+
+// console.log('b.length == %d', b.length);
+assert.strictEqual(1024, b.length);
+
+b[0] = -1;
+assert.strictEqual(b[0], 255);
+
+for (var i = 0; i < 1024; i++) {
+ b[i] = i % 256;
+}
+
+for (var i = 0; i < 1024; i++) {
+ assert.strictEqual(i % 256, b[i]);
+}
+
+var c = Buffer.allocUnsafe(512);
+// console.log('c.length == %d', c.length);
+assert.strictEqual(512, c.length);
+
+var d = Buffer.from([]);
+assert.strictEqual(0, d.length);
+
+var ui32 = new Uint32Array(4).fill(42);
+var e = Buffer.from(ui32);
+assert.deepEqual(ui32, e);
+
+// First check Buffer#fill() works as expected.
+
+assert.throws(function() {
+ Buffer.allocUnsafe(8).fill('a', -1);
+});
+
+assert.throws(function() {
+ Buffer.allocUnsafe(8).fill('a', 0, 9);
+});
+
+// Make sure this doesn't hang indefinitely.
+Buffer.allocUnsafe(8).fill('');
+Buffer.alloc(8, '');
+
+{
+ var buf = Buffer.alloc(64, 10);
+ for (var i = 0; i < buf.length; i++)
+ assert.equal(buf[i], 10);
+
+ buf.fill(11, 0, buf.length >> 1);
+ for (var i = 0; i < buf.length >> 1; i++)
+ assert.equal(buf[i], 11);
+ for (var i = (buf.length >> 1) + 1; i < buf.length; i++)
+ assert.equal(buf[i], 10);
+
+ buf.fill('h');
+ for (var i = 0; i < buf.length; i++)
+ assert.equal('h'.charCodeAt(0), buf[i]);
+
+ buf.fill(0);
+ for (var i = 0; i < buf.length; i++)
+ assert.equal(0, buf[i]);
+
+ buf.fill(null);
+ for (var i = 0; i < buf.length; i++)
+ assert.equal(0, buf[i]);
+
+ buf.fill(1, 16, 32);
+ for (var i = 0; i < 16; i++)
+ assert.equal(0, buf[i]);
+ for (var i = 16; i < 32; i++)
+ assert.equal(1, buf[i]);
+ for (var i = 32; i < buf.length; i++)
+ assert.equal(0, buf[i]);
+}
+
+{
+ var buf = Buffer.alloc(10, 'abc');
+ assert.equal(buf.toString(), 'abcabcabca');
+ buf.fill('է');
+ assert.equal(buf.toString(), 'էէէէէ');
+}
+
+{
+ // copy 512 bytes, from 0 to 512.
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c, 0, 0, 512);
+// console.log('copied %d bytes from b into c', copied);
+ assert.strictEqual(512, copied);
+ for (var i = 0; i < c.length; i++) {
+ assert.strictEqual(b[i], c[i]);
+ }
+}
+
+{
+ // copy c into b, without specifying sourceEnd
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = c.copy(b, 0, 0);
+// console.log('copied %d bytes from c into b w/o sourceEnd', copied);
+ assert.strictEqual(c.length, copied);
+ for (var i = 0; i < c.length; i++) {
+ assert.strictEqual(c[i], b[i]);
+ }
+}
+
+{
+ // copy c into b, without specifying sourceStart
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = c.copy(b, 0);
+// console.log('copied %d bytes from c into b w/o sourceStart', copied);
+ assert.strictEqual(c.length, copied);
+ for (var i = 0; i < c.length; i++) {
+ assert.strictEqual(c[i], b[i]);
+ }
+}
+
+{
+ // copy longer buffer b to shorter c without targetStart
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c);
+// console.log('copied %d bytes from b into c w/o targetStart', copied);
+ assert.strictEqual(c.length, copied);
+ for (var i = 0; i < c.length; i++) {
+ assert.strictEqual(b[i], c[i]);
+ }
+}
+
+{
+ // copy starting near end of b to c
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
+// console.log('copied %d bytes from end of b into beginning of c', copied);
+ assert.strictEqual(Math.floor(c.length / 2), copied);
+ for (var i = 0; i < Math.floor(c.length / 2); i++) {
+ assert.strictEqual(b[b.length - Math.floor(c.length / 2) + i], c[i]);
+ }
+ for (var i = Math.floor(c.length / 2) + 1; i < c.length; i++) {
+ assert.strictEqual(c[c.length - 1], c[i]);
+ }
+}
+
+{
+ // try to copy 513 bytes, and check we don't overrun c
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c, 0, 0, 513);
+// console.log('copied %d bytes from b trying to overrun c', copied);
+ assert.strictEqual(c.length, copied);
+ for (var i = 0; i < c.length; i++) {
+ assert.strictEqual(b[i], c[i]);
+ }
+}
+
+{
+ // copy 768 bytes from b into b
+ b.fill(++cntr);
+ b.fill(++cntr, 256);
+ var copied = b.copy(b, 0, 256, 1024);
+// console.log('copied %d bytes from b into b', copied);
+ assert.strictEqual(768, copied);
+ for (var i = 0; i < b.length; i++) {
+ assert.strictEqual(cntr, b[i]);
+ }
+}
+
+// copy string longer than buffer length (failure will segfault)
+var bb = Buffer.allocUnsafe(10);
+bb.fill('hello crazy world');
+
+
+// try to copy from before the beginning of b
+assert.doesNotThrow(() => { b.copy(c, 0, 100, 10); });
+
+// copy throws at negative sourceStart
+assert.throws(function() {
+ Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, -1);
+}, RangeError);
+
+{
+ // check sourceEnd resets to targetEnd if former is greater than the latter
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c, 0, 0, 1025);
+// console.log('copied %d bytes from b into c', copied);
+ for (var i = 0; i < c.length; i++) {
+ assert.strictEqual(b[i], c[i]);
+ }
+}
+
+// throw with negative sourceEnd
+// console.log('test copy at negative sourceEnd');
+assert.throws(function() {
+ b.copy(c, 0, 0, -1);
+}, RangeError);
+
+// when sourceStart is greater than sourceEnd, zero copied
+assert.equal(b.copy(c, 0, 100, 10), 0);
+
+// when targetStart > targetLength, zero copied
+assert.equal(b.copy(c, 512, 0, 10), 0);
+
+var caught_error;
+
+// invalid encoding for Buffer.toString
+caught_error = null;
+try {
+ b.toString('invalid');
+} catch (err) {
+ caught_error = err;
+}
+assert.strictEqual('Unknown encoding: invalid', caught_error.message);
+
+// invalid encoding for Buffer.write
+caught_error = null;
+try {
+ b.write('test string', 0, 5, 'invalid');
+} catch (err) {
+ caught_error = err;
+}
+assert.strictEqual('Unknown encoding: invalid', caught_error.message);
+
+// try to create 0-length buffers
+Buffer.from('');
+Buffer.from('', 'ascii');
+Buffer.from('', 'binary');
+Buffer.alloc(0);
+Buffer.allocUnsafe(0);
+
+// try to write a 0-length string beyond the end of b
+assert.throws(function() {
+ b.write('', 2048);
+}, RangeError);
+
+// throw when writing to negative offset
+assert.throws(function() {
+ b.write('a', -1);
+}, RangeError);
+
+// throw when writing past bounds from the pool
+assert.throws(function() {
+ b.write('a', 2048);
+}, RangeError);
+
+// throw when writing to negative offset
+assert.throws(function() {
+ b.write('a', -1);
+}, RangeError);
+
+// try to copy 0 bytes worth of data into an empty buffer
+b.copy(Buffer.alloc(0), 0, 0, 0);
+
+// try to copy 0 bytes past the end of the target buffer
+b.copy(Buffer.alloc(0), 1, 1, 1);
+b.copy(Buffer.alloc(1), 1, 1, 1);
+
+// try to copy 0 bytes from past the end of the source buffer
+b.copy(Buffer.alloc(1), 0, 2048, 2048);
+
+var rangeBuffer = Buffer.from('abc');
+
+// if start >= buffer's length, empty string will be returned
+assert.equal(rangeBuffer.toString('ascii', 3), '');
+assert.equal(rangeBuffer.toString('ascii', +Infinity), '');
+assert.equal(rangeBuffer.toString('ascii', 3.14, 3), '');
+assert.equal(rangeBuffer.toString('ascii', 'Infinity', 3), '');
+
+// if end <= 0, empty string will be returned
+assert.equal(rangeBuffer.toString('ascii', 1, 0), '');
+assert.equal(rangeBuffer.toString('ascii', 1, -1.2), '');
+assert.equal(rangeBuffer.toString('ascii', 1, -100), '');
+assert.equal(rangeBuffer.toString('ascii', 1, -Infinity), '');
+
+// if start < 0, start will be taken as zero
+assert.equal(rangeBuffer.toString('ascii', -1, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', -1.99, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', -Infinity, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', '-1', 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', '-1.99', 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', '-Infinity', 3), 'abc');
+
+// if start is an invalid integer, start will be taken as zero
+assert.equal(rangeBuffer.toString('ascii', 'node.js', 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', {}, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', [], 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', NaN, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', null, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', undefined, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', false, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', '', 3), 'abc');
+
+// but, if start is an integer when coerced, then it will be coerced and used.
+assert.equal(rangeBuffer.toString('ascii', '-1', 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', '1', 3), 'bc');
+assert.equal(rangeBuffer.toString('ascii', '-Infinity', 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', '3', 3), '');
+assert.equal(rangeBuffer.toString('ascii', Number(3), 3), '');
+assert.equal(rangeBuffer.toString('ascii', '3.14', 3), '');
+assert.equal(rangeBuffer.toString('ascii', '1.99', 3), 'bc');
+assert.equal(rangeBuffer.toString('ascii', '-1.99', 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 1.99, 3), 'bc');
+assert.equal(rangeBuffer.toString('ascii', true, 3), 'bc');
+
+// if end > buffer's length, end will be taken as buffer's length
+assert.equal(rangeBuffer.toString('ascii', 0, 5), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, 6.99), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, Infinity), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, '5'), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, '6.99'), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, 'Infinity'), 'abc');
+
+// if end is an invalid integer, end will be taken as buffer's length
+assert.equal(rangeBuffer.toString('ascii', 0, 'node.js'), '');
+assert.equal(rangeBuffer.toString('ascii', 0, {}), '');
+assert.equal(rangeBuffer.toString('ascii', 0, NaN), '');
+assert.equal(rangeBuffer.toString('ascii', 0, undefined), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, null), '');
+assert.equal(rangeBuffer.toString('ascii', 0, []), '');
+assert.equal(rangeBuffer.toString('ascii', 0, false), '');
+assert.equal(rangeBuffer.toString('ascii', 0, ''), '');
+
+// but, if end is an integer when coerced, then it will be coerced and used.
+assert.equal(rangeBuffer.toString('ascii', 0, '-1'), '');
+assert.equal(rangeBuffer.toString('ascii', 0, '1'), 'a');
+assert.equal(rangeBuffer.toString('ascii', 0, '-Infinity'), '');
+assert.equal(rangeBuffer.toString('ascii', 0, '3'), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, Number(3)), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, '3.14'), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, '1.99'), 'a');
+assert.equal(rangeBuffer.toString('ascii', 0, '-1.99'), '');
+assert.equal(rangeBuffer.toString('ascii', 0, 1.99), 'a');
+assert.equal(rangeBuffer.toString('ascii', 0, true), 'a');
+
+// try toString() with a object as a encoding
+assert.equal(rangeBuffer.toString({toString: function() {
+ return 'ascii';
+}}), 'abc');
+
+// testing for smart defaults and ability to pass string values as offset
+var writeTest = Buffer.from('abcdes');
+writeTest.write('n', 'ascii');
+writeTest.write('o', '1', 'ascii');
+writeTest.write('d', '2', 'ascii');
+writeTest.write('e', 3, 'ascii');
+writeTest.write('j', 4, 'ascii');
+assert.equal(writeTest.toString(), 'nodejs');
+
+// ASCII slice test
+{
+ var asciiString = 'hello world';
+
+ for (var i = 0; i < asciiString.length; i++) {
+ b[i] = asciiString.charCodeAt(i);
+ }
+ var asciiSlice = b.toString('ascii', 0, asciiString.length);
+ assert.equal(asciiString, asciiSlice);
+}
+
+{
+ var asciiString = 'hello world';
+ var offset = 100;
+
+ var written = b.write(asciiString, offset, 'ascii');
+ assert.equal(asciiString.length, written);
+ var asciiSlice = b.toString('ascii', offset, offset + asciiString.length);
+ assert.equal(asciiString, asciiSlice);
+}
+
+{
+ var asciiString = 'hello world';
+ var offset = 100;
+
+ var sliceA = b.slice(offset, offset + asciiString.length);
+ var sliceB = b.slice(offset, offset + asciiString.length);
+ for (var i = 0; i < asciiString.length; i++) {
+ assert.equal(sliceA[i], sliceB[i]);
+ }
+}
+
+// UTF-8 slice test
+
+var utf8String = '¡hέlló wôrld!';
+var offset = 100;
+
+b.write(utf8String, 0, Buffer.byteLength(utf8String), 'utf8');
+var utf8Slice = b.toString('utf8', 0, Buffer.byteLength(utf8String));
+assert.equal(utf8String, utf8Slice);
+
+var written = b.write(utf8String, offset, 'utf8');
+assert.equal(Buffer.byteLength(utf8String), written);
+utf8Slice = b.toString('utf8', offset, offset + Buffer.byteLength(utf8String));
+assert.equal(utf8String, utf8Slice);
+
+var sliceA = b.slice(offset, offset + Buffer.byteLength(utf8String));
+var sliceB = b.slice(offset, offset + Buffer.byteLength(utf8String));
+for (var i = 0; i < Buffer.byteLength(utf8String); i++) {
+ assert.equal(sliceA[i], sliceB[i]);
+}
+
+{
+ var slice = b.slice(100, 150);
+ assert.equal(50, slice.length);
+ for (var i = 0; i < 50; i++) {
+ assert.equal(b[100 + i], slice[i]);
+ }
+}
+
+{
+ // make sure only top level parent propagates from allocPool
+ var b = Buffer.allocUnsafe(5);
+ var c = b.slice(0, 4);
+ var d = c.slice(0, 2);
+ assert.equal(b.parent, c.parent);
+ assert.equal(b.parent, d.parent);
+}
+
+{
+ // also from a non-pooled instance
+ var b = Buffer.allocUnsafeSlow(5);
+ var c = b.slice(0, 4);
+ var d = c.slice(0, 2);
+ assert.equal(c.parent, d.parent);
+}
+
+{
+ // Bug regression test
+ var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語
+ var buffer = Buffer.allocUnsafe(32);
+ var size = buffer.write(testValue, 0, 'utf8');
+// console.log('bytes written to buffer: ' + size);
+ var slice = buffer.toString('utf8', 0, size);
+ assert.equal(slice, testValue);
+}
+
+{
+ // Test triple slice
+ var a = Buffer.allocUnsafe(8);
+ for (var i = 0; i < 8; i++) a[i] = i;
+ var b = a.slice(4, 8);
+ assert.equal(4, b[0]);
+ assert.equal(5, b[1]);
+ assert.equal(6, b[2]);
+ assert.equal(7, b[3]);
+ var c = b.slice(2, 4);
+ assert.equal(6, c[0]);
+ assert.equal(7, c[1]);
+}
+
+{
+ var d = Buffer.from([23, 42, 255]);
+ assert.equal(d.length, 3);
+ assert.equal(d[0], 23);
+ assert.equal(d[1], 42);
+ assert.equal(d[2], 255);
+ assert.deepEqual(d, Buffer.from(d));
+}
+
+{
+ var e = Buffer.from('über');
+// console.error('uber: \'%s\'', e.toString());
+ assert.deepEqual(e, Buffer.from([195, 188, 98, 101, 114]));
+}
+
+{
+ var f = Buffer.from('über', 'ascii');
+// console.error('f.length: %d (should be 4)', f.length);
+ assert.deepEqual(f, Buffer.from([252, 98, 101, 114]));
+}
+
+['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
+ {
+ var f = Buffer.from('über', encoding);
+// console.error('f.length: %d (should be 8)', f.length);
+ assert.deepEqual(f, Buffer.from([252, 0, 98, 0, 101, 0, 114, 0]));
+ }
+
+ {
+ var f = Buffer.from('привет', encoding);
+// console.error('f.length: %d (should be 12)', f.length);
+ assert.deepEqual(f,
+ Buffer.from([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]));
+ assert.equal(f.toString(encoding), 'привет');
+ }
+
+ {
+ var f = Buffer.from([0, 0, 0, 0, 0]);
+ assert.equal(f.length, 5);
+ var size = f.write('あいうえお', encoding);
+// console.error('bytes written to buffer: %d (should be 4)', size);
+ assert.equal(size, 4);
+ assert.deepEqual(f, Buffer.from([0x42, 0x30, 0x44, 0x30, 0x00]));
+ }
+});
+
+{
+ var f = Buffer.from('\uD83D\uDC4D', 'utf-16le'); // THUMBS UP SIGN (U+1F44D)
+ assert.equal(f.length, 4);
+ assert.deepEqual(f, Buffer.from('3DD84DDC', 'hex'));
+}
+
+
+var arrayIsh = {0: 0, 1: 1, 2: 2, 3: 3, length: 4};
+var g = Buffer.from(arrayIsh);
+assert.deepEqual(g, Buffer.from([0, 1, 2, 3]));
+var strArrayIsh = {0: '0', 1: '1', 2: '2', 3: '3', length: 4};
+g = Buffer.from(strArrayIsh);
+assert.deepEqual(g, Buffer.from([0, 1, 2, 3]));
+
+
+//
+// Test toString('base64')
+//
+assert.equal('TWFu', (Buffer.from('Man')).toString('base64'));
+
+{
+ // test that regular and URL-safe base64 both work
+ var expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff];
+ assert.deepEqual(Buffer.from('//++/++/++//', 'base64'),
+ Buffer.from(expected));
+ assert.deepEqual(Buffer.from('__--_--_--__', 'base64'),
+ Buffer.from(expected));
+}
+
+{
+ // big example
+ var quote = 'Man is distinguished, not only by his reason, but by this ' +
+ 'singular passion from other animals, which is a lust ' +
+ 'of the mind, that by a perseverance of delight in the ' +
+ 'continued and indefatigable generation of knowledge, ' +
+ 'exceeds the short vehemence of any carnal pleasure.';
+ var expected = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb' +
+ '24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci' +
+ 'BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQ' +
+ 'gYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu' +
+ 'dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZ' +
+ 'GdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm' +
+ '5hbCBwbGVhc3VyZS4=';
+ assert.equal(expected, (Buffer.from(quote)).toString('base64'));
+
+ var b = Buffer.allocUnsafe(1024);
+ var bytesWritten = b.write(expected, 0, 'base64');
+ assert.equal(quote.length, bytesWritten);
+ assert.equal(quote, b.toString('ascii', 0, quote.length));
+
+ // check that the base64 decoder ignores whitespace
+ var expectedWhite = expected.slice(0, 60) + ' \n' +
+ expected.slice(60, 120) + ' \n' +
+ expected.slice(120, 180) + ' \n' +
+ expected.slice(180, 240) + ' \n' +
+ expected.slice(240, 300) + '\n' +
+ expected.slice(300, 360) + '\n';
+ b = Buffer.allocUnsafe(1024);
+ bytesWritten = b.write(expectedWhite, 0, 'base64');
+ assert.equal(quote.length, bytesWritten);
+ assert.equal(quote, b.toString('ascii', 0, quote.length));
+
+ // check that the base64 decoder on the constructor works
+ // even in the presence of whitespace.
+ b = Buffer.from(expectedWhite, 'base64');
+ assert.equal(quote.length, b.length);
+ assert.equal(quote, b.toString('ascii', 0, quote.length));
+
+ // check that the base64 decoder ignores illegal chars
+ var expectedIllegal = expected.slice(0, 60) + ' \x80' +
+ expected.slice(60, 120) + ' \xff' +
+ expected.slice(120, 180) + ' \x00' +
+ expected.slice(180, 240) + ' \x98' +
+ expected.slice(240, 300) + '\x03' +
+ expected.slice(300, 360);
+ b = Buffer.from(expectedIllegal, 'base64');
+ assert.equal(quote.length, b.length);
+ assert.equal(quote, b.toString('ascii', 0, quote.length));
+}
+
+assert.equal(Buffer.from('', 'base64').toString(), '');
+assert.equal(Buffer.from('K', 'base64').toString(), '');
+
+// multiple-of-4 with padding
+assert.equal(Buffer.from('Kg==', 'base64').toString(), '*');
+assert.equal(Buffer.from('Kio=', 'base64').toString(), '**');
+assert.equal(Buffer.from('Kioq', 'base64').toString(), '***');
+assert.equal(Buffer.from('KioqKg==', 'base64').toString(), '****');
+assert.equal(Buffer.from('KioqKio=', 'base64').toString(), '*****');
+assert.equal(Buffer.from('KioqKioq', 'base64').toString(), '******');
+assert.equal(Buffer.from('KioqKioqKg==', 'base64').toString(), '*******');
+assert.equal(Buffer.from('KioqKioqKio=', 'base64').toString(), '********');
+assert.equal(Buffer.from('KioqKioqKioq', 'base64').toString(), '*********');
+assert.equal(Buffer.from('KioqKioqKioqKg==', 'base64').toString(),
+ '**********');
+assert.equal(Buffer.from('KioqKioqKioqKio=', 'base64').toString(),
+ '***********');
+assert.equal(Buffer.from('KioqKioqKioqKioq', 'base64').toString(),
+ '************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKg==', 'base64').toString(),
+ '*************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKio=', 'base64').toString(),
+ '**************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKioq', 'base64').toString(),
+ '***************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKioqKg==', 'base64').toString(),
+ '****************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKioqKio=', 'base64').toString(),
+ '*****************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKioqKioq', 'base64').toString(),
+ '******************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKioqKioqKg==', 'base64').toString(),
+ '*******************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKioqKioqKio=', 'base64').toString(),
+ '********************');
+
+// no padding, not a multiple of 4
+assert.equal(Buffer.from('Kg', 'base64').toString(), '*');
+assert.equal(Buffer.from('Kio', 'base64').toString(), '**');
+assert.equal(Buffer.from('KioqKg', 'base64').toString(), '****');
+assert.equal(Buffer.from('KioqKio', 'base64').toString(), '*****');
+assert.equal(Buffer.from('KioqKioqKg', 'base64').toString(), '*******');
+assert.equal(Buffer.from('KioqKioqKio', 'base64').toString(), '********');
+assert.equal(Buffer.from('KioqKioqKioqKg', 'base64').toString(), '**********');
+assert.equal(Buffer.from('KioqKioqKioqKio', 'base64').toString(),
+ '***********');
+assert.equal(Buffer.from('KioqKioqKioqKioqKg', 'base64').toString(),
+ '*************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKio', 'base64').toString(),
+ '**************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKioqKg', 'base64').toString(),
+ '****************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKioqKio', 'base64').toString(),
+ '*****************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKioqKioqKg', 'base64').toString(),
+ '*******************');
+assert.equal(Buffer.from('KioqKioqKioqKioqKioqKioqKio', 'base64').toString(),
+ '********************');
+
+// handle padding graciously, multiple-of-4 or not
+assert.equal(Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw==',
+ 'base64').length, 32);
+assert.equal(Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw=',
+ 'base64').length, 32);
+assert.equal(Buffer.from('72INjkR5fchcxk9+VgdGPFJDxUBFR5/rMFsghgxADiw',
+ 'base64').length, 32);
+assert.equal(Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg==',
+ 'base64').length, 31);
+assert.equal(Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg=',
+ 'base64').length, 31);
+assert.equal(Buffer.from('w69jACy6BgZmaFvv96HG6MYksWytuZu3T1FvGnulPg',
+ 'base64').length, 31);
+
+// This string encodes single '.' character in UTF-16
+var dot = Buffer.from('//4uAA==', 'base64');
+assert.equal(dot[0], 0xff);
+assert.equal(dot[1], 0xfe);
+assert.equal(dot[2], 0x2e);
+assert.equal(dot[3], 0x00);
+assert.equal(dot.toString('base64'), '//4uAA==');
+
+{
+ // Writing base64 at a position > 0 should not mangle the result.
+ //
+ // https://github.com/joyent/node/issues/402
+ var segments = ['TWFkbmVzcz8h', 'IFRoaXM=', 'IGlz', 'IG5vZGUuanMh'];
+ var b = Buffer.allocUnsafe(64);
+ var pos = 0;
+
+ for (var i = 0; i < segments.length; ++i) {
+ pos += b.write(segments[i], pos, 'base64');
+ }
+ assert.equal(b.toString('binary', 0, pos), 'Madness?! This is node.js!');
+}
+
+// Regression test for https://github.com/nodejs/node/issues/3496.
+// TODO assert.equal(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0);
+
+{
+ // Creating buffers larger than pool size.
+ var l = Buffer.poolSize + 5;
+ var s = '';
+ for (var i = 0; i < l; i++) {
+ s += 'h';
+ }
+
+ var b = Buffer.from(s);
+
+ for (var i = 0; i < l; i++) {
+ assert.equal('h'.charCodeAt(0), b[i]);
+ }
+
+ var sb = b.toString();
+ assert.equal(sb.length, s.length);
+ assert.equal(sb, s);
+}
+
+{
+ // Single argument slice
+ var b = Buffer.from('abcde');
+ assert.equal('bcde', b.slice(1).toString());
+}
+
+// slice(0,0).length === 0
+assert.equal(0, Buffer.from('hello').slice(0, 0).length);
+
+// test hex toString
+// console.log('Create hex string from buffer');
+var hexb = Buffer.allocUnsafe(256);
+for (var i = 0; i < 256; i++) {
+ hexb[i] = i;
+}
+var hexStr = hexb.toString('hex');
+assert.equal(hexStr,
+ '000102030405060708090a0b0c0d0e0f' +
+ '101112131415161718191a1b1c1d1e1f' +
+ '202122232425262728292a2b2c2d2e2f' +
+ '303132333435363738393a3b3c3d3e3f' +
+ '404142434445464748494a4b4c4d4e4f' +
+ '505152535455565758595a5b5c5d5e5f' +
+ '606162636465666768696a6b6c6d6e6f' +
+ '707172737475767778797a7b7c7d7e7f' +
+ '808182838485868788898a8b8c8d8e8f' +
+ '909192939495969798999a9b9c9d9e9f' +
+ 'a0a1a2a3a4a5a6a7a8a9aaabacadaeaf' +
+ 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' +
+ 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' +
+ 'd0d1d2d3d4d5d6d7d8d9dadbdcdddedf' +
+ 'e0e1e2e3e4e5e6e7e8e9eaebecedeeef' +
+ 'f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff');
+
+// console.log('Create buffer from hex string');
+var hexb2 = Buffer.from(hexStr, 'hex');
+for (var i = 0; i < 256; i++) {
+ assert.equal(hexb2[i], hexb[i]);
+}
+
+{
+ // test an invalid slice end.
+// console.log('Try to slice off the end of the buffer');
+ var b = Buffer.from([1, 2, 3, 4, 5]);
+ var b2 = b.toString('hex', 1, 10000);
+ var b3 = b.toString('hex', 1, 5);
+ var b4 = b.toString('hex', 1);
+ assert.equal(b2, b3);
+ assert.equal(b2, b4);
+}
+
+function buildBuffer(data) {
+ if (Array.isArray(data)) {
+ var buffer = Buffer.allocUnsafe(data.length);
+ data.forEach(function(v, k) {
+ buffer[k] = v;
+ });
+ return buffer;
+ }
+ return null;
+}
+
+var x = buildBuffer([0x81, 0xa3, 0x66, 0x6f, 0x6f, 0xa3, 0x62, 0x61, 0x72]);
+
+// console.log(x.inspect());
+assert.equal('<Buffer 81 a3 66 6f 6f a3 62 61 72>', x.inspect());
+
+{
+ var z = x.slice(4);
+// console.log(z.inspect());
+// console.log(z.length);
+ assert.equal(5, z.length);
+ assert.equal(0x6f, z[0]);
+ assert.equal(0xa3, z[1]);
+ assert.equal(0x62, z[2]);
+ assert.equal(0x61, z[3]);
+ assert.equal(0x72, z[4]);
+}
+
+{
+ var z = x.slice(0);
+// console.log(z.inspect());
+// console.log(z.length);
+ assert.equal(z.length, x.length);
+}
+
+{
+ var z = x.slice(0, 4);
+// console.log(z.inspect());
+// console.log(z.length);
+ assert.equal(4, z.length);
+ assert.equal(0x81, z[0]);
+ assert.equal(0xa3, z[1]);
+}
+
+{
+ var z = x.slice(0, 9);
+// console.log(z.inspect());
+// console.log(z.length);
+ assert.equal(9, z.length);
+}
+
+{
+ var z = x.slice(1, 4);
+// console.log(z.inspect());
+// console.log(z.length);
+ assert.equal(3, z.length);
+ assert.equal(0xa3, z[0]);
+}
+
+{
+ var z = x.slice(2, 4);
+// console.log(z.inspect());
+// console.log(z.length);
+ assert.equal(2, z.length);
+ assert.equal(0x66, z[0]);
+ assert.equal(0x6f, z[1]);
+}
+
+assert.equal(0, Buffer.from('hello').slice(0, 0).length);
+
+['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
+ var b = Buffer.allocUnsafe(10);
+ b.write('あいうえお', encoding);
+ assert.equal(b.toString(encoding), 'あいうえお');
+});
+
+{
+ // Binary encoding should write only one byte per character.
+ var b = Buffer.from([0xde, 0xad, 0xbe, 0xef]);
+ var s = String.fromCharCode(0xffff);
+ b.write(s, 0, 'binary');
+ assert.equal(0xff, b[0]);
+ assert.equal(0xad, b[1]);
+ assert.equal(0xbe, b[2]);
+ assert.equal(0xef, b[3]);
+ s = String.fromCharCode(0xaaee);
+ b.write(s, 0, 'binary');
+ assert.equal(0xee, b[0]);
+ assert.equal(0xad, b[1]);
+ assert.equal(0xbe, b[2]);
+ assert.equal(0xef, b[3]);
+}
+
+{
+ // #1210 Test UTF-8 string includes null character
+ var buf = Buffer.from('\0');
+ assert.equal(buf.length, 1);
+ buf = Buffer.from('\0\0');
+ assert.equal(buf.length, 2);
+}
+
+{
+ var buf = Buffer.allocUnsafe(2);
+ var written = buf.write(''); // 0byte
+ assert.equal(written, 0);
+ written = buf.write('\0'); // 1byte (v8 adds null terminator)
+ assert.equal(written, 1);
+ written = buf.write('a\0'); // 1byte * 2
+ assert.equal(written, 2);
+ written = buf.write('あ'); // 3bytes
+ assert.equal(written, 0);
+ written = buf.write('\0あ'); // 1byte + 3bytes
+ assert.equal(written, 1);
+ written = buf.write('\0\0あ'); // 1byte * 2 + 3bytes
+ assert.equal(written, 2);
+}
+
+{
+ var buf = Buffer.allocUnsafe(10);
+ written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator)
+ assert.equal(written, 9);
+ written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
+ assert.equal(written, 10);
+}
+
+{
+ // #243 Test write() with maxLength
+ var buf = Buffer.allocUnsafe(4);
+ buf.fill(0xFF);
+ var written = buf.write('abcd', 1, 2, 'utf8');
+// console.log(buf);
+ assert.equal(written, 2);
+ assert.equal(buf[0], 0xFF);
+ assert.equal(buf[1], 0x61);
+ assert.equal(buf[2], 0x62);
+ assert.equal(buf[3], 0xFF);
+
+ buf.fill(0xFF);
+ written = buf.write('abcd', 1, 4);
+// console.log(buf);
+ assert.equal(written, 3);
+ assert.equal(buf[0], 0xFF);
+ assert.equal(buf[1], 0x61);
+ assert.equal(buf[2], 0x62);
+ assert.equal(buf[3], 0x63);
+
+ buf.fill(0xFF);
+ written = buf.write('abcd', 1, 2, 'utf8');
+// console.log(buf);
+ assert.equal(written, 2);
+ assert.equal(buf[0], 0xFF);
+ assert.equal(buf[1], 0x61);
+ assert.equal(buf[2], 0x62);
+ assert.equal(buf[3], 0xFF);
+
+ buf.fill(0xFF);
+ written = buf.write('abcdef', 1, 2, 'hex');
+// console.log(buf);
+ assert.equal(written, 2);
+ assert.equal(buf[0], 0xFF);
+ assert.equal(buf[1], 0xAB);
+ assert.equal(buf[2], 0xCD);
+ assert.equal(buf[3], 0xFF);
+
+ ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
+ buf.fill(0xFF);
+ written = buf.write('abcd', 0, 2, encoding);
+// console.log(buf);
+ assert.equal(written, 2);
+ assert.equal(buf[0], 0x61);
+ assert.equal(buf[1], 0x00);
+ assert.equal(buf[2], 0xFF);
+ assert.equal(buf[3], 0xFF);
+ });
+}
+
+{
+ // test offset returns are correct
+ var b = Buffer.allocUnsafe(16);
+ assert.equal(4, b.writeUInt32LE(0, 0));
+ assert.equal(6, b.writeUInt16LE(0, 4));
+ assert.equal(7, b.writeUInt8(0, 6));
+ assert.equal(8, b.writeInt8(0, 7));
+ assert.equal(16, b.writeDoubleLE(0, 8));
+}
+
+{
+ // test unmatched surrogates not producing invalid utf8 output
+ // ef bf bd = utf-8 representation of unicode replacement character
+ // see https://codereview.chromium.org/121173009/
+ var buf = Buffer.from('ab\ud800cd', 'utf8');
+ assert.equal(buf[0], 0x61);
+ assert.equal(buf[1], 0x62);
+ assert.equal(buf[2], 0xef);
+ assert.equal(buf[3], 0xbf);
+ assert.equal(buf[4], 0xbd);
+ assert.equal(buf[5], 0x63);
+ assert.equal(buf[6], 0x64);
+}
+
+{
+ // test for buffer overrun
+ var buf = Buffer.from([0, 0, 0, 0, 0]); // length: 5
+ var sub = buf.slice(0, 4); // length: 4
+ written = sub.write('12345', 'binary');
+ assert.equal(written, 4);
+ assert.equal(buf[4], 0);
+}
+
+// Check for fractional length args, junk length args, etc.
+// https://github.com/joyent/node/issues/1758
+
+// Call .fill() first, stops valgrind warning about uninitialized memory reads.
+Buffer.allocUnsafe(3.3).fill().toString();
+ // throws bad argument error in commit 43cb4ec
+Buffer.alloc(3.3).fill().toString();
+assert.equal(Buffer.allocUnsafe(-1).length, 0);
+assert.equal(Buffer.allocUnsafe(NaN).length, 0);
+assert.equal(Buffer.allocUnsafe(3.3).length, 3);
+assert.equal(Buffer.from({length: 3.3}).length, 3);
+assert.equal(Buffer.from({length: 'BAM'}).length, 0);
+
+// Make sure that strings are not coerced to numbers.
+assert.equal(Buffer.from('99').length, 2);
+assert.equal(Buffer.from('13.37').length, 5);
+
+// Ensure that the length argument is respected.
+'ascii utf8 hex base64 binary'.split(' ').forEach(function(enc) {
+ assert.equal(Buffer.allocUnsafe(1).write('aaaaaa', 0, 1, enc), 1);
+});
+
+{
+ // Regression test, guard against buffer overrun in the base64 decoder.
+ var a = Buffer.allocUnsafe(3);
+ var b = Buffer.from('xxx');
+ a.write('aaaaaaaa', 'base64');
+ assert.equal(b.toString(), 'xxx');
+}
+
+// issue GH-3416
+Buffer.from(Buffer.allocUnsafe(0), 0, 0);
+
+[ 'hex',
+ 'utf8',
+ 'utf-8',
+ 'ascii',
+ 'binary',
+ 'base64',
+ 'ucs2',
+ 'ucs-2',
+ 'utf16le',
+ 'utf-16le' ].forEach(function(enc) {
+ assert.equal(Buffer.isEncoding(enc), true);
+ });
+
+[ 'utf9',
+ 'utf-7',
+ 'Unicode-FTW',
+ 'new gnu gun' ].forEach(function(enc) {
+ assert.equal(Buffer.isEncoding(enc), false);
+ });
+
+
+// GH-5110
+(function() {
+ var buffer = Buffer.from('test');
+ var string = JSON.stringify(buffer);
+
+ assert.equal(string, '{"type":"Buffer","data":[116,101,115,116]}');
+
+ assert.deepEqual(buffer, JSON.parse(string, function(key, value) {
+ return value && value.type === 'Buffer'
+ ? Buffer.from(value.data)
+ : value;
+ }));
+})();
+
+// issue GH-7849
+(function() {
+ var buf = Buffer.from('test');
+ var json = JSON.stringify(buf);
+ var obj = JSON.parse(json);
+ var copy = Buffer.from(obj);
+
+ assert(buf.equals(copy));
+})();
+
+// issue GH-4331
+assert.throws(function() {
+ Buffer.allocUnsafe(0xFFFFFFFF);
+}, RangeError);
+assert.throws(function() {
+ Buffer.allocUnsafe(0xFFFFFFFFF);
+}, RangeError);
+
+
+// attempt to overflow buffers, similar to previous bug in array buffers
+assert.throws(function() {
+ var buf = Buffer.allocUnsafe(8);
+ buf.readFloatLE(0xffffffff);
+}, RangeError);
+
+assert.throws(function() {
+ var buf = Buffer.allocUnsafe(8);
+ buf.writeFloatLE(0.0, 0xffffffff);
+}, RangeError);
+
+assert.throws(function() {
+ var buf = Buffer.allocUnsafe(8);
+ buf.readFloatLE(0xffffffff);
+}, RangeError);
+
+assert.throws(function() {
+ var buf = Buffer.allocUnsafe(8);
+ buf.writeFloatLE(0.0, 0xffffffff);
+}, RangeError);
+
+
+// ensure negative values can't get past offset
+assert.throws(function() {
+ var buf = Buffer.allocUnsafe(8);
+ buf.readFloatLE(-1);
+}, RangeError);
+
+assert.throws(function() {
+ var buf = Buffer.allocUnsafe(8);
+ buf.writeFloatLE(0.0, -1);
+}, RangeError);
+
+assert.throws(function() {
+ var buf = Buffer.allocUnsafe(8);
+ buf.readFloatLE(-1);
+}, RangeError);
+
+assert.throws(function() {
+ var buf = Buffer.allocUnsafe(8);
+ buf.writeFloatLE(0.0, -1);
+}, RangeError);
+
+// offset checks
+{
+ var buf = Buffer.allocUnsafe(0);
+
+ assert.throws(function() { buf.readUInt8(0); }, RangeError);
+ assert.throws(function() { buf.readInt8(0); }, RangeError);
+}
+
+{
+ var buf = Buffer.from([0xFF]);
+
+ assert.equal(buf.readUInt8(0), 255);
+ assert.equal(buf.readInt8(0), -1);
+}
+
+[16, 32].forEach(function(bits) {
+ var buf = Buffer.allocUnsafe(bits / 8 - 1);
+
+ assert.throws(function() { buf['readUInt' + bits + 'BE'](0); },
+ RangeError,
+ 'readUInt' + bits + 'BE');
+
+ assert.throws(function() { buf['readUInt' + bits + 'LE'](0); },
+ RangeError,
+ 'readUInt' + bits + 'LE');
+
+ assert.throws(function() { buf['readInt' + bits + 'BE'](0); },
+ RangeError,
+ 'readInt' + bits + 'BE()');
+
+ assert.throws(function() { buf['readInt' + bits + 'LE'](0); },
+ RangeError,
+ 'readInt' + bits + 'LE()');
+});
+
+[16, 32].forEach(function(bits) {
+ var buf = Buffer.from([0xFF, 0xFF, 0xFF, 0xFF]);
+
+ assert.equal(buf['readUInt' + bits + 'BE'](0),
+ (0xFFFFFFFF >>> (32 - bits)));
+
+ assert.equal(buf['readUInt' + bits + 'LE'](0),
+ (0xFFFFFFFF >>> (32 - bits)));
+
+ assert.equal(buf['readInt' + bits + 'BE'](0),
+ (0xFFFFFFFF >> (32 - bits)));
+
+ assert.equal(buf['readInt' + bits + 'LE'](0),
+ (0xFFFFFFFF >> (32 - bits)));
+});
+
+// test for common read(U)IntLE/BE
+(function() {
+ var buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06]);
+
+ assert.equal(buf.readUIntLE(0, 1), 0x01);
+ assert.equal(buf.readUIntBE(0, 1), 0x01);
+ assert.equal(buf.readUIntLE(0, 3), 0x030201);
+ assert.equal(buf.readUIntBE(0, 3), 0x010203);
+ assert.equal(buf.readUIntLE(0, 5), 0x0504030201);
+ assert.equal(buf.readUIntBE(0, 5), 0x0102030405);
+ assert.equal(buf.readUIntLE(0, 6), 0x060504030201);
+ assert.equal(buf.readUIntBE(0, 6), 0x010203040506);
+ assert.equal(buf.readIntLE(0, 1), 0x01);
+ assert.equal(buf.readIntBE(0, 1), 0x01);
+ assert.equal(buf.readIntLE(0, 3), 0x030201);
+ assert.equal(buf.readIntBE(0, 3), 0x010203);
+ assert.equal(buf.readIntLE(0, 5), 0x0504030201);
+ assert.equal(buf.readIntBE(0, 5), 0x0102030405);
+ assert.equal(buf.readIntLE(0, 6), 0x060504030201);
+ assert.equal(buf.readIntBE(0, 6), 0x010203040506);
+})();
+
+// test for common write(U)IntLE/BE
+(function() {
+ var buf = Buffer.allocUnsafe(3);
+ buf.writeUIntLE(0x123456, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
+ assert.equal(buf.readUIntLE(0, 3), 0x123456);
+
+ buf = Buffer.allocUnsafe(3);
+ buf.writeUIntBE(0x123456, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);
+ assert.equal(buf.readUIntBE(0, 3), 0x123456);
+
+ buf = Buffer.allocUnsafe(3);
+ buf.writeIntLE(0x123456, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
+ assert.equal(buf.readIntLE(0, 3), 0x123456);
+
+ buf = Buffer.allocUnsafe(3);
+ buf.writeIntBE(0x123456, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);
+ assert.equal(buf.readIntBE(0, 3), 0x123456);
+
+ buf = Buffer.allocUnsafe(3);
+ buf.writeIntLE(-0x123456, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]);
+ assert.equal(buf.readIntLE(0, 3), -0x123456);
+
+ buf = Buffer.allocUnsafe(3);
+ buf.writeIntBE(-0x123456, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]);
+ assert.equal(buf.readIntBE(0, 3), -0x123456);
+
+ buf = Buffer.allocUnsafe(3);
+ buf.writeIntLE(-0x123400, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]);
+ assert.equal(buf.readIntLE(0, 3), -0x123400);
+
+ buf = Buffer.allocUnsafe(3);
+ buf.writeIntBE(-0x123400, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]);
+ assert.equal(buf.readIntBE(0, 3), -0x123400);
+
+ buf = Buffer.allocUnsafe(3);
+ buf.writeIntLE(-0x120000, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0x00, 0x00, 0xee]);
+ assert.equal(buf.readIntLE(0, 3), -0x120000);
+
+ buf = Buffer.allocUnsafe(3);
+ buf.writeIntBE(-0x120000, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0xee, 0x00, 0x00]);
+ assert.equal(buf.readIntBE(0, 3), -0x120000);
+
+ buf = Buffer.allocUnsafe(5);
+ buf.writeUIntLE(0x1234567890, 0, 5);
+ assert.deepEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);
+ assert.equal(buf.readUIntLE(0, 5), 0x1234567890);
+
+ buf = Buffer.allocUnsafe(5);
+ buf.writeUIntBE(0x1234567890, 0, 5);
+ assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);
+ assert.equal(buf.readUIntBE(0, 5), 0x1234567890);
+
+ buf = Buffer.allocUnsafe(5);
+ buf.writeIntLE(0x1234567890, 0, 5);
+ assert.deepEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);
+ assert.equal(buf.readIntLE(0, 5), 0x1234567890);
+
+ buf = Buffer.allocUnsafe(5);
+ buf.writeIntBE(0x1234567890, 0, 5);
+ assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);
+ assert.equal(buf.readIntBE(0, 5), 0x1234567890);
+
+ buf = Buffer.allocUnsafe(5);
+ buf.writeIntLE(-0x1234567890, 0, 5);
+ assert.deepEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]);
+ assert.equal(buf.readIntLE(0, 5), -0x1234567890);
+
+ buf = Buffer.allocUnsafe(5);
+ buf.writeIntBE(-0x1234567890, 0, 5);
+ assert.deepEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]);
+ assert.equal(buf.readIntBE(0, 5), -0x1234567890);
+
+ buf = Buffer.allocUnsafe(5);
+ buf.writeIntLE(-0x0012000000, 0, 5);
+ assert.deepEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]);
+ assert.equal(buf.readIntLE(0, 5), -0x0012000000);
+
+ buf = Buffer.allocUnsafe(5);
+ buf.writeIntBE(-0x0012000000, 0, 5);
+ assert.deepEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]);
+ assert.equal(buf.readIntBE(0, 5), -0x0012000000);
+})();
+
+// test Buffer slice
+(function() {
+ var buf = Buffer.from('0123456789');
+ assert.equal(buf.slice(-10, 10), '0123456789');
+ assert.equal(buf.slice(-20, 10), '0123456789');
+ assert.equal(buf.slice(-20, -10), '');
+ assert.equal(buf.slice(), '0123456789');
+ assert.equal(buf.slice(0), '0123456789');
+ assert.equal(buf.slice(0, 0), '');
+ assert.equal(buf.slice(undefined), '0123456789');
+ assert.equal(buf.slice('foobar'), '0123456789');
+ assert.equal(buf.slice(undefined, undefined), '0123456789');
+
+ assert.equal(buf.slice(2), '23456789');
+ assert.equal(buf.slice(5), '56789');
+ assert.equal(buf.slice(10), '');
+ assert.equal(buf.slice(5, 8), '567');
+ assert.equal(buf.slice(8, -1), '8');
+ assert.equal(buf.slice(-10), '0123456789');
+ assert.equal(buf.slice(0, -9), '0');
+ assert.equal(buf.slice(0, -10), '');
+ assert.equal(buf.slice(0, -1), '012345678');
+ assert.equal(buf.slice(2, -2), '234567');
+ assert.equal(buf.slice(0, 65536), '0123456789');
+ assert.equal(buf.slice(65536, 0), '');
+ assert.equal(buf.slice(-5, -8), '');
+ assert.equal(buf.slice(-5, -3), '56');
+ assert.equal(buf.slice(-10, 10), '0123456789');
+ for (var i = 0, s = buf.toString(); i < buf.length; ++i) {
+ assert.equal(buf.slice(i), s.slice(i));
+ assert.equal(buf.slice(0, i), s.slice(0, i));
+ assert.equal(buf.slice(-i), s.slice(-i));
+ assert.equal(buf.slice(0, -i), s.slice(0, -i));
+ }
+
+ var utf16Buf = Buffer.from('0123456789', 'utf16le');
+ // TODO assert.deepEqual(utf16Buf.slice(0, 6), Buffer.from('012', 'utf16le'));
+
+ assert.equal(buf.slice('0', '1'), '0');
+ assert.equal(buf.slice('-5', '10'), '56789');
+ assert.equal(buf.slice('-10', '10'), '0123456789');
+ assert.equal(buf.slice('-10', '-5'), '01234');
+ assert.equal(buf.slice('-10', '-0'), '');
+ assert.equal(buf.slice('111'), '');
+ assert.equal(buf.slice('0', '-111'), '');
+
+ // try to slice a zero length Buffer
+ // see https://github.com/joyent/node/issues/5881
+ Buffer.alloc(0).slice(0, 1);
+})();
+
+// Regression test for #5482: should throw but not assert in C++ land.
+assert.throws(function() {
+ Buffer.from('', 'buffer');
+}, TypeError);
+
+// Regression test for #6111. Constructing a buffer from another buffer
+// should a) work, and b) not corrupt the source buffer.
+(function() {
+ var a = [0];
+ for (var i = 0; i < 7; ++i) a = a.concat(a);
+ a = a.map(function(_, i) { return i; });
+ var b = Buffer.from(a);
+ var c = Buffer.from(b);
+ assert.equal(b.length, a.length);
+ assert.equal(c.length, a.length);
+ for (var i = 0, k = a.length; i < k; ++i) {
+ assert.equal(a[i], i);
+ assert.equal(b[i], i);
+ assert.equal(c[i], i);
+ }
+})();
+
+
+assert.throws(function() {
+ Buffer.allocUnsafe((-1 >>> 0) + 1);
+}, RangeError);
+
+assert.throws(function() {
+ Buffer.allocUnsafeSlow((-1 >>> 0) + 1);
+}, RangeError);
+
+if (common.hasCrypto) {
+ // Test truncation after decode
+ var crypto = require('crypto');
+
+ var b1 = Buffer.from('YW55=======', 'base64');
+ var b2 = Buffer.from('YW55', 'base64');
+
+ assert.equal(
+ crypto.createHash('sha1').update(b1).digest('hex'),
+ crypto.createHash('sha1').update(b2).digest('hex')
+ );
+} else {
+// console.log('1..0 # Skipped: missing crypto');
+}
+
+// Test Compare
+{
+ var b = Buffer.alloc(1, 'a');
+ var c = Buffer.alloc(1, 'c');
+ var d = Buffer.alloc(2, 'aa');
+
+ assert.equal(b.compare(c), -1);
+ assert.equal(c.compare(d), 1);
+ assert.equal(d.compare(b), 1);
+ assert.equal(b.compare(d), -1);
+ assert.equal(b.compare(b), 0);
+
+ assert.equal(Buffer.compare(b, c), -1);
+ assert.equal(Buffer.compare(c, d), 1);
+ assert.equal(Buffer.compare(d, b), 1);
+ assert.equal(Buffer.compare(b, d), -1);
+ assert.equal(Buffer.compare(c, c), 0);
+
+ assert.equal(Buffer.compare(Buffer.alloc(0), Buffer.alloc(0)), 0);
+ assert.equal(Buffer.compare(Buffer.alloc(0), Buffer.alloc(1)), -1);
+ assert.equal(Buffer.compare(Buffer.alloc(1), Buffer.alloc(0)), 1);
+}
+
+assert.throws(function() {
+ var b = Buffer.allocUnsafe(1);
+ Buffer.compare(b, 'abc');
+});
+
+assert.throws(function() {
+ var b = Buffer.allocUnsafe(1);
+ Buffer.compare('abc', b);
+});
+
+assert.throws(function() {
+ var b = Buffer.allocUnsafe(1);
+ b.compare('abc');
+});
+
+// Test Equals
+{
+ var b = Buffer.alloc(5, 'abcdf');
+ var c = Buffer.alloc(5, 'abcdf');
+ var d = Buffer.alloc(5, 'abcde');
+ var e = Buffer.alloc(6, 'abcdef');
+
+ assert.ok(b.equals(c));
+ assert.ok(!c.equals(d));
+ assert.ok(!d.equals(e));
+ assert.ok(d.equals(d));
+}
+
+assert.throws(function() {
+ var b = Buffer.allocUnsafe(1);
+ b.equals('abc');
+});
+
+// Regression test for https://github.com/nodejs/node/issues/649.
+assert.throws(() => { Buffer.allocUnsafe(1422561062959).toString('utf8');});
+
+var ps = Buffer.poolSize;
+Buffer.poolSize = 0;
+assert.equal(Buffer.allocUnsafe(1).parent, undefined);
+Buffer.poolSize = ps;
+
+// Test Buffer.copy() segfault
+assert.throws(function() {
+ Buffer.allocUnsafe(10).copy();
+});
+
+var regErrorMsg = new RegExp('First argument must be a string, Buffer, ' +
+ 'ArrayBuffer, Array, or array-like object.');
+
+assert.throws(function() {
+ Buffer.from();
+}, regErrorMsg);
+
+assert.throws(function() {
+ Buffer.from(null);
+}, regErrorMsg);
+
+
+// Test that ParseArrayIndex handles full uint32
+assert.throws(function() {
+ Buffer.from(new ArrayBuffer(0), -1 >>> 0);
+}, /RangeError: 'offset' is out of bounds/);
+
+// Unpooled buffer (replaces SlowBuffer)
+var ubuf = Buffer.allocUnsafeSlow(10);
+assert(ubuf);
+assert(ubuf.buffer);
+assert.equal(ubuf.buffer.byteLength, 10);
+
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
+
+var assert = require('assert');
+
+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);
+
+
+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, undefined);
+assert.equal(buf.buffer, ab);
+assert.equal(buf.length, ab.byteLength);
+
+
+buf.fill(0xC);
+for (var i = 0; i < LENGTH; i++) {
+ assert.equal(ui[i], 0xC);
+ ui[i] = 0xF;
+ assert.equal(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);
+
+
+// Now test protecting users from doing stupid things
+
+assert.throws(function() {
+ function AB() { }
+ Object.setPrototypeOf(AB, ArrayBuffer);
+ Object.setPrototypeOf(AB.prototype, ArrayBuffer.prototype);
+ Buffer.from(new AB());
+}, TypeError);
+
+// write{Double,Float}{LE,BE} with noAssert should not crash, cf. #3766
+var b = Buffer.allocUnsafe(1);
+b.writeFloatLE(11.11, 0, true);
+b.writeFloatBE(11.11, 0, true);
+b.writeDoubleLE(11.11, 0, true);
+b.writeDoubleBE(11.11, 0, true);
+
+// Test the byteOffset and length arguments
+{
+ var 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);
+ buf[0] = 9;
+ assert.equal(ab[1], 9);
+
+ assert.throws(() => Buffer.from(ab.buffer, 6), (err) => {
+ assert(err instanceof RangeError);
+ assert(/'offset' is out of bounds/.test(err.message));
+ return true;
+ });
+ assert.throws(() => Buffer.from(ab.buffer, 3, 6), (err) => {
+ assert(err instanceof RangeError);
+ assert(/'length' is out of bounds/.test(err.message));
+ return true;
+ });
+}
+
+// Test the deprecated Buffer() version also
+{
+ var 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);
+ buf[0] = 9;
+ assert.equal(ab[1], 9);
+
+ assert.throws(() => Buffer(ab.buffer, 6), (err) => {
+ assert(err instanceof RangeError);
+ assert(/'offset' is out of bounds/.test(err.message));
+ return true;
+ });
+ assert.throws(() => Buffer(ab.buffer, 3, 6), (err) => {
+ assert(err instanceof RangeError);
+ assert(/'length' is out of bounds/.test(err.message));
+ return true;
+ });
+}
+
'use strict';
-if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
-var common = {};
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
// ASCII conversion in node.js simply masks off the high bits,
// it doesn't do transliteration.
-assert.equal(Buffer('hérité').toString('ascii'), 'hC)ritC)');
+assert.equal(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 ' +
'db\u0000\u0019un accent aigu et db\u0000\u0019un ' +
'accent grave.';
-var buf = Buffer(input);
+var buf = Buffer.from(input);
for (var i = 0; i < expected.length; ++i) {
assert.equal(buf.slice(i).toString('ascii'), expected.slice(i));
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
+var assert = require('assert');
+
+assert.doesNotThrow(function() {
+ Buffer.allocUnsafe(10);
+});
+
+assert.throws(function() {
+ Buffer.from(10, 'hex');
+});
+
+assert.doesNotThrow(function() {
+ Buffer.from('deadbeaf', 'hex');
+});
+
'use strict';
-if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
-var common = {};
var assert = require('assert');
var Buffer = require('../../').Buffer;
+var SlowBuffer = require('../../').SlowBuffer;
// coerce values to string
-assert.equal(Buffer.byteLength(32, 'raw'), 2);
+assert.equal(Buffer.byteLength(32, 'binary'), 2);
assert.equal(Buffer.byteLength(NaN, 'utf8'), 3);
-assert.equal(Buffer.byteLength({}, 'raws'), 15);
+assert.equal(Buffer.byteLength({}, 'binary'), 15);
assert.equal(Buffer.byteLength(), 9);
+var buff = new Buffer(10);
+assert(ArrayBuffer.isView(buff));
+var slowbuff = new SlowBuffer(10);
+assert(ArrayBuffer.isView(slowbuff));
+
+// buffer
+var incomplete = Buffer.from([0xe4, 0xb8, 0xad, 0xe6, 0x96]);
+assert.equal(Buffer.byteLength(incomplete), 5);
+var ascii = Buffer.from('abc');
+assert.equal(Buffer.byteLength(ascii), 3);
+
+// ArrayBuffer
+var buffer = new ArrayBuffer(8);
+assert.equal(Buffer.byteLength(buffer), 8);
+
+// TypedArray
+var int8 = new Int8Array(8);
+assert.equal(Buffer.byteLength(int8), 8);
+var uint8 = new Uint8Array(8);
+assert.equal(Buffer.byteLength(uint8), 8);
+var uintc8 = new Uint8ClampedArray(2);
+assert.equal(Buffer.byteLength(uintc8), 2);
+var int16 = new Int16Array(8);
+assert.equal(Buffer.byteLength(int16), 16);
+var uint16 = new Uint16Array(8);
+assert.equal(Buffer.byteLength(uint16), 16);
+var int32 = new Int32Array(8);
+assert.equal(Buffer.byteLength(int32), 32);
+var uint32 = new Uint32Array(8);
+assert.equal(Buffer.byteLength(uint32), 32);
+var float32 = new Float32Array(8);
+assert.equal(Buffer.byteLength(float32), 32);
+var float64 = new Float64Array(8);
+assert.equal(Buffer.byteLength(float64), 64);
+
+// DataView
+var dv = new DataView(new ArrayBuffer(2));
+assert.equal(Buffer.byteLength(dv), 2);
+
// special case: zero length string
assert.equal(Buffer.byteLength('', 'ascii'), 0);
assert.equal(Buffer.byteLength('', 'HeX'), 0);
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
+
+var assert = require('assert');
+
+var a = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]);
+var b = Buffer.from([5, 6, 7, 8, 9, 0, 1, 2, 3, 4]);
+
+assert.equal(-1, a.compare(b));
+
+// Equivalent to a.compare(b).
+assert.equal(-1, a.compare(b, 0));
+assert.equal(-1, a.compare(b, '0'));
+
+// Equivalent to a.compare(b).
+assert.equal(-1, a.compare(b, 0, undefined, 0));
+
+// Zero-length targer, return 1
+assert.equal(1, a.compare(b, 0, 0, 0));
+assert.equal(1, a.compare(b, '0', '0', '0'));
+
+// Equivalent to Buffer.compare(a, b.slice(6, 10))
+assert.equal(1, a.compare(b, 6, 10));
+
+// Zero-length source, return -1
+assert.equal(-1, a.compare(b, 6, 10, 0, 0));
+
+// Equivalent to Buffer.compare(a.slice(4), b.slice(0, 5))
+assert.equal(1, a.compare(b, 0, 5, 4));
+
+// Equivalent to Buffer.compare(a.slice(1), b.slice(5))
+assert.equal(1, a.compare(b, 5, undefined, 1));
+
+// Equivalent to Buffer.compare(a.slice(2), b.slice(2, 4))
+assert.equal(-1, a.compare(b, 2, 4, 2));
+
+// Equivalent to Buffer.compare(a.slice(4), b.slice(0, 7))
+assert.equal(-1, a.compare(b, 0, 7, 4));
+
+// Equivalent to Buffer.compare(a.slice(4, 6), b.slice(0, 7));
+assert.equal(-1, a.compare(b, 0, 7, 4, 6));
+
+// zero length target
+assert.equal(1, a.compare(b, 0, null));
+
+// coerces to targetEnd == 5
+assert.equal(-1, a.compare(b, 0, {valueOf: () => 5}));
+
+// zero length target
+assert.equal(1, a.compare(b, Infinity, -Infinity));
+
+// zero length target because default for targetEnd <= targetSource
+assert.equal(1, a.compare(b, '0xff'));
+
+var oor = /out of range index/;
+
+assert.throws(() => a.compare(b, 0, 100, 0), oor);
+assert.throws(() => a.compare(b, 0, 1, 0, 100), oor);
+assert.throws(() => a.compare(b, -1), oor);
+assert.throws(() => a.compare(b, 0, '0xff'), oor);
+assert.throws(() => a.compare(b, 0, Infinity), oor);
+assert.throws(() => a.compare(b, -Infinity, Infinity), oor);
+assert.throws(() => a.compare(), /Argument must be a Buffer/);
+
'use strict';
-if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
-var common = {};
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
var zero = [];
-var one = [ new Buffer('asdf') ];
+var one = [ Buffer.from('asdf') ];
var long = [];
-for (var i = 0; i < 10; i++) long.push(new Buffer('asdf'));
+for (var i = 0; i < 10; i++) long.push(Buffer.from('asdf'));
var flatZero = Buffer.concat(zero);
var flatOne = Buffer.concat(one);
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);
+assertWrongList();
+assertWrongList(null);
+assertWrongList(Buffer.from('hello'));
+assertWrongList([42]);
+assertWrongList(['hello', 'world']);
+assertWrongList(['hello', Buffer.from('world')]);
-// console.log('ok');
+function assertWrongList(value) {
+ assert.throws(function() {
+ Buffer.concat(value);
+ }, function(err) {
+ return err instanceof TypeError &&
+ err.message === '"list" argument must be an Array of Buffers';
+ });
+}
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
+
+var assert = require('assert');
+var os = require('os');
+var SIZE = 28;
+
+var buf1 = Buffer.allocUnsafe(SIZE);
+var buf2 = Buffer.allocUnsafe(SIZE);
+
+
+// Default encoding
+testBufs('abc');
+testBufs('\u0222aa');
+testBufs('a\u0234b\u0235c\u0236');
+testBufs('abc', 4);
+testBufs('abc', 5);
+testBufs('abc', SIZE);
+testBufs('\u0222aa', 2);
+testBufs('\u0222aa', 8);
+testBufs('a\u0234b\u0235c\u0236', 4);
+testBufs('a\u0234b\u0235c\u0236', 12);
+testBufs('abc', 4, -1);
+testBufs('abc', 4, 1);
+testBufs('abc', 5, 1);
+testBufs('\u0222aa', 2, -1);
+testBufs('\u0222aa', 8, 1);
+testBufs('a\u0234b\u0235c\u0236', 4, -1);
+testBufs('a\u0234b\u0235c\u0236', 4, 1);
+testBufs('a\u0234b\u0235c\u0236', 12, 1);
+
+
+// UTF8
+testBufs('abc', 'utf8');
+testBufs('\u0222aa', 'utf8');
+testBufs('a\u0234b\u0235c\u0236', 'utf8');
+testBufs('abc', 4, 'utf8');
+testBufs('abc', 5, 'utf8');
+testBufs('abc', SIZE, 'utf8');
+testBufs('\u0222aa', 2, 'utf8');
+testBufs('\u0222aa', 8, 'utf8');
+testBufs('a\u0234b\u0235c\u0236', 4, 'utf8');
+testBufs('a\u0234b\u0235c\u0236', 12, 'utf8');
+testBufs('abc', 4, -1, 'utf8');
+testBufs('abc', 4, 1, 'utf8');
+testBufs('abc', 5, 1, 'utf8');
+testBufs('\u0222aa', 2, -1, 'utf8');
+testBufs('\u0222aa', 8, 1, 'utf8');
+testBufs('a\u0234b\u0235c\u0236', 4, -1, 'utf8');
+testBufs('a\u0234b\u0235c\u0236', 4, 1, 'utf8');
+testBufs('a\u0234b\u0235c\u0236', 12, 1, 'utf8');
+assert.equal(Buffer.allocUnsafe(1).fill(0).fill('\u0222')[0], 0xc8);
+
+
+// BINARY
+testBufs('abc', 'binary');
+testBufs('\u0222aa', 'binary');
+testBufs('a\u0234b\u0235c\u0236', 'binary');
+testBufs('abc', 4, 'binary');
+testBufs('abc', 5, 'binary');
+testBufs('abc', SIZE, 'binary');
+testBufs('\u0222aa', 2, 'binary');
+testBufs('\u0222aa', 8, 'binary');
+testBufs('a\u0234b\u0235c\u0236', 4, 'binary');
+testBufs('a\u0234b\u0235c\u0236', 12, 'binary');
+testBufs('abc', 4, -1, 'binary');
+testBufs('abc', 4, 1, 'binary');
+testBufs('abc', 5, 1, 'binary');
+testBufs('\u0222aa', 2, -1, 'binary');
+testBufs('\u0222aa', 8, 1, 'binary');
+testBufs('a\u0234b\u0235c\u0236', 4, -1, 'binary');
+testBufs('a\u0234b\u0235c\u0236', 4, 1, 'binary');
+testBufs('a\u0234b\u0235c\u0236', 12, 1, 'binary');
+
+
+// UCS2
+testBufs('abc', 'ucs2');
+testBufs('\u0222aa', 'ucs2');
+testBufs('a\u0234b\u0235c\u0236', 'ucs2');
+testBufs('abc', 4, 'ucs2');
+testBufs('abc', SIZE, 'ucs2');
+testBufs('\u0222aa', 2, 'ucs2');
+testBufs('\u0222aa', 8, 'ucs2');
+testBufs('a\u0234b\u0235c\u0236', 4, 'ucs2');
+testBufs('a\u0234b\u0235c\u0236', 12, 'ucs2');
+testBufs('abc', 4, -1, 'ucs2');
+testBufs('abc', 4, 1, 'ucs2');
+testBufs('abc', 5, 1, 'ucs2');
+testBufs('\u0222aa', 2, -1, 'ucs2');
+testBufs('\u0222aa', 8, 1, 'ucs2');
+testBufs('a\u0234b\u0235c\u0236', 4, -1, 'ucs2');
+testBufs('a\u0234b\u0235c\u0236', 4, 1, 'ucs2');
+testBufs('a\u0234b\u0235c\u0236', 12, 1, 'ucs2');
+assert.equal(Buffer.allocUnsafe(1).fill('\u0222', 'ucs2')[0],
+ os.endianness() === 'LE' ? 0x22 : 0x02);
+
+
+// HEX
+testBufs('616263', 'hex');
+testBufs('c8a26161', 'hex');
+testBufs('61c8b462c8b563c8b6', 'hex');
+testBufs('616263', 4, 'hex');
+testBufs('616263', 5, 'hex');
+testBufs('616263', SIZE, 'hex');
+testBufs('c8a26161', 2, 'hex');
+testBufs('c8a26161', 8, 'hex');
+testBufs('61c8b462c8b563c8b6', 4, 'hex');
+testBufs('61c8b462c8b563c8b6', 12, 'hex');
+testBufs('616263', 4, -1, 'hex');
+testBufs('616263', 4, 1, 'hex');
+testBufs('616263', 5, 1, 'hex');
+testBufs('c8a26161', 2, -1, 'hex');
+testBufs('c8a26161', 8, 1, 'hex');
+testBufs('61c8b462c8b563c8b6', 4, -1, 'hex');
+testBufs('61c8b462c8b563c8b6', 4, 1, 'hex');
+testBufs('61c8b462c8b563c8b6', 12, 1, 'hex');
+// Make sure this operation doesn't go on forever
+buf1.fill('yKJh', 'hex');
+assert.throws(() => buf1.fill('\u0222', 'hex'));
+
+
+// BASE64
+testBufs('YWJj', 'ucs2');
+testBufs('yKJhYQ==', 'ucs2');
+testBufs('Yci0Ysi1Y8i2', 'ucs2');
+testBufs('YWJj', 4, 'ucs2');
+testBufs('YWJj', SIZE, 'ucs2');
+testBufs('yKJhYQ==', 2, 'ucs2');
+testBufs('yKJhYQ==', 8, 'ucs2');
+testBufs('Yci0Ysi1Y8i2', 4, 'ucs2');
+testBufs('Yci0Ysi1Y8i2', 12, 'ucs2');
+testBufs('YWJj', 4, -1, 'ucs2');
+testBufs('YWJj', 4, 1, 'ucs2');
+testBufs('YWJj', 5, 1, 'ucs2');
+testBufs('yKJhYQ==', 2, -1, 'ucs2');
+testBufs('yKJhYQ==', 8, 1, 'ucs2');
+testBufs('Yci0Ysi1Y8i2', 4, -1, 'ucs2');
+testBufs('Yci0Ysi1Y8i2', 4, 1, 'ucs2');
+testBufs('Yci0Ysi1Y8i2', 12, 1, 'ucs2');
+
+
+// Buffer
+var buf2Fill = Buffer.allocUnsafe(1).fill(2);
+assert.deepEqual(genBuffer(4, [buf2Fill]), [2, 2, 2, 2]);
+assert.deepEqual(genBuffer(4, [buf2Fill, 1]), [0, 2, 2, 2]);
+assert.deepEqual(genBuffer(4, [buf2Fill, 1, 3]), [0, 2, 2, 0]);
+assert.deepEqual(genBuffer(4, [buf2Fill, 1, 1]), [0, 0, 0, 0]);
+assert.deepEqual(genBuffer(4, [buf2Fill, 1, -1]), [0, 0, 0, 0]);
+var hexBufFill = Buffer.allocUnsafe(2).fill(0).fill('0102', 'hex');
+assert.deepEqual(genBuffer(4, [hexBufFill]), [1, 2, 1, 2]);
+assert.deepEqual(genBuffer(4, [hexBufFill, 1]), [0, 1, 2, 1]);
+assert.deepEqual(genBuffer(4, [hexBufFill, 1, 3]), [0, 1, 2, 0]);
+assert.deepEqual(genBuffer(4, [hexBufFill, 1, 1]), [0, 0, 0, 0]);
+assert.deepEqual(genBuffer(4, [hexBufFill, 1, -1]), [0, 0, 0, 0]);
+
+
+// Check exceptions
+assert.throws(() => buf1.fill(0, -1));
+assert.throws(() => buf1.fill(0, 0, buf1.length + 1));
+assert.throws(() => buf1.fill('', -1));
+assert.throws(() => buf1.fill('', 0, buf1.length + 1));
+assert.throws(() => buf1.fill('a', 0, buf1.length, 'node rocks!'));
+assert.throws(() => buf1.fill('a', 0, 0, NaN));
+assert.throws(() => buf1.fill('a', 0, 0, null));
+assert.throws(() => buf1.fill('a', 0, 0, 'foo'));
+
+
+function genBuffer(size, args) {
+ var b = Buffer.allocUnsafe(size);
+ return b.fill(0).fill.apply(b, args);
+}
+
+
+function bufReset() {
+ buf1.fill(0);
+ buf2.fill(0);
+}
+
+
+// This is mostly accurate. Except write() won't write partial bytes to the
+// string while fill() blindly copies bytes into memory. To account for that an
+// error will be thrown if not all the data can be written, and the SIZE has
+// been massaged to work with the input characters.
+function writeToFill(string, offset, end, encoding) {
+ if (typeof offset === 'string') {
+ encoding = offset;
+ offset = 0;
+ end = buf2.length;
+ } else if (typeof end === 'string') {
+ encoding = end;
+ end = buf2.length;
+ } else if (end === undefined) {
+ end = buf2.length;
+ }
+
+ if (offset < 0 || end > buf2.length)
+ throw new RangeError('Out of range index');
+
+ if (end <= offset)
+ return buf2;
+
+ offset >>>= 0;
+ end >>>= 0;
+ assert(offset <= buf2.length);
+
+ // Convert "end" to "length" (which write understands).
+ var length = end - offset < 0 ? 0 : end - offset;
+
+ var wasZero = false;
+ do {
+ var written = buf2.write(string, offset, length, encoding);
+ offset += written;
+ // Safety check in case write falls into infinite loop.
+ if (written === 0) {
+ if (wasZero)
+ throw new Error('Could not write all data to Buffer');
+ else
+ wasZero = true;
+ }
+ } while (offset < buf2.length);
+
+ // Correction for UCS2 operations.
+ if (os.endianness() === 'BE' && encoding === 'ucs2') {
+ for (var i = 0; i < buf2.length; i += 2) {
+ var tmp = buf2[i];
+ buf2[i] = buf2[i + 1];
+ buf2[i + 1] = tmp;
+ }
+ }
+
+ return buf2;
+}
+
+
+function testBufs(string, offset, length, encoding) {
+ bufReset();
+ buf1.fill.apply(buf1, arguments);
+ // Swap bytes on BE archs for ucs2 encoding.
+ assert.deepStrictEqual(buf1.fill.apply(buf1, arguments),
+ writeToFill.apply(null, arguments));
+}
+
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
+var assert = require('assert');
+
+var Buffer = require('../../').Buffer;
+
+var b = Buffer.from('abcdef');
+var buf_a = Buffer.from('a');
+var buf_bc = Buffer.from('bc');
+var buf_f = Buffer.from('f');
+var buf_z = Buffer.from('z');
+var buf_empty = Buffer.from('');
+
+assert(b.includes('a'));
+assert(!b.includes('a', 1));
+assert(!b.includes('a', -1));
+assert(!b.includes('a', -4));
+assert(b.includes('a', -b.length));
+assert(b.includes('a', NaN));
+assert(b.includes('a', -Infinity));
+assert(!b.includes('a', Infinity));
+assert(b.includes('bc'));
+assert(!b.includes('bc', 2));
+assert(!b.includes('bc', -1));
+assert(!b.includes('bc', -3));
+assert(b.includes('bc', -5));
+assert(b.includes('bc', NaN));
+assert(b.includes('bc', -Infinity));
+assert(!b.includes('bc', Infinity));
+assert(b.includes('f'), b.length - 1);
+assert(!b.includes('z'));
+assert(!b.includes(''));
+assert(!b.includes('', 1));
+assert(!b.includes('', b.length + 1));
+assert(!b.includes('', Infinity));
+assert(b.includes(buf_a));
+assert(!b.includes(buf_a, 1));
+assert(!b.includes(buf_a, -1));
+assert(!b.includes(buf_a, -4));
+assert(b.includes(buf_a, -b.length));
+assert(b.includes(buf_a, NaN));
+assert(b.includes(buf_a, -Infinity));
+assert(!b.includes(buf_a, Infinity));
+assert(b.includes(buf_bc));
+assert(!b.includes(buf_bc, 2));
+assert(!b.includes(buf_bc, -1));
+assert(!b.includes(buf_bc, -3));
+assert(b.includes(buf_bc, -5));
+assert(b.includes(buf_bc, NaN));
+assert(b.includes(buf_bc, -Infinity));
+assert(!b.includes(buf_bc, Infinity));
+assert(b.includes(buf_f), b.length - 1);
+assert(!b.includes(buf_z));
+assert(!b.includes(buf_empty));
+assert(!b.includes(buf_empty, 1));
+assert(!b.includes(buf_empty, b.length + 1));
+assert(!b.includes(buf_empty, Infinity));
+assert(b.includes(0x61));
+assert(!b.includes(0x61, 1));
+assert(!b.includes(0x61, -1));
+assert(!b.includes(0x61, -4));
+assert(b.includes(0x61, -b.length));
+assert(b.includes(0x61, NaN));
+assert(b.includes(0x61, -Infinity));
+assert(!b.includes(0x61, Infinity));
+assert(!b.includes(0x0));
+
+// test offsets
+assert(b.includes('d', 2));
+assert(b.includes('f', 5));
+assert(b.includes('f', -1));
+assert(!b.includes('f', 6));
+
+assert(b.includes(Buffer.from('d'), 2));
+assert(b.includes(Buffer.from('f'), 5));
+assert(b.includes(Buffer.from('f'), -1));
+assert(!b.includes(Buffer.from('f'), 6));
+
+assert(!Buffer.from('ff').includes(Buffer.from('f'), 1, 'ucs2'));
+
+// test hex encoding
+assert(
+ Buffer.from(b.toString('hex'), 'hex')
+ .includes('64', 0, 'hex'));
+assert(
+ Buffer.from(b.toString('hex'), 'hex')
+ .includes(Buffer.from('64', 'hex'), 0, 'hex'));
+
+// test base64 encoding
+assert(
+ Buffer.from(b.toString('base64'), 'base64')
+ .includes('ZA==', 0, 'base64'));
+assert(
+ Buffer.from(b.toString('base64'), 'base64')
+ .includes(Buffer.from('ZA==', 'base64'), 0, 'base64'));
+
+// test ascii encoding
+assert(
+ Buffer.from(b.toString('ascii'), 'ascii')
+ .includes('d', 0, 'ascii'));
+assert(
+ Buffer.from(b.toString('ascii'), 'ascii')
+ .includes(Buffer.from('d', 'ascii'), 0, 'ascii'));
+
+// test binary encoding
+assert(
+ Buffer.from(b.toString('binary'), 'binary')
+ .includes('d', 0, 'binary'));
+assert(
+ Buffer.from(b.toString('binary'), 'binary')
+ .includes(Buffer.from('d', 'binary'), 0, 'binary'));
+
+
+// test usc2 encoding
+var twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
+
+assert(twoByteString.includes('\u0395', 4, 'ucs2'));
+assert(twoByteString.includes('\u03a3', -4, 'ucs2'));
+assert(twoByteString.includes('\u03a3', -6, 'ucs2'));
+assert(twoByteString.includes(
+ Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2'));
+assert(!twoByteString.includes('\u03a3', -2, 'ucs2'));
+
+var mixedByteStringUcs2 =
+ Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
+assert(mixedByteStringUcs2.includes('bc', 0, 'ucs2'));
+assert(mixedByteStringUcs2.includes('\u03a3', 0, 'ucs2'));
+assert(!mixedByteStringUcs2.includes('\u0396', 0, 'ucs2'));
+
+assert(
+ 6, mixedByteStringUcs2.includes(Buffer.from('bc', 'ucs2'), 0, 'ucs2'));
+assert(
+ 10, mixedByteStringUcs2.includes(Buffer.from('\u03a3', 'ucs2'),
+ 0, 'ucs2'));
+assert(
+ -1, mixedByteStringUcs2.includes(Buffer.from('\u0396', 'ucs2'),
+ 0, 'ucs2'));
+
+twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
+
+// Test single char pattern
+assert(twoByteString.includes('\u039a', 0, 'ucs2'));
+assert(twoByteString.includes('\u0391', 0, 'ucs2'), 'Alpha');
+assert(twoByteString.includes('\u03a3', 0, 'ucs2'), 'First Sigma');
+assert(twoByteString.includes('\u03a3', 6, 'ucs2'), 'Second Sigma');
+assert(twoByteString.includes('\u0395', 0, 'ucs2'), 'Epsilon');
+assert(!twoByteString.includes('\u0392', 0, 'ucs2'), 'Not beta');
+
+// Test multi-char pattern
+assert(twoByteString.includes('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha');
+assert(twoByteString.includes('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma');
+assert(twoByteString.includes('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma');
+assert(twoByteString.includes('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon');
+
+var mixedByteStringUtf8 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395');
+assert(mixedByteStringUtf8.includes('bc'));
+assert(mixedByteStringUtf8.includes('bc', 5));
+assert(mixedByteStringUtf8.includes('bc', -8));
+assert(mixedByteStringUtf8.includes('\u03a3'));
+assert(!mixedByteStringUtf8.includes('\u0396'));
+
+
+// Test complex string includes algorithms. Only trigger for long strings.
+// Long string that isn't a simple repeat of a shorter string.
+var longString = 'A';
+for (var i = 66; i < 76; i++) { // from 'B' to 'K'
+ longString = longString + String.fromCharCode(i) + longString;
+}
+
+var longBufferString = Buffer.from(longString);
+
+// pattern of 15 chars, repeated every 16 chars in long
+var pattern = 'ABACABADABACABA';
+for (var i = 0; i < longBufferString.length - pattern.length; i += 7) {
+ var includes = longBufferString.includes(pattern, i);
+ assert(includes, 'Long ABACABA...-string at index ' + i);
+}
+assert(longBufferString.includes('AJABACA'), 'Long AJABACA, First J');
+assert(longBufferString.includes('AJABACA', 511), 'Long AJABACA, Second J');
+
+pattern = 'JABACABADABACABA';
+assert(longBufferString.includes(pattern), 'Long JABACABA..., First J');
+assert(longBufferString.includes(pattern, 512), 'Long JABACABA..., Second J');
+
+// Search for a non-ASCII string in a pure ASCII string.
+var asciiString = Buffer.from(
+ 'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf');
+assert(!asciiString.includes('\x2061'));
+assert(asciiString.includes('leb', 0));
+
+// Search in string containing many non-ASCII chars.
+var allCodePoints = [];
+for (var i = 0; i < 65536; i++) allCodePoints[i] = i;
+var allCharsString = String.fromCharCode.apply(String, allCodePoints);
+var allCharsBufferUtf8 = Buffer.from(allCharsString);
+var allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2');
+
+// Search for string long enough to trigger complex search with ASCII pattern
+// and UC16 subject.
+assert(!allCharsBufferUtf8.includes('notfound'));
+assert(!allCharsBufferUcs2.includes('notfound'));
+
+// Find substrings in Utf8.
+var lengths = [1, 3, 15]; // Single char, simple and complex.
+var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
+for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
+ for (var i = 0; i < indices.length; i++) {
+ var index = indices[i];
+ var length = lengths[lengthIndex];
+
+ if (index + length > 0x7F) {
+ length = 2 * length;
+ }
+
+ if (index + length > 0x7FF) {
+ length = 3 * length;
+ }
+
+ if (index + length > 0xFFFF) {
+ length = 4 * length;
+ }
+
+ var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length);
+ assert(index, allCharsBufferUtf8.includes(patternBufferUtf8));
+
+ var patternStringUtf8 = patternBufferUtf8.toString();
+ assert(index, allCharsBufferUtf8.includes(patternStringUtf8));
+ }
+}
+
+// Find substrings in Usc2.
+lengths = [2, 4, 16]; // Single char, simple and complex.
+indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
+for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
+ for (var i = 0; i < indices.length; i++) {
+ var index = indices[i] * 2;
+ var length = lengths[lengthIndex];
+
+ var patternBufferUcs2 =
+ allCharsBufferUcs2.slice(index, index + length);
+ assert(
+ index, allCharsBufferUcs2.includes(patternBufferUcs2, 0, 'ucs2'));
+
+ var patternStringUcs2 = patternBufferUcs2.toString('ucs2');
+ assert(
+ index, allCharsBufferUcs2.includes(patternStringUcs2, 0, 'ucs2'));
+ }
+}
+
+assert.throws(function() {
+ b.includes(function() { });
+});
+assert.throws(function() {
+ b.includes({});
+});
+assert.throws(function() {
+ b.includes([]);
+});
+
'use strict';
-if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
-var common = {};
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
var Buffer = require('../../').Buffer;
-var b = new Buffer('abcdef');
-var buf_a = new Buffer('a');
-var buf_bc = new Buffer('bc');
-var buf_f = new Buffer('f');
-var buf_z = new Buffer('z');
-var buf_empty = new Buffer('');
+var b = Buffer.from('abcdef');
+var buf_a = Buffer.from('a');
+var buf_bc = Buffer.from('bc');
+var buf_f = Buffer.from('f');
+var buf_z = Buffer.from('z');
+var buf_empty = Buffer.from('');
assert.equal(b.indexOf('a'), 0);
assert.equal(b.indexOf('a', 1), -1);
assert.equal(b.indexOf(0x61, Infinity), -1);
assert.equal(b.indexOf(0x0), -1);
+// test offsets
+assert.equal(b.indexOf('d', 2), 3);
+assert.equal(b.indexOf('f', 5), 5);
+assert.equal(b.indexOf('f', -1), 5);
+assert.equal(b.indexOf('f', 6), -1);
+
+assert.equal(b.indexOf(Buffer.from('d'), 2), 3);
+assert.equal(b.indexOf(Buffer.from('f'), 5), 5);
+assert.equal(b.indexOf(Buffer.from('f'), -1), 5);
+assert.equal(b.indexOf(Buffer.from('f'), 6), -1);
+
+assert.equal(Buffer.from('ff').indexOf(Buffer.from('f'), 1, 'ucs2'), -1);
+
+// test hex encoding
+assert.equal(
+ Buffer.from(b.toString('hex'), 'hex')
+ .indexOf('64', 0, 'hex'), 3);
+assert.equal(
+ Buffer.from(b.toString('hex'), 'hex')
+ .indexOf(Buffer.from('64', 'hex'), 0, 'hex'), 3);
+
+// test base64 encoding
+assert.equal(
+ Buffer.from(b.toString('base64'), 'base64')
+ .indexOf('ZA==', 0, 'base64'), 3);
+assert.equal(
+ Buffer.from(b.toString('base64'), 'base64')
+ .indexOf(Buffer.from('ZA==', 'base64'), 0, 'base64'), 3);
+
+// test ascii encoding
+assert.equal(
+ Buffer.from(b.toString('ascii'), 'ascii')
+ .indexOf('d', 0, 'ascii'), 3);
+assert.equal(
+ Buffer.from(b.toString('ascii'), 'ascii')
+ .indexOf(Buffer.from('d', 'ascii'), 0, 'ascii'), 3);
+
+// test binary encoding
+assert.equal(
+ Buffer.from(b.toString('binary'), 'binary')
+ .indexOf('d', 0, 'binary'), 3);
+assert.equal(
+ Buffer.from(b.toString('binary'), 'binary')
+ .indexOf(Buffer.from('d', 'binary'), 0, 'binary'), 3);
+assert.equal(
+ Buffer.from('aa\u00e8aa', 'binary')
+ .indexOf('\u00e8', 'binary'), 2);
+assert.equal(
+ Buffer.from('\u00e8', 'binary')
+ .indexOf('\u00e8', 'binary'), 0);
+assert.equal(
+ Buffer.from('\u00e8', 'binary')
+ .indexOf(Buffer.from('\u00e8', 'binary'), 'binary'), 0);
+
+
+// test optional offset with passed encoding
+assert.equal(Buffer.from('aaaa0').indexOf('30', 'hex'), 4);
+assert.equal(Buffer.from('aaaa00a').indexOf('3030', 'hex'), 4);
+
+{
+ // test usc2 encoding
+ var twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
+
+ assert.equal(8, twoByteString.indexOf('\u0395', 4, 'ucs2'));
+ assert.equal(6, twoByteString.indexOf('\u03a3', -4, 'ucs2'));
+ assert.equal(4, twoByteString.indexOf('\u03a3', -6, 'ucs2'));
+ assert.equal(4, twoByteString.indexOf(
+ Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2'));
+ assert.equal(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2'));
+}
+
+var mixedByteStringUcs2 =
+ Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2');
+assert.equal(6, mixedByteStringUcs2.indexOf('bc', 0, 'ucs2'));
+assert.equal(10, mixedByteStringUcs2.indexOf('\u03a3', 0, 'ucs2'));
+assert.equal(-1, mixedByteStringUcs2.indexOf('\u0396', 0, 'ucs2'));
+
+assert.equal(
+ 6, mixedByteStringUcs2.indexOf(Buffer.from('bc', 'ucs2'), 0, 'ucs2'));
+assert.equal(
+ 10, mixedByteStringUcs2.indexOf(Buffer.from('\u03a3', 'ucs2'), 0, 'ucs2'));
+assert.equal(
+ -1, mixedByteStringUcs2.indexOf(Buffer.from('\u0396', 'ucs2'), 0, 'ucs2'));
+
+{
+ var twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
+
+ // Test single char pattern
+ assert.equal(0, twoByteString.indexOf('\u039a', 0, 'ucs2'));
+ assert.equal(2, twoByteString.indexOf('\u0391', 0, 'ucs2'), 'Alpha');
+ assert.equal(4, twoByteString.indexOf('\u03a3', 0, 'ucs2'), 'First Sigma');
+ assert.equal(6, twoByteString.indexOf('\u03a3', 6, 'ucs2'), 'Second Sigma');
+ assert.equal(8, twoByteString.indexOf('\u0395', 0, 'ucs2'), 'Epsilon');
+ assert.equal(-1, twoByteString.indexOf('\u0392', 0, 'ucs2'), 'Not beta');
+
+ // Test multi-char pattern
+ assert.equal(
+ 0, twoByteString.indexOf('\u039a\u0391', 0, 'ucs2'), 'Lambda Alpha');
+ assert.equal(
+ 2, twoByteString.indexOf('\u0391\u03a3', 0, 'ucs2'), 'Alpha Sigma');
+ assert.equal(
+ 4, twoByteString.indexOf('\u03a3\u03a3', 0, 'ucs2'), 'Sigma Sigma');
+ assert.equal(
+ 6, twoByteString.indexOf('\u03a3\u0395', 0, 'ucs2'), 'Sigma Epsilon');
+}
+
+var mixedByteStringUtf8 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395');
+assert.equal(5, mixedByteStringUtf8.indexOf('bc'));
+assert.equal(5, mixedByteStringUtf8.indexOf('bc', 5));
+assert.equal(5, mixedByteStringUtf8.indexOf('bc', -8));
+assert.equal(7, mixedByteStringUtf8.indexOf('\u03a3'));
+assert.equal(-1, mixedByteStringUtf8.indexOf('\u0396'));
+
+
+// Test complex string indexOf algorithms. Only trigger for long strings.
+// Long string that isn't a simple repeat of a shorter string.
+var longString = 'A';
+for (var i = 66; i < 76; i++) { // from 'B' to 'K'
+ longString = longString + String.fromCharCode(i) + longString;
+}
+
+var longBufferString = Buffer.from(longString);
+
+// pattern of 15 chars, repeated every 16 chars in long
+var pattern = 'ABACABADABACABA';
+for (var i = 0; i < longBufferString.length - pattern.length; i += 7) {
+ var index = longBufferString.indexOf(pattern, i);
+ assert.equal((i + 15) & ~0xf, index, 'Long ABACABA...-string at index ' + i);
+}
+assert.equal(510, longBufferString.indexOf('AJABACA'), 'Long AJABACA, First J');
+assert.equal(
+ 1534, longBufferString.indexOf('AJABACA', 511), 'Long AJABACA, Second J');
+
+pattern = 'JABACABADABACABA';
+assert.equal(
+ 511, longBufferString.indexOf(pattern), 'Long JABACABA..., First J');
+assert.equal(
+ 1535, longBufferString.indexOf(pattern, 512), 'Long JABACABA..., Second J');
+
+// Search for a non-ASCII string in a pure ASCII string.
+var asciiString = Buffer.from(
+ 'arglebargleglopglyfarglebargleglopglyfarglebargleglopglyf');
+assert.equal(-1, asciiString.indexOf('\x2061'));
+assert.equal(3, asciiString.indexOf('leb', 0));
+
+// Search in string containing many non-ASCII chars.
+var allCodePoints = [];
+for (var i = 0; i < 65536; i++) allCodePoints[i] = i;
+var allCharsString = String.fromCharCode.apply(String, allCodePoints);
+var allCharsBufferUtf8 = Buffer.from(allCharsString);
+var allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2');
+
+// Search for string long enough to trigger complex search with ASCII pattern
+// and UC16 subject.
+assert.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
+assert.equal(-1, allCharsBufferUcs2.indexOf('notfound'));
+
+{
+ // Find substrings in Utf8.
+ var lengths = [1, 3, 15]; // Single char, simple and complex.
+ var indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b];
+ for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
+ for (var i = 0; i < indices.length; i++) {
+ var index = indices[i];
+ var length = lengths[lengthIndex];
+
+ if (index + length > 0x7F) {
+ length = 2 * length;
+ }
+
+ if (index + length > 0x7FF) {
+ length = 3 * length;
+ }
+
+ if (index + length > 0xFFFF) {
+ length = 4 * length;
+ }
+
+ var patternBufferUtf8 = allCharsBufferUtf8.slice(index, index + length);
+ assert.equal(index, allCharsBufferUtf8.indexOf(patternBufferUtf8));
+
+ var patternStringUtf8 = patternBufferUtf8.toString();
+ assert.equal(index, allCharsBufferUtf8.indexOf(patternStringUtf8));
+ }
+ }
+}
+
+{
+ // Find substrings in Usc2.
+ var lengths = [2, 4, 16]; // Single char, simple and complex.
+ var indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0];
+ for (var lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
+ for (var i = 0; i < indices.length; i++) {
+ var index = indices[i] * 2;
+ var length = lengths[lengthIndex];
+
+ var patternBufferUcs2 =
+ allCharsBufferUcs2.slice(index, index + length);
+ assert.equal(
+ index, allCharsBufferUcs2.indexOf(patternBufferUcs2, 0, 'ucs2'));
+
+ var patternStringUcs2 = patternBufferUcs2.toString('ucs2');
+ assert.equal(
+ index, allCharsBufferUcs2.indexOf(patternStringUcs2, 0, 'ucs2'));
+ }
+ }
+}
+
assert.throws(function() {
b.indexOf(function() { });
});
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
+
+var assert = require('assert');
+
+
+function T(n) {
+ var ui8 = new Uint8Array(n);
+ Object.setPrototypeOf(ui8, T.prototype);
+ return ui8;
+}
+Object.setPrototypeOf(T.prototype, Buffer.prototype);
+Object.setPrototypeOf(T, Buffer);
+
+T.prototype.sum = function sum() {
+ var cntr = 0;
+ for (var i = 0; i < this.length; i++)
+ cntr += this[i];
+ return cntr;
+};
+
+
+var 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);
+
+ t.fill(5);
+ var cntr = 0;
+ for (var i = 0; i < t.length; i++)
+ cntr += t[i];
+ assert.equal(t.length * 5, cntr);
+
+ // Check this does not throw
+ t.toString();
+});
+
'use strict';
-if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
-var common = {};
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
var util = require('util');
buffer.INSPECT_MAX_BYTES = 2;
-var b = new Buffer(4);
+var b = Buffer.allocUnsafe(4);
b.fill('1234');
-var s = new buffer.SlowBuffer(4);
+var s = buffer.SlowBuffer(4);
s.fill('1234');
var expected = '<Buffer 31 32 ... >';
assert.strictEqual(util.inspect(b), expected);
assert.strictEqual(util.inspect(s), expected);
-b = new Buffer(2);
+b = Buffer.allocUnsafe(2);
b.fill('12');
-s = new buffer.SlowBuffer(2);
+s = buffer.SlowBuffer(2);
s.fill('12');
expected = '<Buffer 31 32>';
'use strict';
-if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
-var common = {};
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
-var buffer = new Buffer([1, 2, 3, 4, 5]);
+var buffer = Buffer.from([1, 2, 3, 4, 5]);
var arr;
var b;
arr = [];
-for (var b of buffer.entries())
+for (b of buffer.entries())
arr.push(b);
assert.deepEqual(arr, [
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
+
+var assert = require('assert');
+
+var safe = Buffer.alloc(10);
+
+function isZeroFilled(buf) {
+ for (var n = 0; n < buf.length; n++)
+ if (buf[n] !== 0) return false;
+ return true;
+}
+
+assert(isZeroFilled(safe));
+
+// Test that unsafe allocations doesn't affect subsequent safe allocations
+Buffer.allocUnsafe(10);
+assert(isZeroFilled(new Float64Array(10)));
+
+new Buffer(10);
+assert(isZeroFilled(new Float64Array(10)));
+
+Buffer.allocUnsafe(10);
+assert(isZeroFilled(Buffer.alloc(10)));
+
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
+
+var assert = require('assert');
+var buffer = require('../../');
+var Buffer = buffer.Buffer;
+var SlowBuffer = buffer.SlowBuffer;
+
+var ones = [1, 1, 1, 1];
+
+// should create a Buffer
+var sb = SlowBuffer(4);
+assert(sb instanceof Buffer);
+assert.strictEqual(sb.length, 4);
+sb.fill(1);
+assert.deepEqual(sb, ones);
+
+// underlying ArrayBuffer should have the same length
+assert.strictEqual(sb.buffer.byteLength, 4);
+
+// should work without new
+sb = SlowBuffer(4);
+assert(sb instanceof Buffer);
+assert.strictEqual(sb.length, 4);
+sb.fill(1);
+assert.deepEqual(sb, ones);
+
+// should work with edge cases
+assert.strictEqual(SlowBuffer(0).length, 0);
+try {
+ assert.strictEqual(
+ SlowBuffer(buffer.kMaxLength).length, buffer.kMaxLength);
+} catch (e) {
+ assert.equal(e.message, 'Array buffer allocation failed');
+}
+
+// should work with number-coercible values
+assert.strictEqual(SlowBuffer('6').length, 6);
+assert.strictEqual(SlowBuffer(true).length, 1);
+
+// should create zero-length buffer if parameter is not a number
+assert.strictEqual(SlowBuffer().length, 0);
+assert.strictEqual(SlowBuffer(NaN).length, 0);
+assert.strictEqual(SlowBuffer({}).length, 0);
+assert.strictEqual(SlowBuffer('string').length, 0);
+
+// should throw with invalid length
+assert.throws(function() {
+ SlowBuffer(Infinity);
+}, 'invalid Buffer length');
+assert.throws(function() {
+ SlowBuffer(-1);
+}, 'invalid Buffer length');
+assert.throws(function() {
+ SlowBuffer(buffer.kMaxLength + 1);
+}, 'invalid Buffer length');
+
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
+
+var assert = require('assert');
+
+var buf = Buffer.from([0x1, 0x2, 0x3, 0x4]);
+
+assert.strictEqual(buf, buf.swap16());
+assert.deepStrictEqual(buf, Buffer.from([0x2, 0x1, 0x4, 0x3]));
+
+assert.strictEqual(buf, buf.swap32());
+assert.deepStrictEqual(buf, Buffer.from([0x3, 0x4, 0x1, 0x2]));
+
+var buf_array = [];
+for (var i = 1; i < 33; i++)
+ buf_array.push(i);
+var buf2 = Buffer.from(buf_array);
+buf2.swap32();
+assert.deepStrictEqual(buf2,
+ Buffer.from([0x04, 0x03, 0x02, 0x01, 0x08, 0x07, 0x06, 0x05, 0x0c,
+ 0x0b, 0x0a, 0x09, 0x10, 0x0f, 0x0e, 0x0d, 0x14, 0x13,
+ 0x12, 0x11, 0x18, 0x17, 0x16, 0x15, 0x1c, 0x1b, 0x1a,
+ 0x19, 0x20, 0x1f, 0x1e, 0x1d]));
+buf2.swap16();
+assert.deepStrictEqual(buf2,
+ Buffer.from([0x03, 0x04, 0x01, 0x02, 0x07, 0x08, 0x05, 0x06, 0x0b,
+ 0x0c, 0x09, 0x0a, 0x0f, 0x10, 0x0d, 0x0e, 0x13, 0x14,
+ 0x11, 0x12, 0x17, 0x18, 0x15, 0x16, 0x1b, 0x1c, 0x19,
+ 0x1a, 0x1f, 0x20, 0x1d, 0x1e]));
+
+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]));
+
+// Force use of native code (Buffer size above threshold limit for js impl)
+var buf4 = Buffer.allocUnsafe(1024).fill([0x1, 0x2, 0x3, 0x4]);
+var buf5 = Buffer.allocUnsafe(1024).fill([0x2, 0x1, 0x4, 0x3]);
+var buf6 = Buffer.allocUnsafe(1024)
+ .fill([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]);
+var buf7 = Buffer.allocUnsafe(1024)
+ .fill([0x4, 0x3, 0x2, 0x1, 0x8, 0x7, 0x6, 0x5]);
+
+buf4.swap16();
+assert.deepStrictEqual(buf4, buf5);
+
+buf6.swap32();
+assert.deepStrictEqual(buf6, buf7);
+
+
+var re16 = /Buffer size must be a multiple of 16-bits/;
+var re32 = /Buffer size must be a multiple of 32-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);
+
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+// 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');
+
+function isZeroFilled(buf) {
+ for (var n = 0; n < buf.length; n++)
+ if (buf[n] > 0) return false;
+ return true;
+}
+
+// This can be somewhat unreliable because the
+// 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 = [
+ Buffer.alloc(20),
+ Buffer.allocUnsafe(20),
+ SlowBuffer(20),
+ Buffer(20),
+ new SlowBuffer(20)
+ ];
+ for (var buf of bufs) {
+ assert(isZeroFilled(buf));
+ }
+}
+
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
+
+var assert = require('assert');
+
+
+function testUint8Array(ui) {
+ var length = ui.length;
+ for (var i = 0; i < length; i++)
+ if (ui[i] !== 0) return false;
+ return true;
+}
+
+
+for (var i = 0; i < 100; i++) {
+ Buffer.alloc(0);
+ var ui = new Uint8Array(65);
+ assert.ok(testUint8Array(ui), 'Uint8Array is not zero-filled');
+}
+
'use strict';
-if (process.env.OBJECT_IMPL) global.TYPED_ARRAY_SUPPORT = false;
var Buffer = require('../../').Buffer;
+if (!Buffer.TYPED_ARRAY_SUPPORT) return;
var common = {};
var assert = require('assert');
// console.log('b.length == %d', b.length);
assert.strictEqual(1024, b.length);
-b[0] = 255;
+b[0] = -1;
assert.strictEqual(b[0], 255);
for (var i = 0; i < 1024; i++) {
assert.strictEqual(i % 256, b[i]);
}
-var c = new Buffer(512);
+var c = Buffer(512);
// console.log('c.length == %d', c.length);
assert.strictEqual(512, c.length);
+var d = new Buffer([]);
+assert.strictEqual(0, d.length);
+
+var ui32 = new Uint32Array(4).fill(42);
+var e = Buffer(ui32);
+assert.deepEqual(ui32, e);
+
// First check Buffer#fill() works as expected.
assert.throws(function() {
// Make sure this doesn't hang indefinitely.
Buffer(8).fill('');
-var buf = new Buffer(64);
-buf.fill(10);
-for (var i = 0; i < buf.length; i++)
- assert.equal(buf[i], 10);
-
-buf.fill(11, 0, buf.length >> 1);
-for (var i = 0; i < buf.length >> 1; i++)
- assert.equal(buf[i], 11);
-for (var i = (buf.length >> 1) + 1; i < buf.length; i++)
- assert.equal(buf[i], 10);
-
-buf.fill('h');
-for (var i = 0; i < buf.length; i++)
- assert.equal('h'.charCodeAt(0), buf[i]);
-
-buf.fill(0);
-for (var i = 0; i < buf.length; i++)
- assert.equal(0, buf[i]);
-
-buf.fill(null);
-for (var i = 0; i < buf.length; i++)
- assert.equal(0, buf[i]);
-
-buf.fill(1, 16, 32);
-for (var i = 0; i < 16; i++)
- assert.equal(0, buf[i]);
-for (; i < 32; i++)
- assert.equal(1, buf[i]);
-for (; i < buf.length; i++)
- assert.equal(0, buf[i]);
-
-var buf = new Buffer(10);
-buf.fill('abc');
-assert.equal(buf.toString(), 'abcabcabca');
-buf.fill('է');
-assert.equal(buf.toString(), 'էէէէէ');
-
-// copy 512 bytes, from 0 to 512.
-b.fill(++cntr);
-c.fill(++cntr);
-var copied = b.copy(c, 0, 0, 512);
-// console.log('copied %d bytes from b into c', copied);
-assert.strictEqual(512, copied);
-for (var i = 0; i < c.length; i++) {
- assert.strictEqual(b[i], c[i]);
+{
+ var buf = new Buffer(64);
+ buf.fill(10);
+ for (var i = 0; i < buf.length; i++)
+ assert.equal(buf[i], 10);
+
+ buf.fill(11, 0, buf.length >> 1);
+ for (var i = 0; i < buf.length >> 1; i++)
+ assert.equal(buf[i], 11);
+ for (var i = (buf.length >> 1) + 1; i < buf.length; i++)
+ assert.equal(buf[i], 10);
+
+ buf.fill('h');
+ for (var i = 0; i < buf.length; i++)
+ assert.equal('h'.charCodeAt(0), buf[i]);
+
+ buf.fill(0);
+ for (var i = 0; i < buf.length; i++)
+ assert.equal(0, buf[i]);
+
+ buf.fill(null);
+ for (var i = 0; i < buf.length; i++)
+ assert.equal(0, buf[i]);
+
+ buf.fill(1, 16, 32);
+ for (var i = 0; i < 16; i++)
+ assert.equal(0, buf[i]);
+ for (var i = 16; i < 32; i++)
+ assert.equal(1, buf[i]);
+ for (var i = 32; i < buf.length; i++)
+ assert.equal(0, buf[i]);
}
-// copy c into b, without specifying sourceEnd
-b.fill(++cntr);
-c.fill(++cntr);
-var copied = c.copy(b, 0, 0);
-// console.log('copied %d bytes from c into b w/o sourceEnd', copied);
-assert.strictEqual(c.length, copied);
-for (var i = 0; i < c.length; i++) {
- assert.strictEqual(c[i], b[i]);
+{
+ var buf = new Buffer(10);
+ buf.fill('abc');
+ assert.equal(buf.toString(), 'abcabcabca');
+ buf.fill('է');
+ assert.equal(buf.toString(), 'էէէէէ');
}
-// copy c into b, without specifying sourceStart
-b.fill(++cntr);
-c.fill(++cntr);
-var copied = c.copy(b, 0);
-// console.log('copied %d bytes from c into b w/o sourceStart', copied);
-assert.strictEqual(c.length, copied);
-for (var i = 0; i < c.length; i++) {
- assert.strictEqual(c[i], b[i]);
+{
+ // copy 512 bytes, from 0 to 512.
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c, 0, 0, 512);
+// console.log('copied %d bytes from b into c', copied);
+ assert.strictEqual(512, copied);
+ for (var i = 0; i < c.length; i++) {
+ assert.strictEqual(b[i], c[i]);
+ }
}
-// copy longer buffer b to shorter c without targetStart
-b.fill(++cntr);
-c.fill(++cntr);
-var copied = b.copy(c);
-// console.log('copied %d bytes from b into c w/o targetStart', copied);
-assert.strictEqual(c.length, copied);
-for (var i = 0; i < c.length; i++) {
- assert.strictEqual(b[i], c[i]);
+{
+ // copy c into b, without specifying sourceEnd
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = c.copy(b, 0, 0);
+// console.log('copied %d bytes from c into b w/o sourceEnd', copied);
+ assert.strictEqual(c.length, copied);
+ for (var i = 0; i < c.length; i++) {
+ assert.strictEqual(c[i], b[i]);
+ }
}
-// copy starting near end of b to c
-b.fill(++cntr);
-c.fill(++cntr);
-var copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
-// console.log('copied %d bytes from end of b into beginning of c', copied);
-assert.strictEqual(Math.floor(c.length / 2), copied);
-for (var i = 0; i < Math.floor(c.length / 2); i++) {
- assert.strictEqual(b[b.length - Math.floor(c.length / 2) + i], c[i]);
+{
+ // copy c into b, without specifying sourceStart
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = c.copy(b, 0);
+// console.log('copied %d bytes from c into b w/o sourceStart', copied);
+ assert.strictEqual(c.length, copied);
+ for (var i = 0; i < c.length; i++) {
+ assert.strictEqual(c[i], b[i]);
+ }
}
-for (var i = Math.floor(c.length / 2) + 1; i < c.length; i++) {
- assert.strictEqual(c[c.length - 1], c[i]);
+
+{
+ // copy longer buffer b to shorter c without targetStart
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c);
+// console.log('copied %d bytes from b into c w/o targetStart', copied);
+ assert.strictEqual(c.length, copied);
+ for (var i = 0; i < c.length; i++) {
+ assert.strictEqual(b[i], c[i]);
+ }
+}
+
+{
+ // copy starting near end of b to c
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
+// console.log('copied %d bytes from end of b into beginning of c', copied);
+ assert.strictEqual(Math.floor(c.length / 2), copied);
+ for (var i = 0; i < Math.floor(c.length / 2); i++) {
+ assert.strictEqual(b[b.length - Math.floor(c.length / 2) + i], c[i]);
+ }
+ for (var i = Math.floor(c.length / 2) + 1; i < c.length; i++) {
+ assert.strictEqual(c[c.length - 1], c[i]);
+ }
}
-// try to copy 513 bytes, and check we don't overrun c
-b.fill(++cntr);
-c.fill(++cntr);
-var copied = b.copy(c, 0, 0, 513);
-// console.log('copied %d bytes from b trying to overrun c', copied);
-assert.strictEqual(c.length, copied);
-for (var i = 0; i < c.length; i++) {
- assert.strictEqual(b[i], c[i]);
+{
+ // try to copy 513 bytes, and check we don't overrun c
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c, 0, 0, 513);
+// console.log('copied %d bytes from b trying to overrun c', copied);
+ assert.strictEqual(c.length, copied);
+ for (var i = 0; i < c.length; i++) {
+ assert.strictEqual(b[i], c[i]);
+ }
}
-// copy 768 bytes from b into b
-b.fill(++cntr);
-b.fill(++cntr, 256);
-var copied = b.copy(b, 0, 256, 1024);
-// console.log('copied %d bytes from b into b', copied);
-assert.strictEqual(768, copied);
-for (var i = 0; i < b.length; i++) {
- assert.strictEqual(cntr, b[i]);
+{
+ // copy 768 bytes from b into b
+ b.fill(++cntr);
+ b.fill(++cntr, 256);
+ var copied = b.copy(b, 0, 256, 1024);
+// console.log('copied %d bytes from b into b', copied);
+ assert.strictEqual(768, copied);
+ for (var i = 0; i < b.length; i++) {
+ assert.strictEqual(cntr, b[i]);
+ }
}
// copy string longer than buffer length (failure will segfault)
-var bb = new Buffer(10);
+var bb = Buffer(10);
bb.fill('hello crazy world');
-var caught_error = null;
-
// try to copy from before the beginning of b
-caught_error = null;
-try {
- var copied = b.copy(c, 0, 100, 10);
-} catch (err) {
- caught_error = err;
-}
+assert.doesNotThrow(() => { b.copy(c, 0, 100, 10); });
// copy throws at negative sourceStart
assert.throws(function() {
Buffer(5).copy(Buffer(5), 0, -1);
}, RangeError);
-// check sourceEnd resets to targetEnd if former is greater than the latter
-b.fill(++cntr);
-c.fill(++cntr);
-var copied = b.copy(c, 0, 0, 1025);
-// console.log('copied %d bytes from b into c', copied);
-for (var i = 0; i < c.length; i++) {
- assert.strictEqual(b[i], c[i]);
+{
+ // check sourceEnd resets to targetEnd if former is greater than the latter
+ b.fill(++cntr);
+ c.fill(++cntr);
+ var copied = b.copy(c, 0, 0, 1025);
+// console.log('copied %d bytes from b into c', copied);
+ for (var i = 0; i < c.length; i++) {
+ assert.strictEqual(b[i], c[i]);
+ }
}
// throw with negative sourceEnd
// invalid encoding for Buffer.toString
caught_error = null;
try {
- var copied = b.toString('invalid');
+ b.toString('invalid');
} catch (err) {
caught_error = err;
}
// invalid encoding for Buffer.write
caught_error = null;
try {
- var copied = b.write('test string', 0, 5, 'invalid');
+ b.write('test string', 0, 5, 'invalid');
} catch (err) {
caught_error = err;
}
new Buffer('');
new Buffer('', 'ascii');
new Buffer('', 'binary');
-new Buffer(0);
+Buffer(0);
// try to write a 0-length string beyond the end of b
assert.throws(function() {
}, RangeError);
// try to copy 0 bytes worth of data into an empty buffer
-b.copy(new Buffer(0), 0, 0, 0);
+b.copy(Buffer(0), 0, 0, 0);
// try to copy 0 bytes past the end of the target buffer
-b.copy(new Buffer(0), 1, 1, 1);
-b.copy(new Buffer(1), 1, 1, 1);
+b.copy(Buffer(0), 1, 1, 1);
+b.copy(Buffer(1), 1, 1, 1);
// try to copy 0 bytes from past the end of the source buffer
-b.copy(new Buffer(1), 0, 2048, 2048);
-
-// try to toString() a 0-length slice of a buffer, both within and without the
-// valid buffer range
-assert.equal(new Buffer('abc').toString('ascii', 0, 0), '');
-assert.equal(new Buffer('abc').toString('ascii', -100, -100), '');
-assert.equal(new Buffer('abc').toString('ascii', 100, 100), '');
+b.copy(Buffer(1), 0, 2048, 2048);
+
+var rangeBuffer = new Buffer('abc');
+
+// if start >= buffer's length, empty string will be returned
+assert.equal(rangeBuffer.toString('ascii', 3), '');
+assert.equal(rangeBuffer.toString('ascii', +Infinity), '');
+assert.equal(rangeBuffer.toString('ascii', 3.14, 3), '');
+assert.equal(rangeBuffer.toString('ascii', 'Infinity', 3), '');
+
+// if end <= 0, empty string will be returned
+assert.equal(rangeBuffer.toString('ascii', 1, 0), '');
+assert.equal(rangeBuffer.toString('ascii', 1, -1.2), '');
+assert.equal(rangeBuffer.toString('ascii', 1, -100), '');
+assert.equal(rangeBuffer.toString('ascii', 1, -Infinity), '');
+
+// if start < 0, start will be taken as zero
+assert.equal(rangeBuffer.toString('ascii', -1, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', -1.99, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', -Infinity, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', '-1', 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', '-1.99', 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', '-Infinity', 3), 'abc');
+
+// if start is an invalid integer, start will be taken as zero
+assert.equal(rangeBuffer.toString('ascii', 'node.js', 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', {}, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', [], 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', NaN, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', null, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', undefined, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', false, 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', '', 3), 'abc');
+
+// but, if start is an integer when coerced, then it will be coerced and used.
+assert.equal(rangeBuffer.toString('ascii', '-1', 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', '1', 3), 'bc');
+assert.equal(rangeBuffer.toString('ascii', '-Infinity', 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', '3', 3), '');
+assert.equal(rangeBuffer.toString('ascii', Number(3), 3), '');
+assert.equal(rangeBuffer.toString('ascii', '3.14', 3), '');
+assert.equal(rangeBuffer.toString('ascii', '1.99', 3), 'bc');
+assert.equal(rangeBuffer.toString('ascii', '-1.99', 3), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 1.99, 3), 'bc');
+assert.equal(rangeBuffer.toString('ascii', true, 3), 'bc');
+
+// if end > buffer's length, end will be taken as buffer's length
+assert.equal(rangeBuffer.toString('ascii', 0, 5), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, 6.99), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, Infinity), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, '5'), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, '6.99'), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, 'Infinity'), 'abc');
+
+// if end is an invalid integer, end will be taken as buffer's length
+assert.equal(rangeBuffer.toString('ascii', 0, 'node.js'), '');
+assert.equal(rangeBuffer.toString('ascii', 0, {}), '');
+assert.equal(rangeBuffer.toString('ascii', 0, NaN), '');
+assert.equal(rangeBuffer.toString('ascii', 0, undefined), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, null), '');
+assert.equal(rangeBuffer.toString('ascii', 0, []), '');
+assert.equal(rangeBuffer.toString('ascii', 0, false), '');
+assert.equal(rangeBuffer.toString('ascii', 0, ''), '');
+
+// but, if end is an integer when coerced, then it will be coerced and used.
+assert.equal(rangeBuffer.toString('ascii', 0, '-1'), '');
+assert.equal(rangeBuffer.toString('ascii', 0, '1'), 'a');
+assert.equal(rangeBuffer.toString('ascii', 0, '-Infinity'), '');
+assert.equal(rangeBuffer.toString('ascii', 0, '3'), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, Number(3)), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, '3.14'), 'abc');
+assert.equal(rangeBuffer.toString('ascii', 0, '1.99'), 'a');
+assert.equal(rangeBuffer.toString('ascii', 0, '-1.99'), '');
+assert.equal(rangeBuffer.toString('ascii', 0, 1.99), 'a');
+assert.equal(rangeBuffer.toString('ascii', 0, true), 'a');
// try toString() with a object as a encoding
-assert.equal(new Buffer('abc').toString({toString: function() {
+assert.equal(rangeBuffer.toString({toString: function() {
return 'ascii';
}}), 'abc');
+// testing for smart defaults and ability to pass string values as offset
+var writeTest = new Buffer('abcdes');
+writeTest.write('n', 'ascii');
+writeTest.write('o', '1', 'ascii');
+writeTest.write('d', '2', 'ascii');
+writeTest.write('e', 3, 'ascii');
+writeTest.write('j', 4, 'ascii');
+assert.equal(writeTest.toString(), 'nodejs');
+
// ASCII slice test
+{
+ var asciiString = 'hello world';
-var asciiString = 'hello world';
-var offset = 100;
+ for (var i = 0; i < asciiString.length; i++) {
+ b[i] = asciiString.charCodeAt(i);
+ }
+ var asciiSlice = b.toString('ascii', 0, asciiString.length);
+ assert.equal(asciiString, asciiSlice);
+}
-for (var i = 0; i < asciiString.length; i++) {
- b[i] = asciiString.charCodeAt(i);
+{
+ var asciiString = 'hello world';
+ var offset = 100;
+
+ var written = b.write(asciiString, offset, 'ascii');
+ assert.equal(asciiString.length, written);
+ var asciiSlice = b.toString('ascii', offset, offset + asciiString.length);
+ assert.equal(asciiString, asciiSlice);
}
-var asciiSlice = b.toString('ascii', 0, asciiString.length);
-assert.equal(asciiString, asciiSlice);
-var written = b.write(asciiString, offset, 'ascii');
-assert.equal(asciiString.length, written);
-var asciiSlice = b.toString('ascii', offset, offset + asciiString.length);
-assert.equal(asciiString, asciiSlice);
+{
+ var asciiString = 'hello world';
+ var offset = 100;
-var sliceA = b.slice(offset, offset + asciiString.length);
-var sliceB = b.slice(offset, offset + asciiString.length);
-for (var i = 0; i < asciiString.length; i++) {
- assert.equal(sliceA[i], sliceB[i]);
+ var sliceA = b.slice(offset, offset + asciiString.length);
+ var sliceB = b.slice(offset, offset + asciiString.length);
+ for (var i = 0; i < asciiString.length; i++) {
+ assert.equal(sliceA[i], sliceB[i]);
+ }
}
// UTF-8 slice test
assert.equal(sliceA[i], sliceB[i]);
}
-var slice = b.slice(100, 150);
-assert.equal(50, slice.length);
-for (var i = 0; i < 50; i++) {
- assert.equal(b[100 + i], slice[i]);
+{
+ var slice = b.slice(100, 150);
+ assert.equal(50, slice.length);
+ for (var i = 0; i < 50; i++) {
+ assert.equal(b[100 + i], slice[i]);
+ }
+}
+
+{
+ // make sure only top level parent propagates from allocPool
+ var b = new Buffer(5);
+ var c = b.slice(0, 4);
+ var d = c.slice(0, 2);
+ assert.equal(b.parent, c.parent);
+ assert.equal(b.parent, d.parent);
+}
+
+{
+ // also from a non-pooled instance
+ var b = new SlowBuffer(5);
+ var c = b.slice(0, 4);
+ var d = c.slice(0, 2);
+ assert.equal(c.parent, d.parent);
+}
+
+{
+ // Bug regression test
+ var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語
+ var buffer = new Buffer(32);
+ var size = buffer.write(testValue, 0, 'utf8');
+// console.log('bytes written to buffer: ' + size);
+ var slice = buffer.toString('utf8', 0, size);
+ assert.equal(slice, testValue);
}
+{
+ // Test triple slice
+ var a = new Buffer(8);
+ for (var i = 0; i < 8; i++) a[i] = i;
+ var b = a.slice(4, 8);
+ assert.equal(4, b[0]);
+ assert.equal(5, b[1]);
+ assert.equal(6, b[2]);
+ assert.equal(7, b[3]);
+ var c = b.slice(2, 4);
+ assert.equal(6, c[0]);
+ assert.equal(7, c[1]);
+}
-// make sure only top level parent propagates from allocPool
-var b = new Buffer(5);
-var c = b.slice(0, 4);
-var d = c.slice(0, 2);
-assert.equal(b.parent, c.parent);
-assert.equal(b.parent, d.parent);
-
-// also from a non-pooled instance
-var b = new SlowBuffer(5);
-var c = b.slice(0, 4);
-var d = c.slice(0, 2);
-
-
-// Bug regression test
-var testValue = '\u00F6\u65E5\u672C\u8A9E'; // ö日本語
-var buffer = new Buffer(32);
-var size = buffer.write(testValue, 0, 'utf8');
-// console.log('bytes written to buffer: ' + size);
-var slice = buffer.toString('utf8', 0, size);
-assert.equal(slice, testValue);
-
-
-// Test triple slice
-var a = new Buffer(8);
-for (var i = 0; i < 8; i++) a[i] = i;
-var b = a.slice(4, 8);
-assert.equal(4, b[0]);
-assert.equal(5, b[1]);
-assert.equal(6, b[2]);
-assert.equal(7, b[3]);
-var c = b.slice(2, 4);
-assert.equal(6, c[0]);
-assert.equal(7, c[1]);
-
-
-var d = new Buffer([23, 42, 255]);
-assert.equal(d.length, 3);
-assert.equal(d[0], 23);
-assert.equal(d[1], 42);
-assert.equal(d[2], 255);
-assert.deepEqual(d, new Buffer(d));
-
-var e = new Buffer('über');
-// console.error('uber: \'%s\'', e.toString());
-assert.deepEqual(e, new Buffer([195, 188, 98, 101, 114]));
-
-var f = new Buffer('über', 'ascii');
-// console.error('f.length: %d (should be 4)', f.length);
-assert.deepEqual(f, new Buffer([252, 98, 101, 114]));
+{
+ var d = new Buffer([23, 42, 255]);
+ assert.equal(d.length, 3);
+ assert.equal(d[0], 23);
+ assert.equal(d[1], 42);
+ assert.equal(d[2], 255);
+ assert.deepEqual(d, new Buffer(d));
+}
+
+{
+ var e = new Buffer('über');
+// console.error('uber: \'%s\'', e.toString());
+ assert.deepEqual(e, new Buffer([195, 188, 98, 101, 114]));
+}
+
+{
+ var f = new Buffer('über', 'ascii');
+// console.error('f.length: %d (should be 4)', f.length);
+ assert.deepEqual(f, new Buffer([252, 98, 101, 114]));
+}
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
- var f = new Buffer('über', encoding);
-// console.error('f.length: %d (should be 8)', f.length);
- assert.deepEqual(f, new Buffer([252, 0, 98, 0, 101, 0, 114, 0]));
-
- var f = new Buffer('привет', encoding);
-// console.error('f.length: %d (should be 12)', f.length);
- assert.deepEqual(f, new Buffer([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]));
- assert.equal(f.toString(encoding), 'привет');
-
- var f = new Buffer([0, 0, 0, 0, 0]);
- assert.equal(f.length, 5);
- var size = f.write('あいうえお', encoding);
-// console.error('bytes written to buffer: %d (should be 4)', size);
- assert.equal(size, 4);
- assert.deepEqual(f, new Buffer([0x42, 0x30, 0x44, 0x30, 0x00]));
+ {
+ var f = new Buffer('über', encoding);
+// console.error('f.length: %d (should be 8)', f.length);
+ assert.deepEqual(f, new Buffer([252, 0, 98, 0, 101, 0, 114, 0]));
+ }
+
+ {
+ var f = new Buffer('привет', encoding);
+// console.error('f.length: %d (should be 12)', f.length);
+ assert.deepEqual(f, new Buffer([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]));
+ assert.equal(f.toString(encoding), 'привет');
+ }
+
+ {
+ var f = new Buffer([0, 0, 0, 0, 0]);
+ assert.equal(f.length, 5);
+ var size = f.write('あいうえお', encoding);
+// console.error('bytes written to buffer: %d (should be 4)', size);
+ assert.equal(size, 4);
+ assert.deepEqual(f, new Buffer([0x42, 0x30, 0x44, 0x30, 0x00]));
+ }
});
-var f = new Buffer('\uD83D\uDC4D', 'utf-16le'); // THUMBS UP SIGN (U+1F44D)
-assert.equal(f.length, 4);
-assert.deepEqual(f, new Buffer('3DD84DDC', 'hex'));
+{
+ var f = new Buffer('\uD83D\uDC4D', 'utf-16le'); // THUMBS UP SIGN (U+1F44D)
+ assert.equal(f.length, 4);
+ assert.deepEqual(f, new Buffer('3DD84DDC', 'hex'));
+}
var arrayIsh = {0: 0, 1: 1, 2: 2, 3: 3, length: 4};
//
assert.equal('TWFu', (new Buffer('Man')).toString('base64'));
-// test that regular and URL-safe base64 both work
-var expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff];
-assert.deepEqual(Buffer('//++/++/++//', 'base64'), Buffer(expected));
-assert.deepEqual(Buffer('__--_--_--__', 'base64'), Buffer(expected));
-
-// big example
-var quote = 'Man is distinguished, not only by his reason, but by this ' +
- 'singular passion from other animals, which is a lust ' +
- 'of the mind, that by a perseverance of delight in the continued ' +
- 'and indefatigable generation of knowledge, exceeds the short ' +
- 'vehemence of any carnal pleasure.';
-var expected = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24s' +
- 'IGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltY' +
- 'WxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZX' +
- 'JzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmR' +
- 'lZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo' +
- 'ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=';
-assert.equal(expected, (new Buffer(quote)).toString('base64'));
-
-
-b = new Buffer(1024);
-var bytesWritten = b.write(expected, 0, 'base64');
-assert.equal(quote.length, bytesWritten);
-assert.equal(quote, b.toString('ascii', 0, quote.length));
-
-// check that the base64 decoder ignores whitespace
-var expectedWhite = expected.slice(0, 60) + ' \n' +
- expected.slice(60, 120) + ' \n' +
- expected.slice(120, 180) + ' \n' +
- expected.slice(180, 240) + ' \n' +
- expected.slice(240, 300) + '\n' +
- expected.slice(300, 360) + '\n';
-b = new Buffer(1024);
-bytesWritten = b.write(expectedWhite, 0, 'base64');
-assert.equal(quote.length, bytesWritten);
-assert.equal(quote, b.toString('ascii', 0, quote.length));
-
-// check that the base64 decoder on the constructor works
-// even in the presence of whitespace.
-b = new Buffer(expectedWhite, 'base64');
-assert.equal(quote.length, b.length);
-assert.equal(quote, b.toString('ascii', 0, quote.length));
-
-// check that the base64 decoder ignores illegal chars
-var expectedIllegal = expected.slice(0, 60) + ' \x80' +
- expected.slice(60, 120) + ' \xff' +
- expected.slice(120, 180) + ' \x00' +
- expected.slice(180, 240) + ' \x98' +
- expected.slice(240, 300) + '\x03' +
- expected.slice(300, 360);
-b = new Buffer(expectedIllegal, 'base64');
-assert.equal(quote.length, b.length);
-assert.equal(quote, b.toString('ascii', 0, quote.length));
+{
+ // test that regular and URL-safe base64 both work
+ var expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff];
+ assert.deepEqual(Buffer('//++/++/++//', 'base64'), Buffer(expected));
+ assert.deepEqual(Buffer('__--_--_--__', 'base64'), Buffer(expected));
+}
+{
+ // big example
+ var quote = 'Man is distinguished, not only by his reason, but by this ' +
+ 'singular passion from other animals, which is a lust ' +
+ 'of the mind, that by a perseverance of delight in the ' +
+ 'continued and indefatigable generation of knowledge, ' +
+ 'exceeds the short vehemence of any carnal pleasure.';
+ var expected = 'TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb' +
+ '24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlci' +
+ 'BhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQ' +
+ 'gYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu' +
+ 'dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZ' +
+ 'GdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm' +
+ '5hbCBwbGVhc3VyZS4=';
+ assert.equal(expected, (new Buffer(quote)).toString('base64'));
+
+ var b = new Buffer(1024);
+ var bytesWritten = b.write(expected, 0, 'base64');
+ assert.equal(quote.length, bytesWritten);
+ assert.equal(quote, b.toString('ascii', 0, quote.length));
+
+ // check that the base64 decoder ignores whitespace
+ var expectedWhite = expected.slice(0, 60) + ' \n' +
+ expected.slice(60, 120) + ' \n' +
+ expected.slice(120, 180) + ' \n' +
+ expected.slice(180, 240) + ' \n' +
+ expected.slice(240, 300) + '\n' +
+ expected.slice(300, 360) + '\n';
+ b = new Buffer(1024);
+ bytesWritten = b.write(expectedWhite, 0, 'base64');
+ assert.equal(quote.length, bytesWritten);
+ assert.equal(quote, b.toString('ascii', 0, quote.length));
+
+ // check that the base64 decoder on the constructor works
+ // even in the presence of whitespace.
+ b = new Buffer(expectedWhite, 'base64');
+ assert.equal(quote.length, b.length);
+ assert.equal(quote, b.toString('ascii', 0, quote.length));
+
+ // check that the base64 decoder ignores illegal chars
+ var expectedIllegal = expected.slice(0, 60) + ' \x80' +
+ expected.slice(60, 120) + ' \xff' +
+ expected.slice(120, 180) + ' \x00' +
+ expected.slice(180, 240) + ' \x98' +
+ expected.slice(240, 300) + '\x03' +
+ expected.slice(300, 360);
+ b = new Buffer(expectedIllegal, 'base64');
+ assert.equal(quote.length, b.length);
+ assert.equal(quote, b.toString('ascii', 0, quote.length));
+}
assert.equal(new Buffer('', 'base64').toString(), '');
assert.equal(new Buffer('K', 'base64').toString(), '');
assert.equal(dot[3], 0x00);
assert.equal(dot.toString('base64'), '//4uAA==');
-// Writing base64 at a position > 0 should not mangle the result.
-//
-// https://github.com/joyent/node/issues/402
-var segments = ['TWFkbmVzcz8h', 'IFRoaXM=', 'IGlz', 'IG5vZGUuanMh'];
-var buf = new Buffer(64);
-var pos = 0;
+{
+ // Writing base64 at a position > 0 should not mangle the result.
+ //
+ // https://github.com/joyent/node/issues/402
+ var segments = ['TWFkbmVzcz8h', 'IFRoaXM=', 'IGlz', 'IG5vZGUuanMh'];
+ var b = new Buffer(64);
+ var pos = 0;
-for (var i = 0; i < segments.length; ++i) {
- pos += b.write(segments[i], pos, 'base64');
+ for (var i = 0; i < segments.length; ++i) {
+ pos += b.write(segments[i], pos, 'base64');
+ }
+ assert.equal(b.toString('binary', 0, pos), 'Madness?! This is node.js!');
}
-assert.equal(b.toString('binary', 0, pos), 'Madness?! This is node.js!');
-// Creating buffers larger than pool size.
-var l = Buffer.poolSize + 5;
-var s = '';
-for (i = 0; i < l; i++) {
- s += 'h';
-}
+// Regression test for https://github.com/nodejs/node/issues/3496.
+// TODO assert.equal(Buffer('=bad'.repeat(1e4), 'base64').length, 0);
-var b = new Buffer(s);
+{
+ // Creating buffers larger than pool size.
+ var l = Buffer.poolSize + 5;
+ var s = 'h'.repeat(l);
-for (i = 0; i < l; i++) {
- assert.equal('h'.charCodeAt(0), b[i]);
-}
+ var b = new Buffer(s);
-var sb = b.toString();
-assert.equal(sb.length, s.length);
-assert.equal(sb, s);
+ for (var i = 0; i < l; i++) {
+ assert.equal('h'.charCodeAt(0), b[i]);
+ }
+ var sb = b.toString();
+ assert.equal(sb.length, s.length);
+ assert.equal(sb, s);
+}
-// Single argument slice
-b = new Buffer('abcde');
-assert.equal('bcde', b.slice(1).toString());
+{
+ // Single argument slice
+ var b = new Buffer('abcde');
+ assert.equal('bcde', b.slice(1).toString());
+}
// slice(0,0).length === 0
assert.equal(0, Buffer('hello').slice(0, 0).length);
assert.equal(hexb2[i], hexb[i]);
}
-// test an invalid slice end.
-// console.log('Try to slice off the end of the buffer');
-var b = new Buffer([1, 2, 3, 4, 5]);
-var b2 = b.toString('hex', 1, 10000);
-var b3 = b.toString('hex', 1, 5);
-var b4 = b.toString('hex', 1);
-assert.equal(b2, b3);
-assert.equal(b2, b4);
-
+{
+ // test an invalid slice end.
+// console.log('Try to slice off the end of the buffer');
+ var b = new Buffer([1, 2, 3, 4, 5]);
+ var b2 = b.toString('hex', 1, 10000);
+ var b3 = b.toString('hex', 1, 5);
+ var b4 = b.toString('hex', 1);
+ assert.equal(b2, b3);
+ assert.equal(b2, b4);
+}
function buildBuffer(data) {
if (Array.isArray(data)) {
- var buffer = new Buffer(data.length);
+ var buffer = Buffer(data.length);
data.forEach(function(v, k) {
buffer[k] = v;
});
// console.log(x.inspect());
assert.equal('<Buffer 81 a3 66 6f 6f a3 62 61 72>', x.inspect());
-var z = x.slice(4);
-// console.log(z.inspect());
-// console.log(z.length);
-assert.equal(5, z.length);
-assert.equal(0x6f, z[0]);
-assert.equal(0xa3, z[1]);
-assert.equal(0x62, z[2]);
-assert.equal(0x61, z[3]);
-assert.equal(0x72, z[4]);
-
-var z = x.slice(0);
-// console.log(z.inspect());
-// console.log(z.length);
-assert.equal(z.length, x.length);
-
-var z = x.slice(0, 4);
-// console.log(z.inspect());
-// console.log(z.length);
-assert.equal(4, z.length);
-assert.equal(0x81, z[0]);
-assert.equal(0xa3, z[1]);
-
-var z = x.slice(0, 9);
-// console.log(z.inspect());
-// console.log(z.length);
-assert.equal(9, z.length);
-
-var z = x.slice(1, 4);
-// console.log(z.inspect());
-// console.log(z.length);
-assert.equal(3, z.length);
-assert.equal(0xa3, z[0]);
-
-var z = x.slice(2, 4);
-// console.log(z.inspect());
-// console.log(z.length);
-assert.equal(2, z.length);
-assert.equal(0x66, z[0]);
-assert.equal(0x6f, z[1]);
+{
+ var z = x.slice(4);
+// console.log(z.inspect());
+// console.log(z.length);
+ assert.equal(5, z.length);
+ assert.equal(0x6f, z[0]);
+ assert.equal(0xa3, z[1]);
+ assert.equal(0x62, z[2]);
+ assert.equal(0x61, z[3]);
+ assert.equal(0x72, z[4]);
+}
+
+{
+ var z = x.slice(0);
+// console.log(z.inspect());
+// console.log(z.length);
+ assert.equal(z.length, x.length);
+}
+
+{
+ var z = x.slice(0, 4);
+// console.log(z.inspect());
+// console.log(z.length);
+ assert.equal(4, z.length);
+ assert.equal(0x81, z[0]);
+ assert.equal(0xa3, z[1]);
+}
+
+{
+ var z = x.slice(0, 9);
+// console.log(z.inspect());
+// console.log(z.length);
+ assert.equal(9, z.length);
+}
+
+{
+ var z = x.slice(1, 4);
+// console.log(z.inspect());
+// console.log(z.length);
+ assert.equal(3, z.length);
+ assert.equal(0xa3, z[0]);
+}
+
+{
+ var z = x.slice(2, 4);
+// console.log(z.inspect());
+// console.log(z.length);
+ assert.equal(2, z.length);
+ assert.equal(0x66, z[0]);
+ assert.equal(0x6f, z[1]);
+}
assert.equal(0, Buffer('hello').slice(0, 0).length);
assert.equal(b.toString(encoding), 'あいうえお');
});
-// Binary encoding should write only one byte per character.
-var b = Buffer([0xde, 0xad, 0xbe, 0xef]);
-var s = String.fromCharCode(0xffff);
-b.write(s, 0, 'binary');
-assert.equal(0xff, b[0]);
-assert.equal(0xad, b[1]);
-assert.equal(0xbe, b[2]);
-assert.equal(0xef, b[3]);
-s = String.fromCharCode(0xaaee);
-b.write(s, 0, 'binary');
-assert.equal(0xee, b[0]);
-assert.equal(0xad, b[1]);
-assert.equal(0xbe, b[2]);
-assert.equal(0xef, b[3]);
-
-// #1210 Test UTF-8 string includes null character
-var buf = new Buffer('\0');
-assert.equal(buf.length, 1);
-buf = new Buffer('\0\0');
-assert.equal(buf.length, 2);
-
-buf = new Buffer(2);
-var written = buf.write(''); // 0byte
-assert.equal(written, 0);
-written = buf.write('\0'); // 1byte (v8 adds null terminator)
-assert.equal(written, 1);
-written = buf.write('a\0'); // 1byte * 2
-assert.equal(written, 2);
-written = buf.write('あ'); // 3bytes
-assert.equal(written, 0);
-written = buf.write('\0あ'); // 1byte + 3bytes
-assert.equal(written, 1);
-written = buf.write('\0\0あ'); // 1byte * 2 + 3bytes
-assert.equal(written, 2);
-
-buf = new Buffer(10);
-written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator)
-assert.equal(written, 9);
-written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
-assert.equal(written, 10);
-
-// #243 Test write() with maxLength
-var buf = new Buffer(4);
-buf.fill(0xFF);
-var written = buf.write('abcd', 1, 2, 'utf8');
-// console.log(buf);
-assert.equal(written, 2);
-assert.equal(buf[0], 0xFF);
-assert.equal(buf[1], 0x61);
-assert.equal(buf[2], 0x62);
-assert.equal(buf[3], 0xFF);
-
-buf.fill(0xFF);
-written = buf.write('abcd', 1, 4);
-// console.log(buf);
-assert.equal(written, 3);
-assert.equal(buf[0], 0xFF);
-assert.equal(buf[1], 0x61);
-assert.equal(buf[2], 0x62);
-assert.equal(buf[3], 0x63);
-
-buf.fill(0xFF);
-written = buf.write('abcdef', 1, 2, 'hex');
-// console.log(buf);
-assert.equal(written, 2);
-assert.equal(buf[0], 0xFF);
-assert.equal(buf[1], 0xAB);
-assert.equal(buf[2], 0xCD);
-assert.equal(buf[3], 0xFF);
+{
+ // Binary encoding should write only one byte per character.
+ var b = Buffer([0xde, 0xad, 0xbe, 0xef]);
+ var s = String.fromCharCode(0xffff);
+ b.write(s, 0, 'binary');
+ assert.equal(0xff, b[0]);
+ assert.equal(0xad, b[1]);
+ assert.equal(0xbe, b[2]);
+ assert.equal(0xef, b[3]);
+ s = String.fromCharCode(0xaaee);
+ b.write(s, 0, 'binary');
+ assert.equal(0xee, b[0]);
+ assert.equal(0xad, b[1]);
+ assert.equal(0xbe, b[2]);
+ assert.equal(0xef, b[3]);
+}
-['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
+{
+ // #1210 Test UTF-8 string includes null character
+ var buf = new Buffer('\0');
+ assert.equal(buf.length, 1);
+ buf = new Buffer('\0\0');
+ assert.equal(buf.length, 2);
+}
+
+{
+ var buf = new Buffer(2);
+ var written = buf.write(''); // 0byte
+ assert.equal(written, 0);
+ written = buf.write('\0'); // 1byte (v8 adds null terminator)
+ assert.equal(written, 1);
+ written = buf.write('a\0'); // 1byte * 2
+ assert.equal(written, 2);
+ written = buf.write('あ'); // 3bytes
+ assert.equal(written, 0);
+ written = buf.write('\0あ'); // 1byte + 3bytes
+ assert.equal(written, 1);
+ written = buf.write('\0\0あ'); // 1byte * 2 + 3bytes
+ assert.equal(written, 2);
+}
+
+{
+ var buf = new Buffer(10);
+ written = buf.write('あいう'); // 3bytes * 3 (v8 adds null terminator)
+ assert.equal(written, 9);
+ written = buf.write('あいう\0'); // 3bytes * 3 + 1byte
+ assert.equal(written, 10);
+}
+
+{
+ // #243 Test write() with maxLength
+ var buf = new Buffer(4);
buf.fill(0xFF);
- written = buf.write('abcd', 0, 2, encoding);
+ var written = buf.write('abcd', 1, 2, 'utf8');
// console.log(buf);
assert.equal(written, 2);
- assert.equal(buf[0], 0x61);
- assert.equal(buf[1], 0x00);
- assert.equal(buf[2], 0xFF);
+ assert.equal(buf[0], 0xFF);
+ assert.equal(buf[1], 0x61);
+ assert.equal(buf[2], 0x62);
assert.equal(buf[3], 0xFF);
-});
-// test offset returns are correct
-var b = new Buffer(16);
-assert.equal(4, b.writeUInt32LE(0, 0));
-assert.equal(6, b.writeUInt16LE(0, 4));
-assert.equal(7, b.writeUInt8(0, 6));
-assert.equal(8, b.writeInt8(0, 7));
-assert.equal(16, b.writeDoubleLE(0, 8));
-
-// test unmatched surrogates not producing invalid utf8 output
-// ef bf bd = utf-8 representation of unicode replacement character
-// see https://codereview.chromium.org/121173009/
-buf = new Buffer('ab\ud800cd', 'utf8');
-assert.equal(buf[0], 0x61);
-assert.equal(buf[1], 0x62);
-assert.equal(buf[2], 0xef);
-assert.equal(buf[3], 0xbf);
-assert.equal(buf[4], 0xbd);
-assert.equal(buf[5], 0x63);
-assert.equal(buf[6], 0x64);
-
-// test for buffer overrun
-buf = new Buffer([0, 0, 0, 0, 0]); // length: 5
-var sub = buf.slice(0, 4); // length: 4
-written = sub.write('12345', 'binary');
-assert.equal(written, 4);
-assert.equal(buf[4], 0);
+ buf.fill(0xFF);
+ written = buf.write('abcd', 1, 4);
+// console.log(buf);
+ assert.equal(written, 3);
+ assert.equal(buf[0], 0xFF);
+ assert.equal(buf[1], 0x61);
+ assert.equal(buf[2], 0x62);
+ assert.equal(buf[3], 0x63);
+
+ buf.fill(0xFF);
+ written = buf.write('abcd', 1, 2, 'utf8');
+// console.log(buf);
+ assert.equal(written, 2);
+ assert.equal(buf[0], 0xFF);
+ assert.equal(buf[1], 0x61);
+ assert.equal(buf[2], 0x62);
+ assert.equal(buf[3], 0xFF);
+
+ buf.fill(0xFF);
+ written = buf.write('abcdef', 1, 2, 'hex');
+// console.log(buf);
+ assert.equal(written, 2);
+ assert.equal(buf[0], 0xFF);
+ assert.equal(buf[1], 0xAB);
+ assert.equal(buf[2], 0xCD);
+ assert.equal(buf[3], 0xFF);
+
+ ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
+ buf.fill(0xFF);
+ written = buf.write('abcd', 0, 2, encoding);
+// console.log(buf);
+ assert.equal(written, 2);
+ assert.equal(buf[0], 0x61);
+ assert.equal(buf[1], 0x00);
+ assert.equal(buf[2], 0xFF);
+ assert.equal(buf[3], 0xFF);
+ });
+}
+
+{
+ // test offset returns are correct
+ var b = new Buffer(16);
+ assert.equal(4, b.writeUInt32LE(0, 0));
+ assert.equal(6, b.writeUInt16LE(0, 4));
+ assert.equal(7, b.writeUInt8(0, 6));
+ assert.equal(8, b.writeInt8(0, 7));
+ assert.equal(16, b.writeDoubleLE(0, 8));
+}
+
+{
+ // test unmatched surrogates not producing invalid utf8 output
+ // ef bf bd = utf-8 representation of unicode replacement character
+ // see https://codereview.chromium.org/121173009/
+ var buf = new Buffer('ab\ud800cd', 'utf8');
+ assert.equal(buf[0], 0x61);
+ assert.equal(buf[1], 0x62);
+ assert.equal(buf[2], 0xef);
+ assert.equal(buf[3], 0xbf);
+ assert.equal(buf[4], 0xbd);
+ assert.equal(buf[5], 0x63);
+ assert.equal(buf[6], 0x64);
+}
+
+{
+ // test for buffer overrun
+ var buf = new Buffer([0, 0, 0, 0, 0]); // length: 5
+ var sub = buf.slice(0, 4); // length: 4
+ written = sub.write('12345', 'binary');
+ assert.equal(written, 4);
+ assert.equal(buf[4], 0);
+}
// Check for fractional length args, junk length args, etc.
// https://github.com/joyent/node/issues/1758
assert.equal(Buffer(1).write('aaaaaa', 0, 1, enc), 1);
});
-// Regression test, guard against buffer overrun in the base64 decoder.
-var a = Buffer(3);
-var b = Buffer('xxx');
-a.write('aaaaaaaa', 'base64');
-assert.equal(b.toString(), 'xxx');
+{
+ // Regression test, guard against buffer overrun in the base64 decoder.
+ var a = Buffer(3);
+ var b = Buffer('xxx');
+ a.write('aaaaaaaa', 'base64');
+ assert.equal(b.toString(), 'xxx');
+}
// issue GH-3416
Buffer(Buffer(0), 0, 0);
// GH-5110
(function() {
- var buffer = new Buffer('test'),
- string = JSON.stringify(buffer);
+ var buffer = new Buffer('test');
+ var string = JSON.stringify(buffer);
assert.equal(string, '{"type":"Buffer","data":[116,101,115,116]}');
// issue GH-4331
assert.throws(function() {
- new Buffer(0xFFFFFFFF);
+ Buffer(0xFFFFFFFF);
}, RangeError);
assert.throws(function() {
- new Buffer(0xFFFFFFFFF);
+ Buffer(0xFFFFFFFFF);
+}, RangeError);
+
+// issue GH-5587
+assert.throws(function() {
+ var buf = new Buffer(8);
+ buf.writeFloatLE(0, 5);
+}, RangeError);
+assert.throws(function() {
+ var buf = new Buffer(16);
+ buf.writeDoubleLE(0, 9);
}, RangeError);
// attempt to overflow buffers, similar to previous bug in array buffers
assert.throws(function() {
- var buf = new Buffer(8);
+ var buf = Buffer(8);
buf.readFloatLE(0xffffffff);
}, RangeError);
assert.throws(function() {
- var buf = new Buffer(8);
+ var buf = Buffer(8);
buf.writeFloatLE(0.0, 0xffffffff);
}, RangeError);
assert.throws(function() {
- var buf = new Buffer(8);
+ var buf = Buffer(8);
buf.readFloatLE(0xffffffff);
}, RangeError);
assert.throws(function() {
- var buf = new Buffer(8);
+ var buf = Buffer(8);
buf.writeFloatLE(0.0, 0xffffffff);
}, RangeError);
// ensure negative values can't get past offset
assert.throws(function() {
- var buf = new Buffer(8);
+ var buf = Buffer(8);
buf.readFloatLE(-1);
}, RangeError);
assert.throws(function() {
- var buf = new Buffer(8);
+ var buf = Buffer(8);
buf.writeFloatLE(0.0, -1);
}, RangeError);
assert.throws(function() {
- var buf = new Buffer(8);
+ var buf = Buffer(8);
buf.readFloatLE(-1);
}, RangeError);
assert.throws(function() {
- var buf = new Buffer(8);
+ var buf = Buffer(8);
buf.writeFloatLE(0.0, -1);
}, RangeError);
// offset checks
-var buf = new Buffer(0);
+{
+ var buf = new Buffer(0);
-assert.throws(function() { buf.readUInt8(0); }, RangeError);
-assert.throws(function() { buf.readInt8(0); }, RangeError);
+ assert.throws(function() { buf.readUInt8(0); }, RangeError);
+ assert.throws(function() { buf.readInt8(0); }, RangeError);
+}
-var buf = new Buffer([0xFF]);
+{
+ var buf = new Buffer([0xFF]);
-assert.equal(buf.readUInt8(0), 255);
-assert.equal(buf.readInt8(0), -1);
+ assert.equal(buf.readUInt8(0), 255);
+ assert.equal(buf.readInt8(0), -1);
+}
[16, 32].forEach(function(bits) {
var buf = new Buffer(bits / 8 - 1);
// test for common write(U)IntLE/BE
(function() {
- var buf = new Buffer(3);
+ var buf = Buffer(3);
buf.writeUIntLE(0x123456, 0, 3);
assert.deepEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
assert.equal(buf.readUIntLE(0, 3), 0x123456);
- buf = new Buffer(3);
+ buf = Buffer(3);
buf.writeUIntBE(0x123456, 0, 3);
assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);
assert.equal(buf.readUIntBE(0, 3), 0x123456);
- buf = new Buffer(3);
+ buf = Buffer(3);
buf.writeIntLE(0x123456, 0, 3);
assert.deepEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
assert.equal(buf.readIntLE(0, 3), 0x123456);
- buf = new Buffer(3);
+ buf = Buffer(3);
buf.writeIntBE(0x123456, 0, 3);
assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);
assert.equal(buf.readIntBE(0, 3), 0x123456);
- buf = new Buffer(3);
+ buf = Buffer(3);
buf.writeIntLE(-0x123456, 0, 3);
assert.deepEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]);
assert.equal(buf.readIntLE(0, 3), -0x123456);
- buf = new Buffer(3);
+ buf = Buffer(3);
buf.writeIntBE(-0x123456, 0, 3);
assert.deepEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]);
assert.equal(buf.readIntBE(0, 3), -0x123456);
- buf = new Buffer(5);
+ buf = Buffer(3);
+ buf.writeIntLE(-0x123400, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]);
+ assert.equal(buf.readIntLE(0, 3), -0x123400);
+
+ buf = Buffer(3);
+ buf.writeIntBE(-0x123400, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]);
+ assert.equal(buf.readIntBE(0, 3), -0x123400);
+
+ buf = Buffer(3);
+ buf.writeIntLE(-0x120000, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0x00, 0x00, 0xee]);
+ assert.equal(buf.readIntLE(0, 3), -0x120000);
+
+ buf = Buffer(3);
+ buf.writeIntBE(-0x120000, 0, 3);
+ assert.deepEqual(buf.toJSON().data, [0xee, 0x00, 0x00]);
+ assert.equal(buf.readIntBE(0, 3), -0x120000);
+
+ buf = Buffer(5);
buf.writeUIntLE(0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);
assert.equal(buf.readUIntLE(0, 5), 0x1234567890);
- buf = new Buffer(5);
+ buf = Buffer(5);
buf.writeUIntBE(0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);
assert.equal(buf.readUIntBE(0, 5), 0x1234567890);
- buf = new Buffer(5);
+ buf = Buffer(5);
buf.writeIntLE(0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);
assert.equal(buf.readIntLE(0, 5), 0x1234567890);
- buf = new Buffer(5);
+ buf = Buffer(5);
buf.writeIntBE(0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);
assert.equal(buf.readIntBE(0, 5), 0x1234567890);
- buf = new Buffer(5);
+ buf = Buffer(5);
buf.writeIntLE(-0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]);
assert.equal(buf.readIntLE(0, 5), -0x1234567890);
- buf = new Buffer(5);
+ buf = Buffer(5);
buf.writeIntBE(-0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]);
assert.equal(buf.readIntBE(0, 5), -0x1234567890);
+
+ buf = Buffer(5);
+ buf.writeIntLE(-0x0012000000, 0, 5);
+ assert.deepEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]);
+ assert.equal(buf.readIntLE(0, 5), -0x0012000000);
+
+ buf = Buffer(5);
+ buf.writeIntBE(-0x0012000000, 0, 5);
+ assert.deepEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]);
+ assert.equal(buf.readIntBE(0, 5), -0x0012000000);
})();
// test Buffer slice
assert.equal(buf.slice(-10, 10), '0123456789');
assert.equal(buf.slice(-20, 10), '0123456789');
assert.equal(buf.slice(-20, -10), '');
+ assert.equal(buf.slice(), '0123456789');
+ assert.equal(buf.slice(0), '0123456789');
+ assert.equal(buf.slice(0, 0), '');
+ assert.equal(buf.slice(undefined), '0123456789');
+ assert.equal(buf.slice('foobar'), '0123456789');
+ assert.equal(buf.slice(undefined, undefined), '0123456789');
+
+ assert.equal(buf.slice(2), '23456789');
+ assert.equal(buf.slice(5), '56789');
+ assert.equal(buf.slice(10), '');
+ assert.equal(buf.slice(5, 8), '567');
+ assert.equal(buf.slice(8, -1), '8');
+ assert.equal(buf.slice(-10), '0123456789');
+ assert.equal(buf.slice(0, -9), '0');
+ assert.equal(buf.slice(0, -10), '');
assert.equal(buf.slice(0, -1), '012345678');
assert.equal(buf.slice(2, -2), '234567');
assert.equal(buf.slice(0, 65536), '0123456789');
assert.equal(buf.slice(65536, 0), '');
+ assert.equal(buf.slice(-5, -8), '');
+ assert.equal(buf.slice(-5, -3), '56');
+ assert.equal(buf.slice(-10, 10), '0123456789');
for (var i = 0, s = buf.toString(); i < buf.length; ++i) {
+ assert.equal(buf.slice(i), s.slice(i));
+ assert.equal(buf.slice(0, i), s.slice(0, i));
assert.equal(buf.slice(-i), s.slice(-i));
assert.equal(buf.slice(0, -i), s.slice(0, -i));
}
+
+ var utf16Buf = new Buffer('0123456789', 'utf16le');
+ // TODO assert.deepEqual(utf16Buf.slice(0, 6), Buffer('012', 'utf16le'));
+
+ assert.equal(buf.slice('0', '1'), '0');
+ assert.equal(buf.slice('-5', '10'), '56789');
+ assert.equal(buf.slice('-10', '10'), '0123456789');
+ assert.equal(buf.slice('-10', '-5'), '01234');
+ assert.equal(buf.slice('-10', '-0'), '');
+ assert.equal(buf.slice('111'), '');
+ assert.equal(buf.slice('0', '-111'), '');
+
// try to slice a zero length Buffer
// see https://github.com/joyent/node/issues/5881
SlowBuffer(0).slice(0, 1);
}, RangeError);
assert.throws(function() {
- new SlowBuffer((-1 >>> 0) + 1);
+ SlowBuffer((-1 >>> 0) + 1);
}, RangeError);
if (common.hasCrypto) {
}
// Test Compare
-var b = new Buffer(1).fill('a');
-var c = new Buffer(1).fill('c');
-var d = new Buffer(2).fill('aa');
-
-assert.equal(b.compare(c), -1);
-assert.equal(c.compare(d), 1);
-assert.equal(d.compare(b), 1);
-assert.equal(b.compare(d), -1);
-assert.equal(b.compare(b), 0);
-
-assert.equal(Buffer.compare(b, c), -1);
-assert.equal(Buffer.compare(c, d), 1);
-assert.equal(Buffer.compare(d, b), 1);
-assert.equal(Buffer.compare(b, d), -1);
-assert.equal(Buffer.compare(c, c), 0);
-
+{
+ var b = new Buffer(1).fill('a');
+ var c = new Buffer(1).fill('c');
+ var d = new Buffer(2).fill('aa');
+
+ assert.equal(b.compare(c), -1);
+ assert.equal(c.compare(d), 1);
+ assert.equal(d.compare(b), 1);
+ assert.equal(b.compare(d), -1);
+ assert.equal(b.compare(b), 0);
+
+ assert.equal(Buffer.compare(b, c), -1);
+ assert.equal(Buffer.compare(c, d), 1);
+ assert.equal(Buffer.compare(d, b), 1);
+ assert.equal(Buffer.compare(b, d), -1);
+ assert.equal(Buffer.compare(c, c), 0);
+
+ assert.equal(Buffer.compare(Buffer(0), Buffer(0)), 0);
+ assert.equal(Buffer.compare(Buffer(0), Buffer(1)), -1);
+ assert.equal(Buffer.compare(Buffer(1), Buffer(0)), 1);
+}
assert.throws(function() {
- var b = new Buffer(1);
+ var b = Buffer(1);
Buffer.compare(b, 'abc');
});
assert.throws(function() {
- var b = new Buffer(1);
+ var b = Buffer(1);
Buffer.compare('abc', b);
});
assert.throws(function() {
- var b = new Buffer(1);
+ var b = Buffer(1);
b.compare('abc');
});
// Test Equals
-var b = new Buffer(5).fill('abcdf');
-var c = new Buffer(5).fill('abcdf');
-var d = new Buffer(5).fill('abcde');
-var e = new Buffer(6).fill('abcdef');
-
-assert.ok(b.equals(c));
-assert.ok(!c.equals(d));
-assert.ok(!d.equals(e));
-assert.ok(d.equals(d));
+{
+ var b = new Buffer(5).fill('abcdf');
+ var c = new Buffer(5).fill('abcdf');
+ var d = new Buffer(5).fill('abcde');
+ var e = new Buffer(6).fill('abcdef');
+
+ assert.ok(b.equals(c));
+ assert.ok(!c.equals(d));
+ assert.ok(!d.equals(e));
+ assert.ok(d.equals(d));
+}
assert.throws(function() {
- var b = new Buffer(1);
+ var b = Buffer(1);
b.equals('abc');
});
-// Regression test for https://github.com/nodejs/io.js/issues/649.
+// Regression test for https://github.com/nodejs/node/issues/649.
assert.throws(function() { Buffer(1422561062959).toString('utf8'); });
var ps = Buffer.poolSize;
Buffer(10).copy();
});
+var regErrorMsg = new RegExp('First argument must be a string, Buffer, ' +
+ 'ArrayBuffer, Array, or array-like object.');
+
assert.throws(function() {
new Buffer();
-}, /Must start with number, buffer, array or string/);
+}, regErrorMsg);
assert.throws(function() {
new Buffer(null);
-}, /Must start with number, buffer, array or string/);
+}, regErrorMsg);
+
+
+// Test prototype getters don't throw
+assert.equal(Buffer.prototype.parent, undefined);
+assert.equal(Buffer.prototype.offset, undefined);
+assert.equal(SlowBuffer.prototype.parent, undefined);
+assert.equal(SlowBuffer.prototype.offset, undefined);