Skip to content

Commit 784ddf8

Browse files
Fix incorrect comparison (#54834) (#55094)
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.
1 parent 8b42b7f commit 784ddf8

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ internal override T MinInternal
132132
{
133133

134134
int comp = _lBoundActive ? Comparer.Compare(_min, current.Item) : -1;
135-
if (comp == 1)
135+
if (comp > 0)
136136
{
137137
current = current.Right;
138138
}
@@ -161,7 +161,7 @@ internal override T MaxInternal
161161
while (current != null)
162162
{
163163
int comp = _uBoundActive ? Comparer.Compare(_max, current.Item) : 1;
164-
if (comp == -1)
164+
if (comp < 0)
165165
{
166166
current = current.Left;
167167
}

src/libraries/System.Collections/tests/Generic/SortedSet/SortedSet.Generic.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,23 @@ protected override ISet<int> GenericISetFactory()
130130
{
131131
return new SortedSet<int>(new Comparer_SameAsDefaultComparer());
132132
}
133+
134+
[Fact]
135+
public void SortedSet_Generic_GetViewBetween_MinMax_WithCustomComparer()
136+
{
137+
var set = (SortedSet<int>)CreateSortedSet(new[] { 5, 15, 25, 35, 45 }, 5, 5);
138+
139+
for (int i = 0; i <= 40; i += 10)
140+
{
141+
for (int j = i + 10; j <= 50; j += 10)
142+
{
143+
SortedSet<int> view = set.GetViewBetween(i, j);
144+
145+
Assert.Equal(i + 5, view.Min);
146+
Assert.Equal(j - 5, view.Max);
147+
}
148+
}
149+
}
133150
}
134151

135152
[OuterLoop]

0 commit comments

Comments
 (0)