rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 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 public static int compare(long x, long y, int zero) {
87 final int xyResult = compareL(x, y, zero);
88 final int yxResult = compareL(y, x, zero);
90 return ((xyResult + yxResult) == 0) ? xyResult : -2;
93 private static int compareL(long x, long y, int zero) {
115 return (trueCount == 1) ? result : -2;
118 @Compare public int parameterSlotCount() {
119 long argCounts = 281479271874563L;
120 int x = unpack(argCounts, 2);
123 private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d
125 final long val = packed >> ((3-word) * 16);
128 @Compare public long conversion() {
129 return Long.MAX_VALUE;
132 @Compare public long negate1() {
133 return neg(0x00fa37d7763e0ca1l);
136 @Compare public long negate2() {
137 return neg(0x80fa37d7763e0ca1l);
140 @Compare public long negate3() {
141 return neg(0xfffffffffffffeddl);
144 @Compare public long addOverflow() {
145 return add(Long.MAX_VALUE, 1l);
148 @Compare public long subUnderflow() {
149 return sub(Long.MIN_VALUE, 1l);
152 @Compare public long addMaxLongAndMaxLong() {
153 return add(Long.MAX_VALUE, Long.MAX_VALUE);
156 @Compare public long subMinLongAndMinLong() {
157 return sub(Long.MIN_VALUE, Long.MIN_VALUE);
160 @Compare public long subMinLongAndMaxLong() {
161 return sub(Long.MIN_VALUE, Long.MAX_VALUE);
164 @Compare public long multiplyMaxLong() {
165 return mul(Long.MAX_VALUE, 2l);
168 @Compare public long multiplyMaxLongAndMaxLong() {
169 return mul(Long.MAX_VALUE, Long.MAX_VALUE);
172 @Compare public long multiplyMinLong() {
173 return mul(Long.MIN_VALUE, 2l);
176 @Compare public long multiplyMinLongAndMinLong() {
177 return mul(Long.MIN_VALUE, Long.MIN_VALUE);
180 @Compare public long multiplyPrecision() {
181 return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
184 @Compare public long divideSmallPositiveNumbers() {
185 return div(0xabcdef, 0x123);
188 @Compare public long divideSmallNegativeNumbers() {
189 return div(-0xabcdef, -0x123);
192 @Compare public long divideSmallMixedNumbers() {
193 return div(0xabcdef, -0x123);
196 @Compare public long dividePositiveNumbersOneDigitDenom() {
197 return div(0xabcdef0102ffffl, 0x654);
200 @Compare public long divideNegativeNumbersOneDigitDenom() {
201 return div(-0xabcdef0102ffffl, -0x654);
204 @Compare public long divideMixedNumbersOneDigitDenom() {
205 return div(-0xabcdef0102ffffl, 0x654);
208 @Compare public long dividePositiveNumbersMultiDigitDenom() {
209 return div(0x7ffefc003322aabbl, 0x89ab1000l);
212 @Compare public long divideNegativeNumbersMultiDigitDenom() {
213 return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
216 @Compare public long divideMixedNumbersMultiDigitDenom() {
217 return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
220 @Compare public long divideWithOverflow() {
221 return div(0x8000fffe0000l, 0x8000ffffl);
224 @Compare public long divideWithCorrection() {
225 return div(0x7fff800000000000l, 0x800000000001l);
228 @Compare public long moduloSmallPositiveNumbers() {
229 return mod(0xabcdef, 0x123);
232 @Compare public long moduloSmallNegativeNumbers() {
233 return mod(-0xabcdef, -0x123);
236 @Compare public long moduloSmallMixedNumbers() {
237 return mod(0xabcdef, -0x123);
240 @Compare public long moduloPositiveNumbersOneDigitDenom() {
241 return mod(0xabcdef0102ffffl, 0x654);
244 @Compare public long moduloNegativeNumbersOneDigitDenom() {
245 return mod(-0xabcdef0102ffffl, -0x654);
248 @Compare public long moduloMixedNumbersOneDigitDenom() {
249 return mod(-0xabcdef0102ffffl, 0x654);
252 @Compare public long moduloPositiveNumbersMultiDigitDenom() {
253 return mod(0x7ffefc003322aabbl, 0x89ab1000l);
256 @Compare public long moduloNegativeNumbersMultiDigitDenom() {
257 return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
260 @Compare public long moduloMixedNumbersMultiDigitDenom() {
261 return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
264 @Compare public long moduloWithOverflow() {
265 return mod(0x8000fffe0000l, 0x8000ffffl);
268 @Compare public long moduloWithCorrection() {
269 return mod(0x7fff800000000000l, 0x800000000001l);
272 @Compare public long conversionFromFloatPositive() {
273 return (long) fadd(2, 0.6f);
276 @Compare public long conversionFromFloatNegative() {
277 return (long) fadd(-2, -0.6f);
280 @Compare public long conversionFromDoublePositive() {
281 return (long) dadd(0x20ffff0000L, 0.6);
284 @Compare public long conversionFromDoubleNegative() {
285 return (long) dadd(-0x20ffff0000L, -0.6);
288 @Compare public boolean divByZeroThrowsArithmeticException() {
292 } catch (final ArithmeticException e) {
297 @Compare public boolean modByZeroThrowsArithmeticException() {
301 } catch (final ArithmeticException e) {
306 @Compare public long shiftL1() {
307 return shl(0x00fa37d7763e0ca1l, 5);
310 @Compare public long shiftL2() {
311 return shl(0x00fa37d7763e0ca1l, 32);
314 @Compare public long shiftL3() {
315 return shl(0x00fa37d7763e0ca1l, 45);
318 @Compare public long shiftL4() {
319 return shl(0x00fa37d7763e0ca1l, 0);
322 @Compare public long shiftL5() {
323 return shl(0x00fa37d7763e0ca1l, 70);
326 @Compare public long shiftR1() {
327 return shr(0x00fa37d7763e0ca1l, 5);
330 @Compare public long shiftR2() {
331 return shr(0x00fa37d7763e0ca1l, 32);
334 @Compare public long shiftR3() {
335 return shr(0x00fa37d7763e0ca1l, 45);
338 @Compare public long shiftR4() {
339 return shr(0x00fa37d7763e0ca1l, 0);
342 @Compare public long shiftR5() {
343 return shr(0x00fa37d7763e0ca1l, 70);
346 @Compare public long uShiftR1() {
347 return ushr(0x00fa37d7763e0ca1l, 5);
350 @Compare public long uShiftR2() {
351 return ushr(0x00fa37d7763e0ca1l, 45);
354 @Compare public long uShiftR3() {
355 return ushr(0x00fa37d7763e0ca1l, 0);
358 @Compare public long uShiftR4() {
359 return ushr(0x00fa37d7763e0ca1l, 70);
362 @Compare public long uShiftR5() {
363 return ushr(0xf0fa37d7763e0ca1l, 5);
366 @Compare public long uShiftR6() {
367 return ushr(0xf0fa37d7763e0ca1l, 45);
370 @Compare public long uShiftR7() {
371 return ushr(0xf0fa37d7763e0ca1l, 0);
374 @Compare public long uShiftR8() {
375 return ushr(0xf0fa37d7763e0ca1l, 70);
378 @Compare public long and1() {
379 return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
382 @Compare public long or1() {
383 return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
386 @Compare public long xor1() {
387 return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
390 @Compare public long xor2() {
391 return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
394 @Compare public long xor3() {
395 return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
398 @Compare public int compareSameNumbers() {
399 return compare(0x0000000000000000l, 0x0000000000000000l, 0);
402 @Compare public int comparePositiveNumbers() {
403 return compare(0x0000000000200000l, 0x0000000010000000l, 0);
406 @Compare public int compareNegativeNumbers() {
407 return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
410 @Compare public int compareMixedNumbers() {
411 return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
415 public static Object[] create() {
416 return VMTest.create(LongArithmeticTest.class);