From ebc989401854e5b7da366973e9006ca69642dcae Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 6 Aug 2016 16:29:02 -0700 Subject: [PATCH 1/2] assert: Fix deepEqual/deepStrictEqual on equivalent typed arrays The typed array's underlying ArrayBuffer is used in `Buffer.from`. Let's respect it's .byteOffset or .byteLength (i.e. position within the parent ArrayBuffer). Fixes: https://github.com/nodejs/node/issues/8001 --- lib/assert.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index ba316d38ff665f..8a316ffa793d19 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -180,8 +180,12 @@ function _deepEqual(actual, expected, strict, memos) { pToString(actual) === pToString(expected) && !(actual instanceof Float32Array || actual instanceof Float64Array)) { - return compare(Buffer.from(actual.buffer), - Buffer.from(expected.buffer)) === 0; + return compare(Buffer.from(actual.buffer, + actual.byteOffset, + actual.byteLength), + Buffer.from(expected.buffer, + expected.byteOffset, + expected.byteLength)) === 0; // 7.5 For all other Object pairs, including Array objects, equivalence is // determined by having the same number of owned properties (as verified From 9e7e7c3d64fc4a6a5d6c5974e8fbcc103d612826 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 7 Aug 2016 13:50:01 -0700 Subject: [PATCH 2/2] assert: Add deepEqual/deepStrictEqual tests for typed arrays Let's test typed arrays which have a .byteOffset and .byteLength (i.e. typed arrays that are slices of parent typed arrays). --- test/parallel/test-assert-typedarray-deepequal.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-assert-typedarray-deepequal.js b/test/parallel/test-assert-typedarray-deepequal.js index a078e96f8f1e8e..24acd81d6aa6e6 100644 --- a/test/parallel/test-assert-typedarray-deepequal.js +++ b/test/parallel/test-assert-typedarray-deepequal.js @@ -26,7 +26,10 @@ const equalArrayPairs = [ [new Int16Array([256]), new Uint16Array([256])], [new Float32Array([+0.0]), new Float32Array([-0.0])], [new Float64Array([+0.0]), new Float32Array([-0.0])], - [new Float64Array([+0.0]), new Float64Array([-0.0])] + [new Float64Array([+0.0]), new Float64Array([-0.0])], + [new Uint8Array([1, 2, 3, 4]).subarray(1), new Uint8Array([2, 3, 4])], + [new Uint16Array([1, 2, 3, 4]).subarray(1), new Uint16Array([2, 3, 4])], + [new Uint32Array([1, 2, 3, 4]).subarray(1, 3), new Uint32Array([2, 3])] ]; const notEqualArrayPairs = [