rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 26 May 2014 14:33:11 +0200
branchclosure
changeset 1595 19d0484c1916
parent 1462 1e7ff3ba3666
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Need to flush the stack before destroying it with return value of a function
     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     @Test public void deserializeMiddleLong() throws Exception {
    81         final byte[] arr = new byte[] {
    82             (byte)0, (byte)0, (byte)64, (byte)32, (byte)23, (byte)0, (byte)0, (byte)0
    83         };
    84         long exp = Numbers.deserLong(arr, 16);
    85         assertExec("Should be " + exp, Numbers.class, "deserLong__J_3BI", 
    86             Double.valueOf(exp), arr, 16);
    87     }
    88     @Test public void deserializeLargeLong() throws Exception {
    89         final byte[] arr = new byte[] {
    90             (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
    91         };
    92         long exp = Numbers.deserLong(arr, 32);
    93         assertExec("Should be " + exp, Numbers.class, "deserLong__J_3BI", 
    94             Double.valueOf(exp), arr, 32);
    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", Numbers.class, "deserFloat__F", 
   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", Numbers.class, "deserDouble__D", f);
   119     }
   120     
   121     @Test public void bytesToLong() throws Exception {
   122         long exp = Numbers.bytesToLong((byte)30, (byte)20, 32);
   123         assertExec("Should be the same", Numbers.class, "bytesToLong__JBBI", 
   124             Double.valueOf(exp), 30, 20, 32);
   125     }
   126     /*
   127     @Test public void serDouble() throws IOException {
   128         double f = 3.0;
   129         ByteArrayOutputStream os = new ByteArrayOutputStream();
   130         DataOutputStream d = new DataOutputStream(os);
   131         d.writeLong(3454);
   132         d.close();
   133         
   134         StringBuilder sb = new StringBuilder();
   135         byte[] arr = os.toByteArray();
   136         for (int i = 0; i < arr.length; i++) {
   137             sb.append("(byte)").append(arr[i]).append(", ");
   138         }
   139         fail("" + sb);
   140     }
   141 */    
   142     @Test public void fiveInStringJS() throws Exception {
   143         String s = Numbers.intToString();
   144         assertExec("Should be the same: " + s, 
   145             Numbers.class, "intToString__Ljava_lang_String_2", 
   146             s
   147         );
   148     }
   149 
   150     @Test public void sevenInStringJS() throws Exception {
   151         String s = Numbers.floatToString();
   152         assertExec("Should be the same: " + s, 
   153             Numbers.class, "floatToString__Ljava_lang_String_2", 
   154             s
   155         );
   156     }
   157 
   158     @Test public void everyNumberHasJavaLangNumberMethods() throws Exception {
   159         assertExec("Can we call doubleValue?", 
   160             Numbers.class, "seven__DI", 
   161             Double.valueOf(7.0), 0
   162         );
   163     }
   164     @Test public void everyNumberHasJavaLangNumberMethodsInt() throws Exception {
   165         assertExec("Can we call doubleValue?", 
   166             Numbers.class, "seven__DI", 
   167             Double.valueOf(7.0), 1
   168         );
   169     }
   170     @Test public void everyNumberHasJavaLangNumberMethodsLong() throws Exception {
   171         assertExec("Can we call doubleValue?", 
   172             Numbers.class, "seven__DI", 
   173             Double.valueOf(7.0), 2
   174         );
   175     }
   176     @Test public void everyNumberHasJavaLangNumberMethodsShort() throws Exception {
   177         assertExec("Can we call doubleValue?", 
   178             Numbers.class, "seven__DI", 
   179             Double.valueOf(7.0), 3
   180         );
   181     }
   182     @Test public void everyNumberHasJavaLangNumberMethodsByte() throws Exception {
   183         assertExec("Can we call doubleValue?", 
   184             Numbers.class, "seven__DI", 
   185             Double.valueOf(7.0), 4
   186         );
   187     }
   188     @Test public void valueOfNumber() throws Exception {
   189         assertExec("Can we call JavaScripts valueOf?", 
   190             Numbers.class, "seven__DI", 
   191             Double.valueOf(7.0), 8
   192         );
   193     }
   194     @Test public void valueOfLongNumber() throws Exception {
   195         assertExec("Can we call JavaScripts valueOf?", 
   196             Numbers.class, "seven__DI", 
   197             Double.valueOf(Long.MAX_VALUE / 5), 9
   198         );
   199     }
   200     @Test public void valueOfLongCharA() throws Exception {
   201         assertExec("Can we call JavaScripts valueOf on Character?", 
   202             Numbers.class, "seven__DI", 
   203             Double.valueOf('A'), 65
   204         );
   205     }
   206 
   207     @Test public void valueOfLongBooleanTrue() throws Exception {
   208         assertExec("Can we call JavaScripts valueOf on Boolean?", 
   209             Numbers.class, "bseven__ZI", 
   210             true, 31
   211         );
   212     }
   213     @Test public void valueOfLongBooleanFalse() throws Exception {
   214         assertExec("Can we call JavaScripts valueOf on Boolean?", 
   215             Numbers.class, "bseven__ZI", 
   216             false, 30
   217         );
   218     }
   219     
   220     @Test public void computeAround() throws Exception {
   221         double exp = Numbers.around(new Object(), 5, 8);
   222         assertExec("Computes the same value", 
   223             Numbers.class, "around__ILjava_lang_Object_2II", 
   224             exp, null, 5, 8
   225         );
   226     }
   227 
   228     private static TestVM code;
   229 
   230     @BeforeClass
   231     public static void compileTheCode() throws Exception {
   232         code = TestVM.compileClass("org/apidesign/vm4brwsr/Numbers");
   233     }
   234     @AfterClass
   235     public static void releaseTheCode() {
   236         code = null;
   237     }
   238 
   239     private static void assertExec(
   240         String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
   241     {
   242         Object ret = code.execCode(msg, clazz, method, expRes, args);
   243         if (ret == null) {
   244             return;
   245         }
   246         if (expRes instanceof Double && ret instanceof Double) {
   247             double expD = ((Double)expRes).doubleValue();
   248             double retD = ((Double)ret).doubleValue();
   249             assertEquals(retD, expD, 0.000004, msg + " "
   250                     + code.toString());
   251             return;
   252         }
   253         assertEquals(ret, expRes, msg + " " + code.toString());
   254     }
   255     
   256 }