rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java
author Martin Soch <Martin.Soch@oracle.com>
Wed, 09 Oct 2013 14:50:29 +0200
brancharithmetic
changeset 1352 7bc78045adfd
parent 1034 28dc692f3b11
child 1663 03f1dd008e83
child 1665 868ad94dc3ec
permissions -rw-r--r--
Fix for bug 5408, error in shift operations on Long.
     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 long conversion() {
   119         return Long.MAX_VALUE;
   120     }
   121 
   122     @Compare public long negate1() {
   123         return neg(0x00fa37d7763e0ca1l);
   124     }
   125 
   126     @Compare public long negate2() {
   127         return neg(0x80fa37d7763e0ca1l);
   128     }
   129 
   130     @Compare public long negate3() {
   131         return neg(0xfffffffffffffeddl);
   132     }
   133 
   134     @Compare public long addOverflow() {
   135         return add(Long.MAX_VALUE, 1l);
   136     }
   137 
   138     @Compare public long subUnderflow() {
   139         return sub(Long.MIN_VALUE, 1l);
   140     }
   141 
   142     @Compare public long addMaxLongAndMaxLong() {
   143         return add(Long.MAX_VALUE, Long.MAX_VALUE);
   144     }
   145 
   146     @Compare public long subMinLongAndMinLong() {
   147         return sub(Long.MIN_VALUE, Long.MIN_VALUE);
   148     }
   149 
   150     @Compare public long subMinLongAndMaxLong() {
   151         return sub(Long.MIN_VALUE, Long.MAX_VALUE);
   152     }
   153 
   154     @Compare public long multiplyMaxLong() {
   155         return mul(Long.MAX_VALUE, 2l);
   156     }
   157 
   158     @Compare public long multiplyMaxLongAndMaxLong() {
   159         return mul(Long.MAX_VALUE, Long.MAX_VALUE);
   160     }
   161 
   162     @Compare public long multiplyMinLong() {
   163         return mul(Long.MIN_VALUE, 2l);
   164     }
   165 
   166     @Compare public long multiplyMinLongAndMinLong() {
   167         return mul(Long.MIN_VALUE, Long.MIN_VALUE);
   168     }
   169 
   170     @Compare public long multiplyPrecision() {
   171         return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   172     }
   173 
   174     @Compare public long divideSmallPositiveNumbers() {
   175         return div(0xabcdef, 0x123);
   176     }
   177 
   178     @Compare public long divideSmallNegativeNumbers() {
   179         return div(-0xabcdef, -0x123);
   180     }
   181 
   182     @Compare public long divideSmallMixedNumbers() {
   183         return div(0xabcdef, -0x123);
   184     }
   185 
   186     @Compare public long dividePositiveNumbersOneDigitDenom() {
   187         return div(0xabcdef0102ffffl, 0x654);
   188     }
   189 
   190     @Compare public long divideNegativeNumbersOneDigitDenom() {
   191         return div(-0xabcdef0102ffffl, -0x654);
   192     }
   193 
   194     @Compare public long divideMixedNumbersOneDigitDenom() {
   195         return div(-0xabcdef0102ffffl, 0x654);
   196     }
   197 
   198     @Compare public long dividePositiveNumbersMultiDigitDenom() {
   199         return div(0x7ffefc003322aabbl, 0x89ab1000l);
   200     }
   201 
   202     @Compare public long divideNegativeNumbersMultiDigitDenom() {
   203         return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
   204     }
   205 
   206     @Compare public long divideMixedNumbersMultiDigitDenom() {
   207         return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
   208     }
   209 
   210     @Compare public long divideWithOverflow() {
   211         return div(0x8000fffe0000l, 0x8000ffffl);
   212     }
   213 
   214     @Compare public long divideWithCorrection() {
   215         return div(0x7fff800000000000l, 0x800000000001l);
   216     }
   217 
   218     @Compare public long moduloSmallPositiveNumbers() {
   219         return mod(0xabcdef, 0x123);
   220     }
   221 
   222     @Compare public long moduloSmallNegativeNumbers() {
   223         return mod(-0xabcdef, -0x123);
   224     }
   225 
   226     @Compare public long moduloSmallMixedNumbers() {
   227         return mod(0xabcdef, -0x123);
   228     }
   229 
   230     @Compare public long moduloPositiveNumbersOneDigitDenom() {
   231         return mod(0xabcdef0102ffffl, 0x654);
   232     }
   233 
   234     @Compare public long moduloNegativeNumbersOneDigitDenom() {
   235         return mod(-0xabcdef0102ffffl, -0x654);
   236     }
   237 
   238     @Compare public long moduloMixedNumbersOneDigitDenom() {
   239         return mod(-0xabcdef0102ffffl, 0x654);
   240     }
   241 
   242     @Compare public long moduloPositiveNumbersMultiDigitDenom() {
   243         return mod(0x7ffefc003322aabbl, 0x89ab1000l);
   244     }
   245 
   246     @Compare public long moduloNegativeNumbersMultiDigitDenom() {
   247         return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
   248     }
   249 
   250     @Compare public long moduloMixedNumbersMultiDigitDenom() {
   251         return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
   252     }
   253 
   254     @Compare public long moduloWithOverflow() {
   255         return mod(0x8000fffe0000l, 0x8000ffffl);
   256     }
   257 
   258     @Compare public long moduloWithCorrection() {
   259         return mod(0x7fff800000000000l, 0x800000000001l);
   260     }
   261 
   262     @Compare public long conversionFromFloatPositive() {
   263         return (long) fadd(2, 0.6f);
   264     }
   265 
   266     @Compare public long conversionFromFloatNegative() {
   267         return (long) fadd(-2, -0.6f);
   268     }
   269 
   270     @Compare public long conversionFromDoublePositive() {
   271         return (long) dadd(0x20ffff0000L, 0.6);
   272     }
   273 
   274     @Compare public long conversionFromDoubleNegative() {
   275         return (long) dadd(-0x20ffff0000L, -0.6);
   276     }
   277 
   278     @Compare public boolean divByZeroThrowsArithmeticException() {
   279         try {
   280             div(1, 0);
   281             return false;
   282         } catch (final ArithmeticException e) {
   283             return true;
   284         }
   285     }
   286 
   287     @Compare public boolean modByZeroThrowsArithmeticException() {
   288         try {
   289             mod(1, 0);
   290             return false;
   291         } catch (final ArithmeticException e) {
   292             return true;
   293         }
   294     }
   295 
   296     @Compare public long shiftL1() {
   297         return shl(0x00fa37d7763e0ca1l, 5);
   298     }
   299 
   300     @Compare public long shiftL2() {
   301         return shl(0x00fa37d7763e0ca1l, 32);
   302     }
   303 
   304     @Compare public long shiftL3() {
   305         return shl(0x00fa37d7763e0ca1l, 45);
   306     }
   307     
   308     @Compare public long shiftL4() {
   309         return shl(0x00fa37d7763e0ca1l, 0);
   310     }
   311     
   312     @Compare public long shiftL5() {
   313         return shl(0x00fa37d7763e0ca1l, 70);
   314     }
   315 
   316     @Compare public long shiftR1() {
   317         return shr(0x00fa37d7763e0ca1l, 5);
   318     }
   319 
   320     @Compare public long shiftR2() {
   321         return shr(0x00fa37d7763e0ca1l, 32);
   322     }
   323 
   324     @Compare public long shiftR3() {
   325         return shr(0x00fa37d7763e0ca1l, 45);
   326     }
   327     
   328     @Compare public long shiftR4() {
   329         return shr(0x00fa37d7763e0ca1l, 0);
   330     }
   331     
   332     @Compare public long shiftR5() {
   333         return shr(0x00fa37d7763e0ca1l, 70);
   334     }
   335 
   336     @Compare public long uShiftR1() {
   337         return ushr(0x00fa37d7763e0ca1l, 5);
   338     }
   339 
   340     @Compare public long uShiftR2() {
   341         return ushr(0x00fa37d7763e0ca1l, 45);
   342     }
   343     
   344     @Compare public long uShiftR3() {
   345         return ushr(0x00fa37d7763e0ca1l, 0);
   346     }
   347     
   348     @Compare public long uShiftR4() {
   349         return ushr(0x00fa37d7763e0ca1l, 70);
   350     }
   351 
   352     @Compare public long uShiftR5() {
   353         return ushr(0xf0fa37d7763e0ca1l, 5);
   354     }
   355 
   356     @Compare public long uShiftR6() {
   357         return ushr(0xf0fa37d7763e0ca1l, 45);
   358     }
   359     
   360     @Compare public long uShiftR7() {
   361         return ushr(0xf0fa37d7763e0ca1l, 0);
   362     }
   363     
   364     @Compare public long uShiftR8() {
   365         return ushr(0xf0fa37d7763e0ca1l, 70);
   366     }
   367 
   368     @Compare public long and1() {
   369         return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   370     }
   371 
   372     @Compare public long or1() {
   373         return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   374     }
   375 
   376     @Compare public long xor1() {
   377         return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   378     }
   379 
   380     @Compare public long xor2() {
   381         return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
   382     }
   383 
   384     @Compare public long xor3() {
   385         return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
   386     }
   387 
   388     @Compare public int compareSameNumbers() {
   389         return compare(0x0000000000000000l, 0x0000000000000000l, 0);
   390     }
   391 
   392     @Compare public int comparePositiveNumbers() {
   393         return compare(0x0000000000200000l, 0x0000000010000000l, 0);
   394     }
   395 
   396     @Compare public int compareNegativeNumbers() {
   397         return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
   398     }
   399 
   400     @Compare public int compareMixedNumbers() {
   401         return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
   402     }
   403     
   404     @Factory
   405     public static Object[] create() {
   406         return VMTest.create(LongArithmeticTest.class);
   407     }
   408 }