rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 16 May 2013 08:49:55 +0200
changeset 1097 8e42a376da73
parent 791 af4001c85438
child 1460 ef506621d1ee
permissions -rw-r--r--
java.lang.Boolean is subclass of JavaScript's Boolean. true and false are instances of java.lang.Boolean
     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     @Test public void everyNumberHasJavaLangNumberMethods() throws Exception {
   147         assertExec("Can we call doubleValue?", 
   148             Numbers.class, "seven__DI", 
   149             Double.valueOf(7.0), 0
   150         );
   151     }
   152     @Test public void everyNumberHasJavaLangNumberMethodsInt() throws Exception {
   153         assertExec("Can we call doubleValue?", 
   154             Numbers.class, "seven__DI", 
   155             Double.valueOf(7.0), 1
   156         );
   157     }
   158     @Test public void everyNumberHasJavaLangNumberMethodsLong() throws Exception {
   159         assertExec("Can we call doubleValue?", 
   160             Numbers.class, "seven__DI", 
   161             Double.valueOf(7.0), 2
   162         );
   163     }
   164     @Test public void everyNumberHasJavaLangNumberMethodsShort() throws Exception {
   165         assertExec("Can we call doubleValue?", 
   166             Numbers.class, "seven__DI", 
   167             Double.valueOf(7.0), 3
   168         );
   169     }
   170     @Test public void everyNumberHasJavaLangNumberMethodsByte() throws Exception {
   171         assertExec("Can we call doubleValue?", 
   172             Numbers.class, "seven__DI", 
   173             Double.valueOf(7.0), 4
   174         );
   175     }
   176     @Test public void valueOfNumber() throws Exception {
   177         assertExec("Can we call JavaScripts valueOf?", 
   178             Numbers.class, "seven__DI", 
   179             Double.valueOf(7.0), 8
   180         );
   181     }
   182     @Test public void valueOfLongNumber() throws Exception {
   183         assertExec("Can we call JavaScripts valueOf?", 
   184             Numbers.class, "seven__DI", 
   185             Double.valueOf(Long.MAX_VALUE / 5), 9
   186         );
   187     }
   188     @Test public void valueOfLongCharA() throws Exception {
   189         assertExec("Can we call JavaScripts valueOf on Character?", 
   190             Numbers.class, "seven__DI", 
   191             Double.valueOf('A'), 65
   192         );
   193     }
   194 
   195     @Test public void valueOfLongBooleanTrue() throws Exception {
   196         assertExec("Can we call JavaScripts valueOf on Boolean?", 
   197             Numbers.class, "bseven__ZI", 
   198             true, 31
   199         );
   200     }
   201     @Test public void valueOfLongBooleanFalse() throws Exception {
   202         assertExec("Can we call JavaScripts valueOf on Boolean?", 
   203             Numbers.class, "bseven__ZI", 
   204             false, 30
   205         );
   206     }
   207 
   208     private static TestVM code;
   209 
   210     @BeforeClass
   211     public static void compileTheCode() throws Exception {
   212         code = TestVM.compileClass("org/apidesign/vm4brwsr/Numbers");
   213     }
   214     @AfterClass
   215     public static void releaseTheCode() {
   216         code = null;
   217     }
   218 
   219     private static void assertExec(
   220         String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
   221     {
   222         Object ret = code.execCode(msg, clazz, method, expRes, args);
   223         if (ret == null) {
   224             return;
   225         }
   226         if (expRes instanceof Double && ret instanceof Double) {
   227             double expD = ((Double)expRes).doubleValue();
   228             double retD = ((Double)ret).doubleValue();
   229             assertEquals(retD, expD, 0.000004, msg + " "
   230                     + code.toString());
   231             return;
   232         }
   233         assertEquals(ret, expRes, msg + " " + code.toString());
   234     }
   235     
   236 }