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 long conversion() {
119 return Long.MAX_VALUE;
122 @Compare public long negate1() {
123 return neg(0x00fa37d7763e0ca1l);
126 @Compare public long negate2() {
127 return neg(0x80fa37d7763e0ca1l);
130 @Compare public long negate3() {
131 return neg(0xfffffffffffffeddl);
134 @Compare public long addOverflow() {
135 return add(Long.MAX_VALUE, 1l);
138 @Compare public long subUnderflow() {
139 return sub(Long.MIN_VALUE, 1l);
142 @Compare public long addMaxLongAndMaxLong() {
143 return add(Long.MAX_VALUE, Long.MAX_VALUE);
146 @Compare public long subMinLongAndMinLong() {
147 return sub(Long.MIN_VALUE, Long.MIN_VALUE);
150 @Compare public long subMinLongAndMaxLong() {
151 return sub(Long.MIN_VALUE, Long.MAX_VALUE);
154 @Compare public long multiplyMaxLong() {
155 return mul(Long.MAX_VALUE, 2l);
158 @Compare public long multiplyMaxLongAndMaxLong() {
159 return mul(Long.MAX_VALUE, Long.MAX_VALUE);
162 @Compare public long multiplyMinLong() {
163 return mul(Long.MIN_VALUE, 2l);
166 @Compare public long multiplyMinLongAndMinLong() {
167 return mul(Long.MIN_VALUE, Long.MIN_VALUE);
170 @Compare public long multiplyPrecision() {
171 return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
174 @Compare public long divideSmallPositiveNumbers() {
175 return div(0xabcdef, 0x123);
178 @Compare public long divideSmallNegativeNumbers() {
179 return div(-0xabcdef, -0x123);
182 @Compare public long divideSmallMixedNumbers() {
183 return div(0xabcdef, -0x123);
186 @Compare public long dividePositiveNumbersOneDigitDenom() {
187 return div(0xabcdef0102ffffl, 0x654);
190 @Compare public long divideNegativeNumbersOneDigitDenom() {
191 return div(-0xabcdef0102ffffl, -0x654);
194 @Compare public long divideMixedNumbersOneDigitDenom() {
195 return div(-0xabcdef0102ffffl, 0x654);
198 @Compare public long dividePositiveNumbersMultiDigitDenom() {
199 return div(0x7ffefc003322aabbl, 0x89ab1000l);
202 @Compare public long divideNegativeNumbersMultiDigitDenom() {
203 return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
206 @Compare public long divideMixedNumbersMultiDigitDenom() {
207 return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
210 @Compare public long divideWithOverflow() {
211 return div(0x8000fffe0000l, 0x8000ffffl);
214 @Compare public long divideWithCorrection() {
215 return div(0x7fff800000000000l, 0x800000000001l);
218 @Compare public long moduloSmallPositiveNumbers() {
219 return mod(0xabcdef, 0x123);
222 @Compare public long moduloSmallNegativeNumbers() {
223 return mod(-0xabcdef, -0x123);
226 @Compare public long moduloSmallMixedNumbers() {
227 return mod(0xabcdef, -0x123);
230 @Compare public long moduloPositiveNumbersOneDigitDenom() {
231 return mod(0xabcdef0102ffffl, 0x654);
234 @Compare public long moduloNegativeNumbersOneDigitDenom() {
235 return mod(-0xabcdef0102ffffl, -0x654);
238 @Compare public long moduloMixedNumbersOneDigitDenom() {
239 return mod(-0xabcdef0102ffffl, 0x654);
242 @Compare public long moduloPositiveNumbersMultiDigitDenom() {
243 return mod(0x7ffefc003322aabbl, 0x89ab1000l);
246 @Compare public long moduloNegativeNumbersMultiDigitDenom() {
247 return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
250 @Compare public long moduloMixedNumbersMultiDigitDenom() {
251 return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
254 @Compare public long moduloWithOverflow() {
255 return mod(0x8000fffe0000l, 0x8000ffffl);
258 @Compare public long moduloWithCorrection() {
259 return mod(0x7fff800000000000l, 0x800000000001l);
262 @Compare public long conversionFromFloatPositive() {
263 return (long) fadd(2, 0.6f);
266 @Compare public long conversionFromFloatNegative() {
267 return (long) fadd(-2, -0.6f);
270 @Compare public long conversionFromDoublePositive() {
271 return (long) dadd(0x20ffff0000L, 0.6);
274 @Compare public long conversionFromDoubleNegative() {
275 return (long) dadd(-0x20ffff0000L, -0.6);
278 @Compare public boolean divByZeroThrowsArithmeticException() {
282 } catch (final ArithmeticException e) {
287 @Compare public boolean modByZeroThrowsArithmeticException() {
291 } catch (final ArithmeticException e) {
296 @Compare public long shiftL1() {
297 return shl(0x00fa37d7763e0ca1l, 5);
300 @Compare public long shiftL2() {
301 return shl(0x00fa37d7763e0ca1l, 32);
304 @Compare public long shiftL3() {
305 return shl(0x00fa37d7763e0ca1l, 45);
308 @Compare public long shiftR1() {
309 return shr(0x00fa37d7763e0ca1l, 5);
312 @Compare public long shiftR2() {
313 return shr(0x00fa37d7763e0ca1l, 32);
316 @Compare public long shiftR3() {
317 return shr(0x00fa37d7763e0ca1l, 45);
320 @Compare public long uShiftR1() {
321 return ushr(0x00fa37d7763e0ca1l, 5);
324 @Compare public long uShiftR2() {
325 return ushr(0x00fa37d7763e0ca1l, 45);
328 @Compare public long uShiftR3() {
329 return ushr(0xf0fa37d7763e0ca1l, 5);
332 @Compare public long uShiftR4() {
333 return ushr(0xf0fa37d7763e0ca1l, 45);
336 @Compare public long and1() {
337 return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
340 @Compare public long or1() {
341 return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
344 @Compare public long xor1() {
345 return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
348 @Compare public long xor2() {
349 return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
352 @Compare public long xor3() {
353 return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
356 @Compare public int compareSameNumbers() {
357 return compare(0x0000000000000000l, 0x0000000000000000l, 0);
360 @Compare public int comparePositiveNumbers() {
361 return compare(0x0000000000200000l, 0x0000000010000000l, 0);
364 @Compare public int compareNegativeNumbers() {
365 return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
368 @Compare public int compareMixedNumbers() {
369 return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
373 public static Object[] create() {
374 return VMTest.create(LongArithmeticTest.class);