Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Containers.Test/ContiguousSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ public void ExceptWith_RemovesCommonElements()
Assert.DoesNotContain(4, set1);
}

[TestMethod]
public void ExceptWith_Self_EmptiesSet()
{
ContiguousSet<int> set = [.. new int[] { 1, 2, 3, 4 }];

set.ExceptWith(set);

Assert.IsEmpty(set);
}

[TestMethod]
public void SymmetricExceptWith_KeepsUniqueElements()
{
Expand Down
10 changes: 10 additions & 0 deletions Containers.Test/InsertionOrderSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ public void ExceptWith_RemovesCommonElements()
Assert.AreEqual(3, set1.ElementAt(1));
}

[TestMethod]
public void ExceptWith_Self_EmptiesSet()
{
InsertionOrderSet<int> set = [.. new int[] { 1, 2, 3, 4 }];

set.ExceptWith(set);

Assert.IsEmpty(set);
}

[TestMethod]
public void WorksWithStrings_MaintainsInsertionOrder()
{
Expand Down
10 changes: 10 additions & 0 deletions Containers.Test/OrderedSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ public void ExceptWith_RemovesSpecifiedElements()
Assert.AreSequenceEqual(expected, set1);
}

[TestMethod]
public void ExceptWith_Self_EmptiesSet()
{
OrderedSet<int> set = [.. new int[] { 1, 2, 3, 4 }];

set.ExceptWith(set);

Assert.IsEmpty(set);
}

[TestMethod]
public void ExceptWith_NullCollection_ThrowsArgumentNullException()
{
Expand Down
7 changes: 7 additions & 0 deletions Containers/ContiguousSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ public void ExceptWith(IEnumerable<T> other)
{
Ensure.NotNull(other);

// Removing while enumerating this set would skip or throw; excepting a set from itself empties it
if (ReferenceEquals(other, this))
{
Clear();
return;
}

foreach (T item in other)
{
Remove(item);
Expand Down
7 changes: 7 additions & 0 deletions Containers/InsertionOrderSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ public void ExceptWith(IEnumerable<T> other)
{
Ensure.NotNull(other);

// Removing while enumerating this set would skip or throw; excepting a set from itself empties it
if (ReferenceEquals(other, this))
{
Clear();
return;
}

foreach (T item in other)
{
Remove(item);
Expand Down
7 changes: 7 additions & 0 deletions Containers/OrderedSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,13 @@ public void ExceptWith(IEnumerable<T> other)
{
Ensure.NotNull(other);

// Removing while enumerating this set would skip or throw; excepting a set from itself empties it
if (ReferenceEquals(other, this))
{
Clear();
return;
}

foreach (T item in other)
{
Remove(item);
Expand Down
Loading