Browse Source

test: improve coverage of the buffer module

Add tests for untested branches and statements.
Also convert some lines to const.

PR-URL: https://github.com/nodejs/node/pull/8552
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
v7.x
Michaël Zasso 9 years ago
parent
commit
8699ecd340
  1. 21
      test/parallel/test-buffer-alloc.js
  2. 6
      test/parallel/test-buffer-compare-offset.js
  3. 22
      test/parallel/test-buffer-indexof.js
  4. 7
      test/parallel/test-buffer-new.js

21
test/parallel/test-buffer-alloc.js

@ -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);
@ -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
@ -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

6
test/parallel/test-buffer-compare-offset.js

@ -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'));
@ -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));

22
test/parallel/test-buffer-indexof.js

@ -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);
@ -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')

7
test/parallel/test-buffer-new.js

@ -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/);
Loading…
Cancel
Save