2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.vm4brwsr;
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;
28 * @author Jaroslav Tulach <jtulach@netbeans.org>
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"
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"
43 @Test public void autoboxDouble() throws Exception {
44 assertExec("Autoboxing of doubles is OK", "org_apidesign_vm4brwsr_Numbers_autoboxDblToStringLjava_lang_String",
49 @Test public void javalog1000() throws Exception {
50 assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
53 @Test public void jslog1000() throws Exception {
54 assertExec("log_10(1000) == 3", "java_lang_Math_log10DD",
55 Double.valueOf(3.0), 1000.0
59 @Test public void javaRem() {
60 assertEquals(3, Numbers.rem(303, 10));
62 @Test public void jsRem() throws Exception {
63 assertExec("Should be three", "org_apidesign_vm4brwsr_Numbers_remIII",
64 Double.valueOf(3.0), 303, 10
68 @Test public void deserializeInt() throws Exception {
69 int exp = Numbers.deserInt();
70 assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserIntI",
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");
81 @Test public void deserializeFloatInJS() throws Exception {
82 float f = 54324.32423f;
83 assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserFloatF",
89 private static CharSequence codeSeq;
90 private static Invocable code;
93 public void compileTheCode() throws Exception {
94 if (codeSeq == null) {
95 StringBuilder sb = new StringBuilder();
96 code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
101 private static void assertExec(
102 String msg, String methodName, Object expRes, Object... args) throws Exception {
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);
112 if (ret == null && expRes == null) {
115 if (expRes.equals(ret)) {
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);
124 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);