rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java Wed Nov 19 19:32:00 2014 +0100
1.3 @@ -0,0 +1,418 @@
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 int parameterSlotCount() {
1.122 + long argCounts = 281479271874563L;
1.123 + int x = unpack(argCounts, 2);
1.124 + return x;
1.125 + }
1.126 + private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d
1.127 + assert(word <= 3);
1.128 + final long val = packed >> ((3-word) * 16);
1.129 + return (char)val;
1.130 + }
1.131 + @Compare public long conversion() {
1.132 + return Long.MAX_VALUE;
1.133 + }
1.134 +
1.135 + @Compare public long negate1() {
1.136 + return neg(0x00fa37d7763e0ca1l);
1.137 + }
1.138 +
1.139 + @Compare public long negate2() {
1.140 + return neg(0x80fa37d7763e0ca1l);
1.141 + }
1.142 +
1.143 + @Compare public long negate3() {
1.144 + return neg(0xfffffffffffffeddl);
1.145 + }
1.146 +
1.147 + @Compare public long addOverflow() {
1.148 + return add(Long.MAX_VALUE, 1l);
1.149 + }
1.150 +
1.151 + @Compare public long subUnderflow() {
1.152 + return sub(Long.MIN_VALUE, 1l);
1.153 + }
1.154 +
1.155 + @Compare public long addMaxLongAndMaxLong() {
1.156 + return add(Long.MAX_VALUE, Long.MAX_VALUE);
1.157 + }
1.158 +
1.159 + @Compare public long subMinLongAndMinLong() {
1.160 + return sub(Long.MIN_VALUE, Long.MIN_VALUE);
1.161 + }
1.162 +
1.163 + @Compare public long subMinLongAndMaxLong() {
1.164 + return sub(Long.MIN_VALUE, Long.MAX_VALUE);
1.165 + }
1.166 +
1.167 + @Compare public long multiplyMaxLong() {
1.168 + return mul(Long.MAX_VALUE, 2l);
1.169 + }
1.170 +
1.171 + @Compare public long multiplyMaxLongAndMaxLong() {
1.172 + return mul(Long.MAX_VALUE, Long.MAX_VALUE);
1.173 + }
1.174 +
1.175 + @Compare public long multiplyMinLong() {
1.176 + return mul(Long.MIN_VALUE, 2l);
1.177 + }
1.178 +
1.179 + @Compare public long multiplyMinLongAndMinLong() {
1.180 + return mul(Long.MIN_VALUE, Long.MIN_VALUE);
1.181 + }
1.182 +
1.183 + @Compare public long multiplyPrecision() {
1.184 + return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
1.185 + }
1.186 +
1.187 + @Compare public long divideSmallPositiveNumbers() {
1.188 + return div(0xabcdef, 0x123);
1.189 + }
1.190 +
1.191 + @Compare public long divideSmallNegativeNumbers() {
1.192 + return div(-0xabcdef, -0x123);
1.193 + }
1.194 +
1.195 + @Compare public long divideSmallMixedNumbers() {
1.196 + return div(0xabcdef, -0x123);
1.197 + }
1.198 +
1.199 + @Compare public long dividePositiveNumbersOneDigitDenom() {
1.200 + return div(0xabcdef0102ffffl, 0x654);
1.201 + }
1.202 +
1.203 + @Compare public long divideNegativeNumbersOneDigitDenom() {
1.204 + return div(-0xabcdef0102ffffl, -0x654);
1.205 + }
1.206 +
1.207 + @Compare public long divideMixedNumbersOneDigitDenom() {
1.208 + return div(-0xabcdef0102ffffl, 0x654);
1.209 + }
1.210 +
1.211 + @Compare public long dividePositiveNumbersMultiDigitDenom() {
1.212 + return div(0x7ffefc003322aabbl, 0x89ab1000l);
1.213 + }
1.214 +
1.215 + @Compare public long divideNegativeNumbersMultiDigitDenom() {
1.216 + return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
1.217 + }
1.218 +
1.219 + @Compare public long divideMixedNumbersMultiDigitDenom() {
1.220 + return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
1.221 + }
1.222 +
1.223 + @Compare public long divideWithOverflow() {
1.224 + return div(0x8000fffe0000l, 0x8000ffffl);
1.225 + }
1.226 +
1.227 + @Compare public long divideWithCorrection() {
1.228 + return div(0x7fff800000000000l, 0x800000000001l);
1.229 + }
1.230 +
1.231 + @Compare public long moduloSmallPositiveNumbers() {
1.232 + return mod(0xabcdef, 0x123);
1.233 + }
1.234 +
1.235 + @Compare public long moduloSmallNegativeNumbers() {
1.236 + return mod(-0xabcdef, -0x123);
1.237 + }
1.238 +
1.239 + @Compare public long moduloSmallMixedNumbers() {
1.240 + return mod(0xabcdef, -0x123);
1.241 + }
1.242 +
1.243 + @Compare public long moduloPositiveNumbersOneDigitDenom() {
1.244 + return mod(0xabcdef0102ffffl, 0x654);
1.245 + }
1.246 +
1.247 + @Compare public long moduloNegativeNumbersOneDigitDenom() {
1.248 + return mod(-0xabcdef0102ffffl, -0x654);
1.249 + }
1.250 +
1.251 + @Compare public long moduloMixedNumbersOneDigitDenom() {
1.252 + return mod(-0xabcdef0102ffffl, 0x654);
1.253 + }
1.254 +
1.255 + @Compare public long moduloPositiveNumbersMultiDigitDenom() {
1.256 + return mod(0x7ffefc003322aabbl, 0x89ab1000l);
1.257 + }
1.258 +
1.259 + @Compare public long moduloNegativeNumbersMultiDigitDenom() {
1.260 + return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
1.261 + }
1.262 +
1.263 + @Compare public long moduloMixedNumbersMultiDigitDenom() {
1.264 + return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
1.265 + }
1.266 +
1.267 + @Compare public long moduloWithOverflow() {
1.268 + return mod(0x8000fffe0000l, 0x8000ffffl);
1.269 + }
1.270 +
1.271 + @Compare public long moduloWithCorrection() {
1.272 + return mod(0x7fff800000000000l, 0x800000000001l);
1.273 + }
1.274 +
1.275 + @Compare public long conversionFromFloatPositive() {
1.276 + return (long) fadd(2, 0.6f);
1.277 + }
1.278 +
1.279 + @Compare public long conversionFromFloatNegative() {
1.280 + return (long) fadd(-2, -0.6f);
1.281 + }
1.282 +
1.283 + @Compare public long conversionFromDoublePositive() {
1.284 + return (long) dadd(0x20ffff0000L, 0.6);
1.285 + }
1.286 +
1.287 + @Compare public long conversionFromDoubleNegative() {
1.288 + return (long) dadd(-0x20ffff0000L, -0.6);
1.289 + }
1.290 +
1.291 + @Compare public boolean divByZeroThrowsArithmeticException() {
1.292 + try {
1.293 + div(1, 0);
1.294 + return false;
1.295 + } catch (final ArithmeticException e) {
1.296 + return true;
1.297 + }
1.298 + }
1.299 +
1.300 + @Compare public boolean modByZeroThrowsArithmeticException() {
1.301 + try {
1.302 + mod(1, 0);
1.303 + return false;
1.304 + } catch (final ArithmeticException e) {
1.305 + return true;
1.306 + }
1.307 + }
1.308 +
1.309 + @Compare public long shiftL1() {
1.310 + return shl(0x00fa37d7763e0ca1l, 5);
1.311 + }
1.312 +
1.313 + @Compare public long shiftL2() {
1.314 + return shl(0x00fa37d7763e0ca1l, 32);
1.315 + }
1.316 +
1.317 + @Compare public long shiftL3() {
1.318 + return shl(0x00fa37d7763e0ca1l, 45);
1.319 + }
1.320 +
1.321 + @Compare public long shiftL4() {
1.322 + return shl(0x00fa37d7763e0ca1l, 0);
1.323 + }
1.324 +
1.325 + @Compare public long shiftL5() {
1.326 + return shl(0x00fa37d7763e0ca1l, 70);
1.327 + }
1.328 +
1.329 + @Compare public long shiftR1() {
1.330 + return shr(0x00fa37d7763e0ca1l, 5);
1.331 + }
1.332 +
1.333 + @Compare public long shiftR2() {
1.334 + return shr(0x00fa37d7763e0ca1l, 32);
1.335 + }
1.336 +
1.337 + @Compare public long shiftR3() {
1.338 + return shr(0x00fa37d7763e0ca1l, 45);
1.339 + }
1.340 +
1.341 + @Compare public long shiftR4() {
1.342 + return shr(0x00fa37d7763e0ca1l, 0);
1.343 + }
1.344 +
1.345 + @Compare public long shiftR5() {
1.346 + return shr(0x00fa37d7763e0ca1l, 70);
1.347 + }
1.348 +
1.349 + @Compare public long uShiftR1() {
1.350 + return ushr(0x00fa37d7763e0ca1l, 5);
1.351 + }
1.352 +
1.353 + @Compare public long uShiftR2() {
1.354 + return ushr(0x00fa37d7763e0ca1l, 45);
1.355 + }
1.356 +
1.357 + @Compare public long uShiftR3() {
1.358 + return ushr(0x00fa37d7763e0ca1l, 0);
1.359 + }
1.360 +
1.361 + @Compare public long uShiftR4() {
1.362 + return ushr(0x00fa37d7763e0ca1l, 70);
1.363 + }
1.364 +
1.365 + @Compare public long uShiftR5() {
1.366 + return ushr(0xf0fa37d7763e0ca1l, 5);
1.367 + }
1.368 +
1.369 + @Compare public long uShiftR6() {
1.370 + return ushr(0xf0fa37d7763e0ca1l, 45);
1.371 + }
1.372 +
1.373 + @Compare public long uShiftR7() {
1.374 + return ushr(0xf0fa37d7763e0ca1l, 0);
1.375 + }
1.376 +
1.377 + @Compare public long uShiftR8() {
1.378 + return ushr(0xf0fa37d7763e0ca1l, 70);
1.379 + }
1.380 +
1.381 + @Compare public long and1() {
1.382 + return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
1.383 + }
1.384 +
1.385 + @Compare public long or1() {
1.386 + return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
1.387 + }
1.388 +
1.389 + @Compare public long xor1() {
1.390 + return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
1.391 + }
1.392 +
1.393 + @Compare public long xor2() {
1.394 + return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
1.395 + }
1.396 +
1.397 + @Compare public long xor3() {
1.398 + return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
1.399 + }
1.400 +
1.401 + @Compare public int compareSameNumbers() {
1.402 + return compare(0x0000000000000000l, 0x0000000000000000l, 0);
1.403 + }
1.404 +
1.405 + @Compare public int comparePositiveNumbers() {
1.406 + return compare(0x0000000000200000l, 0x0000000010000000l, 0);
1.407 + }
1.408 +
1.409 + @Compare public int compareNegativeNumbers() {
1.410 + return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
1.411 + }
1.412 +
1.413 + @Compare public int compareMixedNumbers() {
1.414 + return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
1.415 + }
1.416 +
1.417 + @Factory
1.418 + public static Object[] create() {
1.419 + return VMTest.create(LongArithmeticTest.class);
1.420 + }
1.421 +}