vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 15 Feb 2013 21:14:49 +0100
branchemul
changeset 747 ae352b763959
parent 708 59d5596a9c6c
parent 699 6cad41d877d2
permissions -rw-r--r--
Merge from default branch to resolve conflicts
jaroslav@114
     1
/**
jaroslav@114
     2
 * Back 2 Browser Bytecode Translator
jaroslav@114
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@114
     4
 *
jaroslav@114
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@114
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@114
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@114
     8
 *
jaroslav@114
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@114
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@114
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@114
    12
 * GNU General Public License for more details.
jaroslav@114
    13
 *
jaroslav@114
    14
 * You should have received a copy of the GNU General Public License
jaroslav@114
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@114
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@114
    17
 */
jaroslav@114
    18
package org.apidesign.vm4brwsr;
jaroslav@114
    19
jtulach@132
    20
import static org.testng.Assert.*;
jaroslav@114
    21
import org.testng.annotations.BeforeClass;
jaroslav@114
    22
import org.testng.annotations.Test;
jaroslav@114
    23
jaroslav@114
    24
/**
jaroslav@114
    25
 *
jaroslav@114
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@114
    27
 */
jaroslav@114
    28
public class NumberTest {
jaroslav@114
    29
    @Test public void integerFromString() throws Exception {
jaroslav@248
    30
        assertExec("Can convert string to integer", Integer.class, "parseInt__ILjava_lang_String_2",
jaroslav@114
    31
            Double.valueOf(333), "333"
jaroslav@114
    32
        );
jaroslav@114
    33
    }
jaroslav@114
    34
jaroslav@114
    35
    @Test public void doubleFromString() throws Exception {
jaroslav@248
    36
        assertExec("Can convert string to double", Double.class, "parseDouble__DLjava_lang_String_2",
jaroslav@114
    37
            Double.valueOf(33.3), "33.3"
jaroslav@114
    38
        );
jaroslav@114
    39
    }
jaroslav@114
    40
jaroslav@114
    41
    @Test public void autoboxDouble() throws Exception {
jaroslav@248
    42
        assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToString__Ljava_lang_String_2",
jaroslav@114
    43
            "3.3"
jaroslav@114
    44
        );
jaroslav@114
    45
    }
jtulach@132
    46
    
jtulach@132
    47
    @Test public void javalog1000() throws Exception {
jtulach@132
    48
        assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
jtulach@132
    49
    }
jtulach@132
    50
jtulach@132
    51
    @Test public void jslog1000() throws Exception {
jaroslav@248
    52
        assertExec("log_10(1000) == 3", Math.class, "log10__DD", 
jtulach@132
    53
            Double.valueOf(3.0), 1000.0
jtulach@132
    54
        );
jtulach@132
    55
    }
jaroslav@178
    56
    
jaroslav@178
    57
    @Test public void javaRem() {
jaroslav@178
    58
        assertEquals(3, Numbers.rem(303, 10));
jaroslav@178
    59
    }
jaroslav@178
    60
    @Test public void jsRem() throws Exception {
jaroslav@248
    61
        assertExec("Should be three", Numbers.class, "rem__III", 
jaroslav@178
    62
            Double.valueOf(3.0), 303, 10
jaroslav@178
    63
        );
jaroslav@178
    64
    }
jaroslav@181
    65
    
jaroslav@181
    66
    @Test public void deserializeInt() throws Exception {
jaroslav@181
    67
        int exp = Numbers.deserInt();
jaroslav@248
    68
        assertExec("Should be the same", Numbers.class, "deserInt__I", 
jaroslav@181
    69
            Double.valueOf(exp)
jaroslav@181
    70
        );
jaroslav@181
    71
    }
jaroslav@185
    72
jaroslav@185
    73
    @Test public void deserializeSimpleLong() throws Exception {
jaroslav@248
    74
        assertExec("Should be 3454", Numbers.class, "deserLong__J_3B", 
jaroslav@185
    75
            Double.valueOf(3454), 
jaroslav@185
    76
            new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
jaroslav@185
    77
        );
jaroslav@185
    78
    }
jaroslav@185
    79
    /* XXX: JavaScript cannot represent as big longs as Java. 
jaroslav@185
    80
    @Test public void deserializeLargeLong() throws Exception {
jaroslav@185
    81
        final byte[] arr = new byte[] {
jaroslav@185
    82
            (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
jaroslav@185
    83
        };
jaroslav@185
    84
        long exp = Numbers.deserLong(arr);
jaroslav@248
    85
        assertExec("Should be " + exp, "org_apidesign_vm4brwsr_Numbers_deserLong__JAB", 
jaroslav@185
    86
            Double.valueOf(exp), arr);
jaroslav@185
    87
    }
jaroslav@185
    88
    */
