Skip to content

Commit 6447cab

Browse files
pd4d10richardlau
authored andcommitted
test: improve buffer coverage
PR-URL: #38538 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Zijian Liu <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2bc2a23 commit 6447cab

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

test/parallel/test-buffer-copy.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ let cntr = 0;
3030
}
3131
}
3232

33+
{
34+
// Floats will be converted to integers via `Math.floor`
35+
b.fill(++cntr);
36+
c.fill(++cntr);
37+
const copied = b.copy(c, 0, 0, 512.5);
38+
assert.strictEqual(copied, 512);
39+
for (let i = 0; i < c.length; i++) {
40+
assert.strictEqual(c[i], b[i]);
41+
}
42+
}
43+
3344
{
3445
// Copy c into b, without specifying sourceEnd
3546
b.fill(++cntr);
@@ -52,6 +63,17 @@ let cntr = 0;
5263
}
5364
}
5465

66+
{
67+
// Copied source range greater than source length
68+
b.fill(++cntr);
69+
c.fill(++cntr);
70+
const copied = c.copy(b, 0, 0, c.length + 1);
71+
assert.strictEqual(copied, c.length);
72+
for (let i = 0; i < c.length; i++) {
73+
assert.strictEqual(b[i], c[i]);
74+
}
75+
}
76+
5577
{
5678
// Copy longer buffer b to shorter c without targetStart
5779
b.fill(++cntr);
@@ -107,6 +129,26 @@ bb.fill('hello crazy world');
107129
// Try to copy from before the beginning of b. Should not throw.
108130
b.copy(c, 0, 100, 10);
109131

132+
// Throw with invalid source type
133+
assert.throws(
134+
() => Buffer.prototype.copy.call(0),
135+
{
136+
code: 'ERR_INVALID_ARG_TYPE',
137+
name: 'TypeError',
138+
}
139+
);
140+
141+
// Copy throws at negative targetStart
142+
assert.throws(
143+
() => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), -1, 0),
144+
{
145+
code: 'ERR_OUT_OF_RANGE',
146+
name: 'RangeError',
147+
message: 'The value of "targetStart" is out of range. ' +
148+
'It must be >= 0. Received -1'
149+
}
150+
);
151+
110152
// Copy throws at negative sourceStart
111153
assert.throws(
112154
() => Buffer.allocUnsafe(5).copy(Buffer.allocUnsafe(5), 0, -1),

test/parallel/test-buffer-indexof.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,18 @@ assert.strictEqual(Buffer.from('aaaa0').indexOf('30', 'hex'), 4);
180180
assert.strictEqual(Buffer.from('aaaa00a').indexOf('3030', 'hex'), 4);
181181

182182
{
183-
// test usc2 encoding
184-
const twoByteString = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');
185-
186-
assert.strictEqual(twoByteString.indexOf('\u0395', 4, 'ucs2'), 8);
187-
assert.strictEqual(twoByteString.indexOf('\u03a3', -4, 'ucs2'), 6);
188-
assert.strictEqual(twoByteString.indexOf('\u03a3', -6, 'ucs2'), 4);
189-
assert.strictEqual(twoByteString.indexOf(
190-
Buffer.from('\u03a3', 'ucs2'), -6, 'ucs2'), 4);
191-
assert.strictEqual(-1, twoByteString.indexOf('\u03a3', -2, 'ucs2'));
183+
// Test usc2 and utf16le encoding
184+
['ucs2', 'utf16le'].forEach((encoding) => {
185+
const twoByteString = Buffer.from(
186+
'\u039a\u0391\u03a3\u03a3\u0395', encoding);
187+
188+
assert.strictEqual(twoByteString.indexOf('\u0395', 4, encoding), 8);
189+
assert.strictEqual(twoByteString.indexOf('\u03a3', -4, encoding), 6);
190+
assert.strictEqual(twoByteString.indexOf('\u03a3', -6, encoding), 4);
191+
assert.strictEqual(twoByteString.indexOf(
192+
Buffer.from('\u03a3', encoding), -6, encoding), 4);
193+
assert.strictEqual(-1, twoByteString.indexOf('\u03a3', -2, encoding));
194+
});
192195
}
193196

194197
const mixedByteStringUcs2 =

0 commit comments

Comments
 (0)