rt/vm/src/test/java/org/apidesign/vm4brwsr/StaticMethodTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 17 Feb 2014 17:49:30 +0100
branchReducedStack
changeset 1473 484248c9c1d6
parent 1468 5d6b648a39db
child 1773 9830c8b761ce
permissions -rw-r--r--
Verify behavior when string constants are used
     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 collectionToString() throws Exception {
   228         String exp = StaticMethod.toStringArr();
   229         assertExec(
   230             "0 to 4",
   231             StaticMethod.class, "toStringArr__Ljava_lang_String_2",
   232             exp
   233         );
   234     }
   235     
   236     @Test public void or() throws Exception {
   237         assertExec(
   238             "Or will be 7",
   239             StaticMethod.class, "orOrAnd__JZII",
   240             Double.valueOf(7),
   241             true,
   242             4,
   243             3
   244         );
   245     }
   246     @Test public void nullCheck() throws Exception {
   247         assertExec(
   248             "Returns nothing",
   249             StaticMethod.class, "none__Ljava_lang_Object_2II",
   250             null, 1, 3
   251         );
   252     }
   253     @Test public void and() throws Exception {
   254         assertExec(
   255             "And will be 3",
   256             StaticMethod.class, "orOrAnd__JZII",
   257             Double.valueOf(3),
   258             false,
   259             7,
   260             3
   261         );
   262     }
   263     @Test public void inc4() throws Exception {
   264         assertExec(
   265             "It will be 4",
   266             StaticMethod.class, "inc4__I",
   267             Double.valueOf(4)
   268         );
   269     }
   270     
   271     @Test public void shiftLeftInJava() throws Exception {
   272         int res = StaticMethod.shiftLeft(1, 8);
   273         assertEquals(res, 256);
   274     }
   275 
   276     @Test public void shiftLeftInJS() throws Exception {
   277         assertExec(
   278             "Setting 9th bit",
   279             StaticMethod.class, "shiftLeft__III",
   280             Double.valueOf(256),
   281             1, 8
   282         );
   283     }
   284 
   285     @Test public void shiftRightInJava() throws Exception {
   286         int res = StaticMethod.shiftArithmRight(-8, 3, true);
   287         assertEquals(res, -1);
   288     }
   289 
   290     @Test public void shiftRightInJS() throws Exception {
   291         assertExec(
   292             "Get -1",
   293             StaticMethod.class, "shiftArithmRight__IIIZ",
   294             Double.valueOf(-1),
   295             -8, 3, true
   296         );
   297     }
   298     @Test public void unsignedShiftRightInJava() throws Exception {
   299         int res = StaticMethod.shiftArithmRight(8, 3, false);
   300         assertEquals(res, 1);
   301     }
   302 
   303     @Test public void unsignedShiftRightInJS() throws Exception {
   304         assertExec(
   305             "Get -1",
   306             StaticMethod.class, "shiftArithmRight__IIIZ",
   307             Double.valueOf(1),
   308             8, 3, false
   309         );
   310     }
   311     
   312     @Test public void javaScriptBody() throws Exception {
   313         assertExec(
   314             "JavaScript string",
   315             StaticMethod.class, "i2s__Ljava_lang_String_2II",
   316             "333",
   317             330, 3
   318         );
   319     }
   320     
   321     @Test public void switchJarda() throws Exception {
   322         assertExec(
   323             "The expected value",
   324             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   325             "Jarda",
   326             0
   327         );
   328     }
   329     
   330     @Test public void switchDarda() throws Exception {
   331         assertExec(
   332             "The expected value",
   333             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   334             "Darda",
   335             1
   336         );
   337     }
   338     @Test public void switchParda() throws Exception {
   339         assertExec(
   340             "The expected value",
   341             StaticMethod.class, "swtch2__Ljava_lang_String_2I",
   342             "Parda",
   343             22
   344         );
   345     }
   346     @Test public void switchMarda() throws Exception {
   347         assertExec(
   348             "The expected value",
   349             StaticMethod.class, "swtch__Ljava_lang_String_2I",
   350             "Marda",
   351             -433
   352         );
   353     }
   354     
   355     @Test public void checkNullCast() throws Exception {
   356         assertExec("Null can be cast to any type",
   357             StaticMethod.class, "castNull__Ljava_lang_String_2Z", 
   358             null, true
   359         );
   360     }
   361     
   362     @Test public void stringConstantIsCopied() throws Exception {
   363         assertExec("String constants are copied between class pools",
   364             StaticMethod.class, "helloWorldLength__ILjava_lang_String_2", 
   365             17, "Jardo"
   366         );
   367     }
   368     
   369     private static TestVM code;
   370     
   371     @BeforeClass 
   372     public static void compileTheCode() throws Exception {
   373         StringBuilder sb = new StringBuilder();
   374         code = TestVM.compileClass(sb, "org/apidesign/vm4brwsr/StaticMethod");
   375     }
   376     @AfterClass
   377     public static void releaseTheCode() {
   378         code = null;
   379     }
   380 
   381     private void assertExec(
   382         String msg, Class<?> clazz, String method, 
   383         Object ret, Object... args
   384     ) throws Exception {
   385         code.assertExec(msg, clazz, method, ret, args);
   386     }
   387 }