Skip to content

Commit

Permalink
test,benchmark: use deepStrictEqual()
Browse files Browse the repository at this point in the history
In preparation for a lint rule that will enforce
assert.deepStrictEqual() over assert.deepEqual(), change tests and
benchmarks accordingly. For tests and benchmarks that are testing or
benchmarking assert.deepEqual() itself, apply a comment to ignore the
upcoming rule.

PR-URL: nodejs#6213
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
Trott committed Apr 22, 2016
1 parent 5eb4ec0 commit a7335bd
Show file tree
Hide file tree
Showing 109 changed files with 474 additions and 378 deletions.
1 change: 1 addition & 0 deletions benchmark/assert/deepequal-prims-and-objs-big-array.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint no-deepEqual: 0 */
'use strict';
var common = require('../common.js');
var assert = require('assert');
Expand Down
1 change: 1 addition & 0 deletions benchmark/assert/deepequal-prims-and-objs-big-loop.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint no-deepEqual: 0 */
'use strict';
var common = require('../common.js');
var assert = require('assert');
Expand Down
1 change: 1 addition & 0 deletions benchmark/assert/deepequal-typedarrays.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint no-deepEqual: 0 */
'use strict';
var common = require('../common.js');
var assert = require('assert');
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-assert-typedarray-deepequal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint no-deepEqual: 0 */
'use strict';

require('../common');
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint no-deepEqual: 0 */
'use strict';
require('../common');
var assert = require('assert');
Expand Down
70 changes: 36 additions & 34 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ assert.strictEqual(0, d.length);

var ui32 = new Uint32Array(4).fill(42);
var e = Buffer.from(ui32);
assert.deepEqual(ui32, e);
for (const [index, value] of e.entries()) {
assert.strictEqual(value, ui32[index]);
}

// First check Buffer#fill() works as expected.

Expand Down Expand Up @@ -463,32 +465,32 @@ for (let i = 0; i < Buffer.byteLength(utf8String); i++) {
assert.equal(d[0], 23);
assert.equal(d[1], 42);
assert.equal(d[2], 255);
assert.deepEqual(d, Buffer.from(d));
assert.deepStrictEqual(d, Buffer.from(d));
}

{
const e = Buffer.from('über');
console.error('uber: \'%s\'', e.toString());
assert.deepEqual(e, Buffer.from([195, 188, 98, 101, 114]));
assert.deepStrictEqual(e, Buffer.from([195, 188, 98, 101, 114]));
}

{
const f = Buffer.from('über', 'ascii');
console.error('f.length: %d (should be 4)', f.length);
assert.deepEqual(f, Buffer.from([252, 98, 101, 114]));
assert.deepStrictEqual(f, Buffer.from([252, 98, 101, 114]));
}

['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) {
{
const f = Buffer.from('über', encoding);
console.error('f.length: %d (should be 8)', f.length);
assert.deepEqual(f, Buffer.from([252, 0, 98, 0, 101, 0, 114, 0]));
assert.deepStrictEqual(f, Buffer.from([252, 0, 98, 0, 101, 0, 114, 0]));
}

{
const f = Buffer.from('привет', encoding);
console.error('f.length: %d (should be 12)', f.length);
assert.deepEqual(f,
assert.deepStrictEqual(f,
Buffer.from([63, 4, 64, 4, 56, 4, 50, 4, 53, 4, 66, 4]));
assert.equal(f.toString(encoding), 'привет');
}
Expand All @@ -499,23 +501,23 @@ for (let i = 0; i < Buffer.byteLength(utf8String); i++) {
const size = f.write('あいうえお', encoding);
console.error('bytes written to buffer: %d (should be 4)', size);
assert.equal(size, 4);
assert.deepEqual(f, Buffer.from([0x42, 0x30, 0x44, 0x30, 0x00]));
assert.deepStrictEqual(f, Buffer.from([0x42, 0x30, 0x44, 0x30, 0x00]));
}
});

{
const f = Buffer.from('\uD83D\uDC4D', 'utf-16le'); // THUMBS UP SIGN (U+1F44D)
assert.equal(f.length, 4);
assert.deepEqual(f, Buffer.from('3DD84DDC', 'hex'));
assert.deepStrictEqual(f, Buffer.from('3DD84DDC', 'hex'));
}


var arrayIsh = {0: 0, 1: 1, 2: 2, 3: 3, length: 4};
var g = Buffer.from(arrayIsh);
assert.deepEqual(g, Buffer.from([0, 1, 2, 3]));
assert.deepStrictEqual(g, Buffer.from([0, 1, 2, 3]));
var strArrayIsh = {0: '0', 1: '1', 2: '2', 3: '3', length: 4};
g = Buffer.from(strArrayIsh);
assert.deepEqual(g, Buffer.from([0, 1, 2, 3]));
assert.deepStrictEqual(g, Buffer.from([0, 1, 2, 3]));


