aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRuben Bridgewater <ruben@bridgewater.de>2018-02-14 23:42:57 +0100
committerRuben Bridgewater <ruben@bridgewater.de>2018-03-02 02:09:24 +0000
commit177b7314cf62f6c268eef9f71657aba9c93385ed (patch)
treea10e1645ca85ae1a9088280d41f6649bb7659433 /test
parentd3af120f7ae0210ad1b5cc751df10138ea4a4e43 (diff)
downloadandroid-node-v8-177b7314cf62f6c268eef9f71657aba9c93385ed.tar.gz
android-node-v8-177b7314cf62f6c268eef9f71657aba9c93385ed.tar.bz2
android-node-v8-177b7314cf62f6c268eef9f71657aba9c93385ed.zip
buffer: improve Buffer#fill performance
1) This improves the performance for Buffer#fill by using shortcuts. 2) It also ports throwing errors to JS. That way they contain the proper error code. 3) Using negative `end` values will from now on result in an error instead of just doing nothing. 4) Passing in `null` as encoding is from now on accepted as 'utf8'. PR-URL: https://github.com/nodejs/node/pull/18790 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-buffer-fill.js98
-rw-r--r--test/parallel/test-internal-util-normalizeencoding.js13
2 files changed, 29 insertions, 82 deletions
diff --git a/test/parallel/test-buffer-fill.js b/test/parallel/test-buffer-fill.js
index ed1be93ac5..bab1279680 100644
--- a/test/parallel/test-buffer-fill.js
+++ b/test/parallel/test-buffer-fill.js
@@ -19,16 +19,12 @@ testBufs('\u0222aa', 2);
testBufs('\u0222aa', 8);
testBufs('a\u0234b\u0235c\u0236', 4);
testBufs('a\u0234b\u0235c\u0236', 12);
-testBufs('abc', 4, -1);
testBufs('abc', 4, 1);
testBufs('abc', 5, 1);
-testBufs('\u0222aa', 2, -1);
testBufs('\u0222aa', 8, 1);
-testBufs('a\u0234b\u0235c\u0236', 4, -1);
testBufs('a\u0234b\u0235c\u0236', 4, 1);
testBufs('a\u0234b\u0235c\u0236', 12, 1);
-
// UTF8
testBufs('abc', 'utf8');
testBufs('\u0222aa', 'utf8');
@@ -40,17 +36,13 @@ testBufs('\u0222aa', 2, 'utf8');
testBufs('\u0222aa', 8, 'utf8');
testBufs('a\u0234b\u0235c\u0236', 4, 'utf8');
testBufs('a\u0234b\u0235c\u0236', 12, 'utf8');
-testBufs('abc', 4, -1, 'utf8');
testBufs('abc', 4, 1, 'utf8');
testBufs('abc', 5, 1, 'utf8');
-testBufs('\u0222aa', 2, -1, 'utf8');
testBufs('\u0222aa', 8, 1, 'utf8');
-testBufs('a\u0234b\u0235c\u0236', 4, -1, 'utf8');
testBufs('a\u0234b\u0235c\u0236', 4, 1, 'utf8');
testBufs('a\u0234b\u0235c\u0236', 12, 1, 'utf8');
assert.strictEqual(Buffer.allocUnsafe(1).fill(0).fill('\u0222')[0], 0xc8);
-
// BINARY
testBufs('abc', 'binary');
testBufs('\u0222aa', 'binary');
@@ -62,16 +54,12 @@ testBufs('\u0222aa', 2, 'binary');
testBufs('\u0222aa', 8, 'binary');
testBufs('a\u0234b\u0235c\u0236', 4, 'binary');
testBufs('a\u0234b\u0235c\u0236', 12, 'binary');
-testBufs('abc', 4, -1, 'binary');
testBufs('abc', 4, 1, 'binary');
testBufs('abc', 5, 1, 'binary');
-testBufs('\u0222aa', 2, -1, 'binary');
testBufs('\u0222aa', 8, 1, 'binary');
-testBufs('a\u0234b\u0235c\u0236', 4, -1, 'binary');
testBufs('a\u0234b\u0235c\u0236', 4, 1, 'binary');
testBufs('a\u0234b\u0235c\u0236', 12, 1, 'binary');
-
// LATIN1
testBufs('abc', 'latin1');
testBufs('\u0222aa', 'latin1');
@@ -83,16 +71,12 @@ testBufs('\u0222aa', 2, 'latin1');
testBufs('\u0222aa', 8, 'latin1');
testBufs('a\u0234b\u0235c\u0236', 4, 'latin1');
testBufs('a\u0234b\u0235c\u0236', 12, 'latin1');
-testBufs('abc', 4, -1, 'latin1');
testBufs('abc', 4, 1, 'latin1');
testBufs('abc', 5, 1, 'latin1');
-testBufs('\u0222aa', 2, -1, 'latin1');
testBufs('\u0222aa', 8, 1, 'latin1');
-testBufs('a\u0234b\u0235c\u0236', 4, -1, 'latin1');
testBufs('a\u0234b\u0235c\u0236', 4, 1, 'latin1');
testBufs('a\u0234b\u0235c\u0236', 12, 1, 'latin1');
-
// UCS2
testBufs('abc', 'ucs2');
testBufs('\u0222aa', 'ucs2');
@@ -103,17 +87,13 @@ testBufs('\u0222aa', 2, 'ucs2');
testBufs('\u0222aa', 8, 'ucs2');
testBufs('a\u0234b\u0235c\u0236', 4, 'ucs2');
testBufs('a\u0234b\u0235c\u0236', 12, 'ucs2');
-testBufs('abc', 4, -1, 'ucs2');
testBufs('abc', 4, 1, 'ucs2');
testBufs('abc', 5, 1, 'ucs2');
-testBufs('\u0222aa', 2, -1, 'ucs2');
testBufs('\u0222aa', 8, 1, 'ucs2');
-testBufs('a\u0234b\u0235c\u0236', 4, -1, 'ucs2');
testBufs('a\u0234b\u0235c\u0236', 4, 1, 'ucs2');
testBufs('a\u0234b\u0235c\u0236', 12, 1, 'ucs2');
assert.strictEqual(Buffer.allocUnsafe(1).fill('\u0222', 'ucs2')[0], 0x22);
-
// HEX
testBufs('616263', 'hex');
testBufs('c8a26161', 'hex');
@@ -125,12 +105,9 @@ testBufs('c8a26161', 2, 'hex');
testBufs('c8a26161', 8, 'hex');
testBufs('61c8b462c8b563c8b6', 4, 'hex');
testBufs('61c8b462c8b563c8b6', 12, 'hex');
-testBufs('616263', 4, -1, 'hex');
testBufs('616263', 4, 1, 'hex');
testBufs('616263', 5, 1, 'hex');
-testBufs('c8a26161', 2, -1, 'hex');
testBufs('c8a26161', 8, 1, 'hex');
-testBufs('61c8b462c8b563c8b6', 4, -1, 'hex');
testBufs('61c8b462c8b563c8b6', 4, 1, 'hex');
testBufs('61c8b462c8b563c8b6', 12, 1, 'hex');
@@ -162,16 +139,12 @@ testBufs('yKJhYQ==', 2, 'ucs2');
testBufs('yKJhYQ==', 8, 'ucs2');
testBufs('Yci0Ysi1Y8i2', 4, 'ucs2');
testBufs('Yci0Ysi1Y8i2', 12, 'ucs2');
-testBufs('YWJj', 4, -1, 'ucs2');
testBufs('YWJj', 4, 1, 'ucs2');
testBufs('YWJj', 5, 1, 'ucs2');
-testBufs('yKJhYQ==', 2, -1, 'ucs2');
testBufs('yKJhYQ==', 8, 1, 'ucs2');
-testBufs('Yci0Ysi1Y8i2', 4, -1, 'ucs2');
testBufs('Yci0Ysi1Y8i2', 4, 1, 'ucs2');
testBufs('Yci0Ysi1Y8i2', 12, 1, 'ucs2');
-
// Buffer
function deepStrictEqualValues(buf, arr) {
for (const [index, value] of buf.entries()) {
@@ -179,27 +152,24 @@ function deepStrictEqualValues(buf, arr) {
}
}
-
const buf2Fill = Buffer.allocUnsafe(1).fill(2);
deepStrictEqualValues(genBuffer(4, [buf2Fill]), [2, 2, 2, 2]);
deepStrictEqualValues(genBuffer(4, [buf2Fill, 1]), [0, 2, 2, 2]);
deepStrictEqualValues(genBuffer(4, [buf2Fill, 1, 3]), [0, 2, 2, 0]);
deepStrictEqualValues(genBuffer(4, [buf2Fill, 1, 1]), [0, 0, 0, 0]);
-deepStrictEqualValues(genBuffer(4, [buf2Fill, 1, -1]), [0, 0, 0, 0]);
const hexBufFill = Buffer.allocUnsafe(2).fill(0).fill('0102', 'hex');
deepStrictEqualValues(genBuffer(4, [hexBufFill]), [1, 2, 1, 2]);
deepStrictEqualValues(genBuffer(4, [hexBufFill, 1]), [0, 1, 2, 1]);
deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, 3]), [0, 1, 2, 0]);
deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, 1]), [0, 0, 0, 0]);
-deepStrictEqualValues(genBuffer(4, [hexBufFill, 1, -1]), [0, 0, 0, 0]);
-
// Check exceptions
[
[0, -1],
[0, 0, buf1.length + 1],
['', -1],
- ['', 0, buf1.length + 1]
+ ['', 0, buf1.length + 1],
+ ['', 1, -1],
].forEach((args) => {
common.expectsError(
() => buf1.fill(...args),
@@ -218,7 +188,7 @@ common.expectsError(
[
['a', 0, 0, NaN],
- ['a', 0, 0, null]
+ ['a', 0, 0, false]
].forEach((args) => {
common.expectsError(
() => buf1.fill(...args),
@@ -356,42 +326,10 @@ Buffer.alloc(8, '');
assert.strictEqual(buf.toString(), 'էէէէէ');
}
-// Testing public API. Make sure "start" is properly checked, even if it's
-// magically mangled using Symbol.toPrimitive.
-{
- let elseWasLast = false;
- common.expectsError(() => {
- let ctr = 0;
- const start = {
- [Symbol.toPrimitive]() {
- // We use this condition to get around the check in lib/buffer.js
- if (ctr <= 0) {
- elseWasLast = false;
- ctr = ctr + 1;
- return 0;
- } else {
- elseWasLast = true;
- // Once buffer.js calls the C++ implementation of fill, return -1
- return -1;
- }
- }
- };
- Buffer.alloc(1).fill(Buffer.alloc(1), start, 1);
- }, {
- code: undefined, type: RangeError, message: 'Index out of range'
- });
- // Make sure -1 is making it to Buffer::Fill().
- assert.ok(elseWasLast,
- 'internal API changed, -1 no longer in correct location');
-}
-
// Testing process.binding. Make sure "start" is properly checked for -1 wrap
// around.
-common.expectsError(() => {
- process.binding('buffer').fill(Buffer.alloc(1), 1, -1, 0, 1);
-}, {
- code: undefined, type: RangeError, message: 'Index out of range'
-});
+assert.strictEqual(
+ process.binding('buffer').fill(Buffer.alloc(1), 1, -1, 0, 1), -2);
// Make sure "end" is properly checked, even if it's magically mangled using
// Symbol.toPrimitive.
@@ -402,20 +340,21 @@ common.expectsError(() => {
const end = {
[Symbol.toPrimitive]() {
// We use this condition to get around the check in lib/buffer.js
- if (ctr <= 1) {
+ if (ctr === 0) {
elseWasLast = false;
- ctr = ctr + 1;
+ ctr++;
return 1;
- } else {
- elseWasLast = true;
- // Once buffer.js calls the C++ implementation of fill, return -1
- return -1;
}
+ elseWasLast = true;
+ // Once buffer.js calls the C++ implementation of fill, return -1
+ return -1;
}
};
Buffer.alloc(1).fill(Buffer.alloc(1), 0, end);
}, {
- code: undefined, type: RangeError, message: 'Index out of range'
+ code: 'ERR_INDEX_OUT_OF_RANGE',
+ type: RangeError,
+ message: 'Index out of range'
});
// Make sure -1 is making it to Buffer::Fill().
assert.ok(elseWasLast,
@@ -424,9 +363,8 @@ common.expectsError(() => {
// Testing process.binding. Make sure "end" is properly checked for -1 wrap
// around.
-common.expectsError(() => {
- process.binding('buffer').fill(Buffer.alloc(1), 1, 1, -2, 1);
-}, { code: undefined, type: RangeError, message: 'Index out of range' });
+assert.strictEqual(
+ process.binding('buffer').fill(Buffer.alloc(1), 1, 1, -2, 1), -2);
// Test that bypassing 'length' won't cause an abort.
common.expectsError(() => {
@@ -436,7 +374,11 @@ common.expectsError(() => {
enumerable: true
});
buf.fill('');
-}, { code: undefined, type: RangeError, message: 'Index out of range' });
+}, {
+ code: 'ERR_INDEX_OUT_OF_RANGE',
+ type: RangeError,
+ message: 'Index out of range'
+});
assert.deepStrictEqual(
Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'),
diff --git a/test/parallel/test-internal-util-normalizeencoding.js b/test/parallel/test-internal-util-normalizeencoding.js
index 9adc3753e8..081255cef0 100644
--- a/test/parallel/test-internal-util-normalizeencoding.js
+++ b/test/parallel/test-internal-util-normalizeencoding.js
@@ -6,6 +6,8 @@ const assert = require('assert');
const util = require('internal/util');
const tests = [
+ [undefined, 'utf8'],
+ [null, 'utf8'],
['', 'utf8'],
['utf8', 'utf8'],
['utf-8', 'utf8'],
@@ -23,17 +25,20 @@ const tests = [
['binary', 'latin1'],
['BINARY', 'latin1'],
['latin1', 'latin1'],
+ ['LaTiN1', 'latin1'],
['base64', 'base64'],
['BASE64', 'base64'],
['hex', 'hex'],
['HEX', 'hex'],
['foo', undefined],
[1, undefined],
- [false, 'utf8'],
- [undefined, 'utf8'],
+ [false, undefined],
+ [NaN, undefined],
+ [0, undefined],
[[], undefined],
];
-tests.forEach((i) => {
- assert.strictEqual(util.normalizeEncoding(i[0]), i[1]);
+tests.forEach((e, i) => {
+ const res = util.normalizeEncoding(e[0]);
+ assert.strictEqual(res, e[1], `#${i} failed: expected ${e[1]}, got ${res}`);
});