Fix behavior of Double.compare and Double.compareTo (external issue 4935)
Review at http://gwt-code-reviews.appspot.com/603801
Review by: jat@google.com
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@8241 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/super/com/google/gwt/emul/java/lang/Double.java b/user/super/com/google/gwt/emul/java/lang/Double.java
index ce65260..95b0605 100644
--- a/user/super/com/google/gwt/emul/java/lang/Double.java
+++ b/user/super/com/google/gwt/emul/java/lang/Double.java
@@ -33,6 +33,16 @@
public static final int SIZE = 64;
public static int compare(double x, double y) {
+ if (isNaN(x)) {
+ if (isNaN(y)) {
+ return 0;
+ } else {
+ return 1;
+ }
+ } else if (isNaN(y)) {
+ return -1;
+ }
+
if (x < y) {
return -1;
} else if (x > y) {
@@ -89,13 +99,7 @@
}
public int compareTo(Double b) {
- if (value < b.value) {
- return -1;
- } else if (value > b.value) {
- return 1;
- } else {
- return 0;
- }
+ return compare(this.value, b.value);
}
@Override
diff --git a/user/test/com/google/gwt/emultest/java/lang/DoubleTest.java b/user/test/com/google/gwt/emultest/java/lang/DoubleTest.java
index d97f2fb..f61a4ab 100644
--- a/user/test/com/google/gwt/emultest/java/lang/DoubleTest.java
+++ b/user/test/com/google/gwt/emultest/java/lang/DoubleTest.java
@@ -77,6 +77,34 @@
// Expected behavior
}
}
+
+ public void testCompare() {
+ assertTrue(Double.compare(Double.NaN, Double.NaN) == 0);
+ assertTrue(Double.compare(0.0, Double.NaN) < 0);
+ assertTrue(Double.compare(Double.NaN, Double.POSITIVE_INFINITY) > 0);
+ assertTrue(Double.compare(Double.NaN, 0.0) > 0);
+ assertTrue(Double.compare(Double.POSITIVE_INFINITY, Double.NaN) < 0);
+ assertTrue(Double.compare(3.0, 500.0) < 0);
+ assertTrue(Double.compare(500.0, 3.0) > 0);
+ assertTrue(Double.compare(500.0, 500.0) == 0);
+ }
+
+ public void testCompareTo() {
+ Double zero = new Double(0.0);
+ Double three = new Double(3.0);
+ Double fiveHundred = new Double(500.0);
+ Double infinity = new Double(Double.POSITIVE_INFINITY);
+ Double nan = new Double(Double.NaN);
+
+ assertTrue(nan.compareTo(nan) == 0);
+ assertTrue(zero.compareTo(nan) < 0);
+ assertTrue(nan.compareTo(infinity) > 0);
+ assertTrue(nan.compareTo(zero) > 0);
+ assertTrue(infinity.compareTo(nan) < 0);
+ assertTrue(three.compareTo(fiveHundred) < 0);
+ assertTrue(fiveHundred.compareTo(three) > 0);
+ assertTrue(fiveHundred.compareTo(fiveHundred) == 0);
+ }
public void testDoubleConstants() {
assertTrue(Double.isNaN(Double.NaN));