test,benchmark: use new Buffer API where appropriate

For tests / benchmarks that are creating Buffer instances for
any reason other than to test Buffer constructor, use the new
Buffer.alloc/Buffer.from API instead of the deprecated API.

PR-URL: https://github.com/nodejs/node/pull/18980
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
This commit is contained in:
Сковорода Никита Андреевич 2018-02-24 19:52:14 +03:00 committed by Anatoli Papirovski
parent 1572a5b6cd
commit 8ed44ff1c4
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0
12 changed files with 19 additions and 18 deletions

View File

@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
});
function main({ n }) {
const b = new Buffer(1024);
const b = Buffer.alloc(1024);
const r = new Readable();
const w = new Writable();

View File

@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
});
function main({ n }) {
const b = new Buffer(32);
const b = Buffer.alloc(32);
const s = new Readable();
function noop() {}
s._read = noop;

View File

@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
});
function main({ n }) {
const b = new Buffer(32);
const b = Buffer.alloc(32);
const s = new Readable();
function noop() {}
s._read = noop;

View File

@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
});
function main({ n }) {
const b = new Buffer(32);
const b = Buffer.alloc(32);
const s = new Readable();
function noop() {}
s._read = noop;

View File

@ -8,7 +8,7 @@ const bench = common.createBenchmark(main, {
});
function main({ n }) {
const b = new Buffer(32);
const b = Buffer.alloc(32);
const s = new Readable();
function noop() {}
s._read = noop;

View File

@ -18,7 +18,7 @@ const sock = dgram
function onlistening() {
sock.send(
new Buffer(2), 0, 2, sock.address().port,
Buffer.alloc(2), 0, 2, sock.address().port,
undefined, common.mustCall(onsent));
// init not called synchronously because dns lookup always wraps

View File

@ -6,12 +6,12 @@ const assert = require('assert');
{
const buf = Buffer.alloc(4);
assert.strictEqual(buf.length, 4);
assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0]));
assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0]));
assert.strictEqual(buf.write('abcdxx', 0, 'hex'), 2);
assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0x00, 0x00]));
assert.deepStrictEqual(buf, Buffer.from([0xab, 0xcd, 0x00, 0x00]));
assert.strictEqual(buf.toString('hex'), 'abcd0000');
assert.strictEqual(buf.write('abcdef01', 0, 'hex'), 4);
assert.deepStrictEqual(buf, new Buffer([0xab, 0xcd, 0xef, 0x01]));
assert.deepStrictEqual(buf, Buffer.from([0xab, 0xcd, 0xef, 0x01]));
assert.strictEqual(buf.toString('hex'), 'abcdef01');
const copy = Buffer.from(buf.toString('hex'), 'hex');
@ -26,13 +26,13 @@ const assert = require('assert');
{
const buf = Buffer.alloc(4);
assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0]));
assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0]));
assert.strictEqual(buf.write('xxabcd', 0, 'hex'), 0);
assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0]));
assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0]));
assert.strictEqual(buf.write('xxab', 1, 'hex'), 0);
assert.deepStrictEqual(buf, new Buffer([0, 0, 0, 0]));
assert.deepStrictEqual(buf, Buffer.from([0, 0, 0, 0]));
assert.strictEqual(buf.write('cdxxab', 0, 'hex'), 1);
assert.deepStrictEqual(buf, new Buffer([0xcd, 0, 0, 0]));
assert.deepStrictEqual(buf, Buffer.from([0xcd, 0, 0, 0]));
}
{

View File

@ -368,7 +368,7 @@ assert.strictEqual(
// Test that bypassing 'length' won't cause an abort.
common.expectsError(() => {
const buf = new Buffer('w00t');
const buf = Buffer.from('w00t');
Object.defineProperty(buf, 'length', {
value: 1337,
enumerable: true

View File

@ -504,7 +504,7 @@ assert.strictEqual(buf_bc.lastIndexOf('你好', 5, 'binary'), -1);
assert.strictEqual(buf_bc.lastIndexOf(Buffer.from('你好'), 7), -1);
// Test lastIndexOf on a longer buffer:
const bufferString = new Buffer('a man a plan a canal panama');
const bufferString = Buffer.from('a man a plan a canal panama');
assert.strictEqual(15, bufferString.lastIndexOf('canal'));
assert.strictEqual(21, bufferString.lastIndexOf('panama'));
assert.strictEqual(0, bufferString.lastIndexOf('a man a plan a canal panama'));
@ -566,7 +566,7 @@ const parts = [];
for (let i = 0; i < 1000000; i++) {
parts.push((countBits(i) % 2 === 0) ? 'yolo' : 'swag');
}
const reallyLong = new Buffer(parts.join(' '));
const reallyLong = Buffer.from(parts.join(' '));
assert.strictEqual('yolo swag swag yolo', reallyLong.slice(0, 19).toString());
// Expensive reverse searches. Stress test lastIndexOf:

View File

@ -3,6 +3,7 @@
require('../common');
const assert = require('assert');
// Tests deprecated Buffer API on purpose
const buf1 = Buffer(100);
const buf2 = new Buffer(100);

View File

@ -16,7 +16,7 @@ server.listen(0, common.mustCall(function() {
socket.cork();
socket.write('one');
socket.write(new Buffer('twø', 'utf8'));
socket.write(Buffer.from('twø', 'utf8'));
socket.uncork();

View File

@ -3,7 +3,7 @@ const common = require('../common');
const zlib = require('zlib');
const { inspect, promisify } = require('util');
const assert = require('assert');
const emptyBuffer = new Buffer(0);
const emptyBuffer = Buffer.alloc(0);
common.crashOnUnhandledRejection();