vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 18 Nov 2012 19:01:38 +0100
branchjavap
changeset 185 d441042e6c11
parent 181 f426de5dc7f6
child 186 123ba97c3718
permissions -rw-r--r--
Reimplementing readDouble to not depend on too high longs
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.ByteArrayInputStream;
    21 import java.io.ByteArrayOutputStream;
    22 import java.io.DataInputStream;
    23 import java.io.DataOutputStream;
    24 import java.io.IOException;
    25 import javax.script.Invocable;
    26 import javax.script.ScriptException;
    27 import static org.testng.Assert.*;
    28 import org.testng.annotations.BeforeClass;
    29 import org.testng.annotations.Test;
    30 
    31 /**
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 public class NumberTest {
    36     @Test public void integerFromString() throws Exception {
    37         assertExec("Can convert string to integer", "java_lang_Integer_parseIntILjava_lang_String",
    38             Double.valueOf(333), "333"
    39         );
    40     }
    41 
    42     @Test public void doubleFromString() throws Exception {
    43         assertExec("Can convert string to double", "java_lang_Double_parseDoubleDLjava_lang_String",
    44             Double.valueOf(33.3), "33.3"
    45         );
    46     }
    47 
    48     @Test public void autoboxDouble() throws Exception {
    49         assertExec("Autoboxing of doubles is OK", "org_apidesign_vm4brwsr_Numbers_autoboxDblToStringLjava_lang_String",
    50             "3.3"
    51         );
    52     }
    53     
    54     @Test public void javalog1000() throws Exception {
    55         assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
    56     }
    57 
    58     @Test public void jslog1000() throws Exception {
    59         assertExec("log_10(1000) == 3", "java_lang_Math_log10DD", 
    60             Double.valueOf(3.0), 1000.0
    61         );
    62     }
    63     
    64     @Test public void javaRem() {
    65         assertEquals(3, Numbers.rem(303, 10));
    66     }
    67     @Test public void jsRem() throws Exception {
    68         assertExec("Should be three", "org_apidesign_vm4brwsr_Numbers_remIII", 
    69             Double.valueOf(3.0), 303, 10
    70         );
    71     }
    72     
    73     @Test public void deserializeInt() throws Exception {
    74         int exp = Numbers.deserInt();
    75         assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserIntI", 
    76             Double.valueOf(exp)
    77         );
    78     }
    79 
    80     @Test public void deserializeSimpleLong() throws Exception {
    81         assertExec("Should be 3454", "org_apidesign_vm4brwsr_Numbers_deserLongJAB", 
    82             Double.valueOf(3454), 
    83             new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
    84         );
    85     }
    86     /* XXX: JavaScript cannot represent as big longs as Java. 
    87     @Test public void deserializeLargeLong() throws Exception {
    88         final byte[] arr = new byte[] {
    89             (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
    90         };
    91         long exp = Numbers.deserLong(arr);
    92         assertExec("Should be " + exp, "org_apidesign_vm4brwsr_Numbers_deserLongJAB", 
    93             Double.valueOf(exp), arr);
    94     }
    95     */
    96     
    97     @Test public void deserializeFloatInJava() throws Exception {
    98         float f = 54324.32423f;
    99         float r = Numbers.deserFloat();
   100         assertEquals(r, f, "Floats are the same");
   101     }
   102     
   103     @Test public void deserializeFloatInJS() throws Exception {
   104         float f = 54324.32423f;
   105         assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserFloatF", 
   106             Double.valueOf(f)
   107         );
   108     }
   109 
   110     @Test public void deserializeDoubleInJava() throws Exception {
   111         double f = 3.0;
   112         double r = Numbers.deserDouble();
   113         assertEquals(r, f, 0.001, "Doubles are the same");
   114     }
   115     
   116     @Test public void deserializeDoubleInJS() throws Exception {
   117         double f = 3.0;
   118         assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserDoubleD", f);
   119     }
   120     /*
   121     @Test public void serDouble() throws IOException {
   122         double f = 3.0;
   123         ByteArrayOutputStream os = new ByteArrayOutputStream();
   124         DataOutputStream d = new DataOutputStream(os);
   125         d.writeLong(3454);
   126         d.close();
   127         
   128         StringBuilder sb = new StringBuilder();
   129         byte[] arr = os.toByteArray();
   130         for (int i = 0; i < arr.length; i++) {
   131             sb.append("(byte)").append(arr[i]).append(", ");
   132         }
   133         fail("" + sb);
   134     }
   135 */    
   136     private static CharSequence codeSeq;
   137     private static Invocable code;
   138 
   139     @BeforeClass
   140     public void compileTheCode() throws Exception {
   141         if (codeSeq == null) {
   142             StringBuilder sb = new StringBuilder();
   143             code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
   144             codeSeq = sb;
   145         }
   146     }
   147 
   148     private static void assertExec(
   149         String msg, String methodName, Object expRes, Object... args) throws Exception {
   150 
   151         Object ret = null;
   152         try {
   153             ret = code.invokeFunction(methodName, args);
   154         } catch (ScriptException ex) {
   155             fail("Execution failed in\n" + codeSeq, ex);
   156         } catch (NoSuchMethodException ex) {
   157             fail("Cannot find method in\n" + codeSeq, ex);
   158         }
   159         if (ret == null && expRes == null) {
   160             return;
   161         }
   162         if (expRes.equals(ret)) {
   163             return;
   164         }
   165         if (expRes instanceof Double && ret instanceof Double) {
   166             double expD = ((Double)expRes).doubleValue();
   167             double retD = ((Double)ret).doubleValue();
   168             assertEquals(retD, expD, 0.000004, msg + " was " + ret + "\n" + codeSeq);
   169             return;
   170         }
   171         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
   172     }
   173     
   174 }