rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 19 Nov 2014 19:32:00 +0100
changeset 1723 3a1f262311cf
parent 1666 rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java@4e349118658f
child 1761 d2a5a7a0e167
permissions -rw-r--r--
Separating compact profile tests into own project, so launcher.http can depend on compact profile JAR
     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.bck2brwsr.tck;
    19 
    20 import org.apidesign.bck2brwsr.vmtest.Compare;
    21 import org.apidesign.bck2brwsr.vmtest.VMTest;
    22 import org.testng.annotations.Factory;
    23 
    24 /**
    25  *
    26  * @author Jaroslav Tulach <jtulach@netbeans.org>
    27  */
    28 public class LongArithmeticTest {
    29 
    30     private static long add(long x, long y) {
    31         return (x + y);
    32     }
    33 
    34     private static long sub(long x, long y) {
    35         return (x - y);
    36     }
    37 
    38     private static long mul(long x, long y) {
    39         return (x * y);
    40     }
    41 
    42     private static long div(long x, long y) {
    43         return (x / y);
    44     }
    45 
    46     private static long mod(long x, long y) {
    47         return (x % y);
    48     }
    49 
    50     private static long neg(long x) {
    51         return (-x);
    52     }
    53 
    54     private static long shl(long x, int b) {
    55         return (x << b);
    56     }
    57 
    58     private static long shr(long x, int b) {
    59         return (x >> b);
    60     }
    61 
    62     private static long ushr(long x, int b) {
    63         return (x >>> b);
    64     }
    65 
    66     private static long and(long x, long y) {
    67         return (x & y);
    68     }
    69 
    70     private static long or(long x, long y) {
    71         return (x | y);
    72     }
    73 
    74     private static long xor(long x, long y) {
    75         return (x ^ y);
    76     }
    77 
    78     private static float fadd(float x, float y) {
    79         return x + y;
    80     }
    81 
    82     private static double dadd(double x, double y) {
    83         return x + y;
    84     }
    85 
    86     public static int compare(long x, long y, int zero) {
    87         final int xyResult = compareL(x, y, zero);
    88         final int yxResult = compareL(y, x, zero);
    89 
    90         return ((xyResult + yxResult) == 0) ? xyResult : -2;
    91     }
    92 
    93     private static int compareL(long x, long y, int zero) {
    94         int result = -2;
    95         int trueCount = 0;
    96 
    97         x += zero;
    98         if (x == y) {
    99             result = 0;
   100             ++trueCount;
   101         }
   102 
   103         x += zero;
   104         if (x < y) {
   105             result = -1;
   106             ++trueCount;
   107         }
   108 
   109         x += zero;
   110         if (x > y) {
   111             result = 1;
   112             ++trueCount;
   113         }
   114 
   115         return (trueCount == 1) ? result : -2;
   116     }
   117     
   118     @Compare public int parameterSlotCount() {
   119         long argCounts = 281479271874563L;
   120         int x = unpack(argCounts, 2);
   121         return x;
   122     }
   123     private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d
   124         assert(word <= 3);
   125         final long val = packed >> ((3-word) * 16);
   126         return (char)val;
   127     }
   128     @Compare public long conversion() {
   129         return Long.MAX_VALUE;
   130     }
   131 
   132     @Compare public long negate1() {
   133         return neg(0x00fa37d7763e0ca1l);
   134     }
   135 
   136     @Compare public long negate2() {
   137         return neg(0x80fa37d7763e0ca1l);
   138     }
   139 
   140     @Compare public long negate3() {
   141         return neg(0xfffffffffffffeddl);
   142     }
   143 
   144     @Compare public long addOverflow() {
   145         return add(Long.MAX_VALUE, 1l);
   146     }
   147 
   148     @Compare public long subUnderflow() {
   149         return sub(Long.MIN_VALUE, 1l);
   150     }
   151 
   152     @Compare public long addMaxLongAndMaxLong() {
   153         return add(Long.MAX_VALUE, Long.MAX_VALUE);
   154     }
   155 
   156     @Compare public long subMinLongAndMinLong() {
   157         return sub(Long.MIN_VALUE, Long.MIN_VALUE);
   158     }
   159 
   160     @Compare public long subMinLongAndMaxLong() {
   161         return sub(Long.MIN_VALUE, Long.MAX_VALUE);
   162     }
   163 
   164     @Compare public long multiplyMaxLong() {
   165         return mul(Long.MAX_VALUE, 2l);
   166     }
   167 
   168     @Compare public long multiplyMaxLongAndMaxLong() {
   169         return mul(Long.MAX_VALUE, Long.MAX_VALUE);
   170     }
   171 
   172     @Compare public long multiplyMinLong() {
   173         return mul(Long.MIN_VALUE, 2l);
   174     }
   175 
   176     @Compare public long multiplyMinLongAndMinLong() {
   177         return mul(Long.MIN_VALUE, Long.MIN_VALUE);
   178     }
   179 
   180     @Compare public long multiplyPrecision() {
   181         return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   182     }
   183 
   184     @Compare public long divideSmallPositiveNumbers() {
   185         return div(0xabcdef, 0x123);
   186     }
   187 
   188     @Compare public long divideSmallNegativeNumbers() {
   189         return div(-0xabcdef, -0x123);
   190     }
   191 
   192     @Compare public long divideSmallMixedNumbers() {
   193         return div(0xabcdef, -0x123);
   194     }
   195 
   196     @Compare public long dividePositiveNumbersOneDigitDenom() {
   197         return div(0xabcdef0102ffffl, 0x654);
   198     }
   199 
   200     @Compare public long divideNegativeNumbersOneDigitDenom() {
   201         return div(-0xabcdef0102ffffl, -0x654);
   202     }
   203 
   204     @Compare public long divideMixedNumbersOneDigitDenom() {
   205         return div(-0xabcdef0102ffffl, 0x654);
   206     }
   207 
   208     @Compare public long dividePositiveNumbersMultiDigitDenom() {
   209         return div(0x7ffefc003322aabbl, 0x89ab1000l);
   210     }
   211 
   212     @Compare public long divideNegativeNumbersMultiDigitDenom() {
   213         return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
   214     }
   215 
   216     @Compare public long divideMixedNumbersMultiDigitDenom() {
   217         return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
   218     }
   219 
   220     @Compare public long divideWithOverflow() {
   221         return div(0x8000fffe0000l, 0x8000ffffl);
   222     }
   223 
   224     @Compare public long divideWithCorrection() {
   225         return div(0x7fff800000000000l, 0x800000000001l);
   226     }
   227 
   228     @Compare public long moduloSmallPositiveNumbers() {
   229         return mod(0xabcdef, 0x123);
   230     }
   231 
   232     @Compare public long moduloSmallNegativeNumbers() {
   233         return mod(-0xabcdef, -0x123);
   234     }
   235 
   236     @Compare public long moduloSmallMixedNumbers() {
   237         return mod(0xabcdef, -0x123);
   238     }
   239 
   240     @Compare public long moduloPositiveNumbersOneDigitDenom() {
   241         return mod(0xabcdef0102ffffl, 0x654);
   242     }
   243 
   244     @Compare public long moduloNegativeNumbersOneDigitDenom() {
   245         return mod(-0xabcdef0102ffffl, -0x654);
   246     }
   247 
   248     @Compare public long moduloMixedNumbersOneDigitDenom() {
   249         return mod(-0xabcdef0102ffffl, 0x654);
   250     }
   251 
   252     @Compare public long moduloPositiveNumbersMultiDigitDenom() {
   253         return mod(0x7ffefc003322aabbl, 0x89ab1000l);
   254     }
   255 
   256     @Compare public long moduloNegativeNumbersMultiDigitDenom() {
   257         return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
   258     }
   259 
   260     @Compare public long moduloMixedNumbersMultiDigitDenom() {
   261         return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
   262     }
   263 
   264     @Compare public long moduloWithOverflow() {
   265         return mod(0x8000fffe0000l, 0x8000ffffl);
   266     }
   267 
   268     @Compare public long moduloWithCorrection() {
   269         return mod(0x7fff800000000000l, 0x800000000001l);
   270     }
   271 
   272     @Compare public long conversionFromFloatPositive() {
   273         return (long) fadd(2, 0.6f);
   274     }
   275 
   276     @Compare public long conversionFromFloatNegative() {
   277         return (long) fadd(-2, -0.6f);
   278     }
   279 
   280     @Compare public long conversionFromDoublePositive() {
   281         return (long) dadd(0x20ffff0000L, 0.6);
   282     }
   283 
   284     @Compare public long conversionFromDoubleNegative() {
   285         return (long) dadd(-0x20ffff0000L, -0.6);
   286     }
   287 
   288     @Compare public boolean divByZeroThrowsArithmeticException() {
   289         try {
   290             div(1, 0);
   291             return false;
   292         } catch (final ArithmeticException e) {
   293             return true;
   294         }
   295     }
   296 
   297     @Compare public boolean modByZeroThrowsArithmeticException() {
   298         try {
   299             mod(1, 0);
   300             return false;
   301         } catch (final ArithmeticException e) {
   302             return true;
   303         }
   304     }
   305 
   306     @Compare public long shiftL1() {
   307         return shl(0x00fa37d7763e0ca1l, 5);
   308     }
   309 
   310     @Compare public long shiftL2() {
   311         return shl(0x00fa37d7763e0ca1l, 32);
   312     }
   313 
   314     @Compare public long shiftL3() {
   315         return shl(0x00fa37d7763e0ca1l, 45);
   316     }
   317     
   318     @Compare public long shiftL4() {
   319         return shl(0x00fa37d7763e0ca1l, 0);
   320     }
   321     
   322     @Compare public long shiftL5() {
   323         return shl(0x00fa37d7763e0ca1l, 70);
   324     }
   325 
   326     @Compare public long shiftR1() {
   327         return shr(0x00fa37d7763e0ca1l, 5);
   328     }
   329 
   330     @Compare public long shiftR2() {
   331         return shr(0x00fa37d7763e0ca1l, 32);
   332     }
   333 
   334     @Compare public long shiftR3() {
   335         return shr(0x00fa37d7763e0ca1l, 45);
   336     }
   337     
   338     @Compare public long shiftR4() {
   339         return shr(0x00fa37d7763e0ca1l, 0);
   340     }
   341     
   342     @Compare public long shiftR5() {
   343         return shr(0x00fa37d7763e0ca1l, 70);
   344     }
   345 
   346     @Compare public long uShiftR1() {
   347         return ushr(0x00fa37d7763e0ca1l, 5);
   348     }
   349 
   350     @Compare public long uShiftR2() {
   351         return ushr(0x00fa37d7763e0ca1l, 45);
   352     }
   353     
   354     @Compare public long uShiftR3() {
   355         return ushr(0x00fa37d7763e0ca1l, 0);
   356     }
   357     
   358     @Compare public long uShiftR4() {
   359         return ushr(0x00fa37d7763e0ca1l, 70);
   360     }
   361 
   362     @Compare public long uShiftR5() {
   363         return ushr(0xf0fa37d7763e0ca1l, 5);
   364     }
   365 
   366     @Compare public long uShiftR6() {
   367         return ushr(0xf0fa37d7763e0ca1l, 45);
   368     }
   369     
   370     @Compare public long uShiftR7() {
   371         return ushr(0xf0fa37d7763e0ca1l, 0);
   372     }
   373     
   374     @Compare public long uShiftR8() {
   375         return ushr(0xf0fa37d7763e0ca1l, 70);
   376     }
   377 
   378     @Compare public long and1() {
   379         return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   380     }
   381 
   382     @Compare public long or1() {
   383         return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   384     }
   385 
   386     @Compare public long xor1() {
   387         return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   388     }
   389 
   390     @Compare public long xor2() {
   391         return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
   392     }
   393 
   394     @Compare public long xor3() {
   395         return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
   396     }
   397 
   398     @Compare public int compareSameNumbers() {
   399         return compare(0x0000000000000000l, 0x0000000000000000l, 0);
   400     }
   401 
   402     @Compare public int comparePositiveNumbers() {
   403         return compare(0x0000000000200000l, 0x0000000010000000l, 0);
   404     }
   405 
   406     @Compare public int compareNegativeNumbers() {
   407         return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
   408     }
   409 
   410     @Compare public int compareMixedNumbers() {
   411         return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
   412     }
   413     
   414     @Factory
   415     public static Object[] create() {
   416         return VMTest.create(LongArithmeticTest.class);
   417     }
   418 }