From 6447cab7bee8ac6b42586a4e698334eaae50c911 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Tue, 4 May 2021 22:41:55 +0800 Subject: [PATCH] test: improve buffer coverage PR-URL: https://github.com/nodejs/node/pull/38538 Reviewed-By: Michael Dawson Reviewed-By: Khaidi Chu Reviewed-By: Colin Ihrig Reviewed-By: Zijian Liu Reviewed-By: Rich Trott Reviewed-By: James M Snell --- test/parallel/test-buffer-copy.js | 42 ++++++++++++++++++++++++++++ test/parallel/test-buffer-indexof.js | 21 ++++++++------ 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/test/parallel/test-buffer-copy.js b/test/parallel/test-buffer-copy.js index d6f734614471e5..f7ccfff9d0cbbe 100644 --- a/test/parallel/test-buffer-copy.js +++ b/test/parallel/test-buffer-copy.js @@ -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); @@ -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); @@ -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), diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js index b17520366e506a..3ed57b357ce9aa 100644 --- a/test/parallel/test-buffer-indexof.js +++ b/test/parallel/test-buffer-indexof.js @@ -180,15 +180,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 =