//
Expand All @@ -526,10 +528,10 @@ assert.equal('TWFu', (Buffer.from('Man')).toString('base64'));
{
// test that regular and URL-safe base64 both work
const expected = [0xff, 0xff, 0xbe, 0xff, 0xef, 0xbf, 0xfb, 0xef, 0xff];
assert.deepEqual(Buffer.from('//++/++/++//', 'base64'),
Buffer.from(expected));
assert.deepEqual(Buffer.from('__--_--_--__', 'base64'),
Buffer.from(expected));
assert.deepStrictEqual(Buffer.from('//++/++/++//', 'base64'),
Buffer.from(expected));
assert.deepStrictEqual(Buffer.from('__--_--_--__', 'base64'),
Buffer.from(expected));
}

{
Expand Down Expand Up @@ -1021,7 +1023,7 @@ Buffer.from(Buffer.allocUnsafe(0), 0, 0);

assert.equal(string, '{"type":"Buffer","data":[116,101,115,116]}');

assert.deepEqual(buffer, JSON.parse(string, function(key, value) {
assert.deepStrictEqual(buffer, JSON.parse(string, function(key, value) {
return value && value.type === 'Buffer'
? Buffer.from(value.data)
: value;
Expand Down Expand Up @@ -1167,92 +1169,92 @@ assert.throws(function() {
(function() {
var buf = Buffer.allocUnsafe(3);
buf.writeUIntLE(0x123456, 0, 3);
assert.deepEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
assert.equal(buf.readUIntLE(0, 3), 0x123456);

buf = Buffer.allocUnsafe(3);
buf.writeUIntBE(0x123456, 0, 3);
assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);
assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);
assert.equal(buf.readUIntBE(0, 3), 0x123456);

buf = Buffer.allocUnsafe(3);
buf.writeIntLE(0x123456, 0, 3);
assert.deepEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
assert.deepStrictEqual(buf.toJSON().data, [0x56, 0x34, 0x12]);
assert.equal(buf.readIntLE(0, 3), 0x123456);

buf = Buffer.allocUnsafe(3);
buf.writeIntBE(0x123456, 0, 3);
assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);
assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56]);
assert.equal(buf.readIntBE(0, 3), 0x123456);

buf = Buffer.allocUnsafe(3);
buf.writeIntLE(-0x123456, 0, 3);
assert.deepEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]);
assert.deepStrictEqual(buf.toJSON().data, [0xaa, 0xcb, 0xed]);
assert.equal(buf.readIntLE(0, 3), -0x123456);

buf = Buffer.allocUnsafe(3);
buf.writeIntBE(-0x123456, 0, 3);
assert.deepEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]);
assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xaa]);
assert.equal(buf.readIntBE(0, 3), -0x123456);

buf = Buffer.allocUnsafe(3);
buf.writeIntLE(-0x123400, 0, 3);
assert.deepEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]);
assert.deepStrictEqual(buf.toJSON().data, [0x00, 0xcc, 0xed]);
assert.equal(buf.readIntLE(0, 3), -0x123400);

buf = Buffer.allocUnsafe(3);
buf.writeIntBE(-0x123400, 0, 3);
assert.deepEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]);
assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcc, 0x00]);
assert.equal(buf.readIntBE(0, 3), -0x123400);

buf = Buffer.allocUnsafe(3);
buf.writeIntLE(-0x120000, 0, 3);
assert.deepEqual(buf.toJSON().data, [0x00, 0x00, 0xee]);
assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0xee]);
assert.equal(buf.readIntLE(0, 3), -0x120000);

buf = Buffer.allocUnsafe(3);
buf.writeIntBE(-0x120000, 0, 3);
assert.deepEqual(buf.toJSON().data, [0xee, 0x00, 0x00]);
assert.deepStrictEqual(buf.toJSON().data, [0xee, 0x00, 0x00]);
assert.equal(buf.readIntBE(0, 3), -0x120000);

buf = Buffer.allocUnsafe(5);
buf.writeUIntLE(0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);
assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);
assert.equal(buf.readUIntLE(0, 5), 0x1234567890);

buf = Buffer.allocUnsafe(5);
buf.writeUIntBE(0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);
assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);
assert.equal(buf.readUIntBE(0, 5), 0x1234567890);

buf = Buffer.allocUnsafe(5);
buf.writeIntLE(0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);
assert.deepStrictEqual(buf.toJSON().data, [0x90, 0x78, 0x56, 0x34, 0x12]);
assert.equal(buf.readIntLE(0, 5), 0x1234567890);

buf = Buffer.allocUnsafe(5);
buf.writeIntBE(0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);
assert.deepStrictEqual(buf.toJSON().data, [0x12, 0x34, 0x56, 0x78, 0x90]);
assert.equal(buf.readIntBE(0, 5), 0x1234567890);

buf = Buffer.allocUnsafe(5);
buf.writeIntLE(-0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]);
assert.deepStrictEqual(buf.toJSON().data, [0x70, 0x87, 0xa9, 0xcb, 0xed]);
assert.equal(buf.readIntLE(0, 5), -0x1234567890);

buf = Buffer.allocUnsafe(5);
buf.writeIntBE(-0x1234567890, 0, 5);
assert.deepEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]);
assert.deepStrictEqual(buf.toJSON().data, [0xed, 0xcb, 0xa9, 0x87, 0x70]);
assert.equal(buf.readIntBE(0, 5), -0x1234567890);

