rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.tck;
20 import org.apidesign.bck2brwsr.vmtest.Compare;
21 import org.apidesign.bck2brwsr.vmtest.VMTest;
22 import org.testng.annotations.Factory;
26 * @author Jaroslav Tulach <jtulach@netbeans.org>
28 public class LongArithmeticTest {
30 private static long add(long x, long y) {
34 private static long sub(long x, long y) {
38 private static long mul(long x, long y) {
42 private static long div(long x, long y) {
46 private static long mod(long x, long y) {
50 private static long neg(long x) {
54 private static long shl(long x, int b) {
58 private static long shr(long x, int b) {
62 private static long ushr(long x, int b) {
66 private static long and(long x, long y) {
70 private static long or(long x, long y) {
74 private static long xor(long x, long y) {
78 private static float fadd(float x, float y) {
82 private static double dadd(double x, double y) {
86 private static long mul64shr64(long base, int mul, int shr) {
87 long tmp = base * mul;
94 public static int compare(long x, long y, int zero) {
95 final int xyResult = compareL(x, y, zero);
96 final int yxResult = compareL(y, x, zero);
98 return ((xyResult + yxResult) == 0) ? xyResult : -2;
101 private static int compareL(long x, long y, int zero) {
123 return (trueCount == 1) ? result : -2;
126 @Compare public int parameterSlotCount() {
127 long argCounts = 281479271874563L;
128 int x = unpack(argCounts, 2);
131 private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d
133 final long val = packed >> ((3-word) * 16);
136 @Compare public long conversion() {
137 return Long.MAX_VALUE;
140 @Compare public long thirteen() {
144 @Compare public long negate1() {
145 return neg(0x00fa37d7763e0ca1l);
148 @Compare public long negate2() {
149 return neg(0x80fa37d7763e0ca1l);
152 @Compare public long negate3() {
153 return neg(0xfffffffffffffeddl);
156 @Compare public long addOverflow() {
157 return add(Long.MAX_VALUE, 1l);
160 @Compare public long subUnderflow() {
161 return sub(Long.MIN_VALUE, 1l);
164 @Compare public long addMaxLongAndMaxLong() {
165 return add(Long.MAX_VALUE, Long.MAX_VALUE);
168 @Compare public long subMinLongAndMinLong() {
169 return sub(Long.MIN_VALUE, Long.MIN_VALUE);
172 @Compare public long subMinLongAndMaxLong() {
173 return sub(Long.MIN_VALUE, Long.MAX_VALUE);
176 @Compare public long multiplyMaxLong() {
177 return mul(Long.MAX_VALUE, 2l);
180 @Compare public long multiplyMaxLongAndMaxLong() {
181 return mul(Long.MAX_VALUE, Long.MAX_VALUE);
184 @Compare public long multiplyMinLong() {
185 return mul(Long.MIN_VALUE, 2l);
188 @Compare public long multiplyMinLongAndMinLong() {
189 return mul(Long.MIN_VALUE, Long.MIN_VALUE);
192 @Compare public long multiplyPrecision() {
193 return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
196 @Compare public long divideSmallPositiveNumbers() {
197 return div(0xabcdef, 0x123);
200 @Compare public long divideSmallNegativeNumbers() {
201 return div(-0xabcdef, -0x123);
204 @Compare public long divideSmallMixedNumbers() {
205 return div(0xabcdef, -0x123);
208 @Compare public long dividePositiveNumbersOneDigitDenom() {
209 return div(0xabcdef0102ffffl, 0x654);
212 @Compare public long divideNegativeNumbersOneDigitDenom() {
213 return div(-0xabcdef0102ffffl, -0x654);
216 @Compare public long divideMixedNumbersOneDigitDenom() {
217 return div(-0xabcdef0102ffffl, 0x654);
220 @Compare public long dividePositiveNumbersMultiDigitDenom() {
221 return div(0x7ffefc003322aabbl, 0x89ab1000l);
224 @Compare public long divideNegativeNumbersMultiDigitDenom() {
225 return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
228 @Compare public long divideMixedNumbersMultiDigitDenom() {
229 return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
232 @Compare public long divideWithOverflow() {
233 return div(0x8000fffe0000l, 0x8000ffffl);
236 @Compare public long divideWithCorrection() {
237 return div(0x7fff800000000000l, 0x800000000001l);
240 @Compare public long moduloSmallPositiveNumbers() {
241 return mod(0xabcdef, 0x123);
244 @Compare public long moduloSmallNegativeNumbers() {
245 return mod(-0xabcdef, -0x123);
248 @Compare public long moduloSmallMixedNumbers() {
249 return mod(0xabcdef, -0x123);
252 @Compare public long moduloPositiveNumbersOneDigitDenom() {
253 return mod(0xabcdef0102ffffl, 0x654);
256 @Compare public long moduloNegativeNumbersOneDigitDenom() {
257 return mod(-0xabcdef0102ffffl, -0x654);
260 @Compare public long moduloMixedNumbersOneDigitDenom() {
261 return mod(-0xabcdef0102ffffl, 0x654);
264 @Compare public long moduloPositiveNumbersMultiDigitDenom() {
265 return mod(0x7ffefc003322aabbl, 0x89ab1000l);
268 @Compare public long moduloNegativeNumbersMultiDigitDenom() {
269 return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
272 @Compare public long moduloMixedNumbersMultiDigitDenom() {
273 return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
276 @Compare public long moduloWithOverflow() {
277 return mod(0x8000fffe0000l, 0x8000ffffl);
280 @Compare public long moduloWithCorrection() {
281 return mod(0x7fff800000000000l, 0x800000000001l);
284 @Compare public long conversionFromFloatPositive() {
285 return (long) fadd(2, 0.6f);
288 @Compare public long conversionFromFloatNegative() {
289 return (long) fadd(-2, -0.6f);
292 @Compare public long conversionFromDoublePositive() {
293 return (long) dadd(0x20ffff0000L, 0.6);
296 @Compare public long conversionFromDoubleNegative() {
297 return (long) dadd(-0x20ffff0000L, -0.6);
300 @Compare public boolean divByZeroThrowsArithmeticException() {
304 } catch (final ArithmeticException e) {
309 @Compare public boolean modByZeroThrowsArithmeticException() {
313 } catch (final ArithmeticException e) {
318 @Compare public long shiftL1() {
319 return shl(0x00fa37d7763e0ca1l, 5);
322 @Compare public long shiftL2() {
323 return shl(0x00fa37d7763e0ca1l, 32);
326 @Compare public long shiftL3() {
327 return shl(0x00fa37d7763e0ca1l, 45);
330 @Compare public long shiftL4() {
331 return shl(0x00fa37d7763e0ca1l, 0);
334 @Compare public long shiftL5() {
335 return shl(0x00fa37d7763e0ca1l, 70);
338 @Compare public long shiftR1() {
339 return shr(0x00fa37d7763e0ca1l, 5);
342 @Compare public long shiftR2() {
343 return shr(0x00fa37d7763e0ca1l, 32);
346 @Compare public long shiftR3() {
347 return shr(0x00fa37d7763e0ca1l, 45);
350 @Compare public long shiftR4() {
351 return shr(0x00fa37d7763e0ca1l, 0);
354 @Compare public long shiftR5() {
355 return shr(0x00fa37d7763e0ca1l, 70);
358 @Compare public long uShiftR1() {
359 return ushr(0x00fa37d7763e0ca1l, 5);
362 @Compare public long uShiftR2() {
363 return ushr(0x00fa37d7763e0ca1l, 45);
366 @Compare public long uShiftR3() {
367 return ushr(0x00fa37d7763e0ca1l, 0);
370 @Compare public long uShiftR4() {
371 return ushr(0x00fa37d7763e0ca1l, 70);
374 @Compare public long uShiftR5() {
375 return ushr(0xf0fa37d7763e0ca1l, 5);
378 @Compare public long uShiftR6() {
379 return ushr(0xf0fa37d7763e0ca1l, 45);
382 @Compare public long uShiftR7() {
383 return ushr(0xf0fa37d7763e0ca1l, 0);
386 @Compare public long uShiftR8() {
387 return ushr(0xf0fa37d7763e0ca1l, 70);
390 @Compare public long and1() {
391 return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
394 @Compare public long or1() {
395 return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
398 @Compare public long xor1() {
399 return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
402 @Compare public long xor2() {
403 return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
406 @Compare public long xor3() {
407 return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
410 @Compare public int compareSameNumbers() {
411 return compare(0x0000000000000000l, 0x0000000000000000l, 0);
414 @Compare public int comparePositiveNumbers() {
415 return compare(0x0000000000200000l, 0x0000000010000000l, 0);
418 @Compare public int compareNegativeNumbers() {
419 return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
422 @Compare public int compareMixedNumbers() {
423 return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
426 @Compare public int intConversionProblemInRandom() {
427 long res = mul64shr64(16, 1561751147, 31);
431 @Compare public long shiftProblemInRandom() {
432 long res = mul64shr64(16, 1561751147, 31);
436 @Compare public long multiplyProblemInRandom() {
437 long res = mul64shr64(16, 1561751147, -1);
442 public static Object[] create() {
443 return VMTest.create(LongArithmeticTest.class);