vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 16 Oct 2012 12:42:00 +0200
changeset 106 346633cd13d6
parent 104 1376481f15e7
child 128 8db5cf267d74
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@21
    18
/*
jaroslav@21
    19
Java 4 Browser Bytecode Translator
jaroslav@21
    20
Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@21
    21
jaroslav@21
    22
This program is free software: you can redistribute it and/or modify
jaroslav@21
    23
it under the terms of the GNU General Public License as published by
jaroslav@21
    24
the Free Software Foundation, version 2 of the License.
jaroslav@21
    25
jaroslav@21
    26
This program is distributed in the hope that it will be useful,
jaroslav@21
    27
but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@21
    28
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@21
    29
GNU General Public License for more details.
jaroslav@21
    30
jaroslav@21
    31
You should have received a copy of the GNU General Public License
jaroslav@21
    32
along with this program. Look for COPYING file in the top folder.
jaroslav@21
    33
If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@21
    34
*/
jaroslav@22
    35
package org.apidesign.vm4brwsr;
jaroslav@21
    36
jaroslav@21
    37
import javax.script.Invocable;
jaroslav@21
    38
import javax.script.ScriptException;
jaroslav@21
    39
import org.testng.annotations.Test;
jaroslav@21
    40
import static org.testng.Assert.*;
jaroslav@103
    41
import org.testng.annotations.BeforeClass;
jaroslav@21
    42
jaroslav@21
    43
/**
jaroslav@21
    44
 *
jaroslav@21
    45
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@21
    46
 */
jaroslav@21
    47
public class ArrayTest {
jaroslav@21
    48
    @Test public void verifySimpleIntOperation() throws Exception {
jaroslav@103
    49
            assertExec("CheckTheSum", "org_apidesign_vm4brwsr_Array_simpleI", 
jaroslav@21
    50
            Double.valueOf(15)
jaroslav@21
    51
        );
jaroslav@21
    52
    }
jaroslav@21
    53
    @Test public void verifyOperationsOnArrays() throws Exception {
jaroslav@22
    54
        assertExec("The sum is 105", "org_apidesign_vm4brwsr_Array_sumD", 
jaroslav@21
    55
            Double.valueOf(105)
jaroslav@21
    56
        );
jaroslav@21
    57
    }
jaroslav@21
    58
    
jaroslav@104
    59
    @Test public void doesCopyArrayWork() throws Exception {
jaroslav@104
    60
        assertExec("Returns 'a'", "org_apidesign_vm4brwsr_Array_copyArrayC", Double.valueOf('a'));
jaroslav@104
    61
    }
jaroslav@104
    62
    
jaroslav@103
    63
    private static CharSequence codeSeq;
jaroslav@103
    64
    private static Invocable code;
jaroslav@103
    65
    
jaroslav@103
    66
    @BeforeClass 
jaroslav@103
    67
    public void compileTheCode() throws Exception {
jaroslav@21
    68
        StringBuilder sb = new StringBuilder();
jaroslav@103
    69
        code = StaticMethodTest.compileClass(sb, 
jaroslav@22
    70
            "org/apidesign/vm4brwsr/Array"
jaroslav@21
    71
        );
jaroslav@103
    72
        codeSeq = sb;
jaroslav@103
    73
    }
jaroslav@103
    74
    private static void assertExec(String msg, String methodName, Object expRes, Object... args) throws Exception {
jaroslav@21
    75
        Object ret = null;
jaroslav@21
    76
        try {
jaroslav@103
    77
            ret = code.invokeFunction(methodName, args);
jaroslav@21
    78
        } catch (ScriptException ex) {
jaroslav@103
    79
            fail("Execution failed in\n" + codeSeq, ex);
jaroslav@21
    80
        } catch (NoSuchMethodException ex) {
jaroslav@103
    81
            fail("Cannot find method in\n" + codeSeq, ex);
jaroslav@21
    82
        }
jaroslav@21
    83
        if (ret == null && expRes == null) {
jaroslav@21
    84
            return;
jaroslav@21
    85
        }
jaroslav@21
    86
        if (expRes.equals(ret)) {
jaroslav@21
    87
            return;
jaroslav@21
    88
        }
jaroslav@103
    89
        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + codeSeq);
jaroslav@21
    90
    }
jaroslav@21
    91
}