rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 26 May 2014 14:33:11 +0200
branchclosure
changeset 1595 19d0484c1916
parent 1462 1e7ff3ba3666
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Need to flush the stack before destroying it with return value of a function
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@789
    21
import org.testng.annotations.AfterClass;
jaroslav@114
    22
import org.testng.annotations.BeforeClass;
jaroslav@114
    23
import org.testng.annotations.Test;
jaroslav@114
    24
jaroslav@114
    25
/**
jaroslav@114
    26
 *
jaroslav@114
    27
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@114
    28
 */
jaroslav@114
    29
public class NumberTest {
jaroslav@114
    30
    @Test public void integerFromString() throws Exception {
jaroslav@248
    31
        assertExec("Can convert string to integer", Integer.class, "parseInt__ILjava_lang_String_2",
jaroslav@114
    32
            Double.valueOf(333), "333"
jaroslav@114
    33
        );
jaroslav@114
    34
    }
jaroslav@114
    35
jaroslav@114
    36
    @Test public void doubleFromString() throws Exception {
jaroslav@248
    37
        assertExec("Can convert string to double", Double.class, "parseDouble__DLjava_lang_String_2",
jaroslav@114
    38
            Double.valueOf(33.3), "33.3"
jaroslav@114
    39
        );
jaroslav@114
    40
    }
jaroslav@114
    41
jaroslav@114
    42
    @Test public void autoboxDouble() throws Exception {
jaroslav@248
    43
        assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToString__Ljava_lang_String_2",
jaroslav@114
    44
            "3.3"
jaroslav@114
    45
        );
jaroslav@114
    46
    }
jtulach@132
    47
    
jtulach@132
    48
    @Test public void javalog1000() throws Exception {
jtulach@132
    49
        assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
jtulach@132
    50
    }
jtulach@132
    51
jtulach@132
    52
    @Test public void jslog1000() throws Exception {
jaroslav@248
    53
        assertExec("log_10(1000) == 3", Math.class, "log10__DD", 
jtulach@132
    54
            Double.valueOf(3.0), 1000.0
jtulach@132
    55
        );
jtulach@132
    56
    }
jaroslav@178
    57
    
jaroslav@178
    58
    @Test public void javaRem() {
jaroslav@178
    59
        assertEquals(3, Numbers.rem(303, 10));
jaroslav@178
    60
    }
jaroslav@178
    61
    @Test public void jsRem() throws Exception {
jaroslav@248
    62
        assertExec("Should be three", Numbers.class, "rem__III", 
jaroslav@178
    63
            Double.valueOf(3.0), 303, 10
jaroslav@178
    64
        );
jaroslav@178
    65
    }
jaroslav@181
    66
    
jaroslav@181
    67
    @Test public void deserializeInt() throws Exception {
jaroslav@181
    68
        int exp = Numbers.deserInt();
jaroslav@248
    69
        assertExec("Should be the same", Numbers.class, "deserInt__I", 
jaroslav@181
    70
            Double.valueOf(exp)
jaroslav@181
    71
        );
jaroslav@181
    72
    }
jaroslav@185
    73
jaroslav@185
    74
    @Test public void deserializeSimpleLong() throws Exception {
jaroslav@248
    75
        assertExec("Should be 3454", Numbers.class, "deserLong__J_3B", 
jaroslav@185
    76
            Double.valueOf(3454), 
jaroslav@185
    77
            new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
jaroslav@185
    78
        );
jaroslav@185
    79
    }
jaroslav@1460
    80
    @Test public void deserializeMiddleLong() throws Exception {
jaroslav@1460
    81
        final byte[] arr = new byte[] {
jaroslav@1460
    82
            (byte)0, (byte)0, (byte)64, (byte)32, (byte)23, (byte)0, (byte)0, (byte)0
jaroslav@1460
    83
        };
jaroslav@1460
    84
        long exp = Numbers.deserLong(arr, 16);
jaroslav@1460
    85
        assertExec("Should be " + exp, Numbers.class, "deserLong__J_3BI", 
jaroslav@1460
    86
            Double.valueOf(exp), arr, 16);
jaroslav@1460
    87
    }
