Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,8 @@ public bool IsSubsetOf(IEnumerable<T> other)

private bool IsSubsetOfSortedSetWithSameComparer(SortedSet<T> asSorted)
{
SortedSet<T> prunedOther = asSorted.GetViewBetween(Min, Max);
// GetViewBetween on a view throws if the bounds fall outside the view, so skip the pruning for views.
SortedSet<T> prunedOther = asSorted is TreeSubSet ? asSorted : asSorted.GetViewBetween(Min, Max);
foreach (T item in this)
{
if (!prunedOther.Contains(item))
Expand Down Expand Up @@ -1240,7 +1241,8 @@ public bool IsSupersetOf(IEnumerable<T> other)
{
if (Count < asSorted.Count)
return false;
SortedSet<T> pruned = GetViewBetween(asSorted.Min, asSorted.Max);
// GetViewBetween on a view throws if the bounds fall outside the view, so skip the pruning for views.
SortedSet<T> pruned = this is TreeSubSet ? this : GetViewBetween(asSorted.Min, asSorted.Max);
foreach (T item in asSorted)
{
if (!pruned.Contains(item))
Expand Down Expand Up @@ -1269,7 +1271,8 @@ public bool IsProperSupersetOf(IEnumerable<T> other)
{
if (asSorted.Count >= Count)
return false;
SortedSet<T> pruned = GetViewBetween(asSorted.Min, asSorted.Max);
// GetViewBetween on a view throws if the bounds fall outside the view, so skip the pruning for views.
SortedSet<T> pruned = this is TreeSubSet ? this : GetViewBetween(asSorted.Min, asSorted.Max);
foreach (T item in asSorted)
{
if (!pruned.Contains(item))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,29 @@ public void ExceptWith(bool useSelf)
Assert.Equal(0, view.Count);
Assert.Equal(new[] { 1, 4 }, set);
}

[Fact]
public static void SetOperationChecksDoNotThrowWhenViewBoundsExcluded()
{
SortedSet<int> set = new SortedSet<int> { 1, 2, 3, 4 };
SortedSet<int> view = set.GetViewBetween(2, 3);

// The other set's bounds fall outside the view. These must report false, not throw.
Assert.False(view.IsSupersetOf(new SortedSet<int> { 1 }));
Assert.False(view.IsSupersetOf(new SortedSet<int> { 2, 9 }));
Assert.False(view.IsSupersetOf(new SortedSet<int> { 4 }));
Assert.False(view.IsProperSupersetOf(new SortedSet<int> { 1 }));

// The same when the view is the argument rather than the instance.
Assert.False(new SortedSet<int> { 5 }.IsSubsetOf(view));
Assert.False(new SortedSet<int> { 5 }.IsProperSubsetOf(view));

// Results that are unaffected by the pruning fast path must not change.
Assert.True(view.IsSupersetOf(new SortedSet<int> { 2, 3 }));
Assert.True(view.IsProperSupersetOf(new SortedSet<int> { 2 }));
Assert.True(view.IsSubsetOf(set));
Assert.True(view.IsProperSubsetOf(set));
}
}

public class SortedSet_TreeSubset_String_Tests : SortedSet_TreeSubset_Tests<string>
Expand Down
Loading