Bug 187223 - CompletionTestsRequestor2.getReversedResults has incorrect comparator
Summary: CompletionTestsRequestor2.getReversedResults has incorrect comparator
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.3 RC1   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-05-16 06:20 EDT by Alexey A. Petrenko CLA
Modified: 2007-05-23 01:58 EDT (History)
1 user (show)

See Also:
kent_johnson: review+


Attachments
Proposed fix (1.72 KB, patch)
2007-05-16 12:39 EDT, Olivier Thomann CLA
no flags Details | Diff
Better patch (1.68 KB, patch)
2007-05-16 13:59 EDT, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Alexey A. Petrenko CLA 2007-05-16 06:20:02 EDT
org.eclipse.jdt.core.tests.model.CompletionTestsRequestor2.getReversedResults() method has incorrect comparator: 

=== cut ===
Arrays.sort(this.proposals, new Comparator() { 
    public int compare(Object o1, Object o2) { 
        if (o1 instanceof CompletionProposal && o2 instanceof CompletionProposal) { 
            CompletionProposal p1 = (CompletionProposal) o1; 
            CompletionProposal p2 = (CompletionProposal) o2; 
            int relDif = p2.getRelevance() - p1.getRelevance(); 
            if(relDif != 0) return relDif; 
            String name1 = getElementName(p1); 
            String name2 = getElementName(p2); 
            return name1.compareTo(name2); 
        } 
        return -1; 
    } 
}); 
=== cut ===

Provided comparator is incorrect for null elements. It returns -1 if any of compared objects is null. But according to spec "The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y." [1]

This error is the cause of Eclipse Unit test failures on Apache Harmony [2]

Sorry for probably wrong component selection.

[1] http://java.sun.com/j2se/1.5.0/docs/api/java/util/Comparator.html#compare(T,%20T)
[2] http://harmony.apache.org
Comment 1 Olivier Thomann CLA 2007-05-16 12:39:00 EDT
Created attachment 67444 [details]
Proposed fix

I'll run all the tests with this patch.
Comment 2 Alexey A. Petrenko CLA 2007-05-16 13:17:13 EDT
Olivier, thanks for the patch.
I would change the initial null checks in the following way:

=== cut ===
if (o1 == o2) 
    return 0;
if (o1 == null)
    return 1;
if (o2 == null)
    return -1;
=== cut ===

It looks more clear and first check could avoid all other comparisons for non-null cases.

What do you think?
Comment 3 Olivier Thomann CLA 2007-05-16 13:58:13 EDT
Yes, even better.
I'll release it later today for 6pm build.
Comment 4 Olivier Thomann CLA 2007-05-16 13:59:08 EDT
Created attachment 67468 [details]
Better patch
Comment 5 Olivier Thomann CLA 2007-05-16 13:59:24 EDT
Kent, please review.
Comment 6 Alexey A. Petrenko CLA 2007-05-16 14:00:10 EDT
Thank you.
Comment 7 Olivier Thomann CLA 2007-05-16 14:13:07 EDT
Released for 3.3RC1.
No regression test.
Alexey, could you please put a note to say this is fixed with next integration build?
Comment 8 Alexey A. Petrenko CLA 2007-05-16 14:16:37 EDT
Yes, sure.
I'll test it tomorrow. It's almost night here :)

Thanks, guys, for so quick response.
Comment 9 Olivier Thomann CLA 2007-05-17 10:38:35 EDT
Alexey, once I have your confirmation that it works now I'll close it as verified.
Comment 10 Alexey A. Petrenko CLA 2007-05-23 01:58:41 EDT
The patch works. Thanks again.