rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java
changeset 1101 56abd247f421
parent 778 6f8683517f1f
child 1352 7bc78045adfd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java	Thu May 16 10:37:44 2013 +0200
     1.3 @@ -0,0 +1,376 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.tck;
    1.22 +
    1.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
    1.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.25 +import org.testng.annotations.Factory;
    1.26 +
    1.27 +/**
    1.28 + *
    1.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.30 + */
    1.31 +public class LongArithmeticTest {
    1.32 +
    1.33 +    private static long add(long x, long y) {
    1.34 +        return (x + y);
    1.35 +    }
    1.36 +
    1.37 +    private static long sub(long x, long y) {
    1.38 +        return (x - y);
    1.39 +    }
    1.40 +
    1.41 +    private static long mul(long x, long y) {
    1.42 +        return (x * y);
    1.43 +    }
    1.44 +
    1.45 +    private static long div(long x, long y) {
    1.46 +        return (x / y);
    1.47 +    }
    1.48 +
    1.49 +    private static long mod(long x, long y) {
    1.50 +        return (x % y);
    1.51 +    }
    1.52 +
    1.53 +    private static long neg(long x) {
    1.54 +        return (-x);
    1.55 +    }
    1.56 +
    1.57 +    private static long shl(long x, int b) {
    1.58 +        return (x << b);
    1.59 +    }
    1.60 +
    1.61 +    private static long shr(long x, int b) {
    1.62 +        return (x >> b);
    1.63 +    }
    1.64 +
    1.65 +    private static long ushr(long x, int b) {
    1.66 +        return (x >>> b);
    1.67 +    }
    1.68 +
    1.69 +    private static long and(long x, long y) {
    1.70 +        return (x & y);
    1.71 +    }
    1.72 +
    1.73 +    private static long or(long x, long y) {
    1.74 +        return (x | y);
    1.75 +    }
    1.76 +
    1.77 +    private static long xor(long x, long y) {
    1.78 +        return (x ^ y);
    1.79 +    }
    1.80 +
    1.81 +    private static float fadd(float x, float y) {
    1.82 +        return x + y;
    1.83 +    }
    1.84 +
    1.85 +    private static double dadd(double x, double y) {
    1.86 +        return x + y;
    1.87 +    }
    1.88 +
    1.89 +    public static int compare(long x, long y, int zero) {
    1.90 +        final int xyResult = compareL(x, y, zero);
    1.91 +        final int yxResult = compareL(y, x, zero);
    1.92 +
    1.93 +        return ((xyResult + yxResult) == 0) ? xyResult : -2;
    1.94 +    }
    1.95 +
    1.96 +    private static int compareL(long x, long y, int zero) {
    1.97 +        int result = -2;
    1.98 +        int trueCount = 0;
    1.99 +
   1.100 +        x += zero;
   1.101 +        if (x == y) {
   1.102 +            result = 0;
   1.103 +            ++trueCount;
   1.104 +        }
   1.105 +
   1.106 +        x += zero;
   1.107 +        if (x < y) {
   1.108 +            result = -1;
   1.109 +            ++trueCount;
   1.110 +        }
   1.111 +
   1.112 +        x += zero;
   1.113 +        if (x > y) {
   1.114 +            result = 1;
   1.115 +            ++trueCount;
   1.116 +        }
   1.117 +
   1.118 +        return (trueCount == 1) ? result : -2;
   1.119 +    }
   1.120 +
   1.121 +    @Compare public long conversion() {
   1.122 +        return Long.MAX_VALUE;
   1.123 +    }
   1.124 +
   1.125 +    @Compare public long negate1() {
   1.126 +        return neg(0x00fa37d7763e0ca1l);
   1.127 +    }
   1.128 +
   1.129 +    @Compare public long negate2() {
   1.130 +        return neg(0x80fa37d7763e0ca1l);
   1.131 +    }
   1.132 +
   1.133 +    @Compare public long negate3() {
   1.134 +        return neg(0xfffffffffffffeddl);
   1.135 +    }
   1.136 +
   1.137 +    @Compare public long addOverflow() {
   1.138 +        return add(Long.MAX_VALUE, 1l);
   1.139 +    }
   1.140 +
   1.141 +    @Compare public long subUnderflow() {
   1.142 +        return sub(Long.MIN_VALUE, 1l);
   1.143 +    }
   1.144 +
   1.145 +    @Compare public long addMaxLongAndMaxLong() {
   1.146 +        return add(Long.MAX_VALUE, Long.MAX_VALUE);
   1.147 +    }
   1.148 +
   1.149 +    @Compare public long subMinLongAndMinLong() {
   1.150 +        return sub(Long.MIN_VALUE, Long.MIN_VALUE);
   1.151 +    }
   1.152 +
   1.153 +    @Compare public long subMinLongAndMaxLong() {
   1.154 +        return sub(Long.MIN_VALUE, Long.MAX_VALUE);
   1.155 +    }
   1.156 +
   1.157 +    @Compare public long multiplyMaxLong() {
   1.158 +        return mul(Long.MAX_VALUE, 2l);
   1.159 +    }
   1.160 +
   1.161 +    @Compare public long multiplyMaxLongAndMaxLong() {
   1.162 +        return mul(Long.MAX_VALUE, Long.MAX_VALUE);
   1.163 +    }
   1.164 +
   1.165 +    @Compare public long multiplyMinLong() {
   1.166 +        return mul(Long.MIN_VALUE, 2l);
   1.167 +    }
   1.168 +
   1.169 +    @Compare public long multiplyMinLongAndMinLong() {
   1.170 +        return mul(Long.MIN_VALUE, Long.MIN_VALUE);
   1.171 +    }
   1.172 +
   1.173 +    @Compare public long multiplyPrecision() {
   1.174 +        return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   1.175 +    }
   1.176 +
   1.177 +    @Compare public long divideSmallPositiveNumbers() {
   1.178 +        return div(0xabcdef, 0x123);
   1.179 +    }
   1.180 +
   1.181 +    @Compare public long divideSmallNegativeNumbers() {
   1.182 +        return div(-0xabcdef, -0x123);
   1.183 +    }
   1.184 +
   1.185 +    @Compare public long divideSmallMixedNumbers() {
   1.186 +        return div(0xabcdef, -0x123);
   1.187 +    }
   1.188 +
   1.189 +    @Compare public long dividePositiveNumbersOneDigitDenom() {
   1.190 +        return div(0xabcdef0102ffffl, 0x654);
   1.191 +    }
   1.192 +
   1.193 +    @Compare public long divideNegativeNumbersOneDigitDenom() {
   1.194 +        return div(-0xabcdef0102ffffl, -0x654);
   1.195 +    }
   1.196 +
   1.197 +    @Compare public long divideMixedNumbersOneDigitDenom() {
   1.198 +        return div(-0xabcdef0102ffffl, 0x654);
   1.199 +    }
   1.200 +
   1.201 +    @Compare public long dividePositiveNumbersMultiDigitDenom() {
   1.202 +        return div(0x7ffefc003322aabbl, 0x89ab1000l);
   1.203 +    }
   1.204 +
   1.205 +    @Compare public long divideNegativeNumbersMultiDigitDenom() {
   1.206 +        return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
   1.207 +    }
   1.208 +
   1.209 +    @Compare public long divideMixedNumbersMultiDigitDenom() {
   1.210 +        return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
   1.211 +    }
   1.212 +
   1.213 +    @Compare public long divideWithOverflow() {
   1.214 +        return div(0x8000fffe0000l, 0x8000ffffl);
   1.215 +    }
   1.216 +
   1.217 +    @Compare public long divideWithCorrection() {
   1.218 +        return div(0x7fff800000000000l, 0x800000000001l);
   1.219 +    }
   1.220 +
   1.221 +    @Compare public long moduloSmallPositiveNumbers() {
   1.222 +        return mod(0xabcdef, 0x123);
   1.223 +    }
   1.224 +
   1.225 +    @Compare public long moduloSmallNegativeNumbers() {
   1.226 +        return mod(-0xabcdef, -0x123);
   1.227 +    }
   1.228 +
   1.229 +    @Compare public long moduloSmallMixedNumbers() {
   1.230 +        return mod(0xabcdef, -0x123);
   1.231 +    }
   1.232 +
   1.233 +    @Compare public long moduloPositiveNumbersOneDigitDenom() {
   1.234 +        return mod(0xabcdef0102ffffl, 0x654);
   1.235 +    }
   1.236 +
   1.237 +    @Compare public long moduloNegativeNumbersOneDigitDenom() {
   1.238 +        return mod(-0xabcdef0102ffffl, -0x654);
   1.239 +    }
   1.240 +
   1.241 +    @Compare public long moduloMixedNumbersOneDigitDenom() {
   1.242 +        return mod(-0xabcdef0102ffffl, 0x654);
   1.243 +    }
   1.244 +
   1.245 +    @Compare public long moduloPositiveNumbersMultiDigitDenom() {
   1.246 +        return mod(0x7ffefc003322aabbl, 0x89ab1000l);
   1.247 +    }
   1.248 +
   1.249 +    @Compare public long moduloNegativeNumbersMultiDigitDenom() {
   1.250 +        return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
   1.251 +    }
   1.252 +
   1.253 +    @Compare public long moduloMixedNumbersMultiDigitDenom() {
   1.254 +        return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
   1.255 +    }
   1.256 +
   1.257 +    @Compare public long moduloWithOverflow() {
   1.258 +        return mod(0x8000fffe0000l, 0x8000ffffl);
   1.259 +    }
   1.260 +
   1.261 +    @Compare public long moduloWithCorrection() {
   1.262 +        return mod(0x7fff800000000000l, 0x800000000001l);
   1.263 +    }
   1.264 +
   1.265 +    @Compare public long conversionFromFloatPositive() {
   1.266 +        return (long) fadd(2, 0.6f);
   1.267 +    }
   1.268 +
   1.269 +    @Compare public long conversionFromFloatNegative() {
   1.270 +        return (long) fadd(-2, -0.6f);
   1.271 +    }
   1.272 +
   1.273 +    @Compare public long conversionFromDoublePositive() {
   1.274 +        return (long) dadd(0x20ffff0000L, 0.6);
   1.275 +    }
   1.276 +
   1.277 +    @Compare public long conversionFromDoubleNegative() {
   1.278 +        return (long) dadd(-0x20ffff0000L, -0.6);
   1.279 +    }
   1.280 +
   1.281 +    @Compare public boolean divByZeroThrowsArithmeticException() {
   1.282 +        try {
   1.283 +            div(1, 0);
   1.284 +            return false;
   1.285 +        } catch (final ArithmeticException e) {
   1.286 +            return true;
   1.287 +        }
   1.288 +    }
   1.289 +
   1.290 +    @Compare public boolean modByZeroThrowsArithmeticException() {
   1.291 +        try {
   1.292 +            mod(1, 0);
   1.293 +            return false;
   1.294 +        } catch (final ArithmeticException e) {
   1.295 +            return true;
   1.296 +        }
   1.297 +    }
   1.298 +
   1.299 +    @Compare public long shiftL1() {
   1.300 +        return shl(0x00fa37d7763e0ca1l, 5);
   1.301 +    }
   1.302 +
   1.303 +    @Compare public long shiftL2() {
   1.304 +        return shl(0x00fa37d7763e0ca1l, 32);
   1.305 +    }
   1.306 +
   1.307 +    @Compare public long shiftL3() {
   1.308 +        return shl(0x00fa37d7763e0ca1l, 45);
   1.309 +    }
   1.310 +
   1.311 +    @Compare public long shiftR1() {
   1.312 +        return shr(0x00fa37d7763e0ca1l, 5);
   1.313 +    }
   1.314 +
   1.315 +    @Compare public long shiftR2() {
   1.316 +        return shr(0x00fa37d7763e0ca1l, 32);
   1.317 +    }
   1.318 +
   1.319 +    @Compare public long shiftR3() {
   1.320 +        return shr(0x00fa37d7763e0ca1l, 45);
   1.321 +    }
   1.322 +
   1.323 +    @Compare public long uShiftR1() {
   1.324 +        return ushr(0x00fa37d7763e0ca1l, 5);
   1.325 +    }
   1.326 +
   1.327 +    @Compare public long uShiftR2() {
   1.328 +        return ushr(0x00fa37d7763e0ca1l, 45);
   1.329 +    }
   1.330 +
   1.331 +    @Compare public long uShiftR3() {
   1.332 +        return ushr(0xf0fa37d7763e0ca1l, 5);
   1.333 +    }
   1.334 +
   1.335 +    @Compare public long uShiftR4() {
   1.336 +        return ushr(0xf0fa37d7763e0ca1l, 45);
   1.337 +    }
   1.338 +
   1.339 +    @Compare public long and1() {
   1.340 +        return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   1.341 +    }
   1.342 +
   1.343 +    @Compare public long or1() {
   1.344 +        return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   1.345 +    }
   1.346 +
   1.347 +    @Compare public long xor1() {
   1.348 +        return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
   1.349 +    }
   1.350 +
   1.351 +    @Compare public long xor2() {
   1.352 +        return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
   1.353 +    }
   1.354 +
   1.355 +    @Compare public long xor3() {
   1.356 +        return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
   1.357 +    }
   1.358 +
   1.359 +    @Compare public int compareSameNumbers() {
   1.360 +        return compare(0x0000000000000000l, 0x0000000000000000l, 0);
   1.361 +    }
   1.362 +
   1.363 +    @Compare public int comparePositiveNumbers() {
   1.364 +        return compare(0x0000000000200000l, 0x0000000010000000l, 0);
   1.365 +    }
   1.366 +
   1.367 +    @Compare public int compareNegativeNumbers() {
   1.368 +        return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
   1.369 +    }
   1.370 +
   1.371 +    @Compare public int compareMixedNumbers() {
   1.372 +        return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
   1.373 +    }
   1.374 +    
   1.375 +    @Factory
   1.376 +    public static Object[] create() {
   1.377 +        return VMTest.create(LongArithmeticTest.class);
   1.378 +    }
   1.379 +}