Skip to content

Commit

Permalink
test: clean up / refactor buffer tests, remove duplication
Browse files Browse the repository at this point in the history
Remove duplication of buffer tests, separate out into separate
files, update and cleanup code, move to using strictEqual where
possible.

PR-URL: nodejs#8256
Reviewed-By: Michaël Zasso <[email protected]>
  • Loading branch information
jasnell committed Aug 26, 2016
1 parent b3e7ac2 commit 7053922
Show file tree
Hide file tree
Showing 12 changed files with 926 additions and 2,443 deletions.
1,375 changes: 463 additions & 912 deletions test/parallel/test-buffer-alloc.js

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions test/parallel/test-buffer-compare.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

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

const b = Buffer.alloc(1, 'a');
const c = Buffer.alloc(1, 'c');
const d = Buffer.alloc(2, 'aa');

assert.strictEqual(b.compare(c), -1);
assert.strictEqual(c.compare(d), 1);
assert.strictEqual(d.compare(b), 1);
assert.strictEqual(b.compare(d), -1);
assert.strictEqual(b.compare(b), 0);

assert.strictEqual(Buffer.compare(b, c), -1);
assert.strictEqual(Buffer.compare(c, d), 1);
assert.strictEqual(Buffer.compare(d, b), 1);
assert.strictEqual(Buffer.compare(b, d), -1);
assert.strictEqual(Buffer.compare(c, c), 0);

assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(0)), 0);
assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(1)), -1);
assert.strictEqual(Buffer.compare(Buffer.alloc(1), Buffer.alloc(0)), 1);

assert.throws(() => Buffer.compare(Buffer.alloc(1), 'abc'));

assert.throws(() => Buffer.compare('abc', Buffer.alloc(1)));

assert.throws(() => Buffer.alloc(1).compare('abc'));
120 changes: 120 additions & 0 deletions test/parallel/test-buffer-copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
'use strict';

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

var b = Buffer.allocUnsafe(1024);
var c = Buffer.allocUnsafe(512);
var cntr = 0;

{
// copy 512 bytes, from 0 to 512.
b.fill(++cntr);
c.fill(++cntr);
const copied = b.copy(c, 0, 0, 512);
assert.strictEqual(512, copied);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}

{
// copy c into b, without specifying sourceEnd
b.fill(++cntr);
c.fill(++cntr);
const copied = c.copy(b, 0, 0);
assert.strictEqual(c.length, copied);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(c[i], b[i]);
}
}

{
// copy c into b, without specifying sourceStart
b.fill(++cntr);
c.fill(++cntr);
const copied = c.copy(b, 0);
assert.strictEqual(c.length, copied);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(c[i], b[i]);
}
}

{
// copy longer buffer b to shorter c without targetStart
b.fill(++cntr);
c.fill(++cntr);
const copied = b.copy(c);
assert.strictEqual(c.length, copied);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}

{
// copy starting near end of b to c
b.fill(++cntr);
c.fill(++cntr);
const copied = b.copy(c, 0, b.length - Math.floor(c.length / 2));
assert.strictEqual(Math.floor(c.length / 2), copied);
for (let i = 0; i < Math.floor(c.length / 2); i++) {
assert.strictEqual(b[b.length - Math.floor(c.length / 2) + i], c[i]);
}
for (let i = Math.floor(c.length / 2) + 1; i < c.length; i++) {
assert.strictEqual(c[c.length - 1], c[i]);
}
}

{
// try to copy 513 bytes, and check we don't overrun c
b.fill(++cntr);
c.fill(++cntr);
const copied = b.copy(c, 0, 0, 513);
assert.strictEqual(c.length, copied);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}

{
// copy 768 bytes from b into b
b.fill(++cntr);
b.fill(++cntr, 256);
const copied = b.copy(b, 0, 256, 1024);
assert.strictEqual(768, copied);
for (let i = 0; i < b.length; i++) {
assert.strictEqual(cntr, b[i]);
}
}

// copy string longer than buffer length (failure will segfault)
var bb = Buffer.allocUnsafe(10);
bb.fill('hello crazy world');


// try to copy from before the beginning of b
assert.doesNotThrow(() => { b.copy(c, 0, 100, 10); });

// copy throws at negative sourceStart
assert.throws(function() {
Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, -1);
}, RangeError);

{
// check sourceEnd resets to targetEnd if former is greater than the latter
b.fill(++cntr);
c.fill(++cntr);
b.copy(c, 0, 0, 1025);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}

