External classes are treated as exported.
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 static org.testng.Assert.*;
21 import org.testng.annotations.AfterClass;
22 import org.testng.annotations.BeforeClass;
23 import org.testng.annotations.Test;
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 public class NumberTest {
30 @Test public void integerFromString() throws Exception {
31 assertExec("Can convert string to integer", Integer.class, "parseInt__ILjava_lang_String_2",
32 Double.valueOf(333), "333"
36 @Test public void doubleFromString() throws Exception {
37 assertExec("Can convert string to double", Double.class, "parseDouble__DLjava_lang_String_2",
38 Double.valueOf(33.3), "33.3"
42 @Test public void autoboxDouble() throws Exception {
43 assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToString__Ljava_lang_String_2",
48 @Test public void javalog1000() throws Exception {
49 assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
52 @Test public void jslog1000() throws Exception {
53 assertExec("log_10(1000) == 3", Math.class, "log10__DD",
54 Double.valueOf(3.0), 1000.0
58 @Test public void javaRem() {
59 assertEquals(3, Numbers.rem(303, 10));
61 @Test public void jsRem() throws Exception {
62 assertExec("Should be three", Numbers.class, "rem__III",
63 Double.valueOf(3.0), 303, 10
67 @Test public void deserializeInt() throws Exception {
68 int exp = Numbers.deserInt();
69 assertExec("Should be the same", Numbers.class, "deserInt__I",
74 @Test public void deserializeSimpleLong() throws Exception {
75 assertExec("Should be 3454", Numbers.class, "deserLong__J_3B",
77 new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
80 @Test public void deserializeMiddleLong() throws Exception {
81 final byte[] arr = new byte[] {
82 (byte)0, (byte)0, (byte)64, (byte)32, (byte)23, (byte)0, (byte)0, (byte)0
84 long exp = Numbers.deserLong(arr, 16);
85 assertExec("Should be " + exp, Numbers.class, "deserLong__J_3BI",
86 Double.valueOf(exp), arr, 16);
88 @Test public void deserializeLargeLong() throws Exception {
89 final byte[] arr = new byte[] {
90 (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
92 long exp = Numbers.deserLong(arr, 32);
93 assertExec("Should be " + exp, Numbers.class, "deserLong__J_3BI",
94 Double.valueOf(exp), arr, 32);
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", Numbers.class, "deserFloat__F",
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", Numbers.class, "deserDouble__D", f);
121 @Test public void bytesToLong() throws Exception {
122 long exp = Numbers.bytesToLong((byte)30, (byte)20, 32);
123 assertExec("Should be the same", Numbers.class, "bytesToLong__JBBI",
124 Double.valueOf(exp), 30, 20, 32);
127 @Test public void serDouble() throws IOException {
129 ByteArrayOutputStream os = new ByteArrayOutputStream();
130 DataOutputStream d = new DataOutputStream(os);
134 StringBuilder sb = new StringBuilder();
135 byte[] arr = os.toByteArray();
136 for (int i = 0; i < arr.length; i++) {
137 sb.append("(byte)").append(arr[i]).append(", ");
142 @Test public void fiveInStringJS() throws Exception {
143 String s = Numbers.intToString();
144 assertExec("Should be the same: " + s,
145 Numbers.class, "intToString__Ljava_lang_String_2",
150 @Test public void sevenInStringJS() throws Exception {
151 String s = Numbers.floatToString();
152 assertExec("Should be the same: " + s,
153 Numbers.class, "floatToString__Ljava_lang_String_2",
158 @Test public void everyNumberHasJavaLangNumberMethods() throws Exception {
159 assertExec("Can we call doubleValue?",
160 Numbers.class, "seven__DI",
161 Double.valueOf(7.0), 0
164 @Test public void everyNumberHasJavaLangNumberMethodsInt() throws Exception {
165 assertExec("Can we call doubleValue?",
166 Numbers.class, "seven__DI",
167 Double.valueOf(7.0), 1
170 @Test public void everyNumberHasJavaLangNumberMethodsLong() throws Exception {
171 assertExec("Can we call doubleValue?",
172 Numbers.class, "seven__DI",
173 Double.valueOf(7.0), 2
176 @Test public void everyNumberHasJavaLangNumberMethodsShort() throws Exception {
177 assertExec("Can we call doubleValue?",
178 Numbers.class, "seven__DI",
179 Double.valueOf(7.0), 3
182 @Test public void everyNumberHasJavaLangNumberMethodsByte() throws Exception {
183 assertExec("Can we call doubleValue?",
184 Numbers.class, "seven__DI",
185 Double.valueOf(7.0), 4
188 @Test public void valueOfNumber() throws Exception {
189 assertExec("Can we call JavaScripts valueOf?",
190 Numbers.class, "seven__DI",
191 Double.valueOf(7.0), 8
194 @Test public void valueOfLongNumber() throws Exception {
195 assertExec("Can we call JavaScripts valueOf?",
196 Numbers.class, "seven__DI",
197 Double.valueOf(Long.MAX_VALUE / 5), 9
200 @Test public void valueOfLongCharA() throws Exception {
201 assertExec("Can we call JavaScripts valueOf on Character?",
202 Numbers.class, "seven__DI",
203 Double.valueOf('A'), 65
207 @Test public void valueOfLongBooleanTrue() throws Exception {
208 assertExec("Can we call JavaScripts valueOf on Boolean?",
209 Numbers.class, "bseven__ZI",
213 @Test public void valueOfLongBooleanFalse() throws Exception {
214 assertExec("Can we call JavaScripts valueOf on Boolean?",
215 Numbers.class, "bseven__ZI",
220 @Test public void computeAround() throws Exception {
221 double exp = Numbers.around(new Object(), 5, 8);
222 assertExec("Computes the same value",
223 Numbers.class, "around__ILjava_lang_Object_2II",
228 private static TestVM code;
231 public static void compileTheCode() throws Exception {
232 code = TestVM.compileClass("org/apidesign/vm4brwsr/Numbers");
235 public static void releaseTheCode() {
239 private static void assertExec(
240 String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
242 Object ret = code.execCode(msg, clazz, method, expRes, args);
246 if (expRes instanceof Double && ret instanceof Double) {
247 double expD = ((Double)expRes).doubleValue();
248 double retD = ((Double)ret).doubleValue();
249 assertEquals(retD, expD, 0.000004, msg + " "
253 assertEquals(ret, expRes, msg + " " + code.toString());