vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 16 Oct 2012 12:42:00 +0200
changeset 106 346633cd13d6
parent 103 e8438996d406
child 115 3d5597011af0
permissions -rw-r--r--
Fixing license in all files
jaroslav@106
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@106
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@106
     4
 *
jaroslav@106
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@106
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@106
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@106
     8
 *
jaroslav@106
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@106
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@106
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@106
    12
 * GNU General Public License for more details.
jaroslav@106
    13
 *
jaroslav@106
    14
 * You should have received a copy of the GNU General Public License
jaroslav@106
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@106
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@106
    17
 */
jaroslav@0
    18
/*
jaroslav@0
    19
Java 4 Browser Bytecode Translator
jaroslav@0
    20
Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@0
    21
jaroslav@0
    22
This program is free software: you can redistribute it and/or modify
jaroslav@0
    23
it under the terms of the GNU General Public License as published by
jaroslav@0
    24
the Free Software Foundation, version 2 of the License.
jaroslav@0
    25
jaroslav@0
    26
This program is distributed in the hope that it will be useful,
jaroslav@0
    27
but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@0
    28
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@0
    29
GNU General Public License for more details.
jaroslav@0
    30
jaroslav@0
    31
You should have received a copy of the GNU General Public License
jaroslav@0
    32
along with this program. Look for COPYING file in the top folder.
jaroslav@0
    33
If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@0
    34
*/
jaroslav@22
    35
package org.apidesign.vm4brwsr;
jaroslav@0
    36
jaroslav@0
    37
import java.io.IOException;
jaroslav@0
    38
import javax.script.Invocable;
jaroslav@0
    39
import javax.script.ScriptEngine;
jaroslav@0
    40
import javax.script.ScriptEngineManager;
jaroslav@0
    41
import javax.script.ScriptException;
jaroslav@0
    42
import static org.testng.Assert.*;
jaroslav@103
    43
import org.testng.annotations.BeforeClass;
jaroslav@0
    44
import org.testng.annotations.Test;
jaroslav@0
    45
jaroslav@0
    46
/** Checks the basic behavior of the translator.
jaroslav@0
    47
 *
jaroslav@0
    48
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@0
    49
 */
jaroslav@0
    50
