Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 43 additions & 68 deletions test/parallel/test-buffer-slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,80 +28,55 @@ assert.strictEqual(0, Buffer.from('hello', 'utf8').slice(0, 0).length);
assert.strictEqual(0, Buffer('hello', 'utf8').slice(0, 0).length);

const buf = Buffer.from('0123456789', 'utf8');
assert.strictEqual(0, Buffer.compare(buf.slice(-10, 10),
Buffer.from('0123456789', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(-20, 10),
Buffer.from('0123456789', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(-20, -10),
Buffer.from('', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(),
Buffer.from('0123456789', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(0),
Buffer.from('0123456789', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(0, 0),
Buffer.from('', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(undefined),
Buffer.from('0123456789', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice('foobar'),
Buffer.from('0123456789', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(undefined, undefined),
Buffer.from('0123456789', 'utf8')));
const expectedSameBufs = [
[buf.slice(-10, 10), Buffer.from('0123456789', 'utf8')],
[buf.slice(-20, 10), Buffer.from('0123456789', 'utf8')],
[buf.slice(-20, -10), Buffer.from('', 'utf8')],
[buf.slice(), Buffer.from('0123456789', 'utf8')],
[buf.slice(0), Buffer.from('0123456789', 'utf8')],
[buf.slice(0, 0), Buffer.from('', 'utf8')],
[buf.slice(undefined), Buffer.from('0123456789', 'utf8')],
[buf.slice('foobar'), Buffer.from('0123456789', 'utf8')],
[buf.slice(undefined, undefined), Buffer.from('0123456789', 'utf8')],
[buf.slice(2), Buffer.from('23456789', 'utf8')],
[buf.slice(5), Buffer.from('56789', 'utf8')],
[buf.slice(10), Buffer.from('', 'utf8')],
[buf.slice(5, 8), Buffer.from('567', 'utf8')],
[buf.slice(8, -1), Buffer.from('8', 'utf8')],
[buf.slice(-10), Buffer.from('0123456789', 'utf8')],
[buf.slice(0, -9), Buffer.from('0', 'utf8')],
[buf.slice(0, -10), Buffer.from('', 'utf8')],
[buf.slice(0, -1), Buffer.from('012345678', 'utf8')],
[buf.slice(2, -2), Buffer.from('234567', 'utf8')],
[buf.slice(0, 65536), Buffer.from('0123456789', 'utf8')],
[buf.slice(65536, 0), Buffer.from('', 'utf8')],
[buf.slice(-5, -8), Buffer.from('', 'utf8')],
[buf.slice(-5, -3), Buffer.from('56', 'utf8')],
[buf.slice(-10, 10), Buffer.from('0123456789', 'utf8')],
[buf.slice('0', '1'), Buffer.from('0', 'utf8')],
[buf.slice('-5', '10'), Buffer.from('56789', 'utf8')],
[buf.slice('-10', '10'), Buffer.from('0123456789', 'utf8')],
[buf.slice('-10', '-5'), Buffer.from('01234', 'utf8')],
[buf.slice('-10', '-0'), Buffer.from('', 'utf8')],
[buf.slice('111'), Buffer.from('', 'utf8')],
[buf.slice('0', '-111'), Buffer.from('', 'utf8')]
];

assert.strictEqual(0, Buffer.compare(buf.slice(2),
Buffer.from('23456789', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(5),
Buffer.from('56789', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(10),
Buffer.from('', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(5, 8),
Buffer.from('567', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(8, -1),
Buffer.from('8', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(-10),
Buffer.from('0123456789', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(0, -9),
Buffer.from('0', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(0, -10),
Buffer.from('', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(0, -1),
Buffer.from('012345678', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(2, -2),
Buffer.from('234567', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(0, 65536),
Buffer.from('0123456789', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(65536, 0),
Buffer.from('', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(-5, -8),
Buffer.from('', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(-5, -3),
Buffer.from('56', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice(-10, 10),
Buffer.from('0123456789', 'utf8')));
for (let i = 0, s = buf; i < buf.length; ++i) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this wasn't introduced by this change, but this segment of the test seems broken to me. s = buf means that they are pointing to the same buffer in memory, so of course buf.slice(i) and s.slice(i) are going to be the same thing. Maybe that's the point of the test? But I suspect not. Anyone understand this? Worth just removing this entire block? If not, it seems like it could use a comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This segment was imported in this commit by @jasnell a year ago. I'm also confused with it.

@jasnell Could you tell us something about it ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@starkwang I don't think that's the commit that introduced it. It's s = buf.toString() in that commit, which is very different from s = buf.

It appears that it was introduced in 02a4c08 and that it was an error. Does it make sense to add a second commit here or else in a different PR to restore it to buf.toString()?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Trott I believe it should be buf.toString() as well, the s probably means "string" here (would be clearer if it gets renamed to str). Fixing it here SGTM.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll change it into buf.toString().

assert.strictEqual(0, Buffer.compare(buf.slice(i), s.slice(i)));
assert.strictEqual(0, Buffer.compare(buf.slice(0, i), s.slice(0, i)));
assert.strictEqual(0, Buffer.compare(buf.slice(-i), s.slice(-i)));
assert.strictEqual(0, Buffer.compare(buf.slice(0, -i), s.slice(0, -i)));
expectedSameBufs.push(
[buf.slice(i), s.slice(i)],
[buf.slice(0, i), s.slice(0, i)],
[buf.slice(-i), s.slice(-i)],
[buf.slice(0, -i), s.slice(0, -i)]
);
}

expectedSameBufs.forEach(([buf1, buf2]) => {
assert.strictEqual(0, Buffer.compare(buf1, buf2));
});

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

assert.strictEqual(0, Buffer.compare(buf.slice('0', '1'),
Buffer.from('0', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice('-5', '10'),
Buffer.from('56789', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice('-10', '10'),
Buffer.from('0123456789', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice('-10', '-5'),
Buffer.from('01234', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice('-10', '-0'),
Buffer.from('', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice('111'),
Buffer.from('', 'utf8')));
assert.strictEqual(0, Buffer.compare(buf.slice('0', '-111'),
Buffer.from('', 'utf8')));

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