Skip to content

Commit

Permalink
test,benchmark: use new Buffer API where appropriate
Browse files Browse the repository at this point in the history
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: #18980
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Daijiro Wachi <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
  • Loading branch information
ChALkeR authored and addaleax committed Mar 5, 2018
1 parent cab6c8e commit f864509
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion benchmark/streams/pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion benchmark/streams/readable-bigread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion benchmark/streams/readable-bigunevenread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion benchmark/streams/readable-readall.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion benchmark/streams/readable-unevenread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/async-hooks/test-udpsendwrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions test/parallel/test-buffer-badhex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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]));
}

{
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-buffer-fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ common.expectsError(() => {

// 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
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-buffer-indexof.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-buffer-zero-fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-net-socket-byteswritten.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-zlib-empty-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit f864509

Please sign in to comment.