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 java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.DataInputStream;
23 import java.io.DataOutputStream;
24 import java.io.IOException;
25 import javax.script.Invocable;
26 import javax.script.ScriptException;
27 import static org.testng.Assert.*;
28 import org.testng.annotations.BeforeClass;
29 import org.testng.annotations.Test;
33 * @author Jaroslav Tulach <jtulach@netbeans.org>
35 public class NumberTest {
36 @Test public void integerFromString() throws Exception {
37 assertExec("Can convert string to integer", "java_lang_Integer_parseIntILjava_lang_String",
38 Double.valueOf(333), "333"
42 @Test public void doubleFromString() throws Exception {
43 assertExec("Can convert string to double", "java_lang_Double_parseDoubleDLjava_lang_String",
44 Double.valueOf(33.3), "33.3"
48 @Test public void autoboxDouble() throws Exception {
49 assertExec("Autoboxing of doubles is OK", "org_apidesign_vm4brwsr_Numbers_autoboxDblToStringLjava_lang_String",
54 @Test public void javalog1000() throws Exception {
55 assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
58 @Test public void jslog1000() throws Exception {
59 assertExec("log_10(1000) == 3", "java_lang_Math_log10DD",
60 Double.valueOf(3.0), 1000.0
64 @Test public void javaRem() {
65 assertEquals(3, Numbers.rem(303, 10));
67 @Test public void jsRem() throws Exception {
68 assertExec("Should be three", "org_apidesign_vm4brwsr_Numbers_remIII",
69 Double.valueOf(3.0), 303, 10
73 @Test public void deserializeInt() throws Exception {
74 int exp = Numbers.deserInt();
75 assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserIntI",
80 @Test public void deserializeSimpleLong() throws Exception {
81 assertExec("Should be 3454", "org_apidesign_vm4brwsr_Numbers_deserLongJAB",
83 new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
86 /* XXX: JavaScript cannot represent as big longs as Java.
87 @Test public void deserializeLargeLong() throws Exception {
88 final byte[] arr = new byte[] {
89 (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
91 long exp = Numbers.deserLong(arr);
92 assertExec("Should be " + exp, "org_apidesign_vm4brwsr_Numbers_deserLongJAB",
93 Double.valueOf(exp), arr);
97 @Test public void deserializeFloatInJava() throws Exception {
98 float f = 54324.32423f;
99 float r = Numbers.deserFloat();
100 assertEquals(r, f, "Floats are the same");
103 @Test public void deserializeFloatInJS() throws Exception {
104 float f = 54324.32423f;
105 assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserFloatF",
110 @Test public void deserializeDoubleInJava() throws Exception {
112 double r = Numbers.deserDouble();
113 assertEquals(r, f, 0.001, "Doubles are the same");
116 @Test public void deserializeDoubleInJS() throws Exception {
118 assertExec("Should be the same", "org_apidesign_vm4brwsr_Numbers_deserDoubleD", f);
121 @Test public void serDouble() throws IOException {
123 ByteArrayOutputStream os = new ByteArrayOutputStream();
124 DataOutputStream d = new DataOutputStream(os);
128 StringBuilder sb = new StringBuilder();
129 byte[] arr = os.toByteArray();
130 for (int i = 0; i < arr.length; i++) {
131 sb.append("(byte)").append(arr[i]).append(", ");
136 @Test public void fiveInStringJS() throws Exception {
137 String s = Numbers.intToString();
138 assertExec("Should be the same: " + s,
139 "org_apidesign_vm4brwsr_Numbers_intToStringLjava_lang_String",
144 private static CharSequence codeSeq;
145 private static Invocable code;
148 public void compileTheCode() throws Exception {
149 if (codeSeq == null) {
150 StringBuilder sb = new StringBuilder();
151 code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
156 private static void assertExec(
157 String msg, String methodName, Object expRes, Object... args) throws Exception {
161 ret = code.invokeFunction(methodName, args);
162 } catch (ScriptException ex) {
163 fail("Execution failed in\n" + codeSeq, ex);
164 } catch (NoSuchMethodException ex) {
165 fail("Cannot find method in\n" + codeSeq, ex);
167 if (ret == null && expRes == null) {
170 if (expRes.equals(ret)) {
173 if (expRes instanceof Double && ret instanceof Double) {
174 double expD = ((Double)expRes).doubleValue();
175 double retD = ((Double)ret).doubleValue();
176 assertEquals(retD, expD, 0.000004, msg + " was " + ret + "\n" + codeSeq);
179 assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);