# HG changeset patch # User Jaroslav Tulach # Date 1362782581 -3600 # Node ID 97fdbed30f8bc7121d8bbf0fe114ea5dd0bd0c5e # Parent 1e9d34a337f2bbf763e17d27691fa3cd5f3e4a66 Only add the trailing '.0' if the number is known to be integer. Otherwise accept the string as provided by JavaScript diff -r 1e9d34a337f2 -r 97fdbed30f8b rt/emul/mini/src/main/java/java/lang/Double.java --- a/rt/emul/mini/src/main/java/java/lang/Double.java Fri Mar 08 00:42:02 2013 +0100 +++ b/rt/emul/mini/src/main/java/java/lang/Double.java Fri Mar 08 23:43:01 2013 +0100 @@ -190,12 +190,11 @@ * @param d the {@code double} to be converted. * @return a string representation of the argument. */ - @JavaScriptBody(args="d", body="var r = d.toString();" - + "if (isFinite(d) && (r.indexOf('.') === -1)) r = r + '.0';" - + "return r;") - public static String toString(double d) { - throw new UnsupportedOperationException(); - } + @JavaScriptBody(args="d", body="var f = Math.floor(d);\n" + + "if (f === d && isFinite(d)) return d.toString() + '.0';\n" + + "else return d.toString();" + ) + public static native String toString(double d); /** * Returns a hexadecimal string representation of the diff -r 1e9d34a337f2 -r 97fdbed30f8b rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java --- a/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java Fri Mar 08 00:42:02 2013 +0100 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java Fri Mar 08 23:43:01 2013 +0100 @@ -83,10 +83,23 @@ @Test(dependsOnGroups = "run") public void compareResults() throws Throwable { Object v1 = first.value; Object v2 = second.value; - if (v1 != null) { - v1 = v1.toString(); + if (v1 instanceof Number) { + try { + v1 = Double.parseDouble(v1.toString()); + } catch (NumberFormatException nfe) { + v1 = "Can't parse " + v1.toString(); + } + try { + v2 = Double.parseDouble(v2.toString()); + } catch (NumberFormatException nfe) { + v2 = "Can't parse " + v2.toString(); + } } else { - v1 = "null"; + if (v1 != null) { + v1 = v1.toString(); + } else { + v1 = "null"; + } } try { Assert.assertEquals(v2, v1, "Comparing results"); diff -r 1e9d34a337f2 -r 97fdbed30f8b rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/DoubleTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/DoubleTest.java Fri Mar 08 23:43:01 2013 +0100 @@ -0,0 +1,61 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.tck; + +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class DoubleTest { + @Compare public String integerToString() { + return toStr(1); + } + + @Compare public String integerAndHalfToString() { + return toStr(1.5); + } + + @Compare public double longToAndBack() { + return Double.parseDouble(toStr(Long.MAX_VALUE / 10)); + } + + @Compare public String negativeIntToString() { + return toStr(-10); + } + + @Compare public String negativeIntAndHalfToString() { + return toStr(-10.5); + } + + @Compare public double negativeLongAndBack() { + return Double.parseDouble(toStr(Long.MIN_VALUE / 10)); + } + + private static String toStr(double d) { + return Double.toString(d); + } + + @Factory + public static Object[] create() { + return VMTest.create(DoubleTest.class); + } +}