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 danielleadams committed Jun 21, 2021
1 parent 5514305 commit a53911b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
42 changes: 42 additions & 0 deletions test/parallel/test-buffer-copy.js
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
28 changes: 19 additions & 9 deletions test/parallel/test-buffer-indexof.js
Expand Up @@ -108,6 +108,13 @@ assert.strictEqual(
3
);

// Test base64url encoding
assert.strictEqual(
Buffer.from(b.toString('base64url'), 'base64url')
.indexOf('ZA==', 0, 'base64url'),
3
);

// test ascii encoding
assert.strictEqual(
Buffer.from(b.toString('ascii'), 'ascii')
Expand Down Expand Up @@ -180,15 +187,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 a53911b

Please sign in to comment.