rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Feb 2014 07:54:35 +0100
branchReducedStack
changeset 1466 39d26d3686d9
parent 789 bb7506513353
child 1467 5538c1eb03be
permissions -rw-r--r--
Don't clear the stack mapping when doing a cast
     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     @Test public void mixedMethodFourParams() throws Exception {
   174         assertExec(
   175             "Should be two",
   176             StaticMethod.class, "mix__IIJBD", 
   177             Double.valueOf(20),
   178             2, 10l, 5, 2.0
   179         );
   180     }
   181     @Test public void factRec() throws Exception {
   182         assertExec(
   183             "Factorial of 5 is 120",
   184             StaticMethod.class, "factRec__JI", 
   185             Double.valueOf(120),
   186             5
   187         );
   188     }
   189     @Test public void factIter() throws Exception {
   190         assertExec(
   191             "Factorial of 5 is 120",
   192             StaticMethod.class, "factIter__JI", 
   193             Double.valueOf(120),
   194             5
   195         );
   196     }
   197     
   198     @Test public void xor() throws Exception {
   199         assertExec(
   200             "Xor is 4",
   201             StaticMethod.class, "xor__JIJ",
   202             Double.valueOf(4),
   203             7,
   204             3
   205         );
   206     }
   207     
   208     @Test public void or() throws Exception {
   209         assertExec(
   210             "Or will be 7",
   211             StaticMethod.class, "orOrAnd__JZII",
   212             Double.valueOf(7),
   213             true,
   214             4,
   215             3
   216         );
   217     }
   218     @Test public void nullCheck() throws Exception {
   219         assertExec(
   220             "Returns nothing",
   221             StaticMethod.class, "none__Ljava_lang_Object_2II",
   222             null, 1, 3
   223         );
   224     }
   225     @Test public void and() throws Exception {
   226         assertExec(
   227             "And will be 3",
   228             StaticMethod.class, "orOrAnd__JZII",
   229             Double.valueOf(3),
   230             false,
   231             7,
   232             3
   233         );
   234     }
   235     @Test public void inc4() throws Exception {
   236         assertExec(
   237             "It will be 4",
   238             StaticMethod.class, "inc4__I",
   239             Double.valueOf(4)
   240         );
   241     }
   242     
   243     @Test public void shiftLeftInJava() throws Exception {
   244         int res = StaticMethod.shiftLeft(1, 8);
   245         assertEquals(res, 256);
   246     }
   247 
   248     @Test public void shiftLeftInJS() throws Exception {
   249         assertExec(
   250             "Setting 9th bit",
   251             StaticMethod.class, "shiftLeft__III",
   252             Double.valueOf(256),
   253             1, 8
   254         );
   255     }
   256 
   257     @Test public void shiftRightInJava() throws Exception {
   258         int res = StaticMethod.shiftArithmRight(-8, 3, true);
   259         assertEquals(res, -1);
   260     }
   261 
   262     @Test public void shiftRightInJS() throws Exception {
   263         assertExec(
   264             "Get -1",
   265             StaticMethod.class, "shiftArithmRight__IIIZ",
   266             Double.valueOf(-1),
   267             -8, 3, true
   268         );
   269     }
   270     @Test public void unsignedShiftRightInJava() throws Exception {
   271         int res = StaticMethod.shiftArithmRight(8, 3, false);
   272         assertEquals(res, 1);
   273     }
   274 
   275     @Test public void unsignedShiftRightInJS() throws Exception {
   276         assertExec(
   277             "Get -1",
   278             StaticMethod.class, "shiftArithmRight__IIIZ",
   279             Double.valueOf(1),
   280             8, 3, false
   281         );
   282     }
   283     
   284     @Test public void javaScriptBody() throws Exception {
   285         assertExec(
   286             "JavaScript string",
   287             StaticMethod.class, "i2s__Ljava_lang_String_2II",
   288             "333",
   289             330, 3
   290         );
   291     }
   292     
   293     @Test public void switchJarda() throws Exception {
   294         assertExec(
   295             "The expected value",
   296             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   297             "Jarda",
   298             0
   299         );
   300     }
   301     
   302     @Test public void switchDarda() throws Exception {
   303         assertExec(
   304             "The expected value",
   305             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   306             "Darda",
   307             1
   308         );
   309     }
   310     @Test public void switchParda() throws Exception {
   311         assertExec(
   312             "The expected value",
   313             StaticMethod.class, "swtch2__Ljava_lang_String_2I",
   314             "Parda",
   315             22
   316         );
   317     }
   318     @Test public void switchMarda() throws Exception {
   319         assertExec(
   320             "The expected value",
   321             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   322             "Marda",
   323             -433
   324         );
   325     }
   326     
   327     @Test public void checkNullCast() throws Exception {
   328         assertExec("Null can be cast to any type",
   329             StaticMethod.class, "castNull__Ljava_lang_String_2Z", 
   330             null, true
   331         );
   332     }
   333     
   334     private static TestVM code;
   335     
   336     @BeforeClass 
   337     public static void compileTheCode() throws Exception {
   338         StringBuilder sb = new StringBuilder();
   339         code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   340     }
   341     @AfterClass
   342     public static void releaseTheCode() {
   343         code = null;
   344     }
   345 
   346     private void assertExec(
   347         String msg, Class<?> clazz, String method, 
   348         Object ret, Object... args
   349     ) throws Exception {
   350         code.assertExec(msg, clazz, method, ret, args);
   351     }
   352 }