// throw with negative sourceEnd
assert.throws(() => b.copy(c, 0, 0, -1), RangeError);

// when sourceStart is greater than sourceEnd, zero copied
assert.strictEqual(b.copy(c, 0, 100, 10), 0);

// when targetStart > targetLength, zero copied
assert.strictEqual(b.copy(c, 512, 0, 10), 0);
16 changes: 16 additions & 0 deletions test/parallel/test-buffer-equals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

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

const b = Buffer.from('abcdf');
const c = Buffer.from('abcdf');
const d = Buffer.from('abcde');
const e = Buffer.from('abcdef');

assert.ok(b.equals(c));
assert.ok(!c.equals(d));
assert.ok(!d.equals(e));
assert.ok(d.equals(d));

assert.throws(() => Buffer.alloc(1).equals('abc'));
33 changes: 33 additions & 0 deletions test/parallel/test-buffer-failed-alloc-typed-arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

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

// Test failed or zero-sized Buffer allocations not affecting typed arrays.
// This test exists because of a regression that occurred. Because Buffer
// instances are allocated with the same underlying allocator as TypedArrays,
// but Buffer's can optional be non-zero filled, there was a regression that
// occurred when a Buffer allocated failed, the internal flag specifying
// whether or not to zero-fill was not being reset, causing TypedArrays to
// allocate incorrectly.
const zeroArray = new Uint32Array(10).fill(0);
const sizes = [1e10, 0, 0.1, -1, 'a', undefined, null, NaN];
const allocators = [
Buffer,
SlowBuffer,
Buffer.alloc,
Buffer.allocUnsafe,
Buffer.allocUnsafeSlow
];
for (const allocator of allocators) {
for (const size of sizes) {
try {
// These allocations are known to fail. If they do,
// Uint32Array should still produce a zeroed out result.
allocator(size);
} catch (e) {
assert.deepStrictEqual(new Uint32Array(10), zeroArray);
}
}
}
47 changes: 47 additions & 0 deletions test/parallel/test-buffer-fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,50 @@ function testBufs(string, offset, length, encoding) {
assert.deepStrictEqual(buf1.fill.apply(buf1, arguments),
writeToFill.apply(null, arguments));
}

// Make sure these throw.
assert.throws(() => Buffer.allocUnsafe(8).fill('a', -1));
assert.throws(() => Buffer.allocUnsafe(8).fill('a', 0, 9));

// Make sure this doesn't hang indefinitely.
Buffer.allocUnsafe(8).fill('');
Buffer.alloc(8, '');

{
const buf = Buffer.alloc(64, 10);
for (let i = 0; i < buf.length; i++)
assert.strictEqual(buf[i], 10);

buf.fill(11, 0, buf.length >> 1);
for (let i = 0; i < buf.length >> 1; i++)
assert.strictEqual(buf[i], 11);
for (let i = (buf.length >> 1) + 1; i < buf.length; i++)
assert.strictEqual(buf[i], 10);

buf.fill('h');
for (let i = 0; i < buf.length; i++)
assert.strictEqual('h'.charCodeAt(0), buf[i]);

buf.fill(0);
for (let i = 0; i < buf.length; i++)
assert.strictEqual(0, buf[i]);

buf.fill(null);
for (let i = 0; i < buf.length; i++)
assert.strictEqual(0, buf[i]);

buf.fill(1, 16, 32);
for (let i = 0; i < 16; i++)
assert.strictEqual(0, buf[i]);
for (let i = 16; i < 32; i++)
assert.strictEqual(1, buf[i]);
for (let i = 32; i < buf.length; i++)
assert.strictEqual(0, buf[i]);
}

{
const buf = Buffer.alloc(10, 'abc');
assert.strictEqual(buf.toString(), 'abcabcabca');
buf.fill('է');
assert.strictEqual(buf.toString(), 'էէէէէ');
}
33 changes: 33 additions & 0 deletions test/parallel/test-buffer-isencoding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

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

[ 'hex',
'utf8',
'utf-8',
'ascii',
'latin1',
'binary',
'base64',
'ucs2',
'ucs-2',
'utf16le',
'utf-16le' ].forEach((enc) => {
assert.strictEqual(Buffer.isEncoding(enc), true);
});

[ 'utf9',
'utf-7',
'Unicode-FTW',
'new gnu gun',
false,
NaN,
{},
Infinity,
[],
1,
0,
-1 ].forEach((enc) => {
assert.strictEqual(Buffer.isEncoding(enc), false);
});
24 changes: 24 additions & 0 deletions test/parallel/test-buffer-no-negative-allocation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

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

