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