Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 340195 - assertEquals fails with large Long values
Summary: assertEquals fails with large Long values
Status: CLOSED FIXED
Alias: None
Product: Epsilon
Classification: Modeling
Component: Core (show other bugs)
Version: unspecified   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Dimitris Kolovos CLA
QA Contact:
URL:
Whiteboard: interim
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-16 13:29 EDT by Antonio Garcia-Dominguez CLA
Modified: 2012-08-29 17:33 EDT (History)
0 users

See Also:


Attachments
Patch which fixes the problem and makes NumberUtil follow JLS 5.6.1 and 5.6.2 (27.26 KB, patch)
2011-03-16 13:30 EDT, Antonio Garcia-Dominguez CLA
dkolovos: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Antonio Garcia-Dominguez CLA 2011-03-16 13:29:21 EDT
I had a look at the code of assertEquals and noticed something interesting. The current implementation relies on EolObjectComparator, which compares two numbers like this:

return ((Number) o1).doubleValue() == ((Number) o2).doubleValue();

This line has one problem: large integer values (likely Long values) will be rounded down, making different integers seem to be equal.

Indeed, adding this line inside testCompareIntegers() in the ComparisonTests.eol file in the EOL acceptance tests project makes the test fail:

assertEquals(9223372036854775807l == 9223372036854775806l, true);

To fix this, I propose that the Java Language Specification is followed more closely, particularly regarding sections 5.6.1 ("unary numeric promotion") and 5.6.2 ("binary numeric promotion"), in the NumberUtil and EolObjectComparator classes:

http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.6.1
Comment 1 Antonio Garcia-Dominguez CLA 2011-03-16 13:30:51 EDT
Created attachment 191322 [details]
Patch which fixes the problem and makes NumberUtil follow JLS 5.6.1 and 5.6.2

Here is a patch which reimplements sections 5.6.1 and 5.6.2 of the Java Language Specification to fix the problem and heavily reduce the number of casts in NumberUtil.
Comment 2 Antonio Garcia-Dominguez CLA 2011-03-17 09:15:12 EDT
> Indeed, adding this line inside testCompareIntegers() in the
> ComparisonTests.eol file in the EOL acceptance tests project makes the test
> fail:
> 
> assertEquals(9223372036854775807l == 9223372036854775806l, true);

Oops, I meant these two lines:

 assertEquals(9223372036854775807l == 9223372036854775807l, true);
 assertEquals(9223372036854775807l == 9223372036854775806l, false);
Comment 3 Antonio Garcia-Dominguez CLA 2011-05-19 11:02:51 EDT
The above patch has been integrated for a while. Closing this bug as fixed.
Comment 4 Dimitris Kolovos CLA 2011-07-25 08:16:59 EDT
Fixed in 0.9.1