Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a53911b

Browse filesBrowse files
pd4d10danielleadams
authored andcommitted
test: improve buffer coverage
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>
1 parent 5514305 commit a53911b
Copy full SHA for a53911b

File tree

Expand file treeCollapse file tree

2 files changed

+61
-9
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+61
-9
lines changed
Open diff view settings
Collapse file

‎test/parallel/test-buffer-copy.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-buffer-copy.js
+42Lines 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),
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-buffer-indexof.js
+19-9Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ assert.strictEqual(
108108
3
109109
);
110110

111+
// Test base64url encoding
112+
assert.strictEqual(
113+
Buffer.from(b.toString('base64url'), 'base64url')
114+
.indexOf('ZA==', 0, 'base64url'),
115+
3
116+
);
117+
111118
// test ascii encoding
112119
assert.strictEqual(
113120
Buffer.from(b.toString('ascii'), 'ascii')
@@ -180,15 +187,18 @@ assert.strictEqual(Buffer.from('aaaa0').indexOf('30', 'hex'), 4);
180187
assert.strictEqual(Buffer.from('aaaa00a').indexOf('3030', 'hex'), 4);
181188

182189
{
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'));
190+
// Test usc2 and utf16le encoding
191+
['ucs2', 'utf16le'].forEach((encoding) => {
192+
const twoByteString = Buffer.from(
193+
'\u039a\u0391\u03a3\u03a3\u0395', encoding);
194+
195+
assert.strictEqual(twoByteString.indexOf('\u0395', 4, encoding), 8);
196+
assert.strictEqual(twoByteString.indexOf('\u03a3', -4, encoding), 6);
197+
assert.strictEqual(twoByteString.indexOf('\u03a3', -6, encoding), 4);
198+
assert.strictEqual(twoByteString.indexOf(
199+
Buffer.from('\u03a3', encoding), -6, encoding), 4);
200+
assert.strictEqual(-1, twoByteString.indexOf('\u03a3', -2, encoding));
201+
});
192202
}
193203

194204
const mixedByteStringUcs2 =

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.