vmtest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Fri, 15 Feb 2013 16:06:12 +0100
brancharithmetic
changeset 737 b2731af0357d
parent 699 6cad41d877d2
permissions -rw-r--r--
Fixed int & long division by zero (Bug 4668) and int negative division
     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     public static int compare(long x, long y, int zero) {
    79         final int xyResult = compareL(x, y, zero);
    80         final int yxResult = compareL(y, x, zero);
    81 
    82         return ((xyResult + yxResult) == 0) ? xyResult : -2;
    83     }
    84 
    85     private static int compareL(long x, long y, int zero) {
    86         int result = -2;
    87         int trueCount = 0;
    88 
    89         x += zero;
    90         if (x == y) {
    91             result = 0;
    92             ++trueCount;
    93         }
    94 
    95         x += zero;
    96         if (x < y) {
    97             result = -1;
    98             ++trueCount;
    99         }
   100 
   101         x += zero;
   102         if (x > y) {
   103             result = 1;
   104             ++trueCount;
   105         }
   106 
   107         return (trueCount == 1) ? result : -2;
   108     }
   109     
   110     @Compare public long conversion() {
   111         return Long.MAX_VALUE;
   112     }
   113     
   114     @Compare public long negate1() {
   115         return neg(0x00fa37d7763e0ca1l);
   116     }
   117     
   118     @Compare public long negate2() {
   119         return neg(0x80fa37d7763e0ca1l);
   120     }
   121 
   122     @Compare public long negate3() {
   123         return neg(0xfffffffffffffeddl);
   124     }
   125 
   126     @Compare public long addOverflow() {
   127         return add(Long.MAX_VALUE, 1l);
   128     }
   129 
   130     @Compare public long subUnderflow() {
   131         return sub(Long.MIN_VALUE, 1l);
   132     }
   133 
   134     @Compare public long addMaxLongAndMaxLong() {
   135         return add(Long.MAX_VALUE, Long.MAX_VALUE);
   136     }
   137     
   138     @Compare public long subMinLongAndMinLong() {
   139         return sub(Long.MIN_VALUE, Long.MIN_VALUE);
   140     }
   141     
   142     @Compare public long subMinLongAndMaxLong() {
   143         return sub(Long.MIN_VALUE, Long.MAX_VALUE);
   144     }
   145 
   146     @Compare public long multiplyMaxLong() {
   147         return mul(Long.MAX_VALUE, 2l);
   148     }
   149     
   150     @Compare public long multiplyMaxLongAndMaxLong() {
   151         return mul(Long.MAX_VALUE, Long.MAX_VALUE);
   152     }
   153     
   154     @Compare public long multiplyMinLong() {
   155         return mul(Long.MIN_VALUE, 2l);
   156     }
   157     
   158     @Compare public long multiplyMinLongAndMinLong() {
   159         return mul(Long.MIN_VALUE, Long.MIN_VALUE);
   160     }
   161     
   162     @Compare public long multiplyPrecision() {
   163         return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   164     }
   165     
   166     @Compare public long divideSmallPositiveNumbers() {
   167         return div(0xabcdef, 0x123);
   168     }
   169 
   170     @Compare public long divideSmallNegativeNumbers() {
   171         return div(-0xabcdef, -0x123);
   172     }
   173 
   174     @Compare public long divideSmallMixedNumbers() {
   175         return div(0xabcdef, -0x123);
   176     }
   177 
   178     @Compare public long dividePositiveNumbersOneDigitDenom() {
   179         return div(0xabcdef0102ffffl, 0x654);
   180     }
   181 
   182     @Compare public long divideNegativeNumbersOneDigitDenom() {
   183         return div(-0xabcdef0102ffffl, -0x654);
   184     }
   185 
   186     @Compare public long divideMixedNumbersOneDigitDenom() {
   187         return div(-0xabcdef0102ffffl, 0x654);
   188     }
   189 
   190     @Compare public long dividePositiveNumbersMultiDigitDenom() {
   191         return div(0x7ffefc003322aabbl, 0x89ab1000l);
   192     }
   193 
   194     @Compare public long divideNegativeNumbersMultiDigitDenom() {
   195         return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
   196     }
   197 
   198     @Compare public long divideMixedNumbersMultiDigitDenom() {
   199         return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
   200     }
   201 
   202     @Compare public long divideWithOverflow() {
   203         return div(0x8000fffe0000l, 0x8000ffffl);
   204     }
   205 
   206     @Compare public long divideWithCorrection() {
   207         return div(0x7fff800000000000l, 0x800000000001l);
   208     }
   209 
   210     @Compare public long moduloSmallPositiveNumbers() {
   211         return mod(0xabcdef, 0x123);
   212     }
   213 
   214     @Compare public long moduloSmallNegativeNumbers() {
   215         return mod(-0xabcdef, -0x123);
   216     }
   217 
   218     @Compare public long moduloSmallMixedNumbers() {
   219         return mod(0xabcdef, -0x123);
   220     }
   221 
   222     @Compare public long moduloPositiveNumbersOneDigitDenom() {
   223         return mod(0xabcdef0102ffffl, 0x654);
   224     }
   225 
   226     @Compare public long moduloNegativeNumbersOneDigitDenom() {
   227         return mod(-0xabcdef0102ffffl, -0x654);
   228     }
   229 
   230     @Compare public long moduloMixedNumbersOneDigitDenom() {
   231         return mod(-0xabcdef0102ffffl, 0x654);
   232     }
   233 
   234     @Compare public long moduloPositiveNumbersMultiDigitDenom() {
   235         return mod(0x7ffefc003322aabbl, 0x89ab1000l);
   236     }
   237 
   238     @Compare public long moduloNegativeNumbersMultiDigitDenom() {
   239         return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
   240     }
   241 
   242     @Compare public long moduloMixedNumbersMultiDigitDenom() {
   243         return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
   244     }
   245 
   246     @Compare public long moduloWithOverflow() {
   247         return mod(0x8000fffe0000l, 0x8000ffffl);
   248     }
   249 
   250     @Compare public long moduloWithCorrection() {
   251         return mod(0x7fff800000000000l, 0x800000000001l);
   252     }
   253 
   254     @Compare public boolean divByZeroThrowsArithmeticException() {
   255         try {
   256             div(1, 0);
   257             return false;
   258         } catch (final ArithmeticException e) {
   259             return true;
   260         }
   261     }
   262 
   263     @Compare public boolean modByZeroThrowsArithmeticException() {
   264         try {
   265             mod(1, 0);
   266             return false;
   267         } catch (final ArithmeticException e) {
   268             return true;
   269         }
   270     }
   271 
   272     @Compare public long shiftL1() {
   273         return shl(0x00fa37d7763e0ca1l, 5);
   274     }
   275     
   276     @Compare public long shiftL2() {
   277         return shl(0x00fa37d7763e0ca1l, 32);
   278     }
   279     
   280     @Compare public long shiftL3() {
   281         return shl(0x00fa37d7763e0ca1l, 45);
   282     }
   283     
   284     @Compare public long shiftR1() {
   285         return shr(0x00fa37d7763e0ca1l, 5);
   286     }
   287     
   288     @Compare public long shiftR2() {
   289         return shr(0x00fa37d7763e0ca1l, 32);
   290     }
   291     
   292     @Compare public long shiftR3() {
   293         return shr(0x00fa37d7763e0ca1l, 45);
   294     }
   295     
   296     @Compare public long uShiftR1() {
   297         return ushr(0x00fa37d7763e0ca1l, 5);
   298     }
   299     
   300     @Compare public long uShiftR2() {
   301         return ushr(0x00fa37d7763e0ca1l, 45);
   302     }
   303     
   304     @Compare public long uShiftR3() {
   305         return ushr(0xf0fa37d7763e0ca1l, 5);
   306     }
   307     
   308     @Compare public long uShiftR4() {
   309         return ushr(0xf0fa37d7763e0ca1l, 45);
   310     }
   311     
   312     @Compare public long and1() {
   313         return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   314     }
   315     
   316     @Compare public long or1() {
   317         return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   318     }
   319     
   320     @Compare public long xor1() {
   321         return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   322     }
   323     
   324     @Compare public long xor2() {
   325         return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
   326     }
   327     
   328     @Compare public long xor3() {
   329         return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
   330     }
   331     
   332     @Compare public int compareSameNumbers() {
   333         return compare(0x0000000000000000l, 0x0000000000000000l, 0);
   334     }
   335 
   336     @Compare public int comparePositiveNumbers() {
   337         return compare(0x0000000000200000l, 0x0000000010000000l, 0);
   338     }
   339 
   340     @Compare public int compareNegativeNumbers() {
   341         return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
   342     }
   343 
   344     @Compare public int compareMixedNumbers() {
   345         return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
   346     }
   347     
   348     @Factory
   349     public static Object[] create() {
   350         return VMTest.create(LongArithmeticTest.class);
   351     }
   352 }