Fixed error in neg64 found bu Lubo.
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 @Test public void longConversion() throws Exception {
148 assertExec("Long from cPool",
149 Numbers.class, "conversionL__J",
150 Double.valueOf(Long.MAX_VALUE)
154 @Test public void longNegate1() throws Exception {
155 final long res = -0x00fa37d7763e0ca1l;
156 assertExec("Long negate",
157 Numbers.class, "negL__J_3B",
159 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 }
163 @Test public void longNegate2() throws Exception {
164 final long res = -0x80fa37d7763e0ca1l;
165 assertExec("Long negate",
166 Numbers.class, "negL__J_3B",
168 new byte[] { (byte)0x80, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 }
172 @Test public void longNegate3() throws Exception {
173 final long res = -0xfffffffffffffeddl;
174 assertExec("Long negate",
175 Numbers.class, "negL__J_3B",
177 new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xfe, (byte)0xdd }
182 @Test public void longAddOverflow() throws Exception {
183 final long res = Long.MAX_VALUE + 1l;
184 assertExec("Addition 1+MAX",
185 Numbers.class, "addL__J_3B_3B",
187 new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff },
188 new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)1 }
192 @Test public void longAddMaxAndMax() throws Exception {
193 final long res = Long.MAX_VALUE + Long.MAX_VALUE;
194 assertExec("Addition MAX+MAX",
195 Numbers.class, "addL__J_3B_3B",
197 new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff },
198 new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }
202 @Test public void longSubUnderflow() throws Exception {
203 final long res = Long.MIN_VALUE - 1l;
204 assertExec("Subtraction MIN-1",
205 Numbers.class, "subL__J_3B_3B",
207 new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
208 new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)1 }
212 @Test public void longSubMinAndMin() throws Exception {
213 final long res = Long.MIN_VALUE - Long.MIN_VALUE;
214 assertExec("Subtraction MIN-MIN",
215 Numbers.class, "subL__J_3B_3B",
217 new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
218 new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }
222 @Test public void longSubMinAndMax() throws Exception {
223 final long res = Long.MIN_VALUE - Long.MAX_VALUE;
224 assertExec("Subtraction MIN-MAX",
225 Numbers.class, "subL__J_3B_3B",
227 new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
228 new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }
232 @Test public void longMultiplyMax() throws Exception {
233 final long res = Long.MAX_VALUE * 2l;
234 assertExec("Multiplication MAX*2",
235 Numbers.class, "mulL__J_3B_3B",
237 new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff },
238 new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02 }
242 @Test public void longMultiplyMaxAndMax() throws Exception {
243 final long res = Long.MAX_VALUE * Long.MAX_VALUE;
244 assertExec("Multiplication MAX*MAX",
245 Numbers.class, "mulL__J_3B_3B",
247 new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff },
248 new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }
252 @Test public void longMultiplyMin() throws Exception {
253 final long res = Long.MIN_VALUE * 2l;
254 assertExec("Multiplication MIN*2",
255 Numbers.class, "mulL__J_3B_3B",
257 new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
258 new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x02 }
262 @Test public void longMultiplyMinAndMin() throws Exception {
263 final long res = Long.MIN_VALUE * Long.MIN_VALUE;
264 assertExec("Multiplication MIN*2",
265 Numbers.class, "mulL__J_3B_3B",
267 new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
268 new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }
272 @Test public void longMultiplyPrecision() throws Exception {
273 final long res = 0x00fa37d7763e0ca1l * 0xa7b3432fff00123el;
274 assertExec("Multiplication",
275 Numbers.class, "mulL__J_3B_3B",
277 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
278 new byte[] { (byte)0xa7, (byte)0xb3, (byte)0x43, (byte)0x2f, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e }
282 @Test public void longShiftL1() throws Exception {
283 final long res = 0x00fa37d7763e0ca1l << 5;
284 assertExec("Long << 5",
285 Numbers.class, "shlL__J_3BI",
287 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
291 @Test public void longShiftL2() throws Exception {
292 final long res = 0x00fa37d7763e0ca1l << 32;
293 assertExec("Long << 32",
294 Numbers.class, "shlL__J_3BI",
296 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
300 @Test public void longShiftL3() throws Exception {
301 final long res = 0x00fa37d7763e0ca1l << 45;
302 assertExec("Long << 45",
303 Numbers.class, "shlL__J_3BI",
305 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
309 @Test public void longShiftR1() throws Exception {
310 final long res = 0x00fa37d7763e0ca1l >> 5;
311 assertExec("Long >> 5",
312 Numbers.class, "shrL__J_3BI",
314 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
318 @Test public void longShiftR2() throws Exception {
319 final long res = 0x00fa37d7763e0ca1l >> 32;
320 assertExec("Long >> 32",
321 Numbers.class, "shrL__J_3BI",
323 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
327 @Test public void longShiftR3() throws Exception {
328 final long res = 0x00fa37d7763e0ca1l >> 45;
329 assertExec("Long >> 45",
330 Numbers.class, "shrL__J_3BI",
332 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
336 @Test public void longUShiftR1() throws Exception {
337 final long res = 0x00fa37d7763e0ca1l >>> 5;
338 assertExec("Long >>> 5",
339 Numbers.class, "ushrL__J_3BI",
341 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
345 @Test public void longUShiftR2() throws Exception {
346 final long res = 0x00fa37d7763e0ca1l >>> 45;
347 assertExec("Long >>> 45",
348 Numbers.class, "ushrL__J_3BI",
350 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
354 @Test public void longUShiftR3() throws Exception {
355 final long res = 0xf0fa37d7763e0ca1l >>> 5;
356 assertExec("Long >>> 5",
357 Numbers.class, "ushrL__J_3BI",
359 new byte[] { (byte)0xf0, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
363 @Test public void longUShiftR4() throws Exception {
364 final long res = 0xf0fa37d7763e0ca1l >>> 45;
365 assertExec("Long >>> 45",
366 Numbers.class, "ushrL__J_3BI",
368 new byte[] { (byte)0xf0, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
372 @Test public void longAnd() throws Exception {
373 final long res = 0x00fa37d7763e0ca1l & 0xa7b3432fff00123el;
374 assertExec("LOng binary AND",
375 Numbers.class, "andL__J_3B_3B",
377 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
378 new byte[] { (byte)0xa7, (byte)0xb3, (byte)0x43, (byte)0x2f, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e }
382 @Test public void longOr() throws Exception {
383 final long res = 0x00fa37d7763e0ca1l | 0xa7b3432fff00123el;
384 assertExec("Long binary OR",
385 Numbers.class, "orL__J_3B_3B",
387 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
388 new byte[] { (byte)0xa7, (byte)0xb3, (byte)0x43, (byte)0x2f, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e }
392 @Test public void longXor1() throws Exception {
393 final long res = 0x00fa37d7763e0ca1l ^ 0xa7b3432fff00123el;
394 assertExec("Long binary XOR",
395 Numbers.class, "xorL__J_3B_3B",
397 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
398 new byte[] { (byte)0xa7, (byte)0xb3, (byte)0x43, (byte)0x2f, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e }
402 @Test public void longXor2() throws Exception {
403 final long res = 0x00fa37d7763e0ca1l ^ 0x00000000ff00123el;
404 assertExec("Long binary XOR",
405 Numbers.class, "xorL__J_3B_3B",
407 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
408 new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e }
412 @Test public void longXor3() throws Exception {
413 final long res = 0x00000000763e0ca1l ^ 0x00000000ff00123el;
414 assertExec("Long binary XOR",
415 Numbers.class, "xorL__J_3B_3B",
417 new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
418 new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e }
422 private static CharSequence codeSeq;
423 private static Invocable code;
426 public void compileTheCode() throws Exception {
427 if (codeSeq == null) {
428 StringBuilder sb = new StringBuilder();
429 code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
434 private static void assertExec(
435 String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
437 Object ret = TestUtils.execCode(code, codeSeq, msg, clazz, method, expRes, args);
441 if (expRes instanceof Double && ret instanceof Double) {
442 double expD = ((Double)expRes).doubleValue();
443 double retD = ((Double)ret).doubleValue();
444 assertEquals(retD, expD, 0.000004, msg + " "
445 + StaticMethodTest.dumpJS(codeSeq));
448 assertEquals(ret, expRes, msg + " " + StaticMethodTest.dumpJS(codeSeq));