rt/vm/src/test/java/org/apidesign/vm4brwsr/NumbersAsExtensionTest.java
branchclosure
changeset 1487 84a744941c9f
child 1497 daa5f903b529
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumbersAsExtensionTest.java	Fri Apr 25 09:59:48 2014 +0200
     1.3 @@ -0,0 +1,238 @@
     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 static org.testng.Assert.*;
    1.24 +import org.testng.annotations.AfterClass;
    1.25 +import org.testng.annotations.BeforeClass;
    1.26 +import org.testng.annotations.Test;
    1.27 +
    1.28 +/**
    1.29 + *
    1.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.31 + */
    1.32 +public class NumbersAsExtensionTest {
    1.33 +    @Test public void integerFromString() throws Exception {
    1.34 +        assertExec("Can convert string to integer", Integer.class, "parseInt__ILjava_lang_String_2",
    1.35 +            Double.valueOf(333), "333"
    1.36 +        );
    1.37 +    }
    1.38 +
    1.39 +    @Test public void doubleFromString() throws Exception {
    1.40 +        assertExec("Can convert string to double", Double.class, "parseDouble__DLjava_lang_String_2",
    1.41 +            Double.valueOf(33.3), "33.3"
    1.42 +        );
    1.43 +    }
    1.44 +
    1.45 +    @Test public void autoboxDouble() throws Exception {
    1.46 +        assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToString__Ljava_lang_String_2",
    1.47 +            "3.3"
    1.48 +        );
    1.49 +    }
    1.50 +    
    1.51 +    @Test public void javalog1000() throws Exception {
    1.52 +        assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
    1.53 +    }
    1.54 +
    1.55 +    @Test public void jslog1000() throws Exception {
    1.56 +        assertExec("log_10(1000) == 3", Math.class, "log10__DD", 
    1.57 +            Double.valueOf(3.0), 1000.0
    1.58 +        );
    1.59 +    }
    1.60 +    
    1.61 +    @Test public void javaRem() {
    1.62 +        assertEquals(3, Numbers.rem(303, 10));
    1.63 +    }
    1.64 +    @Test public void jsRem() throws Exception {
    1.65 +        assertExec("Should be three", Numbers.class, "rem__III", 
    1.66 +            Double.valueOf(3.0), 303, 10
    1.67 +        );
    1.68 +    }
    1.69 +    
    1.70 +    @Test public void deserializeInt() throws Exception {
    1.71 +        int exp = Numbers.deserInt();
    1.72 +        assertExec("Should be the same", Numbers.class, "deserInt__I", 
    1.73 +            Double.valueOf(exp)
    1.74 +        );
    1.75 +    }
    1.76 +
    1.77 +    @Test public void deserializeSimpleLong() throws Exception {
    1.78 +        assertExec("Should be 3454", Numbers.class, "deserLong__J_3B", 
    1.79 +            Double.valueOf(3454), 
    1.80 +            new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
    1.81 +        );
    1.82 +    }
    1.83 +    /* XXX: JavaScript cannot represent as big longs as Java. 
    1.84 +    @Test public void deserializeLargeLong() throws Exception {
    1.85 +        final byte[] arr = new byte[] {
    1.86 +            (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
    1.87 +        };
    1.88 +        long exp = Numbers.deserLong(arr);
    1.89 +        assertExec("Should be " + exp, "org_apidesign_vm4brwsr_Numbers_deserLong__JAB", 
    1.90 +            Double.valueOf(exp), arr);
    1.91 +    }
    1.92 +    */
    1.93 +    
    1.94 +    @Test public void deserializeFloatInJava() throws Exception {
    1.95 +        float f = 54324.32423f;
    1.96 +        float r = Numbers.deserFloat();
    1.97 +        assertEquals(r, f, "Floats are the same");
    1.98 +    }
    1.99 +    
   1.100 +    @Test public void deserializeFloatInJS() throws Exception {
   1.101 +        float f = 54324.32423f;
   1.102 +        assertExec("Should be the same", Numbers.class, "deserFloat__F", 
   1.103 +            Double.valueOf(f)
   1.104 +        );
   1.105 +    }
   1.106 +
   1.107 +    @Test public void deserializeDoubleInJava() throws Exception {
   1.108 +        double f = 3.0;
   1.109 +        double r = Numbers.deserDouble();
   1.110 +        assertEquals(r, f, 0.001, "Doubles are the same");
   1.111 +    }
   1.112 +    
   1.113 +    @Test public void deserializeDoubleInJS() throws Exception {
   1.114 +        double f = 3.0;
   1.115 +        assertExec("Should be the same", Numbers.class, "deserDouble__D", f);
   1.116 +    }
   1.117 +    /*
   1.118 +    @Test public void serDouble() throws IOException {
   1.119 +        double f = 3.0;
   1.120 +        ByteArrayOutputStream os = new ByteArrayOutputStream();
   1.121 +        DataOutputStream d = new DataOutputStream(os);
   1.122 +        d.writeLong(3454);
   1.123 +        d.close();
   1.124 +        
   1.125 +        StringBuilder sb = new StringBuilder();
   1.126 +        byte[] arr = os.toByteArray();
   1.127 +        for (int i = 0; i < arr.length; i++) {
   1.128 +            sb.append("(byte)").append(arr[i]).append(", ");
   1.129 +        }
   1.130 +        fail("" + sb);
   1.131 +    }
   1.132 +*/    
   1.133 +    @Test public void fiveInStringJS() throws Exception {
   1.134 +        String s = Numbers.intToString();
   1.135 +        assertExec("Should be the same: " + s, 
   1.136 +            Numbers.class, "intToString__Ljava_lang_String_2", 
   1.137 +            s
   1.138 +        );
   1.139 +    }
   1.140 +
   1.141 +    @Test public void sevenInStringJS() throws Exception {
   1.142 +        String s = Numbers.floatToString();
   1.143 +        assertExec("Should be the same: " + s, 
   1.144 +            Numbers.class, "floatToString__Ljava_lang_String_2", 
   1.145 +            s
   1.146 +        );
   1.147 +    }
   1.148 +
   1.149 +    @Test public void everyNumberHasJavaLangNumberMethods() throws Exception {
   1.150 +        assertExec("Can we call doubleValue?", 
   1.151 +            Numbers.class, "seven__DI", 
   1.152 +            Double.valueOf(7.0), 0
   1.153 +        );
   1.154 +    }
   1.155 +    @Test public void everyNumberHasJavaLangNumberMethodsInt() throws Exception {
   1.156 +        assertExec("Can we call doubleValue?", 
   1.157 +            Numbers.class, "seven__DI", 
   1.158 +            Double.valueOf(7.0), 1
   1.159 +        );
   1.160 +    }
   1.161 +    @Test public void everyNumberHasJavaLangNumberMethodsLong() throws Exception {
   1.162 +        assertExec("Can we call doubleValue?", 
   1.163 +            Numbers.class, "seven__DI", 
   1.164 +            Double.valueOf(7.0), 2
   1.165 +        );
   1.166 +    }
   1.167 +    @Test public void everyNumberHasJavaLangNumberMethodsShort() throws Exception {
   1.168 +        assertExec("Can we call doubleValue?", 
   1.169 +            Numbers.class, "seven__DI", 
   1.170 +            Double.valueOf(7.0), 3
   1.171 +        );
   1.172 +    }
   1.173 +    @Test public void everyNumberHasJavaLangNumberMethodsByte() throws Exception {
   1.174 +        assertExec("Can we call doubleValue?", 
   1.175 +            Numbers.class, "seven__DI", 
   1.176 +            Double.valueOf(7.0), 4
   1.177 +        );
   1.178 +    }
   1.179 +    @Test public void valueOfNumber() throws Exception {
   1.180 +        assertExec("Can we call JavaScripts valueOf?", 
   1.181 +            Numbers.class, "seven__DI", 
   1.182 +            Double.valueOf(7.0), 8
   1.183 +        );
   1.184 +    }
   1.185 +    @Test public void valueOfLongNumber() throws Exception {
   1.186 +        assertExec("Can we call JavaScripts valueOf?", 
   1.187 +            Numbers.class, "seven__DI", 
   1.188 +            Double.valueOf(Long.MAX_VALUE / 5), 9
   1.189 +        );
   1.190 +    }
   1.191 +    @Test public void valueOfLongCharA() throws Exception {
   1.192 +        assertExec("Can we call JavaScripts valueOf on Character?", 
   1.193 +            Numbers.class, "seven__DI", 
   1.194 +            Double.valueOf('A'), 65
   1.195 +        );
   1.196 +    }
   1.197 +
   1.198 +    @Test public void valueOfLongBooleanTrue() throws Exception {
   1.199 +        assertExec("Can we call JavaScripts valueOf on Boolean?", 
   1.200 +            Numbers.class, "bseven__ZI", 
   1.201 +            true, 31
   1.202 +        );
   1.203 +    }
   1.204 +    @Test public void valueOfLongBooleanFalse() throws Exception {
   1.205 +        assertExec("Can we call JavaScripts valueOf on Boolean?", 
   1.206 +            Numbers.class, "bseven__ZI", 
   1.207 +            false, 30
   1.208 +        );
   1.209 +    }
   1.210 +
   1.211 +    private static TestVM code;
   1.212 +
   1.213 +    @BeforeClass
   1.214 +    public static void compileTheCode() throws Exception {
   1.215 +        code = TestVM.compileClassAsExtension(null, null, "org/apidesign/vm4brwsr/Numbers");
   1.216 +    }
   1.217 +    @AfterClass
   1.218 +    public static void releaseTheCode() {
   1.219 +        code = null;
   1.220 +    }
   1.221 +
   1.222 +    private static void assertExec(
   1.223 +        String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
   1.224 +    {
   1.225 +        Object ret = code.execCode(msg, clazz, method, expRes, args);
   1.226 +        if (ret == null) {
   1.227 +            return;
   1.228 +        }
   1.229 +        if (expRes instanceof Double && ret instanceof Double) {
   1.230 +            double expD = ((Double)expRes).doubleValue();
   1.231 +            double retD = ((Double)ret).doubleValue();
   1.232 +            assertEquals(retD, expD, 0.000004, msg + " "
   1.233 +                    + code.toString());
   1.234 +            return;
   1.235 +        }
   1.236 +        assertEquals(ret, expRes, msg + " " + code.toString());
   1.237 +    }
   1.238 +
   1.239 +
   1.240 +    
   1.241 +}