提交 cd4631d7 编写于 作者: S Stephen Toub 提交者: Santiago Fernandez Madero

Fix HashSet.Remove to use provided comparer (#154)

上级 714b1439
......@@ -20,6 +20,12 @@ public static IEnumerable<object[]> ValidCollectionSizes()
yield return new object[] { 75 };
}
public static IEnumerable<object[]> ValidPositiveCollectionSizes()
{
yield return new object[] { 1 };
yield return new object[] { 75 };
}
public enum EnumerableType
{
HashSet,
......
......@@ -366,5 +366,23 @@ public struct ValueDelegateEquatable : IEquatable<ValueDelegateEquatable>
public bool Equals(ValueDelegateEquatable other) => EqualsWorker(other);
}
public sealed class TrackingEqualityComparer<T> : IEqualityComparer<T>
{
public int EqualsCalls;
public int GetHashCodeCalls;
public bool Equals(T x, T y)
{
EqualsCalls++;
return EqualityComparer<T>.Default.Equals(x, y);
}
public int GetHashCode(T obj)
{
GetHashCodeCalls++;
return EqualityComparer<T>.Default.GetHashCode(obj);
}
}
#endregion
}
......@@ -416,7 +416,7 @@ public bool Remove(T item)
for (i = _buckets[bucket] - 1; i >= 0; last = i, i = slots[i].next)
{
if (slots[i].hashCode == hashCode && EqualityComparer<T>.Default.Equals(slots[i].value, item))
if (slots[i].hashCode == hashCode && comparer.Equals(slots[i].value, item))
{
goto ReturnFound;
}
......
......@@ -626,5 +626,29 @@ public void EnsureCapacity_Generic_GrowCapacityWithFreeList(int setLength)
}
#endregion
#region Remove
[Theory]
[MemberData(nameof(ValidPositiveCollectionSizes))]
public void Remove_NonDefaultComparer_ComparerUsed(int capacity)
{
var c = new TrackingEqualityComparer<T>();
var set = new HashSet<T>(capacity, c);
AddToCollection(set, capacity);
T first = set.First();
c.EqualsCalls = 0;
c.GetHashCodeCalls = 0;
Assert.Equal(capacity, set.Count);
set.Remove(first);
Assert.Equal(capacity - 1, set.Count);
Assert.InRange(c.EqualsCalls, 1, int.MaxValue);
Assert.InRange(c.GetHashCodeCalls, 1, int.MaxValue);
}
#endregion
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册