buf = Buffer.allocUnsafe(5);
buf.writeIntLE(-0x0012000000, 0, 5);
assert.deepEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]);
assert.deepStrictEqual(buf.toJSON().data, [0x00, 0x00, 0x00, 0xee, 0xff]);
assert.equal(buf.readIntLE(0, 5), -0x0012000000);

buf = Buffer.allocUnsafe(5);
buf.writeIntBE(-0x0012000000, 0, 5);
assert.deepEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]);
assert.deepStrictEqual(buf.toJSON().data, [0xff, 0xee, 0x00, 0x00, 0x00]);
assert.equal(buf.readIntBE(0, 5), -0x0012000000);
})();

Expand Down Expand Up @@ -1292,7 +1294,7 @@ assert.throws(function() {
}

var utf16Buf = Buffer.from('0123456789', 'utf16le');
assert.deepEqual(utf16Buf.slice(0, 6), Buffer.from('012', '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');
Expand Down
27 changes: 17 additions & 10 deletions test/parallel/test-buffer-fill.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,25 @@ testBufs('Yci0Ysi1Y8i2', 12, 1, 'ucs2');


// Buffer
function deepStrictEqualValues(buf, arr) {
for (const [index, value] of buf.entries()) {
assert.deepStrictEqual(value, arr[index]);
}
}


const buf2Fill = Buffer.allocUnsafe(1).fill(2);
assert.deepEqual(genBuffer(4, [buf2Fill]), [2, 2, 2, 2]);
assert.deepEqual(genBuffer(4, [buf2Fill, 1]), [0, 2, 2, 2]);
assert.deepEqual(genBuffer(4, [buf2Fill, 1, 3]), [0, 2, 2, 0]);
assert.deepEqual(genBuffer(4, [buf2Fill, 1, 1]), [0, 0, 0, 0]);
assert.deepEqual(genBuffer(4, [buf2Fill, 1, -1]), [0, 0, 0, 0]);
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');
assert.deepEqual(genBuffer(4, [hexBufFill]), [1, 2, 1, 2]);
assert.deepEqual(genBuffer(4, [hexBufFill, 1]), [0, 1, 2, 1]);
assert.deepEqual(genBuffer(4, [hexBufFill, 1, 3]), [0, 1, 2, 0]);
assert.deepEqual(genBuffer(4, [hexBufFill, 1, 1]), [0, 0, 0, 0]);
assert.deepEqual(genBuffer(4, [hexBufFill, 1, -1]), [0, 0, 0, 0]);
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
Expand Down
10 changes: 5 additions & 5 deletions test/parallel/test-buffer-iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ arr = [];
for (b of buffer)
arr.push(b);

assert.deepEqual(arr, [1, 2, 3, 4, 5]);
assert.deepStrictEqual(arr, [1, 2, 3, 4, 5]);


// buffer iterators should be iterable
Expand All @@ -23,7 +23,7 @@ arr = [];
for (b of buffer[Symbol.iterator]())
arr.push(b);

assert.deepEqual(arr, [1, 2, 3, 4, 5]);
assert.deepStrictEqual(arr, [1, 2, 3, 4, 5]);


// buffer#values() should return iterator for values
Expand All @@ -33,7 +33,7 @@ arr = [];
for (b of buffer.values())
arr.push(b);

assert.deepEqual(arr, [1, 2, 3, 4, 5]);
assert.deepStrictEqual(arr, [1, 2, 3, 4, 5]);


// buffer#keys() should return iterator for keys
Expand All @@ -43,7 +43,7 @@ arr = [];
for (b of buffer.keys())
arr.push(b);

assert.deepEqual(arr, [0, 1, 2, 3, 4]);
assert.deepStrictEqual(arr, [0, 1, 2, 3, 4]);


// buffer#entries() should return iterator for entries
Expand All @@ -53,7 +53,7 @@ arr = [];
for (b of buffer.entries())
arr.push(b);

assert.deepEqual(arr, [
assert.deepStrictEqual(arr, [
[0, 1],
[1, 2],
[2, 3],
Expand Down
8 changes: 6 additions & 2 deletions test/parallel/test-buffer-slow.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ let sb = SlowBuffer(4);
assert(sb instanceof Buffer);
assert.strictEqual(sb.length, 4);
sb.fill(1);
assert.deepEqual(sb, ones);
for (const [key, value] of sb.entries()) {
assert.deepStrictEqual(value, ones[key]);
}

// underlying ArrayBuffer should have the same length
assert.strictEqual(sb.buffer.byteLength, 4);
Expand All @@ -23,7 +25,9 @@ sb = SlowBuffer(4);
assert(sb instanceof Buffer);
assert.strictEqual(sb.length, 4);
sb.fill(1);
assert.deepEqual(sb, ones);
for (const [key, value] of sb.entries()) {
assert.deepStrictEqual(value, ones[key]);
}

// should work with edge cases
assert.strictEqual(SlowBuffer(0).length, 0);
Expand Down
Loading

0 comments on commit a7335bd

Please sign in to comment.