public class StaticMethodTest {
jaroslav@0
    51
    @Test public void threePlusFour() throws Exception {
jaroslav@2
    52
        assertExec(
jaroslav@2
    53
            "Should be seven", 
jaroslav@22
    54
            "org_apidesign_vm4brwsr_StaticMethod_sumIII", 
jaroslav@2
    55
            Double.valueOf(7), 
jaroslav@2
    56
            3, 4
jaroslav@2
    57
        );
jaroslav@0
    58
    }
jaroslav@0
    59
jaroslav@1
    60
    @Test public void powerOfThree() throws Exception {
jaroslav@2
    61
        assertExec(
jaroslav@2
    62
            "Should be nine", 
jaroslav@22
    63
            "org_apidesign_vm4brwsr_StaticMethod_powerFF", 
jaroslav@2
    64
            Double.valueOf(9),
jaroslav@2
    65
            3.0f
jaroslav@2
    66
        );
jaroslav@1
    67
    }
jaroslav@1
    68
jaroslav@48
    69
    @Test public void minusOne() throws Exception {
jaroslav@48
    70
        assertExec(
jaroslav@48
    71
            "Should be minus one", 
jaroslav@48
    72
            "org_apidesign_vm4brwsr_StaticMethod_minusOneI", 
jaroslav@48
    73
            Double.valueOf(-1)
jaroslav@48
    74
        );
jaroslav@48
    75
    }
jaroslav@48
    76
jaroslav@2
    77
    @Test public void doubleWithoutLong() throws Exception {
jaroslav@2
    78
        assertExec(
jaroslav@2
    79
            "Should be two",
jaroslav@22
    80
            "org_apidesign_vm4brwsr_StaticMethod_minusDDJ", 
jaroslav@2
    81
            Double.valueOf(2),
jaroslav@2
    82
            3.0d, 1l
jaroslav@2
    83
        );
jaroslav@2
    84
    }
jaroslav@3
    85
jaroslav@3
    86
    @Test public void divAndRound() throws Exception {
jaroslav@3
    87
        assertExec(
jaroslav@3
    88
            "Should be rounded to one",
jaroslav@22
    89
            "org_apidesign_vm4brwsr_StaticMethod_divIBD", 
jaroslav@3
    90
            Double.valueOf(1),
jaroslav@3
    91
            3, 3.75
jaroslav@3
    92
        );
jaroslav@3
    93
    }
jaroslav@3
    94
    @Test public void mixedMethodFourParams() throws Exception {
jaroslav@3
    95
        assertExec(
jaroslav@3
    96
            "Should be two",
jaroslav@22
    97
            "org_apidesign_vm4brwsr_StaticMethod_mixIIJBD", 
jaroslav@3
    98
            Double.valueOf(20),
jaroslav@3
    99
            2, 10l, 5, 2.0
jaroslav@3
   100
        );
jaroslav@3
   101
    }
jaroslav@4
   102
    @Test public void factRec() throws Exception {
jaroslav@4
   103
        assertExec(
jaroslav@4
   104
            "Factorial of 5 is 120",
jaroslav@22
   105
            "org_apidesign_vm4brwsr_StaticMethod_factRecJI", 
jaroslav@4
   106
            Double.valueOf(120),
jaroslav@4
   107
            5
jaroslav@4
   108
        );
jaroslav@4
   109
    }
jaroslav@5
   110
    @Test public void factIter() throws Exception {
jaroslav@5
   111
        assertExec(
jaroslav@5
   112
            "Factorial of 5 is 120",
jaroslav@22
   113
            "org_apidesign_vm4brwsr_StaticMethod_factIterJI", 
jaroslav@5
   114
            Double.valueOf(120),
jaroslav@5
   115
            5
jaroslav@5
   116
        );
jaroslav@5
   117
    }
jaroslav@2
   118
    
jaroslav@6
   119
    @Test public void xor() throws Exception {
jaroslav@6
   120
        assertExec(
jaroslav@6
   121
            "Xor is 4",
jaroslav@22
   122
            "org_apidesign_vm4brwsr_StaticMethod_xorJIJ",
jaroslav@6
   123
            Double.valueOf(4),
jaroslav@6
   124
            7,
jaroslav@6
   125
            3
jaroslav@6
   126
        );
jaroslav@6
   127
    }
jaroslav@6
   128
    
jaroslav@7
   129
    @Test public void or() throws Exception {
jaroslav@7
   130
        assertExec(
jaroslav@7
   131
            "Or will be 7",
jaroslav@22
   132
            "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
jaroslav@7
   133
            Double.valueOf(7),
jaroslav@7
   134
            true,
jaroslav@7
   135
            4,
jaroslav@7
   136
            3
jaroslav@7
   137
        );
jaroslav@7
   138
    }
jaroslav@46
   139
    @Test public void nullCheck() throws Exception {
jaroslav@46
   140
        assertExec(
jaroslav@46
   141
            "Returns nothing",
jaroslav@46
   142
            "org_apidesign_vm4brwsr_StaticMethod_noneLjava_lang_ObjectII",
jaroslav@46
   143
            null, 1, 3
jaroslav@46
   144
        );
jaroslav@46
   145
    }
jaroslav@7
   146
    @Test public void and() throws Exception {
jaroslav@7
   147
        assertExec(
jaroslav@7
   148
            "And will be 3",
jaroslav@22
   149
            "org_apidesign_vm4brwsr_StaticMethod_orOrAndJZII",
jaroslav@7
   150
            Double.valueOf(3),
jaroslav@7
   151
            false,
jaroslav@7
   152
            7,
jaroslav@7
   153
            3
jaroslav@7
   154
        );
jaroslav@7
   155
    }
jaroslav@9
   156
    @Test public void inc4() throws Exception {
jaroslav@9
   157
        assertExec(
jaroslav@9
   158
            "It will be 4",
jaroslav@22
   159
            "org_apidesign_vm4brwsr_StaticMethod_inc4I",
jaroslav@9
   160
            Double.valueOf(4)
jaroslav@9
   161
        );
jaroslav@9
   162
    }
jaroslav@7
   163
    
jaroslav@93
   164
    @Test public void shiftLeftInJava() throws Exception {
jaroslav@93
   165
        int res = StaticMethod.shiftLeft(1, 8);
jaroslav@93
   166
        assertEquals(res, 256);
jaroslav@93
   167
    }
jaroslav@93
   168
jaroslav@93
   169
    @Test public void shiftLeftInJS() throws Exception {
jaroslav@93
   170
        assertExec(
jaroslav@93
   171
            "Setting 9th bit",
jaroslav@93
   172
            "org_apidesign_vm4brwsr_StaticMethod_shiftLeftIII",
jaroslav@93
   173
            Double.valueOf(256),
jaroslav@93
   174
            1, 8
jaroslav@93
   175
        );
jaroslav@93
   176
    }
jaroslav@93
   177
jaroslav@93
   178
    @Test public void shiftRightInJava() throws Exception {
jaroslav@93
   179
        int res = StaticMethod.shiftArithmRight(-8, 3, true);
jaroslav@93
   180
        assertEquals(res, -1);
jaroslav@93
   181
    }
jaroslav@93
   182
jaroslav@93
   183
    @Test public void shiftRightInJS() throws Exception {
jaroslav@93
   184
        assertExec(
jaroslav@93
   185
            "Get -1",
jaroslav@93
   186
            "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
jaroslav@93
   187
            Double.valueOf(-1),
jaroslav@93
   188
            -8, 3, true
jaroslav@93
   189
        );
jaroslav@93
   190
    }
jaroslav@93
   191
    @Test public void unsignedShiftRightInJava() throws Exception {
jaroslav@93
   192
        int res = StaticMethod.shiftArithmRight(8, 3, false);
jaroslav@93
   193
        assertEquals(res, 1);
jaroslav@93
   194
    }
jaroslav@93
   195
jaroslav@93
   196
    @Test public void unsignedShiftRightInJS() throws Exception {
jaroslav@93
   197
        assertExec(
jaroslav@93
   198
            "Get -1",
jaroslav@93
   199
            "org_apidesign_vm4brwsr_StaticMethod_shiftArithmRightIIIZ",
jaroslav@93
   200
            Double.valueOf(1),
jaroslav@93
   201
            8, 3, false
jaroslav@93
   202
        );
jaroslav@93
   203
    }
jaroslav@93
   204
    
jaroslav@94
   205
    @Test public void javaScriptBody() throws Exception {
jaroslav@94
   206
        assertExec(
jaroslav@94
   207
            "JavaScript string",
jaroslav@99
   208
            "org_apidesign_vm4brwsr_StaticMethod_i2sLjava_lang_StringII",
jaroslav@94
   209
            "333",
jaroslav@99
   210
            330, 3
jaroslav@94
   211
        );
jaroslav@94
   212
    }
jaroslav@94
   213
    
jaroslav@103
   214
    private static CharSequence codeSeq;
jaroslav@103
   215
    private static Invocable code;
jaroslav@103
   216
    
jaroslav@103
   217
    @BeforeClass 
jaroslav@103
   218
    public void compileTheCode() throws Exception {
jaroslav@103
   219
        StringBuilder sb = new StringBuilder();
jaroslav@103
   220
        code = compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
jaroslav@103
   221
        codeSeq = sb;
jaroslav@103
   222
    }
jaroslav@103
   223
    
jaroslav@103
   224
    
jaroslav@2
   225
    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
jaroslav@2
   226
        StringBuilder sb = new StringBuilder();
jaroslav@2
   227
        
jaroslav@2
   228
        Object ret = null;
jaroslav@2
   229
        try {
jaroslav@103
   230
            ret = code.invokeFunction(methodName, args);
jaroslav@5
   231
        } catch (ScriptException ex) {
jaroslav@103
   232
            fail("Execution failed in\n" + codeSeq, ex);
jaroslav@2
   233
        } catch (NoSuchMethodException ex) {
jaroslav@103
   234
            fail("Cannot find method in\n" + codeSeq, ex);
jaroslav@2
   235
        }
jaroslav@2
   236
        if (ret == null && expRes == null) {
jaroslav@2
   237
            return;
jaroslav@2
   238
        }
jaroslav@46
   239
        if (expRes != null && expRes.equals(ret)) {
jaroslav@2
   240
            return;
jaroslav@2
   241
        }
jaroslav@103
   242
        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
jaroslav@2
   243
        
jaroslav@2
   244
    }
jaroslav@2
   245
jaroslav@13
   246
    static Invocable compileClass(StringBuilder sb, String... names) throws ScriptException, IOException {
jaroslav@19
   247
        if (sb == null) {
jaroslav@19
   248
            sb = new StringBuilder();
jaroslav@19
   249
        }
jaroslav@29
   250
        GenJS.compile(sb, names);
jaroslav@0
   251
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@0
   252
        ScriptEngine js = sem.getEngineByExtension("js");
jaroslav@0
   253
        try {
jaroslav@0
   254
            Object res = js.eval(sb.toString());
jaroslav@0
   255
            assertTrue(js instanceof Invocable, "It is invocable object: " + res);
jaroslav@0
   256
            return (Invocable)js;
jaroslav@0
   257
        } catch (ScriptException ex) {
jaroslav@0
   258
            fail("Could not compile:\n" + sb, ex);
jaroslav@0
   259
            return null;
jaroslav@0
   260
        }
jaroslav@0
   261
    }
jaroslav@0
   262
}