rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Feb 2014 12:08:31 +0100
branchReducedStack
changeset 1467 5538c1eb03be
parent 1466 39d26d3686d9
child 1468 5d6b648a39db
permissions -rw-r--r--
Correctly encode the index of the type using bit shift
     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 /** 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 rintNegativeUp() throws Exception {
    83         final double cnts = -453904.634;
    84         assertExec(
    85             "Should round up to end with 5",
    86             Math.class, "rint__DD", 
    87             -453905.0, cnts
    88         );
    89     }
    90 
    91     @Test public void rintNegativeDown() throws Exception {
    92         final double cnts = -453904.434;
    93         assertExec(
    94             "Should round up to end with 4",
    95             Math.class, "rint__DD", 
    96             -453904.0, cnts
    97         );
    98     }
    99 
   100     @Test public void rintPositiveUp() throws Exception {
   101         final double cnts = 453904.634;
   102         assertExec(
   103             "Should round up to end with 5",
   104             Math.class, "rint__DD", 
   105             453905.0, cnts
   106         );
   107     }
   108     @Test public void rintPositiveDown() throws Exception {
   109         final double cnts = 453904.434;
   110         assertExec(
   111             "Should round up to end with 4",
   112             Math.class, "rint__DD", 
   113             453904.0, cnts
   114         );
   115     }
   116     @Test public void rintOneHalf() throws Exception {
   117         final double cnts = 1.5;
   118         assertExec(
   119             "Should round up to end with 2",
   120             Math.class, "rint__DD", 
   121             2.0, cnts
   122         );
   123     }
   124     @Test public void rintNegativeOneHalf() throws Exception {
   125         final double cnts = -1.5;
   126         assertExec(
   127             "Should round up to end with 2",
   128             Math.class, "rint__DD", 
   129             -2.0, cnts
   130         );
   131     }
   132     @Test public void rintTwoAndHalf() throws Exception {
   133         final double cnts = 2.5;
   134         assertExec(
   135             "Should round up to end with 2",
   136             Math.class, "rint__DD", 
   137             2.0, cnts
   138         );
   139     }
   140     @Test public void rintNegativeTwoOneHalf() throws Exception {
   141         final double cnts = -2.5;
   142         assertExec(
   143             "Should round up to end with 2",
   144             Math.class, "rint__DD", 
   145             -2.0, cnts
   146         );
   147     }
   148 
   149     @Test public void ieeeReminder1() throws Exception {
   150         assertExec(
   151             "Same result 1",
   152             Math.class, "IEEEremainder__DDD", 
   153             Math.IEEEremainder(10.0, 4.5), 10.0, 4.5
   154         );
   155     }
   156 
   157     @Test public void ieeeReminder2() throws Exception {
   158         assertExec(
   159             "Same result 1",
   160             Math.class, "IEEEremainder__DDD", 
   161             Math.IEEEremainder(Integer.MAX_VALUE, -4.5), Integer.MAX_VALUE, -4.5
   162         );
   163     }
   164 
   165     @Test public void divAndRound() throws Exception {
   166         assertExec(
   167             "Should be rounded to one",
   168             StaticMethod.class, "div__IBD", 
   169             Double.valueOf(1),
   170             3, 3.75
   171         );
   172     }
   173   
   174     @Test public void inflaterInit() throws Exception {
   175         assertExec(
   176             "Down and minus",
   177             StaticMethod.class, "initInflater__IIZ",
   178             Double.valueOf(-9),
   179             10, true
   180         );
   181     }
   182 
   183     @Test public void inflaterInitNoNeg() throws Exception {
   184         assertExec(
   185             "One up",
   186             StaticMethod.class, "initInflater__IIZ",
   187             Double.valueOf(11),
   188             10, false
   189         );
   190     }
   191     
   192     @Test public void mixedMethodFourParams() throws Exception {
   193         assertExec(
   194             "Should be two",
   195             StaticMethod.class, "mix__IIJBD", 
   196             Double.valueOf(20),
   197             2, 10l, 5, 2.0
   198         );
   199     }
   200     @Test public void factRec() throws Exception {
   201         assertExec(
   202             "Factorial of 5 is 120",
   203             StaticMethod.class, "factRec__JI", 
   204             Double.valueOf(120),
   205             5
   206         );
   207     }
   208     @Test public void factIter() throws Exception {
   209         assertExec(
   210             "Factorial of 5 is 120",
   211             StaticMethod.class, "factIter__JI", 
   212             Double.valueOf(120),
   213             5
   214         );
   215     }
   216     
   217     @Test public void xor() throws Exception {
   218         assertExec(
   219             "Xor is 4",
   220             StaticMethod.class, "xor__JIJ",
   221             Double.valueOf(4),
   222             7,
   223             3
   224         );
   225     }
   226     
   227     @Test public void or() throws Exception {
   228         assertExec(
   229             "Or will be 7",
   230             StaticMethod.class, "orOrAnd__JZII",
   231             Double.valueOf(7),
   232             true,
   233             4,
   234             3
   235         );
   236     }
   237     @Test public void nullCheck() throws Exception {
   238         assertExec(
   239             "Returns nothing",
   240             StaticMethod.class, "none__Ljava_lang_Object_2II",
   241             null, 1, 3
   242         );
   243     }
   244     @Test public void and() throws Exception {
   245         assertExec(
   246             "And will be 3",
   247             StaticMethod.class, "orOrAnd__JZII",
   248             Double.valueOf(3),
   249             false,
   250             7,
   251             3
   252         );
   253     }
   254     @Test public void inc4() throws Exception {
   255         assertExec(
   256             "It will be 4",
   257             StaticMethod.class, "inc4__I",
   258             Double.valueOf(4)
   259         );
   260     }
   261     
   262     @Test public void shiftLeftInJava() throws Exception {
   263         int res = StaticMethod.shiftLeft(1, 8);
   264         assertEquals(res, 256);
   265     }
   266 
   267     @Test public void shiftLeftInJS() throws Exception {
   268         assertExec(
   269             "Setting 9th bit",
   270             StaticMethod.class, "shiftLeft__III",
   271             Double.valueOf(256),
   272             1, 8
   273         );
   274     }
   275 
   276     @Test public void shiftRightInJava() throws Exception {
   277         int res = StaticMethod.shiftArithmRight(-8, 3, true);
   278         assertEquals(res, -1);
   279     }
   280 
   281     @Test public void shiftRightInJS() throws Exception {
   282         assertExec(
   283             "Get -1",
   284             StaticMethod.class, "shiftArithmRight__IIIZ",
   285             Double.valueOf(-1),
   286             -8, 3, true
   287         );
   288     }
   289     @Test public void unsignedShiftRightInJava() throws Exception {
   290         int res = StaticMethod.shiftArithmRight(8, 3, false);
   291         assertEquals(res, 1);
   292     }
   293 
   294     @Test public void unsignedShiftRightInJS() throws Exception {
   295         assertExec(
   296             "Get -1",
   297             StaticMethod.class, "shiftArithmRight__IIIZ",
   298             Double.valueOf(1),
   299             8, 3, false
   300         );
   301     }
   302     
   303     @Test public void javaScriptBody() throws Exception {
   304         assertExec(
   305             "JavaScript string",
   306             StaticMethod.class, "i2s__Ljava_lang_String_2II",
   307             "333",
   308             330, 3
   309         );
   310     }
   311     
   312     @Test public void switchJarda() throws Exception {
   313         assertExec(
   314             "The expected value",
   315             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   316             "Jarda",
   317             0
   318         );
   319     }
   320     
   321     @Test public void switchDarda() throws Exception {
   322         assertExec(
   323             "The expected value",
   324             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   325             "Darda",
   326             1
   327         );
   328     }
   329     @Test public void switchParda() throws Exception {
   330         assertExec(
   331             "The expected value",
   332             StaticMethod.class, "swtch2__Ljava_lang_String_2I",
   333             "Parda",
   334             22
   335         );
   336     }
   337     @Test public void switchMarda() throws Exception {
   338         assertExec(
   339             "The expected value",
   340             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   341             "Marda",
   342             -433
   343         );
   344     }
   345     
   346     @Test public void checkNullCast() throws Exception {
   347         assertExec("Null can be cast to any type",
   348             StaticMethod.class, "castNull__Ljava_lang_String_2Z", 
   349             null, true
   350         );
   351     }
   352     
   353     private static TestVM code;
   354     
   355     @BeforeClass 
   356     public static void compileTheCode() throws Exception {
   357         StringBuilder sb = new StringBuilder();
   358         code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   359     }
   360     @AfterClass
   361     public static void releaseTheCode() {
   362         code = null;
   363     }
   364 
   365     private void assertExec(
   366         String msg, Class<?> clazz, String method, 
   367         Object ret, Object... args
   368     ) throws Exception {
   369         code.assertExec(msg, clazz, method, ret, args);
   370     }
   371 }