rt/vm/src/test/java/org/apidesign/vm4brwsr/NumbersAsExtensionTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 24 Feb 2015 11:12:53 +0100
changeset 1787 ea12a3bb4b33
parent 1497 daa5f903b529
permissions -rw-r--r--
Using year range 2012-2015 in copyright header
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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 javax.script.ScriptEngine;
    21 import static org.testng.Assert.*;
    22 import org.testng.annotations.AfterClass;
    23 import org.testng.annotations.BeforeClass;
    24 import org.testng.annotations.Test;
    25 
    26 /**
    27  *
    28  * @author Jaroslav Tulach <jtulach@netbeans.org>
    29  */
    30 public class NumbersAsExtensionTest {
    31     @Test public void integerFromString() throws Exception {
    32         assertExec("Can convert string to integer", Integer.class, "parseInt__ILjava_lang_String_2",
    33             Double.valueOf(333), "333"
    34         );
    35     }
    36 
    37     @Test public void doubleFromString() throws Exception {
    38         assertExec("Can convert string to double", Double.class, "parseDouble__DLjava_lang_String_2",
    39             Double.valueOf(33.3), "33.3"
    40         );
    41     }
    42 
    43     @Test public void autoboxDouble() throws Exception {
    44         assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToString__Ljava_lang_String_2",
    45             "3.3"
    46         );
    47     }
    48     
    49     @Test public void javalog1000() throws Exception {
    50         assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
    51     }
    52 
    53     @Test public void jslog1000() throws Exception {
    54         assertExec("log_10(1000) == 3", Math.class, "log10__DD", 
    55             Double.valueOf(3.0), 1000.0
    56         );
    57     }
    58     
    59     @Test public void javaRem() {
    60         assertEquals(3, Numbers.rem(303, 10));
    61     }
    62     @Test public void jsRem() throws Exception {
    63         assertExec("Should be three", Numbers.class, "rem__III", 
    64             Double.valueOf(3.0), 303, 10
    65         );
    66     }
    67     
    68     @Test public void deserializeInt() throws Exception {
    69         int exp = Numbers.deserInt();
    70         assertExec("Should be the same", Numbers.class, "deserInt__I", 
    71             Double.valueOf(exp)
    72         );
    73     }
    74 
    75     @Test public void deserializeSimpleLong() throws Exception {
    76         assertExec("Should be 3454", Numbers.class, "deserLong__J_3B", 
    77             Double.valueOf(3454), 
    78             new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
    79         );
    80     }
    81     /* XXX: JavaScript cannot represent as big longs as Java. 
    82     @Test public void deserializeLargeLong() throws Exception {
    83         final byte[] arr = new byte[] {
    84             (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
    85         };
    86         long exp = Numbers.deserLong(arr);
    87         assertExec("Should be " + exp, "org_apidesign_vm4brwsr_Numbers_deserLong__JAB", 
    88             Double.valueOf(exp), arr);
    89     }
    90     */
    91     
    92     @Test public void deserializeFloatInJava() throws Exception {
    93         float f = 54324.32423f;
    94         float r = Numbers.deserFloat();
    95         assertEquals(r, f, "Floats are the same");
    96     }
    97     
    98     @Test public void deserializeFloatInJS() throws Exception {
    99         float f = 54324.32423f;
   100         assertExec("Should be the same", Numbers.class, "deserFloat__F", 
   101             Double.valueOf(f)
   102         );
   103     }
   104 
   105     @Test public void deserializeDoubleInJava() throws Exception {
   106         double f = 3.0;
   107         double r = Numbers.deserDouble();
   108         assertEquals(r, f, 0.001, "Doubles are the same");
   109     }
   110     
   111     @Test public void deserializeDoubleInJS() throws Exception {
   112         double f = 3.0;
   113         assertExec("Should be the same", Numbers.class, "deserDouble__D", f);
   114     }
   115     /*
   116     @Test public void serDouble() throws IOException {
   117         double f = 3.0;
   118         ByteArrayOutputStream os = new ByteArrayOutputStream();
   119         DataOutputStream d = new DataOutputStream(os);
   120         d.writeLong(3454);
   121         d.close();
   122         
   123         StringBuilder sb = new StringBuilder();
   124         byte[] arr = os.toByteArray();
   125         for (int i = 0; i < arr.length; i++) {
   126             sb.append("(byte)").append(arr[i]).append(", ");
   127         }
   128         fail("" + sb);
   129     }
   130 */    
   131     @Test public void fiveInStringJS() throws Exception {
   132         String s = Numbers.intToString();
   133         assertExec("Should be the same: " + s, 
   134             Numbers.class, "intToString__Ljava_lang_String_2", 
   135             s
   136         );
   137     }
   138 
   139     @Test public void sevenInStringJS() throws Exception {
   140         String s = Numbers.floatToString();
   141         assertExec("Should be the same: " + s, 
   142             Numbers.class, "floatToString__Ljava_lang_String_2", 
   143             s
   144         );
   145     }
   146 
   147     @Test public void everyNumberHasJavaLangNumberMethods() throws Exception {
   148         assertExec("Can we call doubleValue?", 
   149             Numbers.class, "seven__DI", 
   150             Double.valueOf(7.0), 0
   151         );
   152     }
   153     @Test public void everyNumberHasJavaLangNumberMethodsInt() throws Exception {
   154         assertExec("Can we call doubleValue?", 
   155             Numbers.class, "seven__DI", 
   156             Double.valueOf(7.0), 1
   157         );
   158     }
   159     @Test public void everyNumberHasJavaLangNumberMethodsLong() throws Exception {
   160         assertExec("Can we call doubleValue?", 
   161             Numbers.class, "seven__DI", 
   162             Double.valueOf(7.0), 2
   163         );
   164     }
   165     @Test public void everyNumberHasJavaLangNumberMethodsShort() throws Exception {
   166         assertExec("Can we call doubleValue?", 
   167             Numbers.class, "seven__DI", 
   168             Double.valueOf(7.0), 3
   169         );
   170     }
   171     @Test public void everyNumberHasJavaLangNumberMethodsByte() throws Exception {
   172         assertExec("Can we call doubleValue?", 
   173             Numbers.class, "seven__DI", 
   174             Double.valueOf(7.0), 4
   175         );
   176     }
   177     @Test public void valueOfNumber() throws Exception {
   178         assertExec("Can we call JavaScripts valueOf?", 
   179             Numbers.class, "seven__DI", 
   180             Double.valueOf(7.0), 8
   181         );
   182     }
   183     @Test public void valueOfLongNumber() throws Exception {
   184         assertExec("Can we call JavaScripts valueOf?", 
   185             Numbers.class, "seven__DI", 
   186             Double.valueOf(Long.MAX_VALUE / 5), 9
   187         );
   188     }
   189     @Test public void valueOfLongCharA() throws Exception {
   190         assertExec("Can we call JavaScripts valueOf on Character?", 
   191             Numbers.class, "seven__DI", 
   192             Double.valueOf('A'), 65
   193         );
   194     }
   195 
   196     @Test public void valueOfLongBooleanTrue() throws Exception {
   197         assertExec("Can we call JavaScripts valueOf on Boolean?", 
   198             Numbers.class, "bseven__ZI", 
   199             true, 31
   200         );
   201     }
   202     @Test public void valueOfLongBooleanFalse() throws Exception {
   203         assertExec("Can we call JavaScripts valueOf on Boolean?", 
   204             Numbers.class, "bseven__ZI", 
   205             false, 30
   206         );
   207     }
   208 
   209     private static TestVM code;
   210 
   211     @BeforeClass
   212     public static void compileTheCode() throws Exception {
   213         ScriptEngine[] eng = { null };
   214         code = TestVM.compileClassAsExtension(null, eng, "org/apidesign/vm4brwsr/Numbers", null, null);
   215     }
   216     @AfterClass
   217     public static void releaseTheCode() {
   218         code = null;
   219     }
   220 
   221     private static void assertExec(
   222         String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
   223     {
   224         Object ret = code.execCode(msg, clazz, method, expRes, args);
   225         if (ret == null) {
   226             return;
   227         }
   228         if (expRes instanceof Double && ret instanceof Double) {
   229             double expD = ((Double)expRes).doubleValue();
   230             double retD = ((Double)ret).doubleValue();
   231             assertEquals(retD, expD, 0.000004, msg + " "
   232                     + code.toString());
   233             return;
   234         }
   235         assertEquals(ret, expRes, msg + " " + code.toString());
   236     }
   237 
   238 
   239     
   240 }