diff --git a/lib/src/collection/impl/list.dart b/lib/src/collection/impl/list.dart index ce5c733..41da704 100644 --- a/lib/src/collection/impl/list.dart +++ b/lib/src/collection/impl/list.dart @@ -10,7 +10,6 @@ class DartList extends Object implements KtList { super(); final List _list; - int? _hashCode; @override Iterable get iter => _list; @@ -76,7 +75,7 @@ class DartList extends Object implements KtList { } @override - int get hashCode => _hashCode ??= 1 + hashObjects(_list); + int get hashCode => 1 + hashObjects(_list); @override bool operator ==(dynamic other) { diff --git a/lib/src/collection/impl/map.dart b/lib/src/collection/impl/map.dart index 52ebe2b..6b300d5 100644 --- a/lib/src/collection/impl/map.dart +++ b/lib/src/collection/impl/map.dart @@ -9,7 +9,6 @@ class DartMap extends Object implements KtMap { super(); final Map _map; - int? _hashCode; @override Iterable> get iter => @@ -63,7 +62,7 @@ class DartMap extends Object implements KtMap { @override int get hashCode { - return _hashCode ??= hashObjects(_map.keys + return hashObjects(_map.keys .map((key) => hash2(key.hashCode, _map[key].hashCode)) .toList(growable: false) ..sort()); diff --git a/lib/src/collection/impl/set.dart b/lib/src/collection/impl/set.dart index ba3f5a0..b7f6cfe 100644 --- a/lib/src/collection/impl/set.dart +++ b/lib/src/collection/impl/set.dart @@ -8,7 +8,6 @@ class DartSet extends Object implements KtSet { super(); final Set _set; - int? _hashCode; @override Iterable get iter => _set; @@ -41,7 +40,7 @@ class DartSet extends Object implements KtSet { int get size => _set.length; @override - int get hashCode => _hashCode ??= + int get hashCode => hashObjects(_set.map((e) => e.hashCode).toList(growable: false)..sort()); @override diff --git a/lib/src/collection/kt_list.dart b/lib/src/collection/kt_list.dart index b2c4a57..be0050f 100644 --- a/lib/src/collection/kt_list.dart +++ b/lib/src/collection/kt_list.dart @@ -428,7 +428,6 @@ class _CastKtList implements KtList { _CastKtList(KtList list) : _list = list; final KtList _list; - int? _hashCode; @override Iterable get iter => _list.asList().cast(); @@ -496,7 +495,7 @@ class _CastKtList implements KtList { } @override - int get hashCode => _hashCode ??= 1 + hashObjects(_list.asList()); + int get hashCode => 1 + hashObjects(_list.asList()); @override bool operator ==(dynamic other) { diff --git a/test/collection/list_empty_test.dart b/test/collection/list_empty_test.dart index 825623b..af832e2 100644 --- a/test/collection/list_empty_test.dart +++ b/test/collection/list_empty_test.dart @@ -20,7 +20,7 @@ void main() { group("KtList.of", () { testEmptyList(() => KtList.of(), mutable: false); }); - group("KtList.of", () { + group("KtList.from", () { testEmptyList(() => KtList.from(), mutable: false); }); group("mutableListOf", () { diff --git a/test/collection/list_test.dart b/test/collection/list_test.dart index f195b2b..1355e19 100644 --- a/test/collection/list_test.dart +++ b/test/collection/list_test.dart @@ -191,6 +191,17 @@ void testList( expect(list0, isNot(equals(list2))); expect(list0.hashCode, isNot(equals(list2.hashCode))); }); + + test("hashcode changes when mutable value change", () { + final list = listOf(mutableListOf('a', 'b'), mutableListOf('c')); + final hash1 = list.hashCode; + + // mutate content in list, not the list itself + list[1].add('d'); + + final hash2 = list.hashCode; + expect(hash2, isNot(hash1)); + }); }); group("reduceRight", () { diff --git a/test/collection/map_test.dart b/test/collection/map_test.dart index 9d064ef..7ea009e 100644 --- a/test/collection/map_test.dart +++ b/test/collection/map_test.dart @@ -159,4 +159,15 @@ void testMap(KtMap Function(Map map) mapFrom, expect(keys, listOf(1, 2)); }); }); + + test("hashcode changes when mutable value change", () { + final map = mapFrom({1: mutableListOf('a', 'b')}); + final hash1 = map.hashCode; + + // mutate content in map, not the map itself + map[1]!.add('d'); + + final hash2 = map.hashCode; + expect(hash2, isNot(hash1)); + }); } diff --git a/test/collection/set_test.dart b/test/collection/set_test.dart index fd3ccc3..a34891a 100644 --- a/test/collection/set_test.dart +++ b/test/collection/set_test.dart @@ -226,5 +226,16 @@ void testSet( catchException(() => ktSet.asSet().add("asdf")); expect(e.message, contains("unmodifiable")); }); + + test("hashcode changes when mutable value change", () { + final set = setOf(mutableListOf('a', 'b')); + final hash1 = set.hashCode; + + // mutate content in set, not the set itself + set.first().add('d'); + + final hash2 = set.hashCode; + expect(hash2, isNot(hash1)); + }); } }