vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 18 Nov 2012 16:45:50 +0100
branchjavap
changeset 184 03be8510007c
parent 178 28143312edb5
child 185 d441042e6c11
permissions -rw-r--r--
Chars need to be treated as numbers, otherwise switch statement does not work
     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 javax.script.Invocable;
    21 import javax.script.ScriptException;
    22 import static org.testng.Assert.*;
    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 NumberTest {
    31     @Test public void integerFromString() throws Exception {
    32         assertExec("Can convert string to integer", "java_lang_Integer_parseIntILjava_lang_String",
    33             Double.valueOf(333), "333"
    34         );
    35     }
    36 
    37     @Test public void doubleFromString() throws Exception {
    38         assertExec("Can convert string to double", "java_lang_Double_parseDoubleDLjava_lang_String",
    39             Double.valueOf(33.3), "33.3"
    40         );
    41     }
    42 
    43     @Test public void autoboxDouble() throws Exception {
    44         assertExec("Autoboxing of doubles is OK", "org_apidesign_vm4brwsr_Numbers_autoboxDblToStringLjava_lang_String",
    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", "java_lang_Math_log10DD", 
    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", "org_apidesign_vm4brwsr_Numbers_remIII", 
    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", "org_apidesign_vm4brwsr_Numbers_deserIntI", 
    71             Double.valueOf(exp)
    72         );
    73     }
    74     
    75     @Test public void deserializeFloatInJava() throws Exception {
    76         float f = 54324.32423f;
    77         float r = Numbers.deserFloat();
    78         assertEquals(r, f, "Floats are the same");
    79     }
    80     
    81     @Test public void deserializeFloatInJS() throws Exception {
    82         float f = 54324.32423f;
    83         assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserFloatF", 
    84             Double.valueOf(f)
    85         );
    86     }
    87 
    88     
    89     private static CharSequence codeSeq;
    90     private static Invocable code;
    91 
    92     @BeforeClass
    93     public void compileTheCode() throws Exception {
    94         if (codeSeq == null) {
    95             StringBuilder sb = new StringBuilder();
    96             code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
    97             codeSeq = sb;
    98         }
    99     }
   100 
   101     private static void assertExec(
   102         String msg, String methodName, Object expRes, Object... args) throws Exception {
   103 
   104         Object ret = null;
   105         try {
   106             ret = code.invokeFunction(methodName, args);
   107         } catch (ScriptException ex) {
   108             fail("Execution failed in\n" + codeSeq, ex);
   109         } catch (NoSuchMethodException ex) {
   110             fail("Cannot find method in\n" + codeSeq, ex);
   111         }
   112         if (ret == null && expRes == null) {
   113             return;
   114         }
   115         if (expRes.equals(ret)) {
   116             return;
   117         }
   118         if (expRes instanceof Double && ret instanceof Double) {
   119             double expD = ((Double)expRes).doubleValue();
   120             double retD = ((Double)ret).doubleValue();
   121             assertEquals(retD, expD, 0.000004, msg + " was " + ret + "\n" + codeSeq);
   122             return;
   123         }
   124         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
   125     }
   126     
   127 }