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", Integer.class, "parseInt__ILjava_lang_String_2",
33 Double.valueOf(333), "333"
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"
43 @Test public void autoboxDouble() throws Exception {
44 assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToString__Ljava_lang_String_2",
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", Math.class, "log10__DD",
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", Numbers.class, "rem__III",
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", Numbers.class, "deserInt__I",
75 @Test public void deserializeSimpleLong() throws Exception {
76 assertExec("Should be 3454", Numbers.class, "deserLong__J_3B",
78 new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
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
86 long exp = Numbers.deserLong(arr);
87 assertExec("Should be " + exp, "org_apidesign_vm4brwsr_Numbers_deserLong__JAB",
88 Double.valueOf(exp), arr);
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");
98 @Test public void deserializeFloatInJS() throws Exception {
99 float f = 54324.32423f;
100 assertExec("Should be the same", Numbers.class, "deserFloat__F",
105 @Test public void deserializeDoubleInJava() throws Exception {
107 double r = Numbers.deserDouble();
108 assertEquals(r, f, 0.001, "Doubles are the same");
111 @Test public void deserializeDoubleInJS() throws Exception {
113 assertExec("Should be the same", Numbers.class, "deserDouble__D", f);
116 @Test public void serDouble() throws IOException {
118 ByteArrayOutputStream os = new ByteArrayOutputStream();
119 DataOutputStream d = new DataOutputStream(os);
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(", ");
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",
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",
147 private static CharSequence codeSeq;
148 private static Invocable code;
151 public void compileTheCode() throws Exception {
152 if (codeSeq == null) {
153 StringBuilder sb = new StringBuilder();
154 code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
159 private static void assertExec(
160 String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception {
164 ret = code.invokeFunction(clazz.getName().replace('.', '_'), true);
165 ret = code.invokeMethod(ret, method, args);
166 } catch (ScriptException ex) {
167 fail("Execution failed in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
168 } catch (NoSuchMethodException ex) {
169 fail("Cannot find method in\n" + StaticMethodTest.dumpJS(codeSeq), ex);
171 if (ret == null && expRes == null) {
174 if (expRes.equals(ret)) {
177 if (expRes instanceof Double && ret instanceof Double) {
178 double expD = ((Double)expRes).doubleValue();
179 double retD = ((Double)ret).doubleValue();
180 assertEquals(retD, expD, 0.000004, msg + " was " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
183 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));