Skip to content

Commit

Permalink
Javascript unsigned signed typed arrays return same hash (#114)
Browse files Browse the repository at this point in the history
* Created failing test

* Fixed bug
  • Loading branch information
ArtiomTr authored Feb 18, 2022
1 parent 5e82c25 commit 04db18b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,23 +348,23 @@ function typeHasher(options, writeTo, context){
return this.dispatch(Array.prototype.slice.call(arr));
},
_int8array: function(arr){
write('uint8array:');
write('int8array:');
return this.dispatch(Array.prototype.slice.call(arr));
},
_uint16array: function(arr){
write('uint16array:');
return this.dispatch(Array.prototype.slice.call(arr));
},
_int16array: function(arr){
write('uint16array:');
write('int16array:');
return this.dispatch(Array.prototype.slice.call(arr));
},
_uint32array: function(arr){
write('uint32array:');
return this.dispatch(Array.prototype.slice.call(arr));
},
_int32array: function(arr){
write('uint32array:');
write('int32array:');
return this.dispatch(Array.prototype.slice.call(arr));
},
_float32array: function(arr){
Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,12 @@ describe('hash', function() {
assert.equal(ha, hb, 'Hashing should not respect the order of Set entries');
});
}

if (typeof Uint8Array !== 'undefined') {
it('respects typed array\'s types', function () {
assert.notEqual(hash(new Int8Array([1,2,3,4])), hash(new Uint8Array([1,2,3,4])), 'Hashing should respect signed / unsigned Int8Array type');
assert.notEqual(hash(new Int16Array([1,2,3,4])), hash(new Uint16Array([1,2,3,4])), 'Hashing should respect signed / unsigned Int16Array type');
assert.notEqual(hash(new Int32Array([1,2,3,4])), hash(new Uint32Array([1,2,3,4])), 'Hashing should respect signed / unsigned Int32Array type');
});
}
});

0 comments on commit 04db18b

Please sign in to comment.