rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 17 Jan 2017 07:04:06 +0100
changeset 1985 cd1cc103a03c
parent 1773 9830c8b761ce
permissions -rw-r--r--
Implementation of ClassValue for bck2brwsr
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 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 /** Checks the basic behavior of the translator.
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 public class StaticMethodTest {
    30     @Test public void threePlusFour() throws Exception {
    31         assertExec(
    32             "Should be seven", 
    33             StaticMethod.class, "sum__III", 
    34             Double.valueOf(7), 
    35             3, 4
    36         );
    37     }
    38 
    39     @Test public void cast() throws Exception {
    40         assertExec(
    41             "Length is four", 
    42             StaticMethod.class, "castString__ILjava_lang_Object_2", 
    43             Double.valueOf(4), 
    44             "Ahoj"
    45         );
    46     }
    47 
    48     @Test public void checkReallyInitializedValues() throws Exception {
    49         assertExec(
    50             "Return true", 
    51             StaticMethod.class, "isNull__Z", 
    52             Double.valueOf(1)
    53         );
    54     }
    55 
    56     @Test public void powerOfThree() throws Exception {
    57         assertExec(
    58             "Should be nine", 
    59             StaticMethod.class, "power__FF", 
    60             Double.valueOf(9),
    61             3.0f
    62         );
    63     }
    64 
    65     @Test public void minusOne() throws Exception {
    66         assertExec(
    67             "Should be minus one", 
    68             StaticMethod.class, "minusOne__I", 
    69             Double.valueOf(-1)
    70         );
    71     }
    72 
    73     @Test public void doubleWithoutLong() throws Exception {
    74         assertExec(
    75             "Should be two",
    76             StaticMethod.class, "minus__DDJ", 
    77             Double.valueOf(2),
    78             3.0d, 1l
    79         );
    80     }
    81     
    82     @Test public void divAndRound() throws Exception {
    83         assertExec(
    84             "Should be rounded to one",
    85             StaticMethod.class, "div__IBD", 
    86             Double.valueOf(1),
    87             3, 3.75
    88         );
    89     }
    90   
    91     @Test public void inflaterInit() throws Exception {
    92         assertExec(
    93             "Down and minus",
    94             StaticMethod.class, "initInflater__IIZ",
    95             Double.valueOf(-9),
    96             10, true
    97         );
    98     }
    99 
   100     @Test public void inflaterInitNoNeg() throws Exception {
   101         assertExec(
   102             "One up",
   103             StaticMethod.class, "initInflater__IIZ",
   104             Double.valueOf(11),
   105             10, false
   106         );
   107     }
   108     
   109     @Test public void mixedMethodFourParams() throws Exception {
   110         assertExec(
   111             "Should be two",
   112             StaticMethod.class, "mix__IIJBD", 
   113             Double.valueOf(20),
   114             2, 10l, 5, 2.0
   115         );
   116     }
   117     @Test public void factRec() throws Exception {
   118         assertExec(
   119             "Factorial of 5 is 120",
   120             StaticMethod.class, "factRec__JI", 
   121             Double.valueOf(120),
   122             5
   123         );
   124     }
   125     @Test public void factIter() throws Exception {
   126         assertExec(
   127             "Factorial of 5 is 120",
   128             StaticMethod.class, "factIter__JI", 
   129             Double.valueOf(120),
   130             5
   131         );
   132     }
   133     
   134     @Test public void xor() throws Exception {
   135         assertExec(
   136             "Xor is 4",
   137             StaticMethod.class, "xor__JIJ",
   138             Double.valueOf(4),
   139             7,
   140             3
   141         );
   142     }
   143     
   144     @Test public void collectionToString() throws Exception {
   145         String exp = StaticMethod.toStringArr();
   146         assertExec(
   147             "0 to 4",
   148             StaticMethod.class, "toStringArr__Ljava_lang_String_2",
   149             exp
   150         );
   151     }
   152     
   153     @Test public void or() throws Exception {
   154         assertExec(
   155             "Or will be 7",
   156             StaticMethod.class, "orOrAnd__JZII",
   157             Double.valueOf(7),
   158             true,
   159             4,
   160             3
   161         );
   162     }
   163     @Test public void nullCheck() throws Exception {
   164         assertExec(
   165             "Returns nothing",
   166             StaticMethod.class, "none__Ljava_lang_Object_2II",
   167             null, 1, 3
   168         );
   169     }
   170     @Test public void and() throws Exception {
   171         assertExec(
   172             "And will be 3",
   173             StaticMethod.class, "orOrAnd__JZII",
   174             Double.valueOf(3),
   175             false,
   176             7,
   177             3
   178         );
   179     }
   180     @Test public void inc4() throws Exception {
   181         assertExec(
   182             "It will be 4",
   183             StaticMethod.class, "inc4__I",
   184             Double.valueOf(4)
   185         );
   186     }
   187     
   188     @Test public void shiftLeftInJava() throws Exception {
   189         int res = StaticMethod.shiftLeft(1, 8);
   190         assertEquals(res, 256);
   191     }
   192 
   193     @Test public void shiftLeftInJS() throws Exception {
   194         assertExec(
   195             "Setting 9th bit",
   196             StaticMethod.class, "shiftLeft__III",
   197             Double.valueOf(256),
   198             1, 8
   199         );
   200     }
   201 
   202     @Test public void shiftRightInJava() throws Exception {
   203         int res = StaticMethod.shiftArithmRight(-8, 3, true);
   204         assertEquals(res, -1);
   205     }
   206 
   207     @Test public void shiftRightInJS() throws Exception {
   208         assertExec(
   209             "Get -1",
   210             StaticMethod.class, "shiftArithmRight__IIIZ",
   211             Double.valueOf(-1),
   212             -8, 3, true
   213         );
   214     }
   215     @Test public void unsignedShiftRightInJava() throws Exception {
   216         int res = StaticMethod.shiftArithmRight(8, 3, false);
   217         assertEquals(res, 1);
   218     }
   219 
   220     @Test public void unsignedShiftRightInJS() throws Exception {
   221         assertExec(
   222             "Get -1",
   223             StaticMethod.class, "shiftArithmRight__IIIZ",
   224             Double.valueOf(1),
   225             8, 3, false
   226         );
   227     }
   228     
   229     @Test public void javaScriptBody() throws Exception {
   230         assertExec(
   231             "JavaScript string",
   232             StaticMethod.class, "i2s__Ljava_lang_String_2II",
   233             "333",
   234             330, 3
   235         );
   236     }
   237     
   238     @Test public void switchJarda() throws Exception {
   239         assertExec(
   240             "The expected value",
   241             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   242             "Jarda",
   243             0
   244         );
   245     }
   246     
   247     @Test public void switchDarda() throws Exception {
   248         assertExec(
   249             "The expected value",
   250             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   251             "Darda",
   252             1
   253         );
   254     }
   255     @Test public void switchParda() throws Exception {
   256         assertExec(
   257             "The expected value",
   258             StaticMethod.class, "swtch2__Ljava_lang_String_2I",
   259             "Parda",
   260             22
   261         );
   262     }
   263     @Test public void switchMarda() throws Exception {
   264         assertExec(
   265             "The expected value",
   266             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   267             "Marda",
   268             -433
   269         );
   270     }
   271     
   272     @Test public void checkNullCast() throws Exception {
   273         assertExec("Null can be cast to any type",
   274             StaticMethod.class, "castNull__Ljava_lang_String_2Z", 
   275             null, true
   276         );
   277     }
   278     
   279     @Test public void stringConstantIsCopied() throws Exception {
   280         assertExec("String constants are copied between class pools",
   281             StaticMethod.class, "helloWorldLength__ILjava_lang_String_2", 
   282             17, "Jardo"
   283         );
   284     }
   285     
   286     private static TestVM code;
   287     
   288     @BeforeClass 
   289     public static void compileTheCode() throws Exception {
   290         StringBuilder sb = new StringBuilder();
   291         code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   292     }
   293     @AfterClass
   294     public static void releaseTheCode() {
   295         code = null;
   296     }
   297 
   298     private void assertExec(
   299         String msg, Class<?> clazz, String method, 
   300         Object ret, Object... args
   301     ) throws Exception {
   302         code.assertExec(msg, clazz, method, ret, args);
   303     }
   304 }