Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TreeSubSet.Min and TreeSubSet.Max work incorrectly with custom comparer #54834

Closed
petrogavriluk opened this issue Jun 28, 2021 · 4 comments · Fixed by #55094
Closed

TreeSubSet.Min and TreeSubSet.Max work incorrectly with custom comparer #54834

petrogavriluk opened this issue Jun 28, 2021 · 4 comments · Fixed by #55094
Assignees
Labels
area-System.Collections bug help wanted [up-for-grabs] Good issue for external contributors Priority:3 Work that is nice to have
Milestone

Comments

@petrogavriluk
Copy link
Contributor

Description

The code below gives the wrong result:

var comparer = Comparer<int>.Create((x, y) => x - y);  
var set = new SortedSet<int>(new[]{1, 4}, comparer);  
var view = set.GetViewBetween(1, 2);  
Console.WriteLine(view.Max); // Prints 4 instead of 1  

According to the documentation of IComparer.Compare() it can return any integer, not only [-1, 0, 1]. So this comparer should be ok.

Configuration

I run it on .net 5, but I think it is present in other versions as well.

Regression?

A similar issue was mentioned in 20474 (without comparer though). So I think it is regression.

Other information

I have found the reason why it happens in code:


The comparison result is checked against 1 and -1, instead of (comp > 0) and (comp < 0).

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Collections untriaged New issue has not been triaged by the area owner labels Jun 28, 2021
@ghost
Copy link

ghost commented Jun 28, 2021

Tagging subscribers to this area: @eiriktsarpalis
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

The code below gives the wrong result:

var comparer = Comparer<int>.Create((x, y) => x - y);  
var set = new SortedSet<int>(new[]{1, 4}, comparer);  
var view = set.GetViewBetween(1, 2);  
Console.WriteLine(view.Max); // Prints 4 instead of 1  

According to the documentation of IComparer.Compare() it can return any integer, not only [-1, 0, 1]. So this comparer should be ok.

Configuration

I run it on .net 5, but I think it is present in other versions as well.

Regression?

A similar issue was mentioned in 20474 (without comparer though). So I think it is regression.

Other information

I have found the reason why it happens in code:


The comparison result is checked against 1 and -1, instead of (comp > 0) and (comp < 0).

Author: petrogavriluk
Assignees: -
Labels:

area-System.Collections, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

This seems like a bug introduced by dotnet/corefx#16771. It seems like a trivial fix, would you be willing to contribute a PR?

@eiriktsarpalis eiriktsarpalis added bug Priority:3 Work that is nice to have help wanted [up-for-grabs] Good issue for external contributors and removed untriaged New issue has not been triaged by the area owner labels Jun 28, 2021
@eiriktsarpalis eiriktsarpalis added this to the 6.0.0 milestone Jun 28, 2021
@petrogavriluk
Copy link
Contributor Author

I haven't contributed here yet, but yes, I can try.
Should I also add tests?

@eiriktsarpalis
Copy link
Member

Thanks, assigning the issue to you. A couple of tests reproducing your scenario for both min and max should suffice.

Please let me know if you have any questions on the dev workflow.

petrogavriluk added a commit to petrogavriluk/runtime that referenced this issue Jul 2, 2021
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.
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 2, 2021
stephentoub pushed a commit that referenced this issue Jul 9, 2021
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.
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 9, 2021
@dotnet dotnet locked as resolved and limited conversation to collaborators Aug 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Collections bug help wanted [up-for-grabs] Good issue for external contributors Priority:3 Work that is nice to have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants