1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,352 @@
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 + public static int compare(long x, long y, int zero) {
1.82 + final int xyResult = compareL(x, y, zero);
1.83 + final int yxResult = compareL(y, x, zero);
1.84 +
1.85 + return ((xyResult + yxResult) == 0) ? xyResult : -2;
1.86 + }
1.87 +
1.88 + private static int compareL(long x, long y, int zero) {
1.89 + int result = -2;
1.90 + int trueCount = 0;
1.91 +
1.92 + x += zero;
1.93 + if (x == y) {
1.94 + result = 0;
1.95 + ++trueCount;
1.96 + }
1.97 +
1.98 + x += zero;
1.99 + if (x < y) {
1.100 + result = -1;
1.101 + ++trueCount;
1.102 + }
1.103 +
1.104 + x += zero;
1.105 + if (x > y) {
1.106 + result = 1;
1.107 + ++trueCount;
1.108 + }
1.109 +
1.110 + return (trueCount == 1) ? result : -2;
1.111 + }
1.112 +
1.113 + @Compare public long conversion() {
1.114 + return Long.MAX_VALUE;
1.115 + }
1.116 +
1.117 + @Compare public long negate1() {
1.118 + return neg(0x00fa37d7763e0ca1l);
1.119 + }
1.120 +
1.121 + @Compare public long negate2() {
1.122 + return neg(0x80fa37d7763e0ca1l);
1.123 + }
1.124 +
1.125 + @Compare public long negate3() {
1.126 + return neg(0xfffffffffffffeddl);
1.127 + }
1.128 +
1.129 + @Compare public long addOverflow() {
1.130 + return add(Long.MAX_VALUE, 1l);
1.131 + }
1.132 +
1.133 + @Compare public long subUnderflow() {
1.134 + return sub(Long.MIN_VALUE, 1l);
1.135 + }
1.136 +
1.137 + @Compare public long addMaxLongAndMaxLong() {
1.138 + return add(Long.MAX_VALUE, Long.MAX_VALUE);
1.139 + }
1.140 +
1.141 + @Compare public long subMinLongAndMinLong() {
1.142 + return sub(Long.MIN_VALUE, Long.MIN_VALUE);
1.143 + }
1.144 +
1.145 + @Compare public long subMinLongAndMaxLong() {
1.146 + return sub(Long.MIN_VALUE, Long.MAX_VALUE);
1.147 + }
1.148 +
1.149 + @Compare public long multiplyMaxLong() {
1.150 + return mul(Long.MAX_VALUE, 2l);
1.151 + }
1.152 +
1.153 + @Compare public long multiplyMaxLongAndMaxLong() {
1.154 + return mul(Long.MAX_VALUE, Long.MAX_VALUE);
1.155 + }
1.156 +
1.157 + @Compare public long multiplyMinLong() {
1.158 + return mul(Long.MIN_VALUE, 2l);
1.159 + }
1.160 +
1.161 + @Compare public long multiplyMinLongAndMinLong() {
1.162 + return mul(Long.MIN_VALUE, Long.MIN_VALUE);
1.163 + }
1.164 +
1.165 + @Compare public long multiplyPrecision() {
1.166 + return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
1.167 + }
1.168 +
1.169 + @Compare public long divideSmallPositiveNumbers() {
1.170 + return div(0xabcdef, 0x123);
1.171 + }
1.172 +
1.173 + @Compare public long divideSmallNegativeNumbers() {
1.174 + return div(-0xabcdef, -0x123);
1.175 + }
1.176 +
1.177 + @Compare public long divideSmallMixedNumbers() {
1.178 + return div(0xabcdef, -0x123);
1.179 + }
1.180 +
1.181 + @Compare public long dividePositiveNumbersOneDigitDenom() {
1.182 + return div(0xabcdef0102ffffl, 0x654);
1.183 + }
1.184 +
1.185 + @Compare public long divideNegativeNumbersOneDigitDenom() {
1.186 + return div(-0xabcdef0102ffffl, -0x654);
1.187 + }
1.188 +
1.189 + @Compare public long divideMixedNumbersOneDigitDenom() {
1.190 + return div(-0xabcdef0102ffffl, 0x654);
1.191 + }
1.192 +
1.193 + @Compare public long dividePositiveNumbersMultiDigitDenom() {
1.194 + return div(0x7ffefc003322aabbl, 0x89ab1000l);
1.195 + }
1.196 +
1.197 + @Compare public long divideNegativeNumbersMultiDigitDenom() {
1.198 + return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
1.199 + }
1.200 +
1.201 + @Compare public long divideMixedNumbersMultiDigitDenom() {
1.202 + return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
1.203 + }
1.204 +
1.205 + @Compare public long divideWithOverflow() {
1.206 + return div(0x8000fffe0000l, 0x8000ffffl);
1.207 + }
1.208 +
1.209 + @Compare public long divideWithCorrection() {
1.210 + return div(0x7fff800000000000l, 0x800000000001l);
1.211 + }
1.212 +
1.213 + @Compare public long moduloSmallPositiveNumbers() {
1.214 + return mod(0xabcdef, 0x123);
1.215 + }
1.216 +
1.217 + @Compare public long moduloSmallNegativeNumbers() {
1.218 + return mod(-0xabcdef, -0x123);
1.219 + }
1.220 +
1.221 + @Compare public long moduloSmallMixedNumbers() {
1.222 + return mod(0xabcdef, -0x123);
1.223 + }
1.224 +
1.225 + @Compare public long moduloPositiveNumbersOneDigitDenom() {
1.226 + return mod(0xabcdef0102ffffl, 0x654);
1.227 + }
1.228 +
1.229 + @Compare public long moduloNegativeNumbersOneDigitDenom() {
1.230 + return mod(-0xabcdef0102ffffl, -0x654);
1.231 + }
1.232 +
1.233 + @Compare public long moduloMixedNumbersOneDigitDenom() {
1.234 + return mod(-0xabcdef0102ffffl, 0x654);
1.235 + }
1.236 +
1.237 + @Compare public long moduloPositiveNumbersMultiDigitDenom() {
1.238 + return mod(0x7ffefc003322aabbl, 0x89ab1000l);
1.239 + }
1.240 +
1.241 + @Compare public long moduloNegativeNumbersMultiDigitDenom() {
1.242 + return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
1.243 + }
1.244 +
1.245 + @Compare public long moduloMixedNumbersMultiDigitDenom() {
1.246 + return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
1.247 + }
1.248 +
1.249 + @Compare public long moduloWithOverflow() {
1.250 + return mod(0x8000fffe0000l, 0x8000ffffl);
1.251 + }
1.252 +
1.253 + @Compare public long moduloWithCorrection() {
1.254 + return mod(0x7fff800000000000l, 0x800000000001l);
1.255 + }
1.256 +
1.257 + @Compare public boolean divByZeroThrowsArithmeticException() {
1.258 + try {
1.259 + div(1, 0);
1.260 + return false;
1.261 + } catch (final ArithmeticException e) {
1.262 + return true;
1.263 + }
1.264 + }
1.265 +
1.266 + @Compare public boolean modByZeroThrowsArithmeticException() {
1.267 + try {
1.268 + mod(1, 0);
1.269 + return false;
1.270 + } catch (final ArithmeticException e) {
1.271 + return true;
1.272 + }
1.273 + }
1.274 +
1.275 + @Compare public long shiftL1() {
1.276 + return shl(0x00fa37d7763e0ca1l, 5);
1.277 + }
1.278 +
1.279 + @Compare public long shiftL2() {
1.280 + return shl(0x00fa37d7763e0ca1l, 32);
1.281 + }
1.282 +
1.283 + @Compare public long shiftL3() {
1.284 + return shl(0x00fa37d7763e0ca1l, 45);
1.285 + }
1.286 +
1.287 + @Compare public long shiftR1() {
1.288 + return shr(0x00fa37d7763e0ca1l, 5);
1.289 + }
1.290 +
1.291 + @Compare public long shiftR2() {
1.292 + return shr(0x00fa37d7763e0ca1l, 32);
1.293 + }
1.294 +
1.295 + @Compare public long shiftR3() {
1.296 + return shr(0x00fa37d7763e0ca1l, 45);
1.297 + }
1.298 +
1.299 + @Compare public long uShiftR1() {
1.300 + return ushr(0x00fa37d7763e0ca1l, 5);
1.301 + }
1.302 +
1.303 + @Compare public long uShiftR2() {
1.304 + return ushr(0x00fa37d7763e0ca1l, 45);
1.305 + }
1.306 +
1.307 + @Compare public long uShiftR3() {
1.308 + return ushr(0xf0fa37d7763e0ca1l, 5);
1.309 + }
1.310 +
1.311 + @Compare public long uShiftR4() {
1.312 + return ushr(0xf0fa37d7763e0ca1l, 45);
1.313 + }
1.314 +
1.315 + @Compare public long and1() {
1.316 + return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
1.317 + }
1.318 +
1.319 + @Compare public long or1() {
1.320 + return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
1.321 + }
1.322 +
1.323 + @Compare public long xor1() {
1.324 + return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
1.325 + }
1.326 +
1.327 + @Compare public long xor2() {
1.328 + return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
1.329 + }
1.330 +
1.331 + @Compare public long xor3() {
1.332 + return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
1.333 + }
1.334 +
1.335 + @Compare public int compareSameNumbers() {
1.336 + return compare(0x0000000000000000l, 0x0000000000000000l, 0);
1.337 + }
1.338 +
1.339 + @Compare public int comparePositiveNumbers() {
1.340 + return compare(0x0000000000200000l, 0x0000000010000000l, 0);
1.341 + }
1.342 +
1.343 + @Compare public int compareNegativeNumbers() {
1.344 + return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
1.345 + }
1.346 +
1.347 + @Compare public int compareMixedNumbers() {
1.348 + return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
1.349 + }
1.350 +
1.351 + @Factory
1.352 + public static Object[] create() {
1.353 + return VMTest.create(LongArithmeticTest.class);
1.354 + }
1.355 +}