Skip to content

Commit

Permalink
test: improve coverage of the buffer module
Browse files Browse the repository at this point in the history
Add tests for untested branches and statements.
Also convert some lines to const.

PR-URL: #8552
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Сковорода Никита Андреевич <[email protected]>
  • Loading branch information
targos authored and Fishrock123 committed Oct 11, 2016
1 parent 3fcdf4e commit ba88f5b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
21 changes: 21 additions & 0 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ assert.strictEqual(512, c.length);
const d = Buffer.from([]);
assert.strictEqual(0, d.length);

// Test offset properties
{
const b = Buffer.alloc(128);
assert.strictEqual(128, b.length);
assert.strictEqual(0, b.byteOffset);
assert.strictEqual(0, b.offset);
}

// Test creating a Buffer from a Uint32Array
{
const ui32 = new Uint32Array(4).fill(42);
Expand All @@ -49,6 +57,9 @@ assert.throws(() => b.toString('invalid'),
// invalid encoding for Buffer.write
assert.throws(() => b.write('test string', 0, 5, 'invalid'),
/Unknown encoding: invalid/);
// unsupported arguments for Buffer.write
assert.throws(() => b.write('test', 'utf8', 0),
/is no longer supported/);


// try to create 0-length buffers
Expand Down Expand Up @@ -706,6 +717,16 @@ assert.strictEqual('<Buffer 81 a3 66 6f 6f a3 62 61 72>', x.inspect());
assert.strictEqual(buf[4], 0);
}

{
// test alloc with fill option
const buf = Buffer.alloc(5, '800A', 'hex');
assert.strictEqual(buf[0], 128);
assert.strictEqual(buf[1], 10);
assert.strictEqual(buf[2], 128);
assert.strictEqual(buf[3], 10);
assert.strictEqual(buf[4], 128);
}


// Check for fractional length args, junk length args, etc.
// https://github.com/joyent/node/issues/1758
Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-buffer-compare-offset.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ assert.strictEqual(-1, a.compare(b, '0'));
// Equivalent to a.compare(b).
assert.strictEqual(-1, a.compare(b, 0, undefined, 0));

// Zero-length targer, return 1
// Zero-length target, return 1
assert.strictEqual(1, a.compare(b, 0, 0, 0));
assert.strictEqual(1, a.compare(b, '0', '0', '0'));

Expand All @@ -25,6 +25,10 @@ assert.strictEqual(1, a.compare(b, 6, 10));
// Zero-length source, return -1
assert.strictEqual(-1, a.compare(b, 6, 10, 0, 0));

// Zero-length source and target, return 0
assert.strictEqual(0, a.compare(b, 0, 0, 0, 0));
assert.strictEqual(0, a.compare(b, 1, 1, 2, 2));

// Equivalent to Buffer.compare(a.slice(4), b.slice(0, 5))
assert.strictEqual(1, a.compare(b, 0, 5, 4));

Expand Down
22 changes: 14 additions & 8 deletions test/parallel/test-buffer-indexof.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';
require('../common');
var assert = require('assert');
const assert = require('assert');

var Buffer = require('buffer').Buffer;
const Buffer = require('buffer').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('');
const b = Buffer.from('abcdef');
const buf_a = Buffer.from('a');
const buf_bc = Buffer.from('bc');
const buf_f = Buffer.from('f');
const buf_z = Buffer.from('z');
const buf_empty = Buffer.from('');

assert.equal(b.indexOf('a'), 0);
assert.equal(b.indexOf('a', 1), -1);
Expand Down Expand Up @@ -78,6 +78,12 @@ assert.equal(b.indexOf(Buffer.from('f'), 6), -1);

assert.equal(Buffer.from('ff').indexOf(Buffer.from('f'), 1, 'ucs2'), -1);

// test invalid and uppercase encoding
assert.strictEqual(b.indexOf('b', 'utf8'), 1);
assert.strictEqual(b.indexOf('b', 'UTF8'), 1);
assert.strictEqual(b.indexOf('62', 'HEX'), 1);
assert.throws(() => b.indexOf('bad', 'enc'), /Unknown encoding: enc/);

// test hex encoding
assert.strictEqual(
Buffer.from(b.toString('hex'), 'hex')
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-buffer-new.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

require('../common');
const assert = require('assert');
const Buffer = require('buffer').Buffer;

assert.throws(() => new Buffer(42, 'utf8'), /first argument must be a string/);

0 comments on commit ba88f5b

Please sign in to comment.