rt/vm/src/test/java/org/apidesign/vm4brwsr/ArrayTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 23 Sep 2014 21:52:27 +0200
changeset 1702 228f26fc1159
parent 1534 ca538fb33f48
child 1787 ea12a3bb4b33
permissions -rw-r--r--
for (var x in array) should return only expected values
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import static org.testng.Assert.*;
    21 import org.testng.annotations.AfterClass;
    22 import org.testng.annotations.BeforeClass;
    23 import org.testng.annotations.Test;
    24 
    25 /**
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 public class ArrayTest {
    30     @Test public void intArrayShouldBeFilledWithZeroes() throws Exception {
    31             assertExec("0 + 0", Array.class, "sum__II", 
    32             Double.valueOf(0), 2
    33         );
    34     }
    35     @Test public void verifySimpleIntOperation() throws Exception {
    36             assertExec("CheckTheSum", Array.class, "simple__IZ", 
    37             Double.valueOf(15), false
    38         );
    39     }
    40     
    41     @Test public void cloneOnArray() throws Exception {
    42             assertExec("CheckTheSum on clone", Array.class, "simple__IZ", 
    43             Double.valueOf(15), true
    44         );
    45     }
    46 
    47     @Test public void cloneOnArrayAndComponentType() throws Exception {
    48         String exp = Array.nameOfClonedComponent();
    49         assertExec("getComponentType on clone", Array.class, "nameOfClonedComponent__Ljava_lang_String_2", 
    50             exp
    51         );
    52     }
    53     
    54     @Test public void realOperationOnArrays() throws Exception {
    55         assertEquals(Array.sum(), 105.0, "Computes to 105");
    56     }
    57     
    58     @Test public void verifyOperationsOnArrays() throws Exception {
    59         assertExec("The sum is 105", Array.class, "sum__D", 
    60             Double.valueOf(105)
    61         );
    62     }
    63 
    64     @Test public void twoDoubles() throws Exception {
    65         assertExec("Elements are initialized", Array.class, "twoDoubles__D", 
    66             Double.valueOf(0)
    67         );
    68     }
    69     @Test public void twoInts() throws Exception {
    70         assertExec("Elements are initialized", Array.class, "twoInts__I", 
    71             Double.valueOf(0)
    72         );
    73     }
    74 
    75     @Test public void iterateEmptyArray() throws Exception {
    76         assertExec("No elements in empty array", Array.class, "iterateArray__Ljava_lang_String_2Z", 
    77             "", false
    78         );
    79     }
    80 
    81     @Test public void iterateEmptyJavaArray() throws Exception {
    82         assertExec("No elements in empty array", Array.class, "iterateArray__Ljava_lang_String_2Z", 
    83             "", true
    84         );
    85     }
    86     
    87     @Test public void doesCopyArrayWork() throws Exception {
    88         assertExec("Returns 'a'", Array.class, "copyArray__C", Double.valueOf('a'));
    89     }
    90 
    91     @Test public void verifyObjectArrayClass() throws Exception {
    92         assertExec("Returns 'Object[]'", Array.class, "objectArrayClass__Ljava_lang_String_2", Array.objectArrayClass());
    93     }
    94     @Test public void verifyInstanceOfArray() throws Exception {
    95         assertExec("Returns 'false'", Array.class, "instanceOfArray__ZLjava_lang_Object_2", Double.valueOf(0), "non-array");
    96     }
    97     @Test public void verifyInstanceOfArrayOnNull() throws Exception {
    98         assertFalse(Array.instanceOfArray(null), "Null is not an instance of array");
    99         assertExec("Returns 'false'", Array.class, "instanceOfArray__ZLjava_lang_Object_2", Double.valueOf(0), (Object) null);
   100     }
   101     @Test public void verifyInstanceOfArrayStrings() throws Exception {
   102         assertExec("Returns 'false'", Array.class, "instanceOfArray__ZLjava_lang_Object_2", Double.valueOf(1), "string-array");
   103     }
   104     @Test public void verifyMultiLen() throws Exception {
   105         assertExec("Multi len is one", Array.class, "multiLen__I", Double.valueOf(1));
   106     }
   107     @Test public void upCastAnArray() throws Exception {
   108         assertExec("Cannot cast int to string array", Array.class, "castArray__ZI", Double.valueOf(0), Double.valueOf(0));
   109     }
   110     @Test public void upCastANullArray() throws Exception {
   111         assertExec("Can cast null to string array", Array.class, "castArray__ZI", Double.valueOf(1), Double.valueOf(1));
   112     }
   113     @Test public void upCastOK() throws Exception {
   114         assertExec("Can cast string to string array", Array.class, "castArray__ZI", Double.valueOf(1), Double.valueOf(2));
   115     }
   116     @Test public void upCastOK2() throws Exception {
   117         assertExec("Can cast string to char sequence array", Array.class, "castArray__ZI", Double.valueOf(1), Double.valueOf(3));
   118     }
   119     
   120     private static TestVM code;
   121     
   122     @BeforeClass 
   123     public void compileTheCode() throws Exception {
   124         code = TestVM.compileClass("org/apidesign/vm4brwsr/Array");
   125     }
   126     @AfterClass
   127     public static void releaseTheCode() {
   128         code = null;
   129     }
   130     private static void assertExec(String msg, Class clazz, String method, Object expRes, Object... args) throws Exception {
   131         code.assertExec(msg, clazz, method, expRes, args);
   132     }
   133 }