Martin@440: /** Martin@440: * Back 2 Browser Bytecode Translator Martin@440: * Copyright (C) 2012 Jaroslav Tulach Martin@440: * Martin@440: * This program is free software: you can redistribute it and/or modify Martin@440: * it under the terms of the GNU General Public License as published by Martin@440: * the Free Software Foundation, version 2 of the License. Martin@440: * Martin@440: * This program is distributed in the hope that it will be useful, Martin@440: * but WITHOUT ANY WARRANTY; without even the implied warranty of Martin@440: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Martin@440: * GNU General Public License for more details. Martin@440: * Martin@440: * You should have received a copy of the GNU General Public License Martin@440: * along with this program. Look for COPYING file in the top folder. Martin@440: * If not, see http://opensource.org/licenses/GPL-2.0. Martin@440: */ Martin@440: package org.apidesign.bck2brwsr.tck; Martin@440: jaroslav@864: import org.apidesign.bck2brwsr.core.JavaScriptBody; Martin@440: import org.apidesign.bck2brwsr.vmtest.Compare; Martin@440: import org.apidesign.bck2brwsr.vmtest.VMTest; Martin@440: import org.testng.annotations.Factory; Martin@440: Martin@440: /** Martin@440: * Martin@440: * @author Jaroslav Tulach Martin@440: */ Martin@440: public class ByteArithmeticTest { Martin@440: Martin@440: private static byte add(byte x, byte y) { Martin@440: return (byte)(x + y); Martin@440: } Martin@440: Martin@440: private static byte sub(byte x, byte y) { Martin@440: return (byte)(x - y); Martin@440: } Martin@440: Martin@440: private static byte mul(byte x, byte y) { Martin@440: return (byte)(x * y); Martin@440: } Martin@440: Martin@440: private static byte div(byte x, byte y) { Martin@440: return (byte)(x / y); Martin@440: } Martin@440: Martin@440: private static byte mod(byte x, byte y) { Martin@440: return (byte)(x % y); Martin@440: } Martin@440: Martin@440: @Compare public byte conversion() { Martin@440: return (byte)123456; Martin@440: } Martin@440: Martin@440: @Compare public byte addOverflow() { Martin@440: return add(Byte.MAX_VALUE, (byte)1); Martin@440: } Martin@440: Martin@440: @Compare public byte subUnderflow() { Martin@440: return sub(Byte.MIN_VALUE, (byte)1); Martin@440: } Martin@440: Martin@440: @Compare public byte addMaxByteAndMaxByte() { Martin@440: return add(Byte.MAX_VALUE, Byte.MAX_VALUE); Martin@440: } Martin@440: Martin@440: @Compare public byte subMinByteAndMinByte() { Martin@440: return sub(Byte.MIN_VALUE, Byte.MIN_VALUE); Martin@440: } Martin@440: Martin@440: @Compare public byte multiplyMaxByte() { Martin@440: return mul(Byte.MAX_VALUE, (byte)2); Martin@440: } Martin@440: Martin@440: @Compare public byte multiplyMaxByteAndMaxByte() { Martin@440: return mul(Byte.MAX_VALUE, Byte.MAX_VALUE); Martin@440: } Martin@440: Martin@440: @Compare public byte multiplyMinByte() { Martin@440: return mul(Byte.MIN_VALUE, (byte)2); Martin@440: } Martin@440: Martin@440: @Compare public byte multiplyMinByteAndMinByte() { Martin@440: return mul(Byte.MIN_VALUE, Byte.MIN_VALUE); Martin@440: } Martin@440: Martin@440: @Compare public byte multiplyPrecision() { Martin@440: return mul((byte)17638, (byte)1103); Martin@440: } Martin@440: Martin@440: @Compare public byte division() { Martin@440: return div((byte)1, (byte)2); Martin@440: } Martin@440: Martin@440: @Compare public byte divisionReminder() { Martin@440: return mod((byte)1, (byte)2); Martin@440: } jaroslav@864: jaroslav@864: private static int readShort(byte[] byteCodes, int offset) { jaroslav@864: int signed = byteCodes[offset]; jaroslav@864: byte b0 = (byte)signed; jaroslav@864: return (b0 << 8) | (byteCodes[offset + 1] & 0xff); jaroslav@864: } jaroslav@864: jaroslav@864: private static int readShortArg(byte[] byteCodes, int offsetInstruction) { jaroslav@864: return readShort(byteCodes, offsetInstruction + 1); jaroslav@864: } jaroslav@864: jaroslav@864: @Compare public int readIntArgs255and156() { jaroslav@864: final byte[] arr = new byte[] { (byte)0, (byte)255, (byte)156 }; jaroslav@864: jaroslav@864: assert arr[1] == -1 : "First byte: " + arr[1]; jaroslav@864: assert arr[2] == -100 : "Second byte: " + arr[2]; jaroslav@864: final int ret = readShortArg(arr, 0); jaroslav@864: assert ret < 65000: "Value: " + ret; jaroslav@864: return ret; jaroslav@864: } jaroslav@864: jaroslav@864: @JavaScriptBody(args = { "arr" }, body = "arr[1] = 255; arr[2] = 156; return arr;") jaroslav@864: private static byte[] fill255and156(byte[] arr) { jaroslav@864: arr[1] = (byte)255; jaroslav@864: arr[2] = (byte)156; jaroslav@864: return arr; jaroslav@864: } jaroslav@864: jaroslav@864: @Compare public int readIntArgs255and156JSArray() { jaroslav@864: final byte[] arr = fill255and156(new byte[] { 0, 0, 0 }); jaroslav@864: jaroslav@864: final int ret = readShortArg(arr, 0); jaroslav@864: assert ret < 65000: "Value: " + ret; jaroslav@864: return ret; jaroslav@864: } jaroslav@864: jaroslav@864: @Compare public int readIntArgsMinus1andMinus100() { jaroslav@864: final byte[] arr = new byte[] { (byte)0, (byte)-1, (byte)-100 }; jaroslav@864: jaroslav@864: assert arr[1] == -1 : "First byte: " + arr[1]; jaroslav@864: assert arr[2] == -100 : "Second byte: " + arr[2]; jaroslav@864: jaroslav@864: return readShortArg(arr, 0); jaroslav@864: } Martin@440: Martin@440: @Factory Martin@440: public static Object[] create() { Martin@440: return VMTest.create(ByteArithmeticTest.class); Martin@440: } Martin@440: }