Skip to content

Commit

Permalink
fix: hashCode computation for Iterables (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
machinescream authored Jul 11, 2020
1 parent 094b3e2 commit e823f9d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/src/equatable_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ int _combine(int hash, dynamic object) {
});
return hash;
}
final objectHashCode =
object is Iterable ? mapPropsToHashCode(object) : object.hashCode;
hash = 0x1fffffff & (hash + objectHashCode);
if (object is Iterable) {
for (final value in object) {
hash = hash ^ _combine(hash, value);
}
return hash ^ object.length;
}

hash = 0x1fffffff & (hash + object.hashCode);
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}
Expand Down
7 changes: 7 additions & 0 deletions test/equatable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class NullProps extends Equatable {

void main() {
EquatableConfig.stringify = false;

group('Empty Equatable', () {
test('should correct toString', () {
final instance = EmptyEquatable();
Expand Down Expand Up @@ -611,6 +612,12 @@ void main() {

group('Collection Equatable', () {
group('Iterable Equatable', () {
test('list of zeros same hashcode check', () {
final s0 = SimpleEquatable([0, 0]);
final s1 = SimpleEquatable([0, 0, 0]);
expect(s0.hashCode != s1.hashCode, true);
});

test('should return when values are same', () {
final instanceA = SimpleEquatable<Iterable>(["A", "B"]);
final instanceB = SimpleEquatable<Iterable>(["A", "B"]);
Expand Down

0 comments on commit e823f9d

Please sign in to comment.