Skip to content

Commit

Permalink
Fix incorrect comparison (#54834) (#55094)
Browse files Browse the repository at this point in the history
Add test to check fix.

Checking comparison result against 1 or -1 causes problems when custom comparer is used.
As a result Min and Max properties return incorrect values.
  • Loading branch information
petrogavriluk committed Jul 9, 2021
1 parent 8b42b7f commit 784ddf8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ internal override T MinInternal
{

int comp = _lBoundActive ? Comparer.Compare(_min, current.Item) : -1;
if (comp == 1)
if (comp > 0)
{
current = current.Right;
}
Expand Down Expand Up @@ -161,7 +161,7 @@ internal override T MaxInternal
while (current != null)
{
int comp = _uBoundActive ? Comparer.Compare(_max, current.Item) : 1;
if (comp == -1)
if (comp < 0)
{
current = current.Left;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ protected override ISet<int> GenericISetFactory()
{
return new SortedSet<int>(new Comparer_SameAsDefaultComparer());
}

[Fact]
public void SortedSet_Generic_GetViewBetween_MinMax_WithCustomComparer()
{
var set = (SortedSet<int>)CreateSortedSet(new[] { 5, 15, 25, 35, 45 }, 5, 5);

for (int i = 0; i <= 40; i += 10)
{
for (int j = i + 10; j <= 50; j += 10)
{
SortedSet<int> view = set.GetViewBetween(i, j);

Assert.Equal(i + 5, view.Min);
Assert.Equal(j - 5, view.Max);
}
}
}
}

[OuterLoop]
Expand Down

0 comments on commit 784ddf8

Please sign in to comment.