rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 747 vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java@ae352b763959
child 783 8264f07b1f46
child 789 bb7506513353
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     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 static org.testng.Assert.*;
    21 import org.testng.annotations.BeforeClass;
    22 import org.testng.annotations.Test;
    23 
    24 /**
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 public class NumberTest {
    29     @Test public void integerFromString() throws Exception {
    30         assertExec("Can convert string to integer", Integer.class, "parseInt__ILjava_lang_String_2",
    31             Double.valueOf(333), "333"
    32         );
    33     }
    34 
    35     @Test public void doubleFromString() throws Exception {
    36         assertExec("Can convert string to double", Double.class, "parseDouble__DLjava_lang_String_2",
    37             Double.valueOf(33.3), "33.3"
    38         );
    39     }
    40 
    41     @Test public void autoboxDouble() throws Exception {
    42         assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToString__Ljava_lang_String_2",
    43             "3.3"
    44         );
    45     }
    46     
    47     @Test public void javalog1000() throws Exception {
    48         assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
    49     }
    50 
    51     @Test public void jslog1000() throws Exception {
    52         assertExec("log_10(1000) == 3", Math.class, "log10__DD", 
    53             Double.valueOf(3.0), 1000.0
    54         );
    55     }
    56     
    57     @Test public void javaRem() {
    58         assertEquals(3, Numbers.rem(303, 10));
    59     }
    60     @Test public void jsRem() throws Exception {
    61         assertExec("Should be three", Numbers.class, "rem__III", 
    62             Double.valueOf(3.0), 303, 10
    63         );
    64     }
    65     
    66     @Test public void deserializeInt() throws Exception {
    67         int exp = Numbers.deserInt();
    68         assertExec("Should be the same", Numbers.class, "deserInt__I", 
    69             Double.valueOf(exp)
    70         );
    71     }
    72 
    73     @Test public void deserializeSimpleLong() throws Exception {
    74         assertExec("Should be 3454", Numbers.class, "deserLong__J_3B", 
    75             Double.valueOf(3454), 
    76             new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
    77         );
    78     }
    79     /* XXX: JavaScript cannot represent as big longs as Java. 
    80     @Test public void deserializeLargeLong() throws Exception {
    81         final byte[] arr = new byte[] {
    82             (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
    83         };
    84         long exp = Numbers.deserLong(arr);
    85         assertExec("Should be " + exp, "org_apidesign_vm4brwsr_Numbers_deserLong__JAB", 
    86             Double.valueOf(exp), arr);
    87     }
    88     */
    89     
    90     @Test public void deserializeFloatInJava() throws Exception {
    91         float f = 54324.32423f;
    92         float r = Numbers.deserFloat();
    93         assertEquals(r, f, "Floats are the same");
    94     }
    95     
    96     @Test public void deserializeFloatInJS() throws Exception {
    97         float f = 54324.32423f;
    98         assertExec("Should be the same", Numbers.class, "deserFloat__F", 
    99             Double.valueOf(f)
   100         );
   101     }
   102 
   103     @Test public void deserializeDoubleInJava() throws Exception {
   104         double f = 3.0;
   105         double r = Numbers.deserDouble();
   106         assertEquals(r, f, 0.001, "Doubles are the same");
   107     }
   108     
   109     @Test public void deserializeDoubleInJS() throws Exception {
   110         double f = 3.0;
   111         assertExec("Should be the same", Numbers.class, "deserDouble__D", f);
   112     }
   113     /*
   114     @Test public void serDouble() throws IOException {
   115         double f = 3.0;
   116         ByteArrayOutputStream os = new ByteArrayOutputStream();
   117         DataOutputStream d = new DataOutputStream(os);
   118         d.writeLong(3454);
   119         d.close();
   120         
   121         StringBuilder sb = new StringBuilder();
   122         byte[] arr = os.toByteArray();
   123         for (int i = 0; i < arr.length; i++) {
   124             sb.append("(byte)").append(arr[i]).append(", ");
   125         }
   126         fail("" + sb);
   127     }
   128 */    
   129     @Test public void fiveInStringJS() throws Exception {
   130         String s = Numbers.intToString();
   131         assertExec("Should be the same: " + s, 
   132             Numbers.class, "intToString__Ljava_lang_String_2", 
   133             s
   134         );
   135     }
   136 
   137     @Test public void sevenInStringJS() throws Exception {
   138         String s = Numbers.floatToString();
   139         assertExec("Should be the same: " + s, 
   140             Numbers.class, "floatToString__Ljava_lang_String_2", 
   141             s
   142         );
   143     }
   144 
   145     private static TestVM code;
   146 
   147     @BeforeClass
   148     public void compileTheCode() throws Exception {
   149         code = TestVM.compileClass("org/apidesign/vm4brwsr/Numbers");
   150     }
   151 
   152     private static void assertExec(
   153         String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
   154     {
   155         Object ret = code.execCode(msg, clazz, method, expRes, args);
   156         if (ret == null) {
   157             return;
   158         }
   159         if (expRes instanceof Double && ret instanceof Double) {
   160             double expD = ((Double)expRes).doubleValue();
   161             double retD = ((Double)ret).doubleValue();
   162             assertEquals(retD, expD, 0.000004, msg + " "
   163                     + code.toString());
   164             return;
   165         }
   166         assertEquals(ret, expRes, msg + " " + code.toString());
   167     }
   168     
   169 }