Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2fbdc85

Browse files
committed
Merge pull request #2162 from jasonwoods-7/master
Fix for SymmetricExcept when using equality comparer.
2 parents 8b15144 + 9eeeb0d commit 2fbdc85

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableHashSet`1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ private static MutationResult SymmetricExcept(IEnumerable<T> other, MutationInpu
852852
{
853853
Requires.NotNull(other, "other");
854854

855-
var otherAsSet = Empty.Union(other);
855+
var otherAsSet = ImmutableHashSet.CreateRange(origin.EqualityComparer, other);
856856

857857
int count = 0;
858858
var result = SortedInt32KeyNode<HashBucket>.EmptyNode;

src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableSortedSet`1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public ImmutableSortedSet<T> SymmetricExcept(IEnumerable<T> other)
278278
{
279279
Requires.NotNull(other, "other");
280280

281-
var otherAsSet = ImmutableSortedSet<T>.Empty.Union(other);
281+
var otherAsSet = ImmutableSortedSet.CreateRange(_comparer, other);
282282

283283
var result = this.Clear();
284284
foreach (T item in this)

src/System.Collections.Immutable/tests/ImmutableHashSetTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ public void DebuggerAttributesValid()
163163
DebuggerAttributes.ValidateDebuggerTypeProxyProperties(ImmutableHashSet.Create<int>(1, 2, 3));
164164
}
165165

166+
[Fact]
167+
public void SymmetricExceptWithComparerTests()
168+
{
169+
var set = ImmutableHashSet.Create<string>("a").WithComparer(StringComparer.OrdinalIgnoreCase);
170+
var otherCollection = new[] {"A"};
171+
172+
var expectedSet = new HashSet<string>(set, set.KeyComparer);
173+
expectedSet.SymmetricExceptWith(otherCollection);
174+
175+
var actualSet = set.SymmetricExcept(otherCollection);
176+
CollectionAssertAreEquivalent(expectedSet.ToList(), actualSet.ToList());
177+
}
178+
166179
protected override IImmutableSet<T> Empty<T>()
167180
{
168181
return ImmutableHashSet<T>.Empty;

src/System.Collections.Immutable/tests/ImmutableSortedSetTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,19 @@ public void DebuggerAttributesValid()
368368
DebuggerAttributes.ValidateDebuggerDisplayReferences(rootNode);
369369
}
370370

371+
[Fact]
372+
public void SymmetricExceptWithComparerTests()
373+
{
374+
var set = ImmutableSortedSet.Create<string>("a").WithComparer(StringComparer.OrdinalIgnoreCase);
375+
var otherCollection = new[] {"A"};
376+
377+
var expectedSet = new SortedSet<string>(set, set.KeyComparer);
378+
expectedSet.SymmetricExceptWith(otherCollection);
379+
380+
var actualSet = set.SymmetricExcept(otherCollection);
381+
CollectionAssertAreEquivalent(expectedSet.ToList(), actualSet.ToList());
382+
}
383+
371384
protected override IImmutableSet<T> Empty<T>()
372385
{
373386
return ImmutableSortedSet<T>.Empty;

0 commit comments

Comments
 (0)