jaroslav@185
    88
    @Test public void deserializeLargeLong() throws Exception {
jaroslav@185
    89
        final byte[] arr = new byte[] {
jaroslav@185
    90
            (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
jaroslav@185
    91
        };
jaroslav@1460
    92
        long exp = Numbers.deserLong(arr, 32);
jaroslav@1460
    93
        assertExec("Should be " + exp, Numbers.class, "deserLong__J_3BI", 
jaroslav@1460
    94
            Double.valueOf(exp), arr, 32);
jaroslav@185
    95
    }
jaroslav@181
    96
    
jaroslav@181
    97
    @Test public void deserializeFloatInJava() throws Exception {
jaroslav@181
    98
        float f = 54324.32423f;
jaroslav@181
    99
        float r = Numbers.deserFloat();
jaroslav@181
   100
        assertEquals(r, f, "Floats are the same");
jaroslav@181
   101
    }
jaroslav@181
   102
    
jaroslav@181
   103
    @Test public void deserializeFloatInJS() throws Exception {
jaroslav@181
   104
        float f = 54324.32423f;
jaroslav@248
   105
        assertExec("Should be the same", Numbers.class, "deserFloat__F", 
jaroslav@181
   106
            Double.valueOf(f)
jaroslav@181
   107
        );
jaroslav@181
   108
    }
jaroslav@114
   109
jaroslav@185
   110
    @Test public void deserializeDoubleInJava() throws Exception {
jaroslav@185
   111
        double f = 3.0;
jaroslav@185
   112
        double r = Numbers.deserDouble();
jaroslav@185
   113
        assertEquals(r, f, 0.001, "Doubles are the same");
jaroslav@185
   114
    }
jaroslav@114
   115
    
jaroslav@185
   116
    @Test public void deserializeDoubleInJS() throws Exception {
jaroslav@185
   117
        double f = 3.0;
jaroslav@248
   118
        assertExec("Should be the same", Numbers.class, "deserDouble__D", f);
jaroslav@185
   119
    }
jaroslav@1462
   120
    
jaroslav@1462
   121
    @Test public void bytesToLong() throws Exception {
jaroslav@1462
   122
        long exp = Numbers.bytesToLong((byte)30, (byte)20, 32);
jaroslav@1462
   123
        assertExec("Should be the same", Numbers.class, "bytesToLong__JBBI", 
jaroslav@1462
   124
            Double.valueOf(exp), 30, 20, 32);
jaroslav@1462
   125
    }
jaroslav@185
   126
    /*
jaroslav@185
   127
    @Test public void serDouble() throws IOException {
jaroslav@185
   128
        double f = 3.0;
jaroslav@185
   129
        ByteArrayOutputStream os = new ByteArrayOutputStream();
jaroslav@185
   130
        DataOutputStream d = new DataOutputStream(os);
jaroslav@185
   131
        d.writeLong(3454);
jaroslav@185
   132
        d.close();
jaroslav@185
   133
        
jaroslav@185
   134
        StringBuilder sb = new StringBuilder();
jaroslav@185
   135
        byte[] arr = os.toByteArray();
jaroslav@185
   136
        for (int i = 0; i < arr.length; i++) {
jaroslav@185
   137
            sb.append("(byte)").append(arr[i]).append(", ");
jaroslav@185
   138
        }
jaroslav@185
   139
        fail("" + sb);
jaroslav@185
   140
    }
jaroslav@185
   141
*/    
jaroslav@186
   142
    @Test public void fiveInStringJS() throws Exception {
jaroslav@186
   143
        String s = Numbers.intToString();
jaroslav@186
   144
        assertExec("Should be the same: " + s, 
jaroslav@248
   145
            Numbers.class, "intToString__Ljava_lang_String_2", 
jaroslav@186
   146
            s
jaroslav@186
   147
        );
jaroslav@186
   148
    }
jaroslav@187
   149
jaroslav@187
   150
    @Test public void sevenInStringJS() throws Exception {
jaroslav@187
   151
        String s = Numbers.floatToString();
jaroslav@187
   152
        assertExec("Should be the same: " + s, 
jaroslav@248
   153
            Numbers.class, "floatToString__Ljava_lang_String_2", 
jaroslav@187
   154
            s
jaroslav@187
   155
        );
jaroslav@187
   156
    }
lubomir@676
   157
jaroslav@783
   158
    @Test public void everyNumberHasJavaLangNumberMethods() throws Exception {
jaroslav@783
   159
        assertExec("Can we call doubleValue?", 
jaroslav@783
   160
            Numbers.class, "seven__DI", 
jaroslav@783
   161
            Double.valueOf(7.0), 0
jaroslav@783
   162
        );
jaroslav@783
   163
    }
jaroslav@783
   164
    @Test public void everyNumberHasJavaLangNumberMethodsInt() throws Exception {
jaroslav@783
   165
        assertExec("Can we call doubleValue?", 
jaroslav@783
   166
            Numbers.class, "seven__DI", 
jaroslav@783
   167
            Double.valueOf(7.0), 1
jaroslav@783
   168
        );
jaroslav@783
   169
    }
jaroslav@783
   170
    @Test public void everyNumberHasJavaLangNumberMethodsLong() throws Exception {
jaroslav@783
   171
        assertExec("Can we call doubleValue?", 
jaroslav@783
   172
            Numbers.class, "seven__DI", 
jaroslav@783
   173
            Double.valueOf(7.0), 2
jaroslav@783
   174
        );
jaroslav@783
   175
    }
jaroslav@783
   176
    @Test public void everyNumberHasJavaLangNumberMethodsShort() throws Exception {
jaroslav@783
   177
        assertExec("Can we call doubleValue?", 
jaroslav@783
   178
            Numbers.class, "seven__DI", 
jaroslav@783
   179
            Double.valueOf(7.0), 3
jaroslav@783
   180
        );
jaroslav@783
   181
    }
jaroslav@783
   182
    @Test public void everyNumberHasJavaLangNumberMethodsByte() throws Exception {
jaroslav@783
   183
        assertExec("Can we call doubleValue?", 
jaroslav@783
   184
            Numbers.class, "seven__DI", 
jaroslav@783
   185
            Double.valueOf(7.0), 4
jaroslav@783
   186
        );
jaroslav@783
   187
    }
jaroslav@783
   188
    @Test public void valueOfNumber() throws Exception {
jaroslav@783
   189
        assertExec("Can we call JavaScripts valueOf?", 
jaroslav@783
   190
            Numbers.class, "seven__DI", 
jaroslav@783
   191
            Double.valueOf(7.0), 8
jaroslav@783
   192
        );
jaroslav@783
   193
    }
jaroslav@783
   194
    @Test public void valueOfLongNumber() throws Exception {
jaroslav@783
   195
        assertExec("Can we call JavaScripts valueOf?", 
jaroslav@783
   196
            Numbers.class, "seven__DI", 
jaroslav@783
   197
            Double.valueOf(Long.MAX_VALUE / 5), 9
jaroslav@783
   198
        );
jaroslav@783
   199
    }
jaroslav@791
   200
    @Test public void valueOfLongCharA() throws Exception {
jaroslav@791
   201
        assertExec("Can we call JavaScripts valueOf on Character?", 
jaroslav@791
   202
            Numbers.class, "seven__DI", 
jaroslav@791
   203
            Double.valueOf('A'), 65
jaroslav@791
   204
        );
jaroslav@791
   205
    }
jaroslav@791
   206
jaroslav@791
   207
    @Test public void valueOfLongBooleanTrue() throws Exception {
jaroslav@791
   208
        assertExec("Can we call JavaScripts valueOf on Boolean?", 
jaroslav@1097
   209
            Numbers.class, "bseven__ZI", 
jaroslav@1097
   210
            true, 31
jaroslav@791
   211
        );
jaroslav@791
   212
    }
jaroslav@791
   213
    @Test public void valueOfLongBooleanFalse() throws Exception {
jaroslav@791
   214
        assertExec("Can we call JavaScripts valueOf on Boolean?", 
jaroslav@1097
   215
            Numbers.class, "bseven__ZI", 
jaroslav@1097
   216
            false, 30
jaroslav@791
   217
        );
jaroslav@791
   218
    }
jaroslav@1595
   219
    
jaroslav@1595
   220
    @Test public void computeAround() throws Exception {
jaroslav@1595
   221
        double exp = Numbers.around(new Object(), 5, 8);
jaroslav@1595
   222
        assertExec("Computes the same value", 
jaroslav@1595
   223
            Numbers.class, "around__ILjava_lang_Object_2II", 
jaroslav@1595
   224
            exp, null, 5, 8
jaroslav@1595
   225
        );
jaroslav@1595
   226
    }
jaroslav@783
   227
jaroslav@708
   228
    private static TestVM code;
jaroslav@114
   229
jaroslav@114
   230
    @BeforeClass
jaroslav@789
   231
    public static void compileTheCode() throws Exception {
jaroslav@708
   232
        code = TestVM.compileClass("org/apidesign/vm4brwsr/Numbers");
jaroslav@114
   233
    }
jaroslav@789
   234
    @AfterClass
jaroslav@789
   235
    public static void releaseTheCode() {
jaroslav@789
   236
        code = null;
jaroslav@789
   237
    }
jaroslav@114
   238
jaroslav@114
   239
    private static void assertExec(
Martin@582
   240
        String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
Martin@582
   241
    {
jaroslav@708
   242
        Object ret = code.execCode(msg, clazz, method, expRes, args);
Martin@582
   243
        if (ret == null) {
jaroslav@114
   244
            return;
jaroslav@114
   245
        }
jtulach@132
   246
        if (expRes instanceof Double && ret instanceof Double) {
jtulach@132
   247
            double expD = ((Double)expRes).doubleValue();
jtulach@132
   248
            double retD = ((Double)ret).doubleValue();
Martin@616
   249
            assertEquals(retD, expD, 0.000004, msg + " "
jaroslav@708
   250
                    + code.toString());
jtulach@132
   251
            return;
jtulach@132
   252
        }
jaroslav@708
   253
        assertEquals(ret, expRes, msg + " " + code.toString());
jaroslav@114
   254
    }
jaroslav@114
   255
    
jaroslav@114
   256
}