rt/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 27 Feb 2015 10:52:26 +0100
changeset 1795 3c23f0cebd32
parent 1787 ea12a3bb4b33
permissions -rw-r--r--
Flush mapper before pushing back a result of (virtual) method invocation. The static method call was already doing that.
jaroslav@106
     1
/**
jaroslav@106
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1787
     3
 * Copyright (C) 2012-2015 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 static org.testng.Assert.*;
jaroslav@789
    21
import org.testng.annotations.AfterClass;
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
    }
jaroslav@1532
    46
jaroslav@1532
    47
    @Test public void cloneOnArrayAndComponentType() throws Exception {
jaroslav@1532
    48
        String exp = Array.nameOfClonedComponent();
jaroslav@1532
    49
        assertExec("getComponentType on clone", Array.class, "nameOfClonedComponent__Ljava_lang_String_2", 
jaroslav@1532
    50
            exp
jaroslav@1532
    51
        );
jaroslav@1532
    52
    }
jtulach@128
    53
    
jtulach@128
    54
    @Test public void realOperationOnArrays() throws Exception {
jtulach@128
    55
        assertEquals(Array.sum(), 105.0, "Computes to 105");
jtulach@128
    56
    }
jtulach@128
    57
    
jaroslav@21
    58
    @Test public void verifyOperationsOnArrays() throws Exception {
jaroslav@248
    59
        assertExec("The sum is 105", Array.class, "sum__D", 
jaroslav@21
    60
            Double.valueOf(105)
jaroslav@21
    61
        );
jaroslav@21
    62
    }
jaroslav@457
    63
jaroslav@457
    64
    @Test public void twoDoubles() throws Exception {
jaroslav@457
    65
        assertExec("Elements are initialized", Array.class, "twoDoubles__D", 
jaroslav@457
    66
            Double.valueOf(0)
jaroslav@457
    67
        );
jaroslav@457
    68
    }
jaroslav@457
    69
    @Test public void twoInts() throws Exception {
jaroslav@457
    70
        assertExec("Elements are initialized", Array.class, "twoInts__I", 
jaroslav@457
    71
            Double.valueOf(0)
jaroslav@457
    72
        );
jaroslav@457
    73
    }
jaroslav@1702
    74
jaroslav@1702
    75
    @Test public void iterateEmptyArray() throws Exception {
jaroslav@1702
    76
        assertExec("No elements in empty array", Array.class, "iterateArray__Ljava_lang_String_2Z", 
jaroslav@1702
    77
            "", false
jaroslav@1702
    78
        );
jaroslav@1702
    79
    }
jaroslav@1702
    80
jaroslav@1702
    81
    @Test public void iterateEmptyJavaArray() throws Exception {
jaroslav@1702
    82
        assertExec("No elements in empty array", Array.class, "iterateArray__Ljava_lang_String_2Z", 
jaroslav@1702
    83
            "", true
jaroslav@1702
    84
        );
jaroslav@1702
    85
    }
jaroslav@21
    86
    
jaroslav@104
    87
    @Test public void doesCopyArrayWork() throws Exception {
jaroslav@248
    88
        assertExec("Returns 'a'", Array.class, "copyArray__C", Double.valueOf('a'));
jaroslav@104
    89
    }
jaroslav@567
    90
jaroslav@567
    91
    @Test public void verifyObjectArrayClass() throws Exception {
jaroslav@567
    92
        assertExec("Returns 'Object[]'", Array.class, "objectArrayClass__Ljava_lang_String_2", Array.objectArrayClass());
jaroslav@567
    93
    }
jaroslav@571
    94
    @Test public void verifyInstanceOfArray() throws Exception {
jaroslav@571
    95
        assertExec("Returns 'false'", Array.class, "instanceOfArray__ZLjava_lang_Object_2", Double.valueOf(0), "non-array");
jaroslav@571
    96
    }
jaroslav@1534
    97
    @Test public void verifyInstanceOfArrayOnNull() throws Exception {
jaroslav@1534
    98
        assertFalse(Array.instanceOfArray(null), "Null is not an instance of array");
jaroslav@1534
    99
        assertExec("Returns 'false'", Array.class, "instanceOfArray__ZLjava_lang_Object_2", Double.valueOf(0), (Object) null);
jaroslav@1534
   100
    }
jaroslav@1534
   101
    @Test public void verifyInstanceOfArrayStrings() throws Exception {
jaroslav@1534
   102
        assertExec("Returns 'false'", Array.class, "instanceOfArray__ZLjava_lang_Object_2", Double.valueOf(1), "string-array");
jaroslav@1534
   103
    }
jaroslav@807
   104
    @Test public void verifyMultiLen() throws Exception {
jaroslav@807
   105
        assertExec("Multi len is one", Array.class, "multiLen__I", Double.valueOf(1));
jaroslav@807
   106
    }
jaroslav@1534
   107
    @Test public void upCastAnArray() throws Exception {
jaroslav@1534
   108
        assertExec("Cannot cast int to string array", Array.class, "castArray__ZI", Double.valueOf(0), Double.valueOf(0));
jaroslav@1534
   109
    }
jaroslav@1534
   110
    @Test public void upCastANullArray() throws Exception {
jaroslav@1534
   111
        assertExec("Can cast null to string array", Array.class, "castArray__ZI", Double.valueOf(1), Double.valueOf(1));
jaroslav@1534
   112
    }
jaroslav@1534
   113
    @Test public void upCastOK() throws Exception {
jaroslav@1534
   114
        assertExec("Can cast string to string array", Array.class, "castArray__ZI", Double.valueOf(1), Double.valueOf(2));
jaroslav@1534
   115
    }
jaroslav@1534
   116
    @Test public void upCastOK2() throws Exception {
jaroslav@1534
   117
        assertExec("Can cast string to char sequence array", Array.class, "castArray__ZI", Double.valueOf(1), Double.valueOf(3));
jaroslav@1534
   118
    }
jaroslav@1795
   119
    @Test public void twoDimensionalArrayAccess() throws Exception {
jaroslav@1795
   120
        int exp = Array.sumTable();
jaroslav@1795
   121
        assertExec("Is 2d access working OK?", Array.class, "sumTable__I", exp);
jaroslav@1795
   122
    }
jaroslav@1795
   123
    @Test public void ordAccess() throws Exception {
jaroslav@1795
   124
        int exp = Array.plusOrd();
jaroslav@1795
   125
        assertExec("Is int allocated OK?", Array.class, "plusOrd__I", exp);
jaroslav@1795
   126
    }
jaroslav@104
   127
    
jaroslav@708
   128
    private static TestVM code;
jaroslav@103
   129
    
jaroslav@103
   130
    @BeforeClass 
jaroslav@103
   131
    public void compileTheCode() throws Exception {
jaroslav@708
   132
        code = TestVM.compileClass("org/apidesign/vm4brwsr/Array");
jaroslav@103
   133
    }
jaroslav@789
   134
    @AfterClass
jaroslav@789
   135
    public static void releaseTheCode() {
jaroslav@789
   136
        code = null;
jaroslav@789
   137
    }
jaroslav@203
   138
    private static void assertExec(String msg, Class clazz, String method, Object expRes, Object... args) throws Exception {
jaroslav@708
   139
        code.assertExec(msg, clazz, method, expRes, args);
jaroslav@21
   140
    }
jaroslav@21
   141
}