rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 28 Feb 2013 07:48:54 +0100
changeset 789 bb7506513353
parent 772 d382dacfd73f
child 1466 39d26d3686d9
permissions -rw-r--r--
releasing the compiled code as soon as it is no longer needed to prevent OutOfMemoryError when adding more and more tests
     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 checkReallyInitializedValues() throws Exception {
    40         assertExec(
    41             "Return true", 
    42             StaticMethod.class, "isNull__Z", 
    43             Double.valueOf(1)
    44         );
    45     }
    46 
    47     @Test public void powerOfThree() throws Exception {
    48         assertExec(
    49             "Should be nine", 
    50             StaticMethod.class, "power__FF", 
    51             Double.valueOf(9),
    52             3.0f
    53         );
    54     }
    55 
    56     @Test public void minusOne() throws Exception {
    57         assertExec(
    58             "Should be minus one", 
    59             StaticMethod.class, "minusOne__I", 
    60             Double.valueOf(-1)
    61         );
    62     }
    63 
    64     @Test public void doubleWithoutLong() throws Exception {
    65         assertExec(
    66             "Should be two",
    67             StaticMethod.class, "minus__DDJ", 
    68             Double.valueOf(2),
    69             3.0d, 1l
    70         );
    71     }
    72     
    73     @Test public void rintNegativeUp() throws Exception {
    74         final double cnts = -453904.634;
    75         assertExec(
    76             "Should round up to end with 5",
    77             Math.class, "rint__DD", 
    78             -453905.0, cnts
    79         );
    80     }
    81 
    82     @Test public void rintNegativeDown() throws Exception {
    83         final double cnts = -453904.434;
    84         assertExec(
    85             "Should round up to end with 4",
    86             Math.class, "rint__DD", 
    87             -453904.0, cnts
    88         );
    89     }
    90 
    91     @Test public void rintPositiveUp() throws Exception {
    92         final double cnts = 453904.634;
    93         assertExec(
    94             "Should round up to end with 5",
    95             Math.class, "rint__DD", 
    96             453905.0, cnts
    97         );
    98     }
    99     @Test public void rintPositiveDown() throws Exception {
   100         final double cnts = 453904.434;
   101         assertExec(
   102             "Should round up to end with 4",
   103             Math.class, "rint__DD", 
   104             453904.0, cnts
   105         );
   106     }
   107     @Test public void rintOneHalf() throws Exception {
   108         final double cnts = 1.5;
   109         assertExec(
   110             "Should round up to end with 2",
   111             Math.class, "rint__DD", 
   112             2.0, cnts
   113         );
   114     }
   115     @Test public void rintNegativeOneHalf() throws Exception {
   116         final double cnts = -1.5;
   117         assertExec(
   118             "Should round up to end with 2",
   119             Math.class, "rint__DD", 
   120             -2.0, cnts
   121         );
   122     }
   123     @Test public void rintTwoAndHalf() throws Exception {
   124         final double cnts = 2.5;
   125         assertExec(
   126             "Should round up to end with 2",
   127             Math.class, "rint__DD", 
   128             2.0, cnts
   129         );
   130     }
   131     @Test public void rintNegativeTwoOneHalf() throws Exception {
   132         final double cnts = -2.5;
   133         assertExec(
   134             "Should round up to end with 2",
   135             Math.class, "rint__DD", 
   136             -2.0, cnts
   137         );
   138     }
   139 
   140     @Test public void ieeeReminder1() throws Exception {
   141         assertExec(
   142             "Same result 1",
   143             Math.class, "IEEEremainder__DDD", 
   144             Math.IEEEremainder(10.0, 4.5), 10.0, 4.5
   145         );
   146     }
   147 
   148     @Test public void ieeeReminder2() throws Exception {
   149         assertExec(
   150             "Same result 1",
   151             Math.class, "IEEEremainder__DDD", 
   152             Math.IEEEremainder(Integer.MAX_VALUE, -4.5), Integer.MAX_VALUE, -4.5
   153         );
   154     }
   155 
   156     @Test public void divAndRound() throws Exception {
   157         assertExec(
   158             "Should be rounded to one",
   159             StaticMethod.class, "div__IBD", 
   160             Double.valueOf(1),
   161             3, 3.75
   162         );
   163     }
   164     @Test public void mixedMethodFourParams() throws Exception {
   165         assertExec(
   166             "Should be two",
   167             StaticMethod.class, "mix__IIJBD", 
   168             Double.valueOf(20),
   169             2, 10l, 5, 2.0
   170         );
   171     }
   172     @Test public void factRec() throws Exception {
   173         assertExec(
   174             "Factorial of 5 is 120",
   175             StaticMethod.class, "factRec__JI", 
   176             Double.valueOf(120),
   177             5
   178         );
   179     }
   180     @Test public void factIter() throws Exception {
   181         assertExec(
   182             "Factorial of 5 is 120",
   183             StaticMethod.class, "factIter__JI", 
   184             Double.valueOf(120),
   185             5
   186         );
   187     }
   188     
   189     @Test public void xor() throws Exception {
   190         assertExec(
   191             "Xor is 4",
   192             StaticMethod.class, "xor__JIJ",
   193             Double.valueOf(4),
   194             7,
   195             3
   196         );
   197     }
   198     
   199     @Test public void or() throws Exception {
   200         assertExec(
   201             "Or will be 7",
   202             StaticMethod.class, "orOrAnd__JZII",
   203             Double.valueOf(7),
   204             true,
   205             4,
   206             3
   207         );
   208     }
   209     @Test public void nullCheck() throws Exception {
   210         assertExec(
   211             "Returns nothing",
   212             StaticMethod.class, "none__Ljava_lang_Object_2II",
   213             null, 1, 3
   214         );
   215     }
   216     @Test public void and() throws Exception {
   217         assertExec(
   218             "And will be 3",
   219             StaticMethod.class, "orOrAnd__JZII",
   220             Double.valueOf(3),
   221             false,
   222             7,
   223             3
   224         );
   225     }
   226     @Test public void inc4() throws Exception {
   227         assertExec(
   228             "It will be 4",
   229             StaticMethod.class, "inc4__I",
   230             Double.valueOf(4)
   231         );
   232     }
   233     
   234     @Test public void shiftLeftInJava() throws Exception {
   235         int res = StaticMethod.shiftLeft(1, 8);
   236         assertEquals(res, 256);
   237     }
   238 
   239     @Test public void shiftLeftInJS() throws Exception {
   240         assertExec(
   241             "Setting 9th bit",
   242             StaticMethod.class, "shiftLeft__III",
   243             Double.valueOf(256),
   244             1, 8
   245         );
   246     }
   247 
   248     @Test public void shiftRightInJava() throws Exception {
   249         int res = StaticMethod.shiftArithmRight(-8, 3, true);
   250         assertEquals(res, -1);
   251     }
   252 
   253     @Test public void shiftRightInJS() throws Exception {
   254         assertExec(
   255             "Get -1",
   256             StaticMethod.class, "shiftArithmRight__IIIZ",
   257             Double.valueOf(-1),
   258             -8, 3, true
   259         );
   260     }
   261     @Test public void unsignedShiftRightInJava() throws Exception {
   262         int res = StaticMethod.shiftArithmRight(8, 3, false);
   263         assertEquals(res, 1);
   264     }
   265 
   266     @Test public void unsignedShiftRightInJS() throws Exception {
   267         assertExec(
   268             "Get -1",
   269             StaticMethod.class, "shiftArithmRight__IIIZ",
   270             Double.valueOf(1),
   271             8, 3, false
   272         );
   273     }
   274     
   275     @Test public void javaScriptBody() throws Exception {
   276         assertExec(
   277             "JavaScript string",
   278             StaticMethod.class, "i2s__Ljava_lang_String_2II",
   279             "333",
   280             330, 3
   281         );
   282     }
   283     
   284     @Test public void switchJarda() throws Exception {
   285         assertExec(
   286             "The expected value",
   287             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   288             "Jarda",
   289             0
   290         );
   291     }
   292     
   293     @Test public void switchDarda() throws Exception {
   294         assertExec(
   295             "The expected value",
   296             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   297             "Darda",
   298             1
   299         );
   300     }
   301     @Test public void switchParda() throws Exception {
   302         assertExec(
   303             "The expected value",
   304             StaticMethod.class, "swtch2__Ljava_lang_String_2I",
   305             "Parda",
   306             22
   307         );
   308     }
   309     @Test public void switchMarda() throws Exception {
   310         assertExec(
   311             "The expected value",
   312             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   313             "Marda",
   314             -433
   315         );
   316     }
   317     
   318     @Test public void checkNullCast() throws Exception {
   319         assertExec("Null can be cast to any type",
   320             StaticMethod.class, "castNull__Ljava_lang_String_2Z", 
   321             null, true
   322         );
   323     }
   324     
   325     private static TestVM code;
   326     
   327     @BeforeClass 
   328     public static void compileTheCode() throws Exception {
   329         StringBuilder sb = new StringBuilder();
   330         code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   331     }
   332     @AfterClass
   333     public static void releaseTheCode() {
   334         code = null;
   335     }
   336 
   337     private void assertExec(
   338         String msg, Class<?> clazz, String method, 
   339         Object ret, Object... args
   340     ) throws Exception {
   341         code.assertExec(msg, clazz, method, ret, args);
   342     }
   343 }