Add tests for broken Double/Float handling on equals and java.util.Arrays.
Change-Id: I6de99113bbe74dc8cf1a81a32736176eecbd7867
Review-Link: https://gwt-review.googlesource.com/#/c/22080/
diff --git a/user/test/com/google/gwt/emultest/CollectionsSuite.java b/user/test/com/google/gwt/emultest/CollectionsSuite.java
index 95d76a6..a273ee0 100644
--- a/user/test/com/google/gwt/emultest/CollectionsSuite.java
+++ b/user/test/com/google/gwt/emultest/CollectionsSuite.java
@@ -17,6 +17,8 @@
import com.google.gwt.emultest.java.util.ArrayDequeTest;
import com.google.gwt.emultest.java.util.ArrayListTest;
+import com.google.gwt.emultest.java.util.ArraysDoubleSemanticsTest;
+import com.google.gwt.emultest.java.util.ArraysFloatSemanticsTest;
import com.google.gwt.emultest.java.util.ArraysTest;
import com.google.gwt.emultest.java.util.BitSetTest;
import com.google.gwt.emultest.java.util.CollectionsTest;
@@ -38,7 +40,6 @@
import com.google.gwt.emultest.java.util.TreeSetIntegerTest;
import com.google.gwt.emultest.java.util.TreeSetIntegerWithComparatorTest;
import com.google.gwt.emultest.java.util.VectorTest;
-
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@@ -48,6 +49,8 @@
@SuiteClasses({
ArrayDequeTest.class,
ArrayListTest.class,
+ ArraysDoubleSemanticsTest.class,
+ ArraysFloatSemanticsTest.class,
ArraysTest.class,
BitSetTest.class,
CollectionsTest.class,
@@ -72,4 +75,4 @@
// Put last to reduce number of times the test framework switches modules
HashMapSmokeTest.class,
})
-public class CollectionsSuite { }
+public class CollectionsSuite {}
diff --git a/user/test/com/google/gwt/emultest/EmulSuite.java b/user/test/com/google/gwt/emultest/EmulSuite.java
index f9246e2..575fd5b 100644
--- a/user/test/com/google/gwt/emultest/EmulSuite.java
+++ b/user/test/com/google/gwt/emultest/EmulSuite.java
@@ -26,7 +26,9 @@
import com.google.gwt.emultest.java.lang.ByteTest;
import com.google.gwt.emultest.java.lang.CharacterTest;
import com.google.gwt.emultest.java.lang.CompilerConstantStringTest;
+import com.google.gwt.emultest.java.lang.DoubleEqualsSemanticsTest;
import com.google.gwt.emultest.java.lang.DoubleTest;
+import com.google.gwt.emultest.java.lang.FloatEqualsSemanticsTest;
import com.google.gwt.emultest.java.lang.FloatTest;
import com.google.gwt.emultest.java.lang.IntegerTest;
import com.google.gwt.emultest.java.lang.JsExceptionTest;
@@ -56,8 +58,6 @@
import com.google.gwt.emultest.java.util.DateTest;
import com.google.gwt.emultest.java.util.ObjectsTest;
import com.google.gwt.emultest.java.util.RandomTest;
-
-
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@@ -81,7 +81,9 @@
CharacterTest.class,
CompilerConstantStringTest.class,
DoubleTest.class,
+ DoubleEqualsSemanticsTest.class,
FloatTest.class,
+ FloatEqualsSemanticsTest.class,
IntegerTest.class,
JsExceptionTest.class,
LongTest.class,
@@ -127,4 +129,4 @@
// Put last to reduce number of times the test framework switches modules
MathContextWithObfuscatedEnumsTest.class,
})
-public class EmulSuite { }
+public class EmulSuite {}
diff --git a/user/test/com/google/gwt/emultest/java/lang/DoubleEqualsSemanticsTest.java b/user/test/com/google/gwt/emultest/java/lang/DoubleEqualsSemanticsTest.java
new file mode 100644
index 0000000..c9112fb
--- /dev/null
+++ b/user/test/com/google/gwt/emultest/java/lang/DoubleEqualsSemanticsTest.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2019 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.emultest.java.lang;
+
+import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.testing.TestUtils;
+
+/** Tests (incorrect) equals semantics for Double. */
+public final class DoubleEqualsSemanticsTest extends GWTTestCase {
+
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.emultest.EmulSuite";
+ }
+
+ public void testEquals() {
+ // Semantics don't match JVM.
+ if (TestUtils.isJvm()) {
+ return;
+ }
+
+ // Should be assertTrue(Double.valueOf(Double.NaN).equals(Double.NaN));
+ assertFalse(Double.valueOf(Double.NaN).equals(Double.NaN));
+ // Should be assertFalse(Double.valueOf(0.0d).equals(-0.0d));
+ assertTrue(Double.valueOf(0.0d).equals(-0.0d));
+
+ // Also make sure the behavior doesn't change when Object trampoline is used.
+ Object o;
+ o = Double.NaN;
+ // Should be assertTrue(o.equals(Double.NaN));
+ assertFalse(o.equals(Double.NaN));
+ o = 0.0d;
+ // Should be assertFalse
+ assertTrue(o.equals(-0.0d));
+ }
+}
diff --git a/user/test/com/google/gwt/emultest/java/lang/FloatEqualsSemanticsTest.java b/user/test/com/google/gwt/emultest/java/lang/FloatEqualsSemanticsTest.java
new file mode 100644
index 0000000..7b95eba
--- /dev/null
+++ b/user/test/com/google/gwt/emultest/java/lang/FloatEqualsSemanticsTest.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2019 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.emultest.java.lang;
+
+import com.google.gwt.junit.client.GWTTestCase;
+import com.google.gwt.testing.TestUtils;
+
+/** Tests (incorrect) equals semantics for Float. */
+public final class FloatEqualsSemanticsTest extends GWTTestCase {
+
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.emultest.EmulSuite";
+ }
+
+ public void testEquals() {
+ // Semantics don't match JVM.
+ if (TestUtils.isJvm()) {
+ return;
+ }
+
+ // Should be assertTrue(Float.valueOf(Float.NaN).equals(Float.NaN));
+ assertFalse(Float.valueOf(Float.NaN).equals(Float.NaN));
+ // Should be assertFalse(Float.valueOf(0.0f).equals(-0.0f));
+ assertTrue(Float.valueOf(0.0f).equals(-0.0f));
+ }
+}
diff --git a/user/test/com/google/gwt/emultest/java/util/ArraysDoubleSemanticsTest.java b/user/test/com/google/gwt/emultest/java/util/ArraysDoubleSemanticsTest.java
new file mode 100644
index 0000000..e1429c9
--- /dev/null
+++ b/user/test/com/google/gwt/emultest/java/util/ArraysDoubleSemanticsTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2019 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.emultest.java.util;
+
+import com.google.gwt.testing.TestUtils;
+import java.util.Arrays;
+
+/** Tests {@link Arrays} (incorrect) Double semantics. */
+public class ArraysDoubleSemanticsTest extends EmulTestBase {
+
+ public void testEquals() throws Exception {
+ // Semantics don't match JVM.
+ if (TestUtils.isJvm()) {
+ return;
+ }
+
+ // Should be assertFalse(Arrays.equals(new Double[] {-0.0d}, new Double[] {0.0d}));
+ assertTrue(Arrays.equals(new Double[] {-0.0d}, new Double[] {0.0d}));
+ // Should be assertTrue(Arrays.equals(new Double[] {-0.0d}, new Double[] {0.0d}));
+ assertFalse(Arrays.equals(new Double[] {Double.NaN}, new Double[] {Double.NaN}));
+
+ // Should be assertFalse(Arrays.equals(new double[] {-0.0d}, new double[] {0.0d}));
+ assertTrue(Arrays.equals(new double[] {-0.0d}, new double[] {0.0d}));
+ // Should be assertTrue(Arrays.equals(new double[] {Double.NaN}, new double[] {Double.NaN}));
+ assertFalse(Arrays.equals(new double[] {Double.NaN}, new double[] {Double.NaN}));
+ }
+
+ public void testBinarySearch() throws Exception {
+ // Semantics don't match JVM.
+ if (TestUtils.isJvm()) {
+ return;
+ }
+
+ // Should be assertEquals(-1, Arrays.binarySearch(new double[] {-0.0d}, 0.0d));
+ assertEquals(0, Arrays.binarySearch(new double[] {-0.0d}, 0.0d));
+ // Should be assertEquals(-1, Arrays.binarySearch(new double[] {0.0d}, Double.NaN));
+ assertEquals(0, Arrays.binarySearch(new double[] {0.0d}, Double.NaN));
+ // Should be assertEquals(1, Arrays.binarySearch(new double[] {0.0d, Double.NaN}, Double.NaN));
+ assertEquals(0, Arrays.binarySearch(new double[] {0.0d, Double.NaN}, Double.NaN));
+ }
+}
diff --git a/user/test/com/google/gwt/emultest/java/util/ArraysFloatSemanticsTest.java b/user/test/com/google/gwt/emultest/java/util/ArraysFloatSemanticsTest.java
new file mode 100644
index 0000000..31de695
--- /dev/null
+++ b/user/test/com/google/gwt/emultest/java/util/ArraysFloatSemanticsTest.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2019 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.emultest.java.util;
+
+import com.google.gwt.testing.TestUtils;
+import java.util.Arrays;
+
+/** Tests {@link Arrays} (incorrect) Float semantics. */
+public class ArraysFloatSemanticsTest extends EmulTestBase {
+
+ public void testEquals() throws Exception {
+ // Semantics don't match JVM.
+ if (TestUtils.isJvm()) {
+ return;
+ }
+
+ // Should be assertFalse(Arrays.equals(new Float[] {-0.0f}, new Float[] {0.0f}));
+ assertTrue(Arrays.equals(new Float[] {-0.0f}, new Float[] {0.0f}));
+ // Should be assertTrue(Arrays.equals(new Float[] {Float.NaN}, new Float[] {Float.NaN}));
+ assertFalse(Arrays.equals(new Float[] {Float.NaN}, new Float[] {Float.NaN}));
+
+ // Should be assertFalse(Arrays.equals(new float[] {-0.0f}, new float[] {0.0f}));
+ assertTrue(Arrays.equals(new float[] {-0.0f}, new float[] {0.0f}));
+ // Should be assertTrue(Arrays.equals(new float[] {Float.NaN}, new float[] {Float.NaN}));
+ assertFalse(Arrays.equals(new float[] {Float.NaN}, new float[] {Float.NaN}));
+ }
+
+ public void testBinarySearch() throws Exception {
+ // Semantics don't match JVM.
+ if (TestUtils.isJvm()) {
+ return;
+ }
+
+ // Should be assertEquals(-1, Arrays.binarySearch(new float[] {-0.0f}, 0.0f));
+ assertEquals(0, Arrays.binarySearch(new float[] {-0.0f}, 0.0f));
+ // Should be assertEquals(-1, Arrays.binarySearch(new float[] {0.0f}, Float.NaN));
+ assertEquals(0, Arrays.binarySearch(new float[] {0.0f}, Float.NaN));
+ // Should be assertEquals(1, Arrays.binarySearch(new float[] {0.0f, Float.NaN}, Float.NaN));
+ assertEquals(0, Arrays.binarySearch(new float[] {0.0f, Float.NaN}, Float.NaN));
+ }
+}
diff --git a/user/test/com/google/gwt/emultest/java/util/ArraysTest.java b/user/test/com/google/gwt/emultest/java/util/ArraysTest.java
index 73f8ee6..47d4e47 100644
--- a/user/test/com/google/gwt/emultest/java/util/ArraysTest.java
+++ b/user/test/com/google/gwt/emultest/java/util/ArraysTest.java
@@ -448,6 +448,18 @@
assertEquals(-2, ret);
}
+ public void testBinarySearchBoxed() {
+ assertEquals(1, Arrays.binarySearch(new Double[] {-0.0d, -0.0d, -0.0d}, -0.0d));
+ assertEquals(-4, Arrays.binarySearch(new Double[] {-0.0d, -0.0d, -0.0d}, 0.0d));
+ assertEquals(0, Arrays.binarySearch(new Double[] {Double.NaN}, Double.NaN));
+ assertEquals(-2, Arrays.binarySearch(new Double[] {0.0d}, Double.NaN));
+
+ assertEquals(1, Arrays.binarySearch(new Float[] {-0.0f, -0.0f, -0.0f}, -0.0f));
+ assertEquals(-4, Arrays.binarySearch(new Float[] {-0.0f, -0.0f, -0.0f}, 0.0f));
+ assertEquals(0, Arrays.binarySearch(new Float[] {Float.NaN}, Float.NaN));
+ assertEquals(-2, Arrays.binarySearch(new Float[] {0.0f}, Float.NaN));
+ }
+
/**
* Test Arrays.binarySearch(Object[], Object, Comparator).
*