jaroslav@181
    89
    
jaroslav@181
    90
    @Test public void deserializeFloatInJava() throws Exception {
jaroslav@181
    91
        float f = 54324.32423f;
jaroslav@181
    92
        float r = Numbers.deserFloat();
jaroslav@181
    93
        assertEquals(r, f, "Floats are the same");
jaroslav@181
    94
    }
jaroslav@181
    95
    
jaroslav@181
    96
    @Test public void deserializeFloatInJS() throws Exception {
jaroslav@181
    97
        float f = 54324.32423f;
jaroslav@248
    98
        assertExec("Should be the same", Numbers.class, "deserFloat__F", 
jaroslav@181
    99
            Double.valueOf(f)
jaroslav@181
   100
        );
jaroslav@181
   101
    }
jaroslav@114
   102
jaroslav@185
   103
    @Test public void deserializeDoubleInJava() throws Exception {
jaroslav@185
   104
        double f = 3.0;
jaroslav@185
   105
        double r = Numbers.deserDouble();
jaroslav@185
   106
        assertEquals(r, f, 0.001, "Doubles are the same");
jaroslav@185
   107
    }
jaroslav@114
   108
    
jaroslav@185
   109
    @Test public void deserializeDoubleInJS() throws Exception {
jaroslav@185
   110
        double f = 3.0;
jaroslav@248
   111
        assertExec("Should be the same", Numbers.class, "deserDouble__D", f);
jaroslav@185
   112
    }
jaroslav@185
   113
    /*
jaroslav@185
   114
    @Test public void serDouble() throws IOException {
jaroslav@185
   115
        double f = 3.0;
jaroslav@185
   116
        ByteArrayOutputStream os = new ByteArrayOutputStream();
jaroslav@185
   117
        DataOutputStream d = new DataOutputStream(os);
jaroslav@185
   118
        d.writeLong(3454);
jaroslav@185
   119
        d.close();
jaroslav@185
   120
        
jaroslav@185
   121
        StringBuilder sb = new StringBuilder();
jaroslav@185
   122
        byte[] arr = os.toByteArray();
jaroslav@185
   123
        for (int i = 0; i < arr.length; i++) {
jaroslav@185
   124
            sb.append("(byte)").append(arr[i]).append(", ");
jaroslav@185
   125
        }
jaroslav@185
   126
        fail("" + sb);
jaroslav@185
   127
    }
jaroslav@185
   128
*/    
jaroslav@186
   129
    @Test public void fiveInStringJS() throws Exception {
jaroslav@186
   130
        String s = Numbers.intToString();
jaroslav@186
   131
        assertExec("Should be the same: " + s, 
jaroslav@248
   132
            Numbers.class, "intToString__Ljava_lang_String_2", 
jaroslav@186
   133
            s
jaroslav@186
   134
        );
jaroslav@186
   135
    }
jaroslav@187
   136
jaroslav@187
   137
    @Test public void sevenInStringJS() throws Exception {
jaroslav@187
   138
        String s = Numbers.floatToString();
jaroslav@187
   139
        assertExec("Should be the same: " + s, 
jaroslav@248
   140
            Numbers.class, "floatToString__Ljava_lang_String_2", 
jaroslav@187
   141
            s
jaroslav@187
   142
        );
jaroslav@187
   143
    }
lubomir@676
   144
jaroslav@708
   145
    private static TestVM code;
jaroslav@114
   146
jaroslav@114
   147
    @BeforeClass
jaroslav@114
   148
    public void compileTheCode() throws Exception {
jaroslav@708
   149
        code = TestVM.compileClass("org/apidesign/vm4brwsr/Numbers");
jaroslav@114
   150
    }
jaroslav@114
   151
jaroslav@114
   152
    private static void assertExec(
Martin@582
   153
        String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
Martin@582
   154
    {
jaroslav@708
   155
        Object ret = code.execCode(msg, clazz, method, expRes, args);
Martin@582
   156
        if (ret == null) {
jaroslav@114
   157
            return;
jaroslav@114
   158
        }
jtulach@132
   159
        if (expRes instanceof Double && ret instanceof Double) {
jtulach@132
   160
            double expD = ((Double)expRes).doubleValue();
jtulach@132
   161
            double retD = ((Double)ret).doubleValue();
Martin@616
   162
            assertEquals(retD, expD, 0.000004, msg + " "
jaroslav@708
   163
                    + code.toString());
jtulach@132
   164
            return;
jtulach@132
   165
        }
jaroslav@708
   166
        assertEquals(ret, expRes, msg + " " + code.toString());
jaroslav@114
   167
    }
jaroslav@114
   168
    
jaroslav@114
   169
}