vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 30 Oct 2012 23:33:29 +0100
changeset 132 2377bb30dd1b
parent 114 a0505844750a
child 178 28143312edb5
permissions -rw-r--r--
Removing StrictMath
     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     
    60     private static CharSequence codeSeq;
    61     private static Invocable code;
    62 
    63     @BeforeClass
    64     public void compileTheCode() throws Exception {
    65         if (codeSeq == null) {
    66             StringBuilder sb = new StringBuilder();
    67             code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
    68             codeSeq = sb;
    69         }
    70     }
    71 
    72     private static void assertExec(
    73         String msg, String methodName, Object expRes, Object... args) throws Exception {
    74 
    75         Object ret = null;
    76         try {
    77             ret = code.invokeFunction(methodName, args);
    78         } catch (ScriptException ex) {
    79             fail("Execution failed in\n" + codeSeq, ex);
    80         } catch (NoSuchMethodException ex) {
    81             fail("Cannot find method in\n" + codeSeq, ex);
    82         }
    83         if (ret == null && expRes == null) {
    84             return;
    85         }
    86         if (expRes.equals(ret)) {
    87             return;
    88         }
    89         if (expRes instanceof Double && ret instanceof Double) {
    90             double expD = ((Double)expRes).doubleValue();
    91             double retD = ((Double)ret).doubleValue();
    92             assertEquals(retD, expD, 0.000004, msg + " was " + ret + "\n" + codeSeq);
    93             return;
    94         }
    95         assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
    96     }
    97     
    98 }