vmtest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java
author Martin Soch <Martin.Soch@oracle.com>
Thu, 07 Feb 2013 17:24:19 +0100
brancharithmetic
changeset 699 6cad41d877d2
parent 698 ff57af563cb8
child 737 b2731af0357d
permissions -rw-r--r--
Final part of Long tests moved from vm/.../NumberTest to vmtest/.../LongArithmeticTest
     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 long shiftL1() {
   255         return shl(0x00fa37d7763e0ca1l, 5);
   256     }
   257     
   258     @Compare public long shiftL2() {
   259         return shl(0x00fa37d7763e0ca1l, 32);
   260     }
   261     
   262     @Compare public long shiftL3() {
   263         return shl(0x00fa37d7763e0ca1l, 45);
   264     }
   265     
   266     @Compare public long shiftR1() {
   267         return shr(0x00fa37d7763e0ca1l, 5);
   268     }
   269     
   270     @Compare public long shiftR2() {
   271         return shr(0x00fa37d7763e0ca1l, 32);
   272     }
   273     
   274     @Compare public long shiftR3() {
   275         return shr(0x00fa37d7763e0ca1l, 45);
   276     }
   277     
   278     @Compare public long uShiftR1() {
   279         return ushr(0x00fa37d7763e0ca1l, 5);
   280     }
   281     
   282     @Compare public long uShiftR2() {
   283         return ushr(0x00fa37d7763e0ca1l, 45);
   284     }
   285     
   286     @Compare public long uShiftR3() {
   287         return ushr(0xf0fa37d7763e0ca1l, 5);
   288     }
   289     
   290     @Compare public long uShiftR4() {
   291         return ushr(0xf0fa37d7763e0ca1l, 45);
   292     }
   293     
   294     @Compare public long and1() {
   295         return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   296     }
   297     
   298     @Compare public long or1() {
   299         return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   300     }
   301     
   302     @Compare public long xor1() {
   303         return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   304     }
   305     
   306     @Compare public long xor2() {
   307         return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
   308     }
   309     
   310     @Compare public long xor3() {
   311         return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
   312     }
   313     
   314     @Compare public int compareSameNumbers() {
   315         return compare(0x0000000000000000l, 0x0000000000000000l, 0);
   316     }
   317 
   318     @Compare public int comparePositiveNumbers() {
   319         return compare(0x0000000000200000l, 0x0000000010000000l, 0);
   320     }
   321 
   322     @Compare public int compareNegativeNumbers() {
   323         return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
   324     }
   325 
   326     @Compare public int compareMixedNumbers() {
   327         return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
   328     }
   329     
   330     @Factory
   331     public static Object[] create() {
   332         return VMTest.create(LongArithmeticTest.class);
   333     }
   334 }