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