line = line.replace(/(const|let) /g, 'var ')
// make `var common = require('common')` work
- line = line.replace(/(var common = require.*)/g, 'var common = {};')
+ line = line.replace(/(var common = require.*)/g, 'var common = { skip: function () {} };')
// make `require('../common')` work
line = line.replace(/require\('\.\.\/common'\);/g, '')
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
-var common = {};
+
+var common = { skip: function () {} };
var assert = require('assert');
var Buffer = require('../../').Buffer;
// try to create 0-length buffers
Buffer.from('');
Buffer.from('', 'ascii');
-Buffer.from('', 'binary');
+Buffer.from('', 'latin1');
Buffer.alloc(0);
Buffer.allocUnsafe(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!');
+ assert.equal(b.toString('latin1', 0, pos), 'Madness?! This is node.js!');
}
// Regression test for https://github.com/nodejs/node/issues/3496.
// 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');
+ b.write(s, 0, 'latin1');
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');
+ b.write(s, 0, 'latin1');
assert.equal(0xee, b[0]);
assert.equal(0xad, b[1]);
assert.equal(0xbe, b[2]);
// 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');
+ written = sub.write('12345', 'latin1');
assert.equal(written, 4);
assert.equal(buf[4], 0);
}
assert.equal(Buffer.from('13.37').length, 5);
// Ensure that the length argument is respected.
-'ascii utf8 hex base64 binary'.split(' ').forEach(function(enc) {
+'ascii utf8 hex base64 latin1'.split(' ').forEach(function(enc) {
assert.equal(Buffer.allocUnsafe(1).write('aaaaaa', 0, 1, enc), 1);
});
'utf8',
'utf-8',
'ascii',
+ 'latin1',
'binary',
'base64',
'ucs2',
[ 'utf9',
'utf-7',
'Unicode-FTW',
- 'new gnu gun' ].forEach(function(enc) {
+ 'new gnu gun',
+ false,
+ NaN,
+ {},
+ Infinity,
+ [],
+ 1,
+ 0,
+ -1 ].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.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}');
assert.deepStrictEqual(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() {
});
// 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);
-})();
+ assert.strictEqual(buf.readUIntLE(0, 1), 0x01);
+ assert.strictEqual(buf.readUIntBE(0, 1), 0x01);
+ assert.strictEqual(buf.readUIntLE(0, 3), 0x030201);
+ assert.strictEqual(buf.readUIntBE(0, 3), 0x010203);
+ assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201);
+ assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405);
+ assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201);
+ assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506);
+ assert.strictEqual(buf.readIntLE(0, 1), 0x01);
+ assert.strictEqual(buf.readIntBE(0, 1), 0x01);
+ assert.strictEqual(buf.readIntLE(0, 3), 0x030201);
+ assert.strictEqual(buf.readIntBE(0, 3), 0x010203);
+ assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201);
+ assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405);
+ assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
+ assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
+}
// test for common write(U)IntLE/BE
-(function() {
+{
var buf = Buffer.allocUnsafe(3);
buf.writeUIntLE(0x123456, 0, 3);
assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
buf.writeIntBE(-0x0012000000, 0, 5);
assert.deepStrictEqual(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');
// 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() {
// 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);
+ assert.strictEqual(b.length, a.length);
+ assert.strictEqual(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.strictEqual(a[i], i);
+ assert.strictEqual(b[i], i);
+ assert.strictEqual(c[i], i);
}
-})();
+}
assert.throws(function() {
crypto.createHash('sha1').update(b2).digest('hex')
);
} else {
-// console.log('1..0 # Skipped: missing crypto');
+ common.skip('missing crypto');
}
// Test Compare
Buffer.from(new ArrayBuffer(0), -1 >>> 0);
}, /RangeError: 'offset' is out of bounds/);
+// ParseArrayIndex() should reject values that don't fit in a 32 bits size_t.
+assert.throws(() => {
+ var a = Buffer(1).fill(0);
+ var b = Buffer(1).fill(0);
+ a.copy(b, 0, 0x100000000, 0x100000001);
+}), /out of range index/;
+
// Unpooled buffer (replaces SlowBuffer)
var ubuf = Buffer.allocUnsafeSlow(10);
assert(ubuf);
assert(ubuf.buffer);
assert.equal(ubuf.buffer.byteLength, 10);
+// Regression test
+assert.doesNotThrow(() => {
+ Buffer.from(new ArrayBuffer());
+});
+
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
--- /dev/null
+'use strict';
+var Buffer = require('../../').Buffer;
+
+
+var assert = require('assert');
+var Buffer = require('../../').Buffer;
+
+// Test hex strings and bad hex strings
+{
+ var buf1 = Buffer.alloc(4);
+ assert.strictEqual(buf1.length, 4);
+ assert.deepStrictEqual(buf1, new Buffer([0, 0, 0, 0]));
+ assert.strictEqual(buf1.write('abcdxx', 0, 'hex'), 2);
+ assert.deepStrictEqual(buf1, new Buffer([0xab, 0xcd, 0x00, 0x00]));
+ assert.strictEqual(buf1.toString('hex'), 'abcd0000');
+ assert.strictEqual(buf1.write('abcdef01', 0, 'hex'), 4);
+ assert.deepStrictEqual(buf1, new Buffer([0xab, 0xcd, 0xef, 0x01]));
+ assert.strictEqual(buf1.toString('hex'), 'abcdef01');
+
+ var buf2 = Buffer.from(buf1.toString('hex'), 'hex');
+ assert.strictEqual(buf1.toString('hex'), buf2.toString('hex'));
+
+ var buf3 = Buffer.alloc(5);
+ assert.strictEqual(buf3.write('abcdxx', 1, 'hex'), 2);
+ assert.strictEqual(buf3.toString('hex'), '00abcd0000');
+
+ var buf4 = Buffer.alloc(4);
+ assert.deepStrictEqual(buf4, new Buffer([0, 0, 0, 0]));
+ assert.strictEqual(buf4.write('xxabcd', 0, 'hex'), 0);
+ assert.deepStrictEqual(buf4, new Buffer([0, 0, 0, 0]));
+ assert.strictEqual(buf4.write('xxab', 1, 'hex'), 0);
+ assert.deepStrictEqual(buf4, new Buffer([0, 0, 0, 0]));
+ assert.strictEqual(buf4.write('cdxxab', 0, 'hex'), 1);
+ assert.deepStrictEqual(buf4, new Buffer([0xcd, 0, 0, 0]));
+
+ var buf5 = Buffer.alloc(256);
+ for (var i = 0; i < 256; i++)
+ buf5[i] = i;
+
+ var hex = buf5.toString('hex');
+ assert.deepStrictEqual(Buffer.from(hex, 'hex'), buf5);
+
+ var badHex = hex.slice(0, 256) + 'xx' + hex.slice(256, 510);
+ assert.deepStrictEqual(Buffer.from(badHex, 'hex'), buf5.slice(0, 128));
+}
+
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
var SlowBuffer = require('../../').SlowBuffer;
// coerce values to string
-assert.equal(Buffer.byteLength(32, 'binary'), 2);
+assert.equal(Buffer.byteLength(32, 'latin1'), 2);
assert.equal(Buffer.byteLength(NaN, 'utf8'), 3);
-assert.equal(Buffer.byteLength({}, 'binary'), 15);
+assert.equal(Buffer.byteLength({}, 'latin1'), 15);
assert.equal(Buffer.byteLength(), 9);
var buff = new Buffer(10);
assert.equal(Buffer.byteLength('Il était tué'), 14);
assert.equal(Buffer.byteLength('Il était tué', 'utf8'), 14);
assert.equal(Buffer.byteLength('Il était tué', 'ascii'), 12);
+assert.equal(Buffer.byteLength('Il était tué', 'latin1'), 12);
assert.equal(Buffer.byteLength('Il était tué', 'binary'), 12);
['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
assert.equal(24, Buffer.byteLength('Il était tué', encoding));
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
var zero = [];
-var one = [ Buffer.from('asdf') ];
+var one = [ Buffer.from('asdf') ];
var long = [];
for (var i = 0; i < 10; i++) long.push(Buffer.from('asdf'));
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
testBufs('a\u0234b\u0235c\u0236', 12, 1, 'binary');
+// LATIN1
+testBufs('abc', 'latin1');
+testBufs('\u0222aa', 'latin1');
+testBufs('a\u0234b\u0235c\u0236', 'latin1');
+testBufs('abc', 4, 'latin1');
+testBufs('abc', 5, 'latin1');
+testBufs('abc', SIZE, 'latin1');
+testBufs('\u0222aa', 2, 'latin1');
+testBufs('\u0222aa', 8, 'latin1');
+testBufs('a\u0234b\u0235c\u0236', 4, 'latin1');
+testBufs('a\u0234b\u0235c\u0236', 12, 'latin1');
+testBufs('abc', 4, -1, 'latin1');
+testBufs('abc', 4, 1, 'latin1');
+testBufs('abc', 5, 1, 'latin1');
+testBufs('\u0222aa', 2, -1, 'latin1');
+testBufs('\u0222aa', 8, 1, 'latin1');
+testBufs('a\u0234b\u0235c\u0236', 4, -1, 'latin1');
+testBufs('a\u0234b\u0235c\u0236', 4, 1, 'latin1');
+testBufs('a\u0234b\u0235c\u0236', 12, 1, 'latin1');
+
+
// UCS2
testBufs('abc', 'ucs2');
testBufs('\u0222aa', 'ucs2');
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
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'));
+assert.strictEqual(
+ Buffer.from(b.toString('hex'), 'hex')
+ .includes('64', 0, 'hex'),
+ true
+);
+assert.strictEqual(
+ Buffer.from(b.toString('hex'), 'hex')
+ .includes(Buffer.from('64', 'hex'), 0, 'hex'),
+ true
+);
// 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'));
+assert.strictEqual(
+ Buffer.from(b.toString('base64'), 'base64')
+ .includes('ZA==', 0, 'base64'),
+ true
+);
+assert.strictEqual(
+ Buffer.from(b.toString('base64'), 'base64')
+ .includes(Buffer.from('ZA==', 'base64'), 0, 'base64'),
+ true
+);
// 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'));
+assert.strictEqual(
+ Buffer.from(b.toString('ascii'), 'ascii')
+ .includes('d', 0, 'ascii'),
+ true
+);
+assert.strictEqual(
+ Buffer.from(b.toString('ascii'), 'ascii')
+ .includes(Buffer.from('d', 'ascii'), 0, 'ascii'),
+ true
+);
+
+// test latin1 encoding
+assert.strictEqual(
+ Buffer.from(b.toString('latin1'), 'latin1')
+ .includes('d', 0, 'latin1'),
+ true
+);
+assert.strictEqual(
+ Buffer.from(b.toString('latin1'), 'latin1')
+ .includes(Buffer.from('d', 'latin1'), 0, 'latin1'),
+ true
+);
// 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'));
+assert.strictEqual(
+ Buffer.from(b.toString('binary'), 'binary')
+ .includes('d', 0, 'binary'),
+ true
+);
+assert.strictEqual(
+ Buffer.from(b.toString('binary'), 'binary')
+ .includes(Buffer.from('d', 'binary'), 0, 'binary'),
+ true
+);
// test usc2 encoding
// 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;
+ longString = longString + String.fromCharCode(i) + longString;
}
var longBufferString = Buffer.from(longString);
b.includes([]);
});
+// test truncation of Number arguments to uint8
+{
+ var buf = Buffer.from('this is a test');
+ assert.ok(buf.includes(0x6973));
+ assert.ok(buf.includes(0x697320));
+ assert.ok(buf.includes(0x69732069));
+ assert.ok(buf.includes(0x697374657374));
+ assert.ok(buf.includes(0x69737374));
+ assert.ok(buf.includes(0x69737465));
+ assert.ok(buf.includes(0x69737465));
+ assert.ok(buf.includes(-140));
+ assert.ok(buf.includes(-152));
+ assert.ok(!buf.includes(0xff));
+ assert.ok(!buf.includes(0xffff));
+}
+
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
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);
+assert.strictEqual(
+ Buffer.from(b.toString('hex'), 'hex')
+ .indexOf('64', 0, 'hex'),
+ 3
+);
+assert.strictEqual(
+ 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);
+assert.strictEqual(
+ Buffer.from(b.toString('base64'), 'base64')
+ .indexOf('ZA==', 0, 'base64'),
+ 3
+);
+assert.strictEqual(
+ 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);
+assert.strictEqual(
+ Buffer.from(b.toString('ascii'), 'ascii')
+ .indexOf('d', 0, 'ascii'),
+ 3
+);
+assert.strictEqual(
+ Buffer.from(b.toString('ascii'), 'ascii')
+ .indexOf(Buffer.from('d', 'ascii'), 0, 'ascii'),
+ 3
+);
+
+// test latin1 encoding
+assert.strictEqual(
+ Buffer.from(b.toString('latin1'), 'latin1')
+ .indexOf('d', 0, 'latin1'),
+ 3
+);
+assert.strictEqual(
+ Buffer.from(b.toString('latin1'), 'latin1')
+ .indexOf(Buffer.from('d', 'latin1'), 0, 'latin1'),
+ 3
+);
+assert.strictEqual(
+ Buffer.from('aa\u00e8aa', 'latin1')
+ .indexOf('\u00e8', 'latin1'),
+ 2
+);
+assert.strictEqual(
+ Buffer.from('\u00e8', 'latin1')
+ .indexOf('\u00e8', 'latin1'),
+ 0
+);
+assert.strictEqual(
+ Buffer.from('\u00e8', 'latin1')
+ .indexOf(Buffer.from('\u00e8', 'latin1'), 'latin1'),
+ 0
+);
// 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);
+assert.strictEqual(
+ Buffer.from(b.toString('binary'), 'binary')
+ .indexOf('d', 0, 'binary'),
+ 3
+);
+assert.strictEqual(
+ Buffer.from(b.toString('binary'), 'binary')
+ .indexOf(Buffer.from('d', 'binary'), 0, 'binary'),
+ 3
+);
+assert.strictEqual(
+ Buffer.from('aa\u00e8aa', 'binary')
+ .indexOf('\u00e8', 'binary'),
+ 2
+);
+assert.strictEqual(
+ Buffer.from('\u00e8', 'binary')
+ .indexOf('\u00e8', 'binary'),
+ 0
+);
+assert.strictEqual(
+ Buffer.from('\u00e8', 'binary')
+ .indexOf(Buffer.from('\u00e8', 'binary'), 'binary'),
+ 0
+);
// test optional offset with passed encoding
// 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;
+ longString = longString + String.fromCharCode(i) + longString;
}
var longBufferString = Buffer.from(longString);
assert.equal(-1, allCharsBufferUtf8.indexOf('notfound'));
assert.equal(-1, allCharsBufferUcs2.indexOf('notfound'));
+// Needle is longer than haystack, but only because it's encoded as UTF-16
+assert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'ucs2'), -1);
+
+assert.strictEqual(Buffer.from('aaaa').indexOf('a'.repeat(4), 'utf8'), 0);
+assert.strictEqual(Buffer.from('aaaa').indexOf('你好', 'ucs2'), -1);
+
+// Haystack has odd length, but the needle is UCS2.
+// assert.strictEqual(Buffer.from('aaaaa').indexOf('b', 'ucs2'), -1);
+
{
// Find substrings in Utf8.
var lengths = [1, 3, 15]; // Single char, simple and complex.
b.indexOf([]);
});
+// All code for handling encodings is shared between Buffer.indexOf and
+// Buffer.lastIndexOf, so only testing the separate lastIndexOf semantics.
+
+// Test lastIndexOf basic functionality; Buffer b contains 'abcdef'.
+// lastIndexOf string:
+// assert.equal(b.lastIndexOf('a'), 0);
+// assert.equal(b.lastIndexOf('a', 1), 0);
+// assert.equal(b.lastIndexOf('b', 1), 1);
+// assert.equal(b.lastIndexOf('c', 1), -1);
+// assert.equal(b.lastIndexOf('a', -1), 0);
+// assert.equal(b.lastIndexOf('a', -4), 0);
+// assert.equal(b.lastIndexOf('a', -b.length), 0);
+// assert.equal(b.lastIndexOf('a', -b.length - 1), -1);
+// assert.equal(b.lastIndexOf('a', NaN), 0);
+// assert.equal(b.lastIndexOf('a', -Infinity), -1);
+// assert.equal(b.lastIndexOf('a', Infinity), 0);
+// lastIndexOf Buffer:
+// assert.equal(b.lastIndexOf(buf_a), 0);
+// assert.equal(b.lastIndexOf(buf_a, 1), 0);
+// assert.equal(b.lastIndexOf(buf_a, -1), 0);
+// assert.equal(b.lastIndexOf(buf_a, -4), 0);
+// assert.equal(b.lastIndexOf(buf_a, -b.length), 0);
+// assert.equal(b.lastIndexOf(buf_a, -b.length - 1), -1);
+// assert.equal(b.lastIndexOf(buf_a, NaN), 0);
+// assert.equal(b.lastIndexOf(buf_a, -Infinity), -1);
+// assert.equal(b.lastIndexOf(buf_a, Infinity), 0);
+// assert.equal(b.lastIndexOf(buf_bc), 1);
+// assert.equal(b.lastIndexOf(buf_bc, 2), 1);
+// assert.equal(b.lastIndexOf(buf_bc, -1), 1);
+// assert.equal(b.lastIndexOf(buf_bc, -3), 1);
+// assert.equal(b.lastIndexOf(buf_bc, -5), 1);
+// assert.equal(b.lastIndexOf(buf_bc, -6), -1);
+// assert.equal(b.lastIndexOf(buf_bc, NaN), 1);
+// assert.equal(b.lastIndexOf(buf_bc, -Infinity), -1);
+// assert.equal(b.lastIndexOf(buf_bc, Infinity), 1);
+// assert.equal(b.lastIndexOf(buf_f), b.length - 1);
+// assert.equal(b.lastIndexOf(buf_z), -1);
+// assert.equal(b.lastIndexOf(buf_empty), -1);
+// assert.equal(b.lastIndexOf(buf_empty, 1), -1);
+// assert.equal(b.lastIndexOf(buf_empty, b.length + 1), -1);
+// assert.equal(b.lastIndexOf(buf_empty, Infinity), -1);
+// lastIndexOf number:
+// assert.equal(b.lastIndexOf(0x61), 0);
+// assert.equal(b.lastIndexOf(0x61, 1), 0);
+// assert.equal(b.lastIndexOf(0x61, -1), 0);
+// assert.equal(b.lastIndexOf(0x61, -4), 0);
+// assert.equal(b.lastIndexOf(0x61, -b.length), 0);
+// assert.equal(b.lastIndexOf(0x61, -b.length - 1), -1);
+// assert.equal(b.lastIndexOf(0x61, NaN), 0);
+// assert.equal(b.lastIndexOf(0x61, -Infinity), -1);
+// assert.equal(b.lastIndexOf(0x61, Infinity), 0);
+// assert.equal(b.lastIndexOf(0x0), -1);
+
+// Test weird offset arguments.
+// Behaviour should match String.lastIndexOf:
+// assert.equal(b.lastIndexOf('b', 0), -1);
+// assert.equal(b.lastIndexOf('b', undefined), 1);
+// assert.equal(b.lastIndexOf('b', null), -1);
+// assert.equal(b.lastIndexOf('b', {}), 1);
+// assert.equal(b.lastIndexOf('b', []), -1);
+// assert.equal(b.lastIndexOf('b', [2]), 1);
+
+// Test needles longer than the haystack.
+// assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'ucs2'), -1);
+// assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'utf8'), -1);
+// assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'latin1'), -1);
+// assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 'binary'), -1);
+// assert.strictEqual(b.lastIndexOf(Buffer.from('aaaaaaaaaaaaaaa')), -1);
+// assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 2, 'ucs2'), -1);
+// assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 3, 'utf8'), -1);
+// assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 5, 'latin1'), -1);
+// assert.strictEqual(b.lastIndexOf('aaaaaaaaaaaaaaa', 5, 'binary'), -1);
+// assert.strictEqual(b.lastIndexOf(Buffer.from('aaaaaaaaaaaaaaa'), 7), -1);
+
+// 你好 expands to a total of 6 bytes using UTF-8 and 4 bytes using UTF-16
+// assert.strictEqual(buf_bc.lastIndexOf('你好', 'ucs2'), -1);
+// assert.strictEqual(buf_bc.lastIndexOf('你好', 'utf8'), -1);
+// assert.strictEqual(buf_bc.lastIndexOf('你好', 'latin1'), -1);
+// assert.strictEqual(buf_bc.lastIndexOf('你好', 'binary'), -1);
+// assert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好')), -1);
+// assert.strictEqual(buf_bc.lastIndexOf('你好', 2, 'ucs2'), -1);
+// assert.strictEqual(buf_bc.lastIndexOf('你好', 3, 'utf8'), -1);
+// assert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'latin1'), -1);
+// assert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'binary'), -1);
+// assert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好'), 7), -1);
+
+// Test lastIndexOf on a longer buffer:
+// var bufferString = new Buffer('a man a plan a canal panama');
+// assert.equal(15, bufferString.lastIndexOf('canal'));
+// assert.equal(21, bufferString.lastIndexOf('panama'));
+// assert.equal(0, bufferString.lastIndexOf('a man a plan a canal panama'));
+// assert.equal(-1, bufferString.lastIndexOf('a man a plan a canal mexico'));
+// assert.equal(-1, bufferString.lastIndexOf('a man a plan a canal mexico city'));
+// assert.equal(-1, bufferString.lastIndexOf(Buffer.from('a'.repeat(1000))));
+// assert.equal(0, bufferString.lastIndexOf('a man a plan', 4));
+// assert.equal(13, bufferString.lastIndexOf('a '));
+// assert.equal(13, bufferString.lastIndexOf('a ', 13));
+// assert.equal(6, bufferString.lastIndexOf('a ', 12));
+// assert.equal(0, bufferString.lastIndexOf('a ', 5));
+// assert.equal(13, bufferString.lastIndexOf('a ', -1));
+// assert.equal(0, bufferString.lastIndexOf('a ', -27));
+// assert.equal(-1, bufferString.lastIndexOf('a ', -28));
+
+// Test lastIndexOf for the case that the first character can be found,
+// but in a part of the buffer that does not make search to search
+// due do length constraints.
+// var abInUCS2 = Buffer.from('ab', 'ucs2');
+// assert.strictEqual(-1, Buffer.from('µaaaa¶bbbb', 'latin1').lastIndexOf('µ'));
+// assert.strictEqual(-1, Buffer.from('µaaaa¶bbbb', 'binary').lastIndexOf('µ'));
+// assert.strictEqual(-1, Buffer.from('bc').lastIndexOf('ab'));
+// assert.strictEqual(-1, Buffer.from('abc').lastIndexOf('qa'));
+// assert.strictEqual(-1, Buffer.from('abcdef').lastIndexOf('qabc'));
+// assert.strictEqual(-1, Buffer.from('bc').lastIndexOf(Buffer.from('ab')));
+// assert.strictEqual(-1, Buffer.from('bc', 'ucs2').lastIndexOf('ab', 'ucs2'));
+// assert.strictEqual(-1, Buffer.from('bc', 'ucs2').lastIndexOf(abInUCS2));
+
+// assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab'));
+// assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 1));
+// assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 2));
+// assert.strictEqual(0, Buffer.from('abc').lastIndexOf('ab', 3));
+
+// The above tests test the LINEAR and SINGLE-CHAR strategies.
+// Now, we test the BOYER-MOORE-HORSPOOL strategy.
+// Test lastIndexOf on a long buffer w multiple matches:
+// pattern = 'JABACABADABACABA';
+// assert.equal(1535, longBufferString.lastIndexOf(pattern));
+// assert.equal(1535, longBufferString.lastIndexOf(pattern, 1535));
+// assert.equal(511, longBufferString.lastIndexOf(pattern, 1534));
+
+// Finally, give it a really long input to trigger fallback from BMH to
+// regular BOYER-MOORE (which has better worst-case complexity).
+
+// Generate a really long Thue-Morse sequence of 'yolo' and 'swag',
+// "yolo swag swag yolo swag yolo yolo swag" ..., goes on for about 5MB.
+// This is hard to search because it all looks similar, but never repeats.
+
+// countBits returns the number of bits in the binary reprsentation of n.
+function countBits(n) {
+ for (var count = 0; n > 0; count++) {
+ n = n & (n - 1); // remove top bit
+ }
+ return count;
+}
+var parts = [];
+for (var i = 0; i < 1000000; i++) {
+ parts.push((countBits(i) % 2 === 0) ? 'yolo' : 'swag');
+}
+var reallyLong = new Buffer(parts.join(' '));
+assert.equal('yolo swag swag yolo', reallyLong.slice(0, 19).toString());
+
+// Expensive reverse searches. Stress test lastIndexOf:
+// pattern = reallyLong.slice(0, 100000); // First 1/50th of the pattern.
+// assert.equal(4751360, reallyLong.lastIndexOf(pattern));
+// assert.equal(3932160, reallyLong.lastIndexOf(pattern, 4000000));
+// assert.equal(2949120, reallyLong.lastIndexOf(pattern, 3000000));
+// pattern = reallyLong.slice(100000, 200000); // Second 1/50th.
+// assert.equal(4728480, reallyLong.lastIndexOf(pattern));
+// pattern = reallyLong.slice(0, 1000000); // First 1/5th.
+// assert.equal(3932160, reallyLong.lastIndexOf(pattern));
+// pattern = reallyLong.slice(0, 2000000); // first 2/5ths.
+// assert.equal(0, reallyLong.lastIndexOf(pattern));
+
+// test truncation of Number arguments to uint8
+{
+ var buf = Buffer.from('this is a test');
+ assert.strictEqual(buf.indexOf(0x6973), 3);
+ assert.strictEqual(buf.indexOf(0x697320), 4);
+ assert.strictEqual(buf.indexOf(0x69732069), 2);
+ assert.strictEqual(buf.indexOf(0x697374657374), 0);
+ assert.strictEqual(buf.indexOf(0x69737374), 0);
+ assert.strictEqual(buf.indexOf(0x69737465), 11);
+ assert.strictEqual(buf.indexOf(0x69737465), 11);
+ assert.strictEqual(buf.indexOf(-140), 0);
+ assert.strictEqual(buf.indexOf(-152), 1);
+ assert.strictEqual(buf.indexOf(0xff), -1);
+ assert.strictEqual(buf.indexOf(0xffff), -1);
+}
+
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
// Test buffers small enough to use the JS implementation
-const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
+var buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);
assert.strictEqual(buf, buf.swap16());
buf3.slice(1, 5).swap16();
assert.deepStrictEqual(buf3, Buffer.from([0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7]));
-const buf3_64 = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+var buf3_64 = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]);
buf6.swap32();
assert.deepStrictEqual(buf6, buf7);
-
var buf8A = new Uint8Array(256 * 8);
var buf9A = new Uint8Array(256 * 8);
-for (let i = 0; i < buf8A.length; i++) {
+for (var i = 0; i < buf8A.length; i++) {
buf8A[i] = i % 8;
buf9A[buf9A.length - i - 1] = i % 8;
}
// Test native code with buffers that are not memory-aligned
var buf10A = new Uint8Array(256 * 8);
var buf11A = new Uint8Array(256 * 8 - 2);
-for (let i = 0; i < buf10A.length; i++) {
+for (var i = 0; i < buf10A.length; i++) {
buf10A[i] = i % 2;
}
-for (let i = 1; i < buf11A.length; i++) {
+for (var i = 1; i < buf11A.length; i++) {
buf11A[buf11A.length - i] = (i + 1) % 2;
}
var buf10 = Buffer.from(buf10A.buffer, buf10A.byteOffset);
var buf12A = new Uint8Array(256 * 8);
var buf13A = new Uint8Array(256 * 8 - 4);
-for (let i = 0; i < buf12A.length; i++) {
+for (var i = 0; i < buf12A.length; i++) {
buf12A[i] = i % 4;
}
-for (let i = 1; i < buf13A.length; i++) {
+for (var i = 1; i < buf13A.length; i++) {
buf13A[buf13A.length - i] = (i + 1) % 4;
}
var buf12 = Buffer.from(buf12A.buffer, buf12A.byteOffset);
var buf14A = new Uint8Array(256 * 8);
var buf15A = new Uint8Array(256 * 8 - 8);
-for (let i = 0; i < buf14A.length; i++) {
+for (var i = 0; i < buf14A.length; i++) {
buf14A[i] = i % 8;
}
-for (let i = 1; i < buf15A.length; i++) {
+for (var i = 1; i < buf15A.length; i++) {
buf15A[buf15A.length - i] = (i + 1) % 8;
}
var buf14 = Buffer.from(buf14A.buffer, buf14A.byteOffset);
assert.deepStrictEqual(buf14.slice(0, buf15.length), buf15);
// Length assertions
-const re16 = /Buffer size must be a multiple of 16-bits/;
-const re32 = /Buffer size must be a multiple of 32-bits/;
-const re64 = /Buffer size must be a multiple of 64-bits/;
+var re16 = /Buffer size must be a multiple of 16-bits/;
+var re32 = /Buffer size must be a multiple of 32-bits/;
+var re64 = /Buffer size must be a multiple of 64-bits/;
assert.throws(() => Buffer.from(buf3).swap16(), re16);
assert.throws(() => Buffer.alloc(1025).swap16(), re16);
assert.throws(() => Buffer.alloc(1025).swap32(), re32);
assert.throws(() => buf3.slice(1, 3).swap64(), re64);
assert.throws(() => Buffer.alloc(1025).swap64(), re64);
+
'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
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
+
var assert = require('assert');
'use strict';
var Buffer = require('../../').Buffer;
-if (!Buffer.TYPED_ARRAY_SUPPORT) return;
-var common = {};
+
+var common = { skip: function () {} };
var assert = require('assert');
var Buffer = require('../../').Buffer;
// try to create 0-length buffers
new Buffer('');
new Buffer('', 'ascii');
+new Buffer('', 'latin1');
new Buffer('', 'binary');
Buffer(0);
for (var i = 0; i < segments.length; ++i) {
pos += b.write(segments[i], pos, 'base64');
}
+ assert.equal(b.toString('latin1', 0, pos), 'Madness?! This is node.js!');
assert.equal(b.toString('binary', 0, pos), 'Madness?! This is node.js!');
}
assert.equal(hexb2[i], hexb[i]);
}
+// Test single hex character throws TypeError
+// - https://github.com/nodejs/node/issues/6770
+assert.throws(function() {
+ Buffer.from('A', 'hex');
+}, TypeError);
+
+// Test single base64 char encodes as 0
+// assert.strictEqual(Buffer.from('A', 'base64').length, 0);
+
{
// test an invalid slice end.
// console.log('Try to slice off the end of the buffer');
assert.equal(b.toString(encoding), 'あいうえお');
});
+{
+ // latin1 encoding should write only one byte per character.
+ var b = Buffer([0xde, 0xad, 0xbe, 0xef]);
+ var s = String.fromCharCode(0xffff);
+ b.write(s, 0, 'latin1');
+ 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, 'latin1');
+ assert.equal(0xee, b[0]);
+ assert.equal(0xad, b[1]);
+ assert.equal(0xbe, b[2]);
+ assert.equal(0xef, b[3]);
+}
+
{
// Binary encoding should write only one byte per character.
var b = Buffer([0xde, 0xad, 0xbe, 0xef]);
// 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', 'latin1');
+ assert.equal(written, 4);
+ assert.equal(buf[4], 0);
written = sub.write('12345', 'binary');
assert.equal(written, 4);
assert.equal(buf[4], 0);
assert.equal(Buffer('13.37').length, 5);
// Ensure that the length argument is respected.
-'ascii utf8 hex base64 binary'.split(' ').forEach(function(enc) {
+'ascii utf8 hex base64 latin1 binary'.split(' ').forEach(function(enc) {
assert.equal(Buffer(1).write('aaaaaa', 0, 1, enc), 1);
});
'utf8',
'utf-8',
'ascii',
+ 'latin1',
'binary',
'base64',
'ucs2',
[ 'utf9',
'utf-7',
'Unicode-FTW',
- 'new gnu gun' ].forEach(function(enc) {
+ 'new gnu gun' ].forEach(function(enc) {
assert.equal(Buffer.isEncoding(enc), false);
});
// GH-5110
-(function() {
+{
var buffer = new Buffer('test');
var string = JSON.stringify(buffer);
- assert.equal(string, '{"type":"Buffer","data":[116,101,115,116]}');
+ assert.strictEqual(string, '{"type":"Buffer","data":[116,101,115,116]}');
assert.deepStrictEqual(buffer, JSON.parse(string, function(key, value) {
return value && value.type === 'Buffer'
? new Buffer(value.data)
: value;
}));
-})();
+}
// issue GH-7849
-(function() {
+{
var buf = new Buffer('test');
var json = JSON.stringify(buf);
var obj = JSON.parse(json);
var copy = new Buffer(obj);
assert(buf.equals(copy));
-})();
+}
// issue GH-4331
assert.throws(function() {
});
// test for common read(U)IntLE/BE
-(function() {
+{
var buf = new Buffer([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);
-})();
+ assert.strictEqual(buf.readUIntLE(0, 1), 0x01);
+ assert.strictEqual(buf.readUIntBE(0, 1), 0x01);
+ assert.strictEqual(buf.readUIntLE(0, 3), 0x030201);
+ assert.strictEqual(buf.readUIntBE(0, 3), 0x010203);
+ assert.strictEqual(buf.readUIntLE(0, 5), 0x0504030201);
+ assert.strictEqual(buf.readUIntBE(0, 5), 0x0102030405);
+ assert.strictEqual(buf.readUIntLE(0, 6), 0x060504030201);
+ assert.strictEqual(buf.readUIntBE(0, 6), 0x010203040506);
+ assert.strictEqual(buf.readIntLE(0, 1), 0x01);
+ assert.strictEqual(buf.readIntBE(0, 1), 0x01);
+ assert.strictEqual(buf.readIntLE(0, 3), 0x030201);
+ assert.strictEqual(buf.readIntBE(0, 3), 0x010203);
+ assert.strictEqual(buf.readIntLE(0, 5), 0x0504030201);
+ assert.strictEqual(buf.readIntBE(0, 5), 0x0102030405);
+ assert.strictEqual(buf.readIntLE(0, 6), 0x060504030201);
+ assert.strictEqual(buf.readIntBE(0, 6), 0x010203040506);
+}
// test for common write(U)IntLE/BE
-(function() {
+{
var buf = Buffer(3);
buf.writeUIntLE(0x123456, 0, 3);
assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
buf.writeIntBE(-0x0012000000, 0, 5);
assert.deepStrictEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]);
assert.equal(buf.readIntBE(0, 5), -0x0012000000);
-})();
+}
// test Buffer slice
-(function() {
+{
var buf = new Buffer('0123456789');
assert.equal(buf.slice(-10, 10), '0123456789');
assert.equal(buf.slice(-20, 10), '0123456789');
}
var utf16Buf = new Buffer('0123456789', 'utf16le');
- // assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer('012', 'utf16le'));
+ assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer('012', 'utf16le'));
assert.equal(buf.slice('0', '1'), '0');
assert.equal(buf.slice('-5', '10'), '56789');
// try to slice a zero length Buffer
// see https://github.com/joyent/node/issues/5881
SlowBuffer(0).slice(0, 1);
-})();
+}
// Regression test for #5482: should throw but not assert in C++ land.
assert.throws(function() {
// 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(a);
var c = Buffer(b);
- assert.equal(b.length, a.length);
- assert.equal(c.length, a.length);
+ assert.strictEqual(b.length, a.length);
+ assert.strictEqual(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.strictEqual(a[i], i);
+ assert.strictEqual(b[i], i);
+ assert.strictEqual(c[i], i);
}
-})();
+}
assert.throws(function() {
1 /*crypto.createHash('sha1').update(b2).digest('hex')*/
);
} else {
-// console.log('1..0 # Skipped: missing crypto');
+ common.skip('missing crypto');
}
// Test Compare
assert.equal(SlowBuffer.prototype.parent, undefined);
assert.equal(SlowBuffer.prototype.offset, undefined);
+{
+ // Test that large negative Buffer length inputs don't affect the pool offset.
+ assert.deepStrictEqual(Buffer(-Buffer.poolSize), Buffer.from(''));
+ assert.deepStrictEqual(Buffer(-100), Buffer.from(''));
+ assert.deepStrictEqual(Buffer.allocUnsafe(-Buffer.poolSize), Buffer.from(''));
+ assert.deepStrictEqual(Buffer.allocUnsafe(-100), Buffer.from(''));
+
+ // Check pool offset after that by trying to write string into the pool.
+ assert.doesNotThrow(() => Buffer.from('abc'));
+}
+
+
+// Test failed or zero-sized Buffer allocations not affecting typed arrays
+{
+ var zeroArray = new Uint32Array(10).fill(0);
+ var sizes = [1e10, 0, 0.1, -1, 'a', undefined, null, NaN];
+ var allocators = [
+ Buffer,
+ SlowBuffer,
+ Buffer.alloc,
+ Buffer.allocUnsafe,
+ Buffer.allocUnsafeSlow
+ ];
+ for (var allocator of allocators) {
+ for (var size of sizes) {
+ try {
+ allocator(size);
+ } catch (e) {
+ assert.deepStrictEqual(new Uint32Array(10), zeroArray);
+ }
+ }
+ }
+}
+