From 02a4c081e93e527c1cff163534364a8d488c3777 Mon Sep 17 00:00:00 2001 From: Michael Alexander Date: Thu, 1 Dec 2016 11:35:42 -0600 Subject: [PATCH] test: strictCompare and explcit inputs mprovement to test-buffer-slice * change implicit string equal() compares to strictEqual compares of buffer output (the slice output default) * explicitly create buffers from utf8 inputs for the compare PR-URL: https://github.com/nodejs/node/pull/10048 Reviewed-By: James M Snell --- test/parallel/test-buffer-slice.js | 127 ++++++++++++++++++----------- 1 file changed, 80 insertions(+), 47 deletions(-) diff --git a/test/parallel/test-buffer-slice.js b/test/parallel/test-buffer-slice.js index 076bf33fb0..2489420c33 100644 --- a/test/parallel/test-buffer-slice.js +++ b/test/parallel/test-buffer-slice.js @@ -3,52 +3,83 @@ require('../common'); const assert = require('assert'); -assert.strictEqual(0, Buffer.from('hello').slice(0, 0).length); -assert.strictEqual(0, Buffer('hello').slice(0, 0).length); +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'); -assert.equal(buf.slice(-10, 10), '0123456789'); -assert.equal(buf.slice(-20, 10), '0123456789'); -assert.equal(buf.slice(-20, -10), ''); -assert.equal(buf.slice(), '0123456789'); -assert.equal(buf.slice(0), '0123456789'); -assert.equal(buf.slice(0, 0), ''); -assert.equal(buf.slice(undefined), '0123456789'); -assert.equal(buf.slice('foobar'), '0123456789'); -assert.equal(buf.slice(undefined, undefined), '0123456789'); +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'))); -assert.equal(buf.slice(2), '23456789'); -assert.equal(buf.slice(5), '56789'); -assert.equal(buf.slice(10), ''); -assert.equal(buf.slice(5, 8), '567'); -assert.equal(buf.slice(8, -1), '8'); -assert.equal(buf.slice(-10), '0123456789'); -assert.equal(buf.slice(0, -9), '0'); -assert.equal(buf.slice(0, -10), ''); -assert.equal(buf.slice(0, -1), '012345678'); -assert.equal(buf.slice(2, -2), '234567'); -assert.equal(buf.slice(0, 65536), '0123456789'); -assert.equal(buf.slice(65536, 0), ''); -assert.equal(buf.slice(-5, -8), ''); -assert.equal(buf.slice(-5, -3), '56'); -assert.equal(buf.slice(-10, 10), '0123456789'); -for (let i = 0, s = buf.toString(); i < buf.length; ++i) { - assert.equal(buf.slice(i), s.slice(i)); - assert.equal(buf.slice(0, i), s.slice(0, i)); - assert.equal(buf.slice(-i), s.slice(-i)); - assert.equal(buf.slice(0, -i), s.slice(0, -i)); +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) { + 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))); } const utf16Buf = Buffer.from('0123456789', 'utf16le'); assert.deepStrictEqual(utf16Buf.slice(0, 6), Buffer.from('012', 'utf16le')); -assert.equal(buf.slice('0', '1'), '0'); -assert.equal(buf.slice('-5', '10'), '56789'); -assert.equal(buf.slice('-10', '10'), '0123456789'); -assert.equal(buf.slice('-10', '-5'), '01234'); -assert.equal(buf.slice('-10', '-0'), ''); -assert.equal(buf.slice('111'), ''); -assert.equal(buf.slice('0', '-111'), ''); +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 @@ -57,16 +88,17 @@ assert.strictEqual(Buffer.alloc(0).slice(0, 1).length, 0); { // Single argument slice - assert.strictEqual('bcde', Buffer.from('abcde').slice(1).toString()); + assert.strictEqual('bcde', + Buffer.from('abcde', 'utf8').slice(1).toString('utf8')); } // slice(0,0).length === 0 -assert.strictEqual(0, Buffer.from('hello').slice(0, 0).length); +assert.strictEqual(0, Buffer.from('hello', 'utf8').slice(0, 0).length); { // Regression tests for https://github.com/nodejs/node/issues/9096 - const buf = Buffer.from('abcd'); - assert.strictEqual(buf.slice(buf.length / 3).toString(), 'bcd'); + const buf = Buffer.from('abcd', 'utf8'); + assert.strictEqual(buf.slice(buf.length / 3).toString('utf8'), 'bcd'); assert.strictEqual( buf.slice(buf.length / 3, buf.length).toString(), 'bcd' @@ -74,13 +106,14 @@ assert.strictEqual(0, Buffer.from('hello').slice(0, 0).length); } { - const buf = Buffer.from('abcdefg'); - assert.strictEqual(buf.slice(-(-1 >>> 0) - 1).toString(), buf.toString()); + const buf = Buffer.from('abcdefg', 'utf8'); + assert.strictEqual(buf.slice(-(-1 >>> 0) - 1).toString('utf8'), + buf.toString('utf8')); } { - const buf = Buffer.from('abc'); - assert.strictEqual(buf.slice(-0.5).toString(), buf.toString()); + const buf = Buffer.from('abc', 'utf8'); + assert.strictEqual(buf.slice(-0.5).toString('utf8'), buf.toString('utf8')); } {