const msg = /"size" argument must not be negative/;

// Test that negative Buffer length inputs throw errors.

assert.throws(() => Buffer(-Buffer.poolSize), msg);
assert.throws(() => Buffer(-100), msg);
assert.throws(() => Buffer(-1), msg);

assert.throws(() => Buffer.alloc(-Buffer.poolSize), msg);
assert.throws(() => Buffer.alloc(-100), msg);
assert.throws(() => Buffer.alloc(-1), msg);

assert.throws(() => Buffer.allocUnsafe(-Buffer.poolSize), msg);
assert.throws(() => Buffer.allocUnsafe(-100), msg);
assert.throws(() => Buffer.allocUnsafe(-1), msg);

assert.throws(() => Buffer.allocUnsafeSlow(-Buffer.poolSize), msg);
assert.throws(() => Buffer.allocUnsafeSlow(-100), msg);
assert.throws(() => Buffer.allocUnsafeSlow(-1), msg);
13 changes: 13 additions & 0 deletions test/parallel/test-buffer-regression-649.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

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

// Regression test for https://github.com/nodejs/node/issues/649.
const len = 1422561062959;
assert.throws(() => Buffer(len).toString('utf8'));
assert.throws(() => SlowBuffer(len).toString('utf8'));
assert.throws(() => Buffer.alloc(len).toString('utf8'));
assert.throws(() => Buffer.allocUnsafe(len).toString('utf8'));
assert.throws(() => Buffer.allocUnsafeSlow(len).toString('utf8'));
63 changes: 63 additions & 0 deletions test/parallel/test-buffer-slice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

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

assert.strictEqual(0, Buffer.from('hello').slice(0, 0).length);
assert.strictEqual(0, Buffer('hello').slice(0, 0).length);

const buf = Buffer.from('0123456789');
assert.equal(buf.slice(-10, 10), '0123456789');
assert.equal(buf.slice(-20, 10), '0123456789');
assert.equal(buf.slice(-20, -10), '');
assert.equal(buf.slice(), '0123456789');
assert.equal(buf.slice(0), '0123456789');
assert.equal(buf.slice(0, 0), '');
assert.equal(buf.slice(undefined), '0123456789');
assert.equal(buf.slice('foobar'), '0123456789');
assert.equal(buf.slice(undefined, undefined), '0123456789');

assert.equal(buf.slice(2), '23456789');
assert.equal(buf.slice(5), '56789');
assert.equal(buf.slice(10), '');
assert.equal(buf.slice(5, 8), '567');
assert.equal(buf.slice(8, -1), '8');
assert.equal(buf.slice(-10), '0123456789');
assert.equal(buf.slice(0, -9), '0');
assert.equal(buf.slice(0, -10), '');
assert.equal(buf.slice(0, -1), '012345678');
assert.equal(buf.slice(2, -2), '234567');
assert.equal(buf.slice(0, 65536), '0123456789');
assert.equal(buf.slice(65536, 0), '');
assert.equal(buf.slice(-5, -8), '');
assert.equal(buf.slice(-5, -3), '56');
assert.equal(buf.slice(-10, 10), '0123456789');
for (let i = 0, s = buf.toString(); i < buf.length; ++i) {
assert.equal(buf.slice(i), s.slice(i));
assert.equal(buf.slice(0, i), s.slice(0, i));
assert.equal(buf.slice(-i), s.slice(-i));
assert.equal(buf.slice(0, -i), s.slice(0, -i));
}

const utf16Buf = Buffer.from('0123456789', 'utf16le');
assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer.from('012', 'utf16le'));

assert.equal(buf.slice('0', '1'), '0');
assert.equal(buf.slice('-5', '10'), '56789');
assert.equal(buf.slice('-10', '10'), '0123456789');
assert.equal(buf.slice('-10', '-5'), '01234');
assert.equal(buf.slice('-10', '-0'), '');
assert.equal(buf.slice('111'), '');
assert.equal(buf.slice('0', '-111'), '');

// try to slice a zero length Buffer
// see https://github.com/joyent/node/issues/5881
Buffer.alloc(0).slice(0, 1);

{
// Single argument slice
assert.strictEqual('bcde', Buffer.from('abcde').slice(1).toString());
}

// slice(0,0).length === 0
assert.strictEqual(0, Buffer.from('hello').slice(0, 0).length);
Loading

0 comments on commit 7053922

Please sign in to comment.