rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
changeset 772 d382dacfd73f
parent 747 ae352b763959
child 783 8264f07b1f46
child 789 bb7506513353
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,169 @@
     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.BeforeClass;
    1.25 +import org.testng.annotations.Test;
    1.26 +
    1.27 +/**
    1.28 + *
    1.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.30 + */
    1.31 +public class NumberTest {
    1.32 +    @Test public void integerFromString() throws Exception {
    1.33 +        assertExec("Can convert string to integer", Integer.class, "parseInt__ILjava_lang_String_2",
    1.34 +            Double.valueOf(333), "333"
    1.35 +        );
    1.36 +    }
    1.37 +
    1.38 +    @Test public void doubleFromString() throws Exception {
    1.39 +        assertExec("Can convert string to double", Double.class, "parseDouble__DLjava_lang_String_2",
    1.40 +            Double.valueOf(33.3), "33.3"
    1.41 +        );
    1.42 +    }
    1.43 +
    1.44 +    @Test public void autoboxDouble() throws Exception {
    1.45 +        assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToString__Ljava_lang_String_2",
    1.46 +            "3.3"
    1.47 +        );
    1.48 +    }
    1.49 +    
    1.50 +    @Test public void javalog1000() throws Exception {
    1.51 +        assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
    1.52 +    }
    1.53 +
    1.54 +    @Test public void jslog1000() throws Exception {
    1.55 +        assertExec("log_10(1000) == 3", Math.class, "log10__DD", 
    1.56 +            Double.valueOf(3.0), 1000.0
    1.57 +        );
    1.58 +    }
    1.59 +    
    1.60 +    @Test public void javaRem() {
    1.61 +        assertEquals(3, Numbers.rem(303, 10));
    1.62 +    }
    1.63 +    @Test public void jsRem() throws Exception {
    1.64 +        assertExec("Should be three", Numbers.class, "rem__III", 
    1.65 +            Double.valueOf(3.0), 303, 10
    1.66 +        );
    1.67 +    }
    1.68 +    
    1.69 +    @Test public void deserializeInt() throws Exception {
    1.70 +        int exp = Numbers.deserInt();
    1.71 +        assertExec("Should be the same", Numbers.class, "deserInt__I", 
    1.72 +            Double.valueOf(exp)
    1.73 +        );
    1.74 +    }
    1.75 +
    1.76 +    @Test public void deserializeSimpleLong() throws Exception {
    1.77 +        assertExec("Should be 3454", Numbers.class, "deserLong__J_3B", 
    1.78 +            Double.valueOf(3454), 
    1.79 +            new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
    1.80 +        );
    1.81 +    }
    1.82 +    /* XXX: JavaScript cannot represent as big longs as Java. 
    1.83 +    @Test public void deserializeLargeLong() throws Exception {
    1.84 +        final byte[] arr = new byte[] {
    1.85 +            (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
    1.86 +        };
    1.87 +        long exp = Numbers.deserLong(arr);
    1.88 +        assertExec("Should be " + exp, "org_apidesign_vm4brwsr_Numbers_deserLong__JAB", 
    1.89 +            Double.valueOf(exp), arr);
    1.90 +    }
    1.91 +    */
    1.92 +    
    1.93 +    @Test public void deserializeFloatInJava() throws Exception {
    1.94 +        float f = 54324.32423f;
    1.95 +        float r = Numbers.deserFloat();
    1.96 +        assertEquals(r, f, "Floats are the same");
    1.97 +    }
    1.98 +    
    1.99 +    @Test public void deserializeFloatInJS() throws Exception {
   1.100 +        float f = 54324.32423f;
   1.101 +        assertExec("Should be the same", Numbers.class, "deserFloat__F", 
   1.102 +            Double.valueOf(f)
   1.103 +        );
   1.104 +    }
   1.105 +
   1.106 +    @Test public void deserializeDoubleInJava() throws Exception {
   1.107 +        double f = 3.0;
   1.108 +        double r = Numbers.deserDouble();
   1.109 +        assertEquals(r, f, 0.001, "Doubles are the same");
   1.110 +    }
   1.111 +    
   1.112 +    @Test public void deserializeDoubleInJS() throws Exception {
   1.113 +        double f = 3.0;
   1.114 +        assertExec("Should be the same", Numbers.class, "deserDouble__D", f);
   1.115 +    }
   1.116 +    /*
   1.117 +    @Test public void serDouble() throws IOException {
   1.118 +        double f = 3.0;
   1.119 +        ByteArrayOutputStream os = new ByteArrayOutputStream();
   1.120 +        DataOutputStream d = new DataOutputStream(os);
   1.121 +        d.writeLong(3454);
   1.122 +        d.close();
   1.123 +        
   1.124 +        StringBuilder sb = new StringBuilder();
   1.125 +        byte[] arr = os.toByteArray();
   1.126 +        for (int i = 0; i < arr.length; i++) {
   1.127 +            sb.append("(byte)").append(arr[i]).append(", ");
   1.128 +        }
   1.129 +        fail("" + sb);
   1.130 +    }
   1.131 +*/    
   1.132 +    @Test public void fiveInStringJS() throws Exception {
   1.133 +        String s = Numbers.intToString();
   1.134 +        assertExec("Should be the same: " + s, 
   1.135 +            Numbers.class, "intToString__Ljava_lang_String_2", 
   1.136 +            s
   1.137 +        );
   1.138 +    }
   1.139 +
   1.140 +    @Test public void sevenInStringJS() throws Exception {
   1.141 +        String s = Numbers.floatToString();
   1.142 +        assertExec("Should be the same: " + s, 
   1.143 +            Numbers.class, "floatToString__Ljava_lang_String_2", 
   1.144 +            s
   1.145 +        );
   1.146 +    }
   1.147 +
   1.148 +    private static TestVM code;
   1.149 +
   1.150 +    @BeforeClass
   1.151 +    public void compileTheCode() throws Exception {
   1.152 +        code = TestVM.compileClass("org/apidesign/vm4brwsr/Numbers");
   1.153 +    }
   1.154 +
   1.155 +    private static void assertExec(
   1.156 +        String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
   1.157 +    {
   1.158 +        Object ret = code.execCode(msg, clazz, method, expRes, args);
   1.159 +        if (ret == null) {
   1.160 +            return;
   1.161 +        }
   1.162 +        if (expRes instanceof Double && ret instanceof Double) {
   1.163 +            double expD = ((Double)expRes).doubleValue();
   1.164 +            double retD = ((Double)ret).doubleValue();
   1.165 +            assertEquals(retD, expD, 0.000004, msg + " "
   1.166 +                    + code.toString());
   1.167 +            return;
   1.168 +        }
   1.169 +        assertEquals(ret, expRes, msg + " " + code.toString());
   1.170 +    }
   1.171 +    
   1.172 +}