1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Thu Feb 14 12:06:16 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,176 +0,0 @@
1.4 -/**
1.5 - * Back 2 Browser Bytecode Translator
1.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, version 2 of the License.
1.11 - *
1.12 - * This program is distributed in the hope that it will be useful,
1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 - * GNU General Public License for more details.
1.16 - *
1.17 - * You should have received a copy of the GNU General Public License
1.18 - * along with this program. Look for COPYING file in the top folder.
1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 - */
1.21 -package org.apidesign.vm4brwsr;
1.22 -
1.23 -import javax.script.Invocable;
1.24 -import javax.script.ScriptException;
1.25 -import static org.testng.Assert.*;
1.26 -import org.testng.annotations.BeforeClass;
1.27 -import org.testng.annotations.Test;
1.28 -
1.29 -/**
1.30 - *
1.31 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.32 - */
1.33 -public class NumberTest {
1.34 - @Test public void integerFromString() throws Exception {
1.35 - assertExec("Can convert string to integer", Integer.class, "parseInt__ILjava_lang_String_2",
1.36 - Double.valueOf(333), "333"
1.37 - );
1.38 - }
1.39 -
1.40 - @Test public void doubleFromString() throws Exception {
1.41 - assertExec("Can convert string to double", Double.class, "parseDouble__DLjava_lang_String_2",
1.42 - Double.valueOf(33.3), "33.3"
1.43 - );
1.44 - }
1.45 -
1.46 - @Test public void autoboxDouble() throws Exception {
1.47 - assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToString__Ljava_lang_String_2",
1.48 - "3.3"
1.49 - );
1.50 - }
1.51 -
1.52 - @Test public void javalog1000() throws Exception {
1.53 - assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
1.54 - }
1.55 -
1.56 - @Test public void jslog1000() throws Exception {
1.57 - assertExec("log_10(1000) == 3", Math.class, "log10__DD",
1.58 - Double.valueOf(3.0), 1000.0
1.59 - );
1.60 - }
1.61 -
1.62 - @Test public void javaRem() {
1.63 - assertEquals(3, Numbers.rem(303, 10));
1.64 - }
1.65 - @Test public void jsRem() throws Exception {
1.66 - assertExec("Should be three", Numbers.class, "rem__III",
1.67 - Double.valueOf(3.0), 303, 10
1.68 - );
1.69 - }
1.70 -
1.71 - @Test public void deserializeInt() throws Exception {
1.72 - int exp = Numbers.deserInt();
1.73 - assertExec("Should be the same", Numbers.class, "deserInt__I",
1.74 - Double.valueOf(exp)
1.75 - );
1.76 - }
1.77 -
1.78 - @Test public void deserializeSimpleLong() throws Exception {
1.79 - assertExec("Should be 3454", Numbers.class, "deserLong__J_3B",
1.80 - Double.valueOf(3454),
1.81 - new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
1.82 - );
1.83 - }
1.84 - /* XXX: JavaScript cannot represent as big longs as Java.
1.85 - @Test public void deserializeLargeLong() throws Exception {
1.86 - final byte[] arr = new byte[] {
1.87 - (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
1.88 - };
1.89 - long exp = Numbers.deserLong(arr);
1.90 - assertExec("Should be " + exp, "org_apidesign_vm4brwsr_Numbers_deserLong__JAB",
1.91 - Double.valueOf(exp), arr);
1.92 - }
1.93 - */
1.94 -
1.95 - @Test public void deserializeFloatInJava() throws Exception {
1.96 - float f = 54324.32423f;
1.97 - float r = Numbers.deserFloat();
1.98 - assertEquals(r, f, "Floats are the same");
1.99 - }
1.100 -
1.101 - @Test public void deserializeFloatInJS() throws Exception {
1.102 - float f = 54324.32423f;
1.103 - assertExec("Should be the same", Numbers.class, "deserFloat__F",
1.104 - Double.valueOf(f)
1.105 - );
1.106 - }
1.107 -
1.108 - @Test public void deserializeDoubleInJava() throws Exception {
1.109 - double f = 3.0;
1.110 - double r = Numbers.deserDouble();
1.111 - assertEquals(r, f, 0.001, "Doubles are the same");
1.112 - }
1.113 -
1.114 - @Test public void deserializeDoubleInJS() throws Exception {
1.115 - double f = 3.0;
1.116 - assertExec("Should be the same", Numbers.class, "deserDouble__D", f);
1.117 - }
1.118 - /*
1.119 - @Test public void serDouble() throws IOException {
1.120 - double f = 3.0;
1.121 - ByteArrayOutputStream os = new ByteArrayOutputStream();
1.122 - DataOutputStream d = new DataOutputStream(os);
1.123 - d.writeLong(3454);
1.124 - d.close();
1.125 -
1.126 - StringBuilder sb = new StringBuilder();
1.127 - byte[] arr = os.toByteArray();
1.128 - for (int i = 0; i < arr.length; i++) {
1.129 - sb.append("(byte)").append(arr[i]).append(", ");
1.130 - }
1.131 - fail("" + sb);
1.132 - }
1.133 -*/
1.134 - @Test public void fiveInStringJS() throws Exception {
1.135 - String s = Numbers.intToString();
1.136 - assertExec("Should be the same: " + s,
1.137 - Numbers.class, "intToString__Ljava_lang_String_2",
1.138 - s
1.139 - );
1.140 - }
1.141 -
1.142 - @Test public void sevenInStringJS() throws Exception {
1.143 - String s = Numbers.floatToString();
1.144 - assertExec("Should be the same: " + s,
1.145 - Numbers.class, "floatToString__Ljava_lang_String_2",
1.146 - s
1.147 - );
1.148 - }
1.149 -
1.150 - private static CharSequence codeSeq;
1.151 - private static Invocable code;
1.152 -
1.153 - @BeforeClass
1.154 - public void compileTheCode() throws Exception {
1.155 - if (codeSeq == null) {
1.156 - StringBuilder sb = new StringBuilder();
1.157 - code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
1.158 - codeSeq = sb;
1.159 - }
1.160 - }
1.161 -
1.162 - private static void assertExec(
1.163 - String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
1.164 - {
1.165 - Object ret = TestUtils.execCode(code, codeSeq, msg, clazz, method, expRes, args);
1.166 - if (ret == null) {
1.167 - return;
1.168 - }
1.169 - if (expRes instanceof Double && ret instanceof Double) {
1.170 - double expD = ((Double)expRes).doubleValue();
1.171 - double retD = ((Double)ret).doubleValue();
1.172 - assertEquals(retD, expD, 0.000004, msg + " "
1.173 - + StaticMethodTest.dumpJS(codeSeq));
1.174 - return;
1.175 - }
1.176 - assertEquals(ret, expRes, msg + " " + StaticMethodTest.dumpJS(codeSeq));
1.177 - }
1.178 -
1.179 -}