From 72d70f9a5cd9548c6ee56be9e8f06d5eb8988fa8 Mon Sep 17 00:00:00 2001 From: Zahidul Islam Date: Sat, 22 Apr 2017 11:07:45 -0700 Subject: [PATCH] refactoring: use block scope in test-buffer-swap.js --- test/parallel/test-buffer-swap.js | 204 ++++++++++++++++-------------- 1 file changed, 109 insertions(+), 95 deletions(-) diff --git a/test/parallel/test-buffer-swap.js b/test/parallel/test-buffer-swap.js index bcb636edb1733f..850105cd423b37 100644 --- a/test/parallel/test-buffer-swap.js +++ b/test/parallel/test-buffer-swap.js @@ -3,6 +3,11 @@ require('../common'); const assert = require('assert'); +// Length assertions +const re16 = /Buffer size must be a multiple of 16-bits/; +const re32 = /Buffer size must be a multiple of 32-bits/; +const re64 = /Buffer size must be a multiple of 64-bits/; + // Test buffers small enough to use the JS implementation const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]); @@ -25,114 +30,123 @@ assert.deepStrictEqual(buf, Buffer.from([0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x0c, 0x0b, 0x0a, 0x09])); // Operates in-place -const buf3 = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7]); -buf3.slice(1, 5).swap32(); -assert.deepStrictEqual(buf3, Buffer.from([0x1, 0x5, 0x4, 0x3, 0x2, 0x6, 0x7])); - -buf3.slice(1, 5).swap16(); -assert.deepStrictEqual(buf3, Buffer.from([0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7])); - -const buf3_64 = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, - 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]); -buf3_64.slice(2, 18).swap64(); -assert.deepStrictEqual(buf3_64, Buffer.from([0x01, 0x02, 0x0a, 0x09, 0x08, 0x07, - 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, - 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, - 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, - 0x0f, 0x10])); - -// Force use of native code (Buffer size above threshold limit for js impl) -const buf4A = new Uint32Array(256).fill(0x04030201); -const buf4 = Buffer.from(buf4A.buffer, buf4A.byteOffset); -const buf5A = new Uint32Array(256).fill(0x03040102); -const buf5 = Buffer.from(buf5A.buffer, buf5A.byteOffset); - -buf4.swap16(); -assert.deepStrictEqual(buf4, buf5); - -const buf6A = new Uint32Array(256).fill(0x04030201); -const buf6 = Buffer.from(buf6A.buffer); -const bu7A = new Uint32Array(256).fill(0x01020304); -const buf7 = Buffer.from(bu7A.buffer, bu7A.byteOffset); - -buf6.swap32(); -assert.deepStrictEqual(buf6, buf7); - -const buf8A = new Uint8Array(256 * 8); -const buf9A = new Uint8Array(256 * 8); -for (let i = 0; i < buf8A.length; i++) { - buf8A[i] = i % 8; - buf9A[buf9A.length - i - 1] = i % 8; +{ + const buf = Buffer.from([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7]); + buf.slice(1, 5).swap32(); + assert.deepStrictEqual(buf, Buffer.from([0x1, 0x5, 0x4, 0x3, 0x2, 0x6, 0x7])); + + buf.slice(1, 5).swap16(); + assert.deepStrictEqual(buf, Buffer.from([0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7])); + assert.throws(() => Buffer.from(buf).swap16(), re16); + assert.throws(() => Buffer.from(buf).swap32(), re32); + assert.throws(() => buf.slice(1, 3).swap32(), re32); + assert.throws(() => buf.slice(1, 3).swap64(), re64); } -const buf8 = Buffer.from(buf8A.buffer, buf8A.byteOffset); -const buf9 = Buffer.from(buf9A.buffer, buf9A.byteOffset); - -buf8.swap64(); -assert.deepStrictEqual(buf8, buf9); -// Test native code with buffers that are not memory-aligned -const buf10A = new Uint8Array(256 * 8); -const buf11A = new Uint8Array(256 * 8 - 2); -for (let i = 0; i < buf10A.length; i++) { - buf10A[i] = i % 2; +{ + const buf = Buffer.from([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10]); + buf.slice(2, 18).swap64(); + assert.deepStrictEqual(buf, Buffer.from([0x01, 0x02, 0x0a, 0x09, 0x08, 0x07, + 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, + 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, + 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, + 0x0f, 0x10])); } -for (let i = 1; i < buf11A.length; i++) { - buf11A[buf11A.length - i] = (i + 1) % 2; -} -const buf10 = Buffer.from(buf10A.buffer, buf10A.byteOffset); -// 0|1 0|1 0|1... -const buf11 = Buffer.from(buf11A.buffer, buf11A.byteOffset); -// 0|0 1|0 1|0... - -buf10.slice(1, buf10.length - 1).swap16(); -assert.deepStrictEqual(buf10.slice(0, buf11.length), buf11); - -const buf12A = new Uint8Array(256 * 8); -const buf13A = new Uint8Array(256 * 8 - 4); -for (let i = 0; i < buf12A.length; i++) { - buf12A[i] = i % 4; -} -for (let i = 1; i < buf13A.length; i++) { - buf13A[buf13A.length - i] = (i + 1) % 4; +// Force use of native code (Buffer size above threshold limit for js impl) +{ + const buf = new Uint32Array(256).fill(0x04030201); + const actual = Buffer.from(buf.buffer, buf.byteOffset); + const buf1 = new Uint32Array(256).fill(0x03040102); + const expected = Buffer.from(buf1.buffer, buf1.byteOffset); + + actual.swap16(); + assert.deepStrictEqual(actual, expected); } -const buf12 = Buffer.from(buf12A.buffer, buf12A.byteOffset); -// 0|1 2 3 0|1 2 3... -const buf13 = Buffer.from(buf13A.buffer, buf13A.byteOffset); -// 0|0 3 2 1|0 3 2... -buf12.slice(1, buf12.length - 3).swap32(); -assert.deepStrictEqual(buf12.slice(0, buf13.length), buf13); +{ + const buf = new Uint32Array(256).fill(0x04030201); + const actual = Buffer.from(buf.buffer); + const buf1 = new Uint32Array(256).fill(0x01020304); + const expected = Buffer.from(buf1.buffer, buf1.byteOffset); + actual.swap32(); + assert.deepStrictEqual(actual, expected); +} -const buf14A = new Uint8Array(256 * 8); -const buf15A = new Uint8Array(256 * 8 - 8); -for (let i = 0; i < buf14A.length; i++) { - buf14A[i] = i % 8; +{ + const buf = new Uint8Array(256 * 8); + const buf1 = new Uint8Array(256 * 8); + for (let i = 0; i < buf.length; i++) { + buf[i] = i % 8; + buf1[buf1.length - i - 1] = i % 8; + } + const actual = Buffer.from(buf.buffer, buf.byteOffset); + const expected = Buffer.from(buf1.buffer, buf1.byteOffset); + + actual.swap64(); + assert.deepStrictEqual(actual, expected); } -for (let i = 1; i < buf15A.length; i++) { - buf15A[buf15A.length - i] = (i + 1) % 8; + +// Test native code with buffers that are not memory-aligned +{ + const buf = new Uint8Array(256 * 8); + const buf1 = new Uint8Array(256 * 8 - 2); + for (let i = 0; i < buf.length; i++) { + buf[i] = i % 2; + } + for (let i = 1; i < buf1.length; i++) { + buf1[buf1.length - i] = (i + 1) % 2; + } + const buf2 = Buffer.from(buf.buffer, buf.byteOffset); + // 0|1 0|1 0|1... + const expected = Buffer.from(buf1.buffer, buf1.byteOffset); + // 0|0 1|0 1|0... + + buf2.slice(1, buf2.length - 1).swap16(); + assert.deepStrictEqual(buf2.slice(0, expected.length), expected); } -const buf14 = Buffer.from(buf14A.buffer, buf14A.byteOffset); -// 0|1 2 3 4 5 6 7 0|1 2 3 4... -const buf15 = Buffer.from(buf15A.buffer, buf15A.byteOffset); -// 0|0 7 6 5 4 3 2 1|0 7 6 5... -buf14.slice(1, buf14.length - 7).swap64(); -assert.deepStrictEqual(buf14.slice(0, buf15.length), buf15); +{ + const buf = new Uint8Array(256 * 8); + const buf1 = new Uint8Array(256 * 8 - 4); + for (let i = 0; i < buf.length; i++) { + buf[i] = i % 4; + } + for (let i = 1; i < buf1.length; i++) { + buf1[buf1.length - i] = (i + 1) % 4; + } + const buf2 = Buffer.from(buf.buffer, buf.byteOffset); + // 0|1 2 3 0|1 2 3... + const expected = Buffer.from(buf1.buffer, buf1.byteOffset); + // 0|0 3 2 1|0 3 2... + + buf2.slice(1, buf2.length - 3).swap32(); + assert.deepStrictEqual(buf2.slice(0, expected.length), expected); +} -// Length assertions -const re16 = /Buffer size must be a multiple of 16-bits/; -const re32 = /Buffer size must be a multiple of 32-bits/; -const re64 = /Buffer size must be a multiple of 64-bits/; +{ + const buf = new Uint8Array(256 * 8); + const buf1 = new Uint8Array(256 * 8 - 8); + for (let i = 0; i < buf.length; i++) { + buf[i] = i % 8; + } + for (let i = 1; i < buf1.length; i++) { + buf1[buf1.length - i] = (i + 1) % 8; + } + const buf2 = Buffer.from(buf.buffer, buf.byteOffset); + // 0|1 2 3 4 5 6 7 0|1 2 3 4... + const expected = Buffer.from(buf1.buffer, buf1.byteOffset); + // 0|0 7 6 5 4 3 2 1|0 7 6 5... + + buf2.slice(1, buf2.length - 7).swap64(); + assert.deepStrictEqual(buf2.slice(0, expected.length), expected); +} -assert.throws(() => Buffer.from(buf3).swap16(), re16); assert.throws(() => Buffer.alloc(1025).swap16(), re16); -assert.throws(() => Buffer.from(buf3).swap32(), re32); -assert.throws(() => buf3.slice(1, 3).swap32(), re32); assert.throws(() => Buffer.alloc(1025).swap32(), re32); -assert.throws(() => buf3.slice(1, 3).swap64(), re64); assert.throws(() => Buffer.alloc(1025).swap64(), re64);