vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
author Martin Soch <Martin.Soch@oracle.com>
Fri, 01 Feb 2013 09:00:30 +0100
brancharithmetic
changeset 630 04e312a7887e
parent 629 2d537f8cd604
child 657 b42bfe334128
permissions -rw-r--r--
Support for Long unary "-"
     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 javax.script.Invocable;
    21 import javax.script.ScriptException;
    22 import static org.testng.Assert.*;
    23 import org.testng.annotations.BeforeClass;
    24 import org.testng.annotations.Test;
    25 
    26 /**
    27  *
    28  * @author Jaroslav Tulach <jtulach@netbeans.org>
    29  */
    30 public class NumberTest {
    31     @Test public void integerFromString() throws Exception {
    32         assertExec("Can convert string to integer", Integer.class, "parseInt__ILjava_lang_String_2",
    33             Double.valueOf(333), "333"
    34         );
    35     }
    36 
    37     @Test public void doubleFromString() throws Exception {
    38         assertExec("Can convert string to double", Double.class, "parseDouble__DLjava_lang_String_2",
    39             Double.valueOf(33.3), "33.3"
    40         );
    41     }
    42 
    43     @Test public void autoboxDouble() throws Exception {
    44         assertExec("Autoboxing of doubles is OK", Numbers.class, "autoboxDblToString__Ljava_lang_String_2",
    45             "3.3"
    46         );
    47     }
    48     
    49     @Test public void javalog1000() throws Exception {
    50         assertEquals(3.0, Math.log10(1000.0), 0.00003, "log_10(1000) == 3");
    51     }
    52 
    53     @Test public void jslog1000() throws Exception {
    54         assertExec("log_10(1000) == 3", Math.class, "log10__DD", 
    55             Double.valueOf(3.0), 1000.0
    56         );
    57     }
    58     
    59     @Test public void javaRem() {
    60         assertEquals(3, Numbers.rem(303, 10));
    61     }
    62     @Test public void jsRem() throws Exception {
    63         assertExec("Should be three", Numbers.class, "rem__III", 
    64             Double.valueOf(3.0), 303, 10
    65         );
    66     }
    67     
    68     @Test public void deserializeInt() throws Exception {
    69         int exp = Numbers.deserInt();
    70         assertExec("Should be the same", Numbers.class, "deserInt__I", 
    71             Double.valueOf(exp)
    72         );
    73     }
    74 
    75     @Test public void deserializeSimpleLong() throws Exception {
    76         assertExec("Should be 3454", Numbers.class, "deserLong__J_3B", 
    77             Double.valueOf(3454), 
    78             new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)13, (byte)126 }
    79         );
    80     }
    81     /* XXX: JavaScript cannot represent as big longs as Java. 
    82     @Test public void deserializeLargeLong() throws Exception {
    83         final byte[] arr = new byte[] {
    84             (byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0
    85         };
    86         long exp = Numbers.deserLong(arr);
    87         assertExec("Should be " + exp, "org_apidesign_vm4brwsr_Numbers_deserLong__JAB", 
    88             Double.valueOf(exp), arr);
    89     }
    90     */
    91     
    92     @Test public void deserializeFloatInJava() throws Exception {
    93         float f = 54324.32423f;
    94         float r = Numbers.deserFloat();
    95         assertEquals(r, f, "Floats are the same");
    96     }
    97     
    98     @Test public void deserializeFloatInJS() throws Exception {
    99         float f = 54324.32423f;
   100         assertExec("Should be the same", Numbers.class, "deserFloat__F", 
   101             Double.valueOf(f)
   102         );
   103     }
   104 
   105     @Test public void deserializeDoubleInJava() throws Exception {
   106         double f = 3.0;
   107         double r = Numbers.deserDouble();
   108         assertEquals(r, f, 0.001, "Doubles are the same");
   109     }
   110     
   111     @Test public void deserializeDoubleInJS() throws Exception {
   112         double f = 3.0;
   113         assertExec("Should be the same", Numbers.class, "deserDouble__D", f);
   114     }
   115     /*
   116     @Test public void serDouble() throws IOException {
   117         double f = 3.0;
   118         ByteArrayOutputStream os = new ByteArrayOutputStream();
   119         DataOutputStream d = new DataOutputStream(os);
   120         d.writeLong(3454);
   121         d.close();
   122         
   123         StringBuilder sb = new StringBuilder();
   124         byte[] arr = os.toByteArray();
   125         for (int i = 0; i < arr.length; i++) {
   126             sb.append("(byte)").append(arr[i]).append(", ");
   127         }
   128         fail("" + sb);
   129     }
   130 */    
   131     @Test public void fiveInStringJS() throws Exception {
   132         String s = Numbers.intToString();
   133         assertExec("Should be the same: " + s, 
   134             Numbers.class, "intToString__Ljava_lang_String_2", 
   135             s
   136         );
   137     }
   138 
   139     @Test public void sevenInStringJS() throws Exception {
   140         String s = Numbers.floatToString();
   141         assertExec("Should be the same: " + s, 
   142             Numbers.class, "floatToString__Ljava_lang_String_2", 
   143             s
   144         );
   145     }
   146     
   147     @Test public void longConversion() throws Exception {
   148         assertExec("Long from cPool",
   149             Numbers.class, "conversionL__J", 
   150             Double.valueOf(Long.MAX_VALUE)
   151         );
   152     }
   153     
   154     @Test public void longNegate1() throws Exception {
   155         final long res = -0x00fa37d7763e0ca1l;
   156         assertExec("Long negate",
   157             Numbers.class, "negL__J_3B", 
   158             Double.valueOf(res),
   159                 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 }
   160         );
   161     }
   162     
   163     @Test public void longNegate2() throws Exception {
   164         final long res = -0x80fa37d7763e0ca1l;
   165         assertExec("Long negate",
   166             Numbers.class, "negL__J_3B", 
   167             Double.valueOf(res),
   168                 new byte[] { (byte)0x80, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 }
   169         );
   170     }
   171     
   172     @Test public void longAddOverflow() throws Exception {
   173         final long res = Long.MAX_VALUE + 1l;
   174         assertExec("Addition 1+MAX",
   175             Numbers.class, "addL__J_3B_3B", 
   176             Double.valueOf(res),
   177                 new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff },
   178                 new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)1 }
   179         );
   180     }
   181     
   182     @Test public void longAddMaxAndMax() throws Exception {
   183         final long res = Long.MAX_VALUE + Long.MAX_VALUE;
   184         assertExec("Addition MAX+MAX",
   185             Numbers.class, "addL__J_3B_3B", 
   186             Double.valueOf(res),
   187             new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff },
   188             new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }
   189         );
   190     }
   191     
   192     @Test public void longSubUnderflow() throws Exception {
   193         final long res = Long.MIN_VALUE - 1l;
   194         assertExec("Subtraction MIN-1",
   195             Numbers.class, "subL__J_3B_3B", 
   196             Double.valueOf(res),
   197                 new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
   198                 new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)1 }
   199         );
   200     }
   201     
   202     @Test public void longSubMinAndMin() throws Exception {
   203         final long res = Long.MIN_VALUE - Long.MIN_VALUE;
   204         assertExec("Subtraction MIN-MIN",
   205             Numbers.class, "subL__J_3B_3B", 
   206             Double.valueOf(res),
   207             new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
   208             new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }
   209         );
   210     }
   211     
   212     @Test public void longSubMinAndMax() throws Exception {
   213         final long res = Long.MIN_VALUE - Long.MAX_VALUE;
   214         assertExec("Subtraction MIN-MAX",
   215             Numbers.class, "subL__J_3B_3B", 
   216             Double.valueOf(res),
   217             new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 },
   218             new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }
   219         );
   220     }
   221     
   222     @Test public void longShiftL1() throws Exception {
   223         final long res = 0x00fa37d7763e0ca1l << 5;
   224         assertExec("Long << 5",
   225             Numbers.class, "shlL__J_3BI", 
   226             Double.valueOf(res),
   227                 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   228                 5);
   229     }
   230     
   231     @Test public void longShiftL2() throws Exception {
   232         final long res = 0x00fa37d7763e0ca1l << 32;
   233         assertExec("Long << 32",
   234             Numbers.class, "shlL__J_3BI", 
   235             Double.valueOf(res),
   236                 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   237                 32);
   238     }
   239     
   240     @Test public void longShiftL3() throws Exception {
   241         final long res = 0x00fa37d7763e0ca1l << 45;
   242         assertExec("Long << 45",
   243             Numbers.class, "shlL__J_3BI", 
   244             Double.valueOf(res),
   245                 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   246                 45);
   247     }
   248     
   249     @Test public void longShiftR1() throws Exception {
   250         final long res = 0x00fa37d7763e0ca1l >> 5;
   251         assertExec("Long >> 5",
   252             Numbers.class, "shrL__J_3BI", 
   253             Double.valueOf(res),
   254                 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   255                 5);
   256     }
   257     
   258     @Test public void longShiftR2() throws Exception {
   259         final long res = 0x00fa37d7763e0ca1l >> 32;
   260         assertExec("Long >> 32",
   261             Numbers.class, "shrL__J_3BI", 
   262             Double.valueOf(res),
   263                 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   264                 32);
   265     }
   266     
   267     @Test public void longShiftR3() throws Exception {
   268         final long res = 0x00fa37d7763e0ca1l >> 45;
   269         assertExec("Long >> 45",
   270             Numbers.class, "shrL__J_3BI", 
   271             Double.valueOf(res),
   272                 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   273                 45);
   274     }
   275     
   276     @Test public void longUShiftR1() throws Exception {
   277         final long res = 0x00fa37d7763e0ca1l >>> 5;
   278         assertExec("Long >>> 5",
   279             Numbers.class, "ushrL__J_3BI", 
   280             Double.valueOf(res),
   281                 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   282                 5);
   283     }
   284     
   285     @Test public void longUShiftR2() throws Exception {
   286         final long res = 0x00fa37d7763e0ca1l >>> 45;
   287         assertExec("Long >>> 45",
   288             Numbers.class, "ushrL__J_3BI", 
   289             Double.valueOf(res),
   290                 new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   291                 45);
   292     }
   293     
   294     @Test public void longUShiftR3() throws Exception {
   295         final long res = 0xf0fa37d7763e0ca1l >>> 5;
   296         assertExec("Long >>> 5",
   297             Numbers.class, "ushrL__J_3BI", 
   298             Double.valueOf(res),
   299                 new byte[] { (byte)0xf0, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   300                 5);
   301     }
   302     
   303     @Test public void longUShiftR4() throws Exception {
   304         final long res = 0xf0fa37d7763e0ca1l >>> 45;
   305         assertExec("Long >>> 45",
   306             Numbers.class, "ushrL__J_3BI", 
   307             Double.valueOf(res),
   308                 new byte[] { (byte)0xf0, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   309                 45);
   310     }
   311     
   312     @Test public void longAnd() throws Exception {
   313         final long res = 0x00fa37d7763e0ca1l & 0xa7b3432fff00123el;
   314         assertExec("LOng binary AND",
   315             Numbers.class, "andL__J_3B_3B", 
   316             Double.valueOf(res),
   317             new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   318             new byte[] { (byte)0xa7, (byte)0xb3, (byte)0x43, (byte)0x2f, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e }
   319         );
   320     }
   321     
   322     @Test public void longOr() throws Exception {
   323         final long res = 0x00fa37d7763e0ca1l | 0xa7b3432fff00123el;
   324         assertExec("Long binary OR",
   325             Numbers.class, "orL__J_3B_3B", 
   326             Double.valueOf(res),
   327             new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   328             new byte[] { (byte)0xa7, (byte)0xb3, (byte)0x43, (byte)0x2f, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e }
   329         );
   330     }
   331     
   332     @Test public void longXor1() throws Exception {
   333         final long res = 0x00fa37d7763e0ca1l ^ 0xa7b3432fff00123el;
   334         assertExec("Long binary XOR",
   335             Numbers.class, "xorL__J_3B_3B", 
   336             Double.valueOf(res),
   337             new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   338             new byte[] { (byte)0xa7, (byte)0xb3, (byte)0x43, (byte)0x2f, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e }
   339         );
   340     }
   341     
   342     @Test public void longXor2() throws Exception {
   343         final long res = 0x00fa37d7763e0ca1l ^ 0x00000000ff00123el;
   344         assertExec("Long binary XOR",
   345             Numbers.class, "xorL__J_3B_3B", 
   346             Double.valueOf(res),
   347             new byte[] { (byte)0x00, (byte)0xfa, (byte)0x37, (byte)0xd7, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   348             new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e }
   349         );
   350     }
   351     
   352     @Test public void longXor3() throws Exception {
   353         final long res = 0x00000000763e0ca1l ^ 0x00000000ff00123el;
   354         assertExec("Long binary XOR",
   355             Numbers.class, "xorL__J_3B_3B", 
   356             Double.valueOf(res),
   357             new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x76, (byte)0x3e, (byte)0x0c, (byte)0xa1 },
   358             new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e }
   359         );
   360     }
   361     
   362     private static CharSequence codeSeq;
   363     private static Invocable code;
   364 
   365     @BeforeClass
   366     public void compileTheCode() throws Exception {
   367         if (codeSeq == null) {
   368             StringBuilder sb = new StringBuilder();
   369             code = StaticMethodTest.compileClass(sb, "org/apidesign/vm4brwsr/Numbers");
   370             codeSeq = sb;
   371         }
   372     }
   373 
   374     private static void assertExec(
   375         String msg, Class<?> clazz, String method, Object expRes, Object... args) throws Exception
   376     {
   377         Object ret = TestUtils.execCode(code, codeSeq, msg, clazz, method, expRes, args);
   378         if (ret == null) {
   379             return;
   380         }
   381         if (expRes instanceof Double && ret instanceof Double) {
   382             double expD = ((Double)expRes).doubleValue();
   383             double retD = ((Double)ret).doubleValue();
   384             assertEquals(retD, expD, 0.000004, msg + " "
   385                     + StaticMethodTest.dumpJS(codeSeq));
   386             return;
   387         }
   388         assertEquals(ret, expRes, msg + " " + StaticMethodTest.dumpJS(codeSeq));
   389     }
   390     
   391 }