Add Math.cbrt, improve Math.log10, add tests.
Public review: http://gwt-code-reviews.appspot.com/150803/show
Issue: 4308, 4637
Patch by: jat, elk
Review by: jlabanca
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@7605 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/super/com/google/gwt/emul/java/lang/Math.java b/user/super/com/google/gwt/emul/java/lang/Math.java
index debd2df..415e6e7 100644
--- a/user/super/com/google/gwt/emul/java/lang/Math.java
+++ b/user/super/com/google/gwt/emul/java/lang/Math.java
@@ -58,10 +58,9 @@
return Math.atan2(y,x);
}-*/;
- /* NYI: Java 1.5 includes this, but JS doesn't give us the ingredients.: Java 1.5 includes this, but JS doesn't give us the ingredients.
public static double cbrt (double x) {
- return 0;
- }; */
+ return Math.pow(x, 1.0 / 3.0);
+ }
public static native double ceil(double x) /*-{
return Math.ceil(x);
@@ -128,13 +127,13 @@
return Math.log(x);
}-*/;
- public static double log10(double x) {
- return Math.log(x) / Math.log(10);
- }
+ public static native double log10(double x) /*-{
+ return Math.log(x) * Math.LOG10E;
+ }-*/;
public static double log1p(double x) {
return Math.log(x + 1.0d);
- };
+ }
public static double max(double x, double y) {
return x > y ? x : y;
@@ -199,7 +198,7 @@
} else {
return round(d);
}
- };
+ }
public static long round(double x) {
return (long) round0(x);
diff --git a/user/test/com/google/gwt/emultest/EmulSuite.java b/user/test/com/google/gwt/emultest/EmulSuite.java
index 6e29683..d036516 100644
--- a/user/test/com/google/gwt/emultest/EmulSuite.java
+++ b/user/test/com/google/gwt/emultest/EmulSuite.java
@@ -23,6 +23,7 @@
import com.google.gwt.emultest.java.lang.FloatTest;
import com.google.gwt.emultest.java.lang.IntegerTest;
import com.google.gwt.emultest.java.lang.LongTest;
+import com.google.gwt.emultest.java.lang.MathTest;
import com.google.gwt.emultest.java.lang.ObjectTest;
import com.google.gwt.emultest.java.lang.ShortTest;
import com.google.gwt.emultest.java.lang.StringBufferDefaultImplTest;
@@ -53,7 +54,7 @@
import junit.framework.Test;
/**
- * TODO: document me.
+ * Test JRE emulations.
*/
public class EmulSuite {
@@ -62,6 +63,7 @@
GWTTestSuite suite = new GWTTestSuite("Tests for com.google.gwt.emul.java");
// $JUnit-BEGIN$
+ // java.lang
suite.addTestSuite(BooleanTest.class);
suite.addTestSuite(ByteTest.class);
suite.addTestSuite(CharacterTest.class);
@@ -69,6 +71,7 @@
suite.addTestSuite(DoubleTest.class);
suite.addTestSuite(FloatTest.class);
suite.addTestSuite(LongTest.class);
+ suite.addTestSuite(MathTest.class);
suite.addTestSuite(IntegerTest.class);
suite.addTestSuite(ObjectTest.class);
suite.addTestSuite(ShortTest.class);
@@ -77,6 +80,7 @@
suite.addTestSuite(StringTest.class);
suite.addTestSuite(SystemTest.class);
+ // java.util
suite.addTestSuite(ApacheMapTest.class);
suite.addTestSuite(ArrayListTest.class);
suite.addTestSuite(ArraysTest.class);
@@ -102,5 +106,4 @@
return suite;
}
-
}
diff --git a/user/test/com/google/gwt/emultest/java/lang/MathTest.java b/user/test/com/google/gwt/emultest/java/lang/MathTest.java
new file mode 100644
index 0000000..81f41b4
--- /dev/null
+++ b/user/test/com/google/gwt/emultest/java/lang/MathTest.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2010 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;
+
+/**
+ * Tests for JRE emulation of java.lang.Math.
+ *
+ * TODO: more tests
+ */
+public class MathTest extends GWTTestCase {
+
+ @Override
+ public String getModuleName() {
+ return "com.google.gwt.emultest.EmulSuite";
+ }
+
+ public void testCbrt() {
+ double v = Math.cbrt(1000.0);
+ assertEquals(10.0, v, 1e-7);
+ }
+
+ public void testCos() {
+ double v = Math.cos(0.0);
+ assertEquals(1.0, v, 1e-7);
+ v = Math.cos(Math.PI * .5);
+ assertEquals(0.0, v, 1e-7);
+ v = Math.cos(Math.PI);
+ assertEquals(-1.0, v, 1e-7);
+ v = Math.cos(Math.PI * 1.5);
+ assertEquals(0.0, v, 1e-7);
+ }
+
+ public void testLog() {
+ double v = Math.log(Math.E);
+ assertEquals(1.0, v, 1e-15);
+ }
+
+ public void testLog10() {
+ double v = Math.log10(1000.0);
+ assertEquals(3.0, v, 1e-15);
+ }
+
+ public void testSin() {
+ double v = Math.sin(0.0);
+ assertEquals(0.0, v, 1e-7);
+ v = Math.sin(Math.PI * .5);
+ assertEquals(1.0, v, 1e-7);
+ v = Math.sin(Math.PI);
+ assertEquals(0.0, v, 1e-7);
+ v = Math.sin(Math.PI * 1.5);
+ assertEquals(-1.0, v, 1e-7);
+ }
+}