Skip to content

Commit cd4631d

Browse files
stephentoubsafern
authored andcommitted
Fix HashSet.Remove to use provided comparer (#154)
1 parent 714b143 commit cd4631d

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

src/libraries/Common/tests/System/Collections/TestBase.NonGeneric.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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,

src/libraries/Common/tests/System/Collections/TestingTypes.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

src/libraries/System.Collections/src/System/Collections/Generic/HashSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
}

src/libraries/System.Collections/tests/Generic/HashSet/HashSet.Generic.Tests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)