Martin@345: /** Martin@345: * Back 2 Browser Bytecode Translator Martin@345: * Copyright (C) 2012 Jaroslav Tulach Martin@345: * Martin@345: * This program is free software: you can redistribute it and/or modify Martin@345: * it under the terms of the GNU General Public License as published by Martin@345: * the Free Software Foundation, version 2 of the License. Martin@345: * Martin@345: * This program is distributed in the hope that it will be useful, Martin@345: * but WITHOUT ANY WARRANTY; without even the implied warranty of Martin@345: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Martin@345: * GNU General Public License for more details. Martin@345: * Martin@345: * You should have received a copy of the GNU General Public License Martin@345: * along with this program. Look for COPYING file in the top folder. Martin@345: * If not, see http://opensource.org/licenses/GPL-2.0. Martin@345: */ Martin@427: package org.apidesign.bck2brwsr.tck; Martin@345: Martin@427: import org.apidesign.bck2brwsr.vmtest.Compare; Martin@427: import org.apidesign.bck2brwsr.vmtest.VMTest; Martin@345: import org.testng.annotations.Factory; Martin@345: Martin@345: /** Martin@345: * Martin@345: * @author Jaroslav Tulach Martin@345: */ Martin@345: public class IntegerArithmeticTest { Martin@345: Martin@351: private static int add(int x, int y) { Martin@351: return x + y; Martin@345: } Martin@345: Martin@351: private static int sub(int x, int y) { Martin@351: return x - y; Martin@345: } Martin@345: Martin@351: private static int mul(int x, int y) { Martin@351: return x * y; Martin@351: } Martin@351: Martin@352: private static int div(int x, int y) { Martin@352: return x / y; Martin@352: } Martin@352: Martin@352: private static int mod(int x, int y) { Martin@352: return x % y; Martin@352: } Martin@352: Martin@700: private static int neg(int x) { Martin@700: return (-x); Martin@700: } Martin@700: Martin@351: @Compare public int addOverflow() { Martin@351: return add(Integer.MAX_VALUE, 1); Martin@351: } Martin@351: Martin@351: @Compare public int subUnderflow() { Martin@351: return sub(Integer.MIN_VALUE, 1); Martin@351: } Martin@351: Martin@351: @Compare public int addMaxIntAndMaxInt() { Martin@351: return add(Integer.MAX_VALUE, Integer.MAX_VALUE); Martin@351: } Martin@351: Martin@351: @Compare public int subMinIntAndMinInt() { Martin@351: return sub(Integer.MIN_VALUE, Integer.MIN_VALUE); Martin@345: } Martin@345: Martin@345: @Compare public int multiplyMaxInt() { Martin@351: return mul(Integer.MAX_VALUE, 2); Martin@351: } Martin@351: Martin@351: @Compare public int multiplyMaxIntAndMaxInt() { Martin@351: return mul(Integer.MAX_VALUE, Integer.MAX_VALUE); Martin@351: } Martin@351: Martin@351: @Compare public int multiplyMinInt() { Martin@351: return mul(Integer.MIN_VALUE, 2); Martin@351: } Martin@351: Martin@351: @Compare public int multiplyMinIntAndMinInt() { Martin@351: return mul(Integer.MIN_VALUE, Integer.MIN_VALUE); Martin@351: } Martin@351: Martin@351: @Compare public int multiplyPrecision() { Martin@351: return mul(119106029, 1103515245); Martin@345: } Martin@345: Martin@352: @Compare public int division() { Martin@352: return div(1, 2); Martin@352: } Martin@352: Martin@352: @Compare public int divisionReminder() { Martin@352: return mod(1, 2); Martin@352: } lubomir@737: lubomir@737: @Compare public int negativeDivision() { lubomir@737: return div(-7, 3); lubomir@737: } lubomir@737: lubomir@737: @Compare public int negativeDivisionReminder() { lubomir@737: return mod(-7, 3); lubomir@737: } lubomir@737: lubomir@737: @Compare public boolean divByZeroThrowsArithmeticException() { lubomir@737: try { lubomir@737: div(1, 0); lubomir@737: return false; lubomir@737: } catch (final ArithmeticException e) { lubomir@737: return true; lubomir@737: } lubomir@737: } lubomir@737: lubomir@737: @Compare public boolean modByZeroThrowsArithmeticException() { lubomir@737: try { lubomir@737: mod(1, 0); lubomir@737: return false; lubomir@737: } catch (final ArithmeticException e) { lubomir@737: return true; lubomir@737: } lubomir@737: } lubomir@737: Martin@700: @Compare public int negate() { Martin@700: return neg(123456); Martin@700: } Martin@700: Martin@700: @Compare public int negateMaxInt() { Martin@700: return neg(Integer.MAX_VALUE); Martin@700: } Martin@700: Martin@700: @Compare public int negateMinInt() { Martin@700: return neg(Integer.MIN_VALUE); Martin@700: } Martin@700: jaroslav@454: @Compare public int sumTwoDimensions() { jaroslav@454: int[][] matrix = createMatrix(4, 3); jaroslav@457: matrix[0][0] += 10; jaroslav@457: return matrix[0][0]; jaroslav@454: } jaroslav@454: jaroslav@454: static int[][] createMatrix(int x, int y) { jaroslav@457: return new int[x][y]; jaroslav@454: } jaroslav@454: Martin@345: @Factory Martin@345: public static Object[] create() { Martin@427: return VMTest.create(IntegerArithmeticTest.class); Martin@345: } Martin@345: }