File tree Expand file tree Collapse file tree 4 files changed +49
-1
lines changed
Common/tests/System/Collections
src/System/Collections/Generic Expand file tree Collapse file tree 4 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,12 @@ public static IEnumerable<object[]> ValidCollectionSizes()
2020 yield return new object [ ] { 75 } ;
2121 }
2222
23+ public static IEnumerable < object [ ] > ValidPositiveCollectionSizes ( )
24+ {
25+ yield return new object [ ] { 1 } ;
26+ yield return new object [ ] { 75 } ;
27+ }
28+
2329 public enum EnumerableType
2430 {
2531 HashSet ,
Original file line number Diff line number Diff line change @@ -366,5 +366,23 @@ public struct ValueDelegateEquatable : IEquatable<ValueDelegateEquatable>
366366 public bool Equals ( ValueDelegateEquatable other ) => EqualsWorker ( other ) ;
367367 }
368368
369+ public sealed class TrackingEqualityComparer < T > : IEqualityComparer < T >
370+ {
371+ public int EqualsCalls ;
372+ public int GetHashCodeCalls ;
373+
374+ public bool Equals ( T x , T y )
375+ {
376+ EqualsCalls ++ ;
377+ return EqualityComparer < T > . Default . Equals ( x , y ) ;
378+ }
379+
380+ public int GetHashCode ( T obj )
381+ {
382+ GetHashCodeCalls ++ ;
383+ return EqualityComparer < T > . Default . GetHashCode ( obj ) ;
384+ }
385+ }
386+
369387 #endregion
370388}
Original file line number Diff line number Diff line change @@ -416,7 +416,7 @@ public bool Remove(T item)
416416
417417 for ( i = _buckets [ bucket ] - 1 ; i >= 0 ; last = i , i = slots [ i ] . next )
418418 {
419- if ( slots [ i ] . hashCode == hashCode && EqualityComparer < T > . Default . Equals ( slots [ i ] . value , item ) )
419+ if ( slots [ i ] . hashCode == hashCode && comparer . Equals ( slots [ i ] . value , item ) )
420420 {
421421 goto ReturnFound ;
422422 }
Original file line number Diff line number Diff line change @@ -626,5 +626,29 @@ public void EnsureCapacity_Generic_GrowCapacityWithFreeList(int setLength)
626626 }
627627
628628 #endregion
629+
630+ #region Remove
631+
632+ [ Theory ]
633+ [ MemberData ( nameof ( ValidPositiveCollectionSizes ) ) ]
634+ public void Remove_NonDefaultComparer_ComparerUsed ( int capacity )
635+ {
636+ var c = new TrackingEqualityComparer < T > ( ) ;
637+ var set = new HashSet < T > ( capacity , c ) ;
638+
639+ AddToCollection ( set , capacity ) ;
640+ T first = set . First ( ) ;
641+ c . EqualsCalls = 0 ;
642+ c . GetHashCodeCalls = 0 ;
643+
644+ Assert . Equal ( capacity , set . Count ) ;
645+ set . Remove ( first ) ;
646+ Assert . Equal ( capacity - 1 , set . Count ) ;
647+
648+ Assert . InRange ( c . EqualsCalls , 1 , int . MaxValue ) ;
649+ Assert . InRange ( c . GetHashCodeCalls , 1 , int . MaxValue ) ;
650+ }
651+
652+ #endregion
629653 }
630654}
You can’t perform that action at this time.
0 commit comments