vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 15 Jan 2013 12:26:19 +0100
changeset 457 b0e82dcf51fb
parent 446 5b3d5ed03014
child 567 62dd2c794431
permissions -rw-r--r--
Initialize multi dimensional arrays with 0 for primitive types
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@22
    18
package org.apidesign.vm4brwsr;
jaroslav@21
    19
jaroslav@21
    20
import javax.script.Invocable;
jaroslav@21
    21
import static org.testng.Assert.*;
jaroslav@103
    22
import org.testng.annotations.BeforeClass;
jtulach@128
    23
import org.testng.annotations.Test;
jaroslav@21
    24
jaroslav@21
    25
/**
jaroslav@21
    26
 *
jaroslav@21
    27
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@21
    28
 */
jaroslav@21
    29
public class ArrayTest {
jaroslav@446
    30
    @Test public void intArrayShouldBeFilledWithZeroes() throws Exception {
jaroslav@446
    31
            assertExec("0 + 0", Array.class, "sum__II", 
jaroslav@446
    32
            Double.valueOf(0), 2
jaroslav@446
    33
        );
jaroslav@446
    34
    }
jaroslav@21
    35
    @Test public void verifySimpleIntOperation() throws Exception {
jaroslav@402
    36
            assertExec("CheckTheSum", Array.class, "simple__IZ", 
jaroslav@402
    37
            Double.valueOf(15), false
jaroslav@402
    38
        );
jaroslav@402
    39
    }
jaroslav@402
    40
    
jaroslav@402
    41
    @Test public void cloneOnArray() throws Exception {
jaroslav@402
    42
            assertExec("CheckTheSum on clone", Array.class, "simple__IZ", 
jaroslav@402
    43
            Double.valueOf(15), true
jaroslav@21
    44
        );
jaroslav@21
    45
    }
jtulach@128
    46
    
jtulach@128
    47
    @Test public void realOperationOnArrays() throws Exception {
jtulach@128
    48
        assertEquals(Array.sum(), 105.0, "Computes to 105");
jtulach@128
    49
    }
jtulach@128
    50
    
jaroslav@21
    51
    @Test public void verifyOperationsOnArrays() throws Exception {
jaroslav@248
    52
        assertExec("The sum is 105", Array.class, "sum__D", 
jaroslav@21
    53
            Double.valueOf(105)
jaroslav@21
    54
        );
jaroslav@21
    55
    }
jaroslav@457
    56
jaroslav@457
    57
    @Test public void twoDoubles() throws Exception {
jaroslav@457
    58
        assertExec("Elements are initialized", Array.class, "twoDoubles__D", 
jaroslav@457
    59
            Double.valueOf(0)
jaroslav@457
    60
        );
jaroslav@457
    61
    }
jaroslav@457
    62
    @Test public void twoInts() throws Exception {
jaroslav@457
    63
        assertExec("Elements are initialized", Array.class, "twoInts__I", 
jaroslav@457
    64
            Double.valueOf(0)
jaroslav@457
    65
        );
jaroslav@457
    66
    }
jaroslav@21
    67
    
jaroslav@104
    68
    @Test public void doesCopyArrayWork() throws Exception {
jaroslav@248
    69
        assertExec("Returns 'a'", Array.class, "copyArray__C", Double.valueOf('a'));
jaroslav@104
    70
    }
jaroslav@104
    71
    
jaroslav@103
    72
    private static CharSequence codeSeq;
jaroslav@103
    73
    private static Invocable code;
jaroslav@103
    74
    
jaroslav@103
    75
    @BeforeClass 
jaroslav@103
    76
    public void compileTheCode() throws Exception {
jaroslav@21
    77
        StringBuilder sb = new StringBuilder();
jaroslav@103
    78
        code = StaticMethodTest.compileClass(sb, 
jaroslav@22
    79
            "org/apidesign/vm4brwsr/Array"
jaroslav@21
    80
        );
jaroslav@103
    81
        codeSeq = sb;
jaroslav@103
    82
    }
jaroslav@203
    83
    private static void assertExec(String msg, Class clazz, String method, Object expRes, Object... args) throws Exception {
jaroslav@203
    84
        StaticMethodTest.assertExec(code, codeSeq, msg, clazz, method, expRes, args);
jaroslav@21
    85
    }
jaroslav@21
    86
}