Skip to content

Commit

Permalink
test: improve buffer coverage
Browse files Browse the repository at this point in the history
PR-URL: #38538
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
pd4d10 authored and richardlau committed Jul 19, 2021
1 parent 7e5b37a commit 8dc2a49
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
42 changes: 42 additions & 0 deletions test/parallel/test-buffer-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ let cntr = 0;
}
}

{
// Floats will be converted to integers via `Math.floor`
b.fill(++cntr);
c.fill(++cntr);
const copied = b.copy(c, 0, 0, 512.5);
assert.strictEqual(copied, 512);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(c[i], b[i]);
}
}

{
// Copy c into b, without specifying sourceEnd
b.fill(++cntr);
Expand All @@ -52,6 +63,17 @@ let cntr = 0;
}
}

{
// Copied source range greater than source length
b.fill(++cntr);
c.fill(++cntr);
const copied = c.copy(b, 0, 0, c.length + 1);
assert.strictEqual(copied, c.length);
for (let i = 0; i < c.length; i++) {
assert.strictEqual(b[i], c[i]);
}
}

{
// Copy longer buffer b to shorter c without targetStart
b.fill(++cntr);
Expand Down Expand Up @@ -107,6 +129,26 @@ bb.fill('hello crazy world');
// Try to copy from before the beginning of b. Should not throw.
b.copy(c, 0, 100, 10);

// Throw with invalid source type
assert.throws(
() => Buffer.prototype.copy.call(0),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}
);

// Copy throws at negative targetStart
assert.throws(
() => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), -1, 0),
{
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "targetStart" is out of range. ' +
'It must be >= 0. Received -1'
}
);

// Copy throws at negative sourceStart
assert.throws(
() => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, -1),
Expand Down
21 changes: 12 additions & 9 deletions test/parallel/test-buffer-indexof.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,18 @@ assert.strictEqual(Buffer.from('aaaa0').indexOf('30', 'hex'), 4);
assert.strictEqual(Buffer.from('aaaa00a').indexOf('3030', 'hex'), 4);

{
// test usc2 encoding
const twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');

assert.strictEqual(twoByteString.indexOf('\u0395', 4, 'ucs2'), 8);
assert.strictEqual(twoByteString.indexOf('\u03a3', -4, 'ucs2'), 6);
assert.strictEqual(twoByteString.indexOf('\u03a3', -6, 'ucs2'), 4);
assert.strictEqual(twoByteString.indexOf(
Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2'), 4);
assert.strictEqual(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2'));
// Test usc2 and utf16le encoding
['ucs2', 'utf16le'].forEach((encoding) => {
const twoByteString = Buffer.from(
'\u039a\u0391\u03a3\u03a3\u0395', encoding);

assert.strictEqual(twoByteString.indexOf('\u0395', 4, encoding), 8);
assert.strictEqual(twoByteString.indexOf('\u03a3', -4, encoding), 6);
assert.strictEqual(twoByteString.indexOf('\u03a3', -6, encoding), 4);
assert.strictEqual(twoByteString.indexOf(
Buffer.from('\u03a3', encoding), -6, encoding), 4);
assert.strictEqual(-1, twoByteString.indexOf('\u03a3', -2, encoding));
});
}

const mixedByteStringUcs2 =
Expand Down

0 comments on commit 8dc2a49

Please sign in to comment.