rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ByteArithmeticTest.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.core.JavaScriptBody;
21 import org.apidesign.bck2brwsr.vmtest.Compare;
22 import org.apidesign.bck2brwsr.vmtest.VMTest;
23 import org.testng.annotations.Factory;
27 * @author Jaroslav Tulach <jtulach@netbeans.org>
29 public class ByteArithmeticTest {
31 private static byte add(byte x, byte y) {
35 private static byte sub(byte x, byte y) {
39 private static byte mul(byte x, byte y) {
43 private static byte div(byte x, byte y) {
47 private static byte mod(byte x, byte y) {
51 @Compare public byte conversion() {
55 @Compare public byte addOverflow() {
56 return add(Byte.MAX_VALUE, (byte)1);
59 @Compare public byte subUnderflow() {
60 return sub(Byte.MIN_VALUE, (byte)1);
63 @Compare public byte addMaxByteAndMaxByte() {
64 return add(Byte.MAX_VALUE, Byte.MAX_VALUE);
67 @Compare public byte subMinByteAndMinByte() {
68 return sub(Byte.MIN_VALUE, Byte.MIN_VALUE);
71 @Compare public byte multiplyMaxByte() {
72 return mul(Byte.MAX_VALUE, (byte)2);
75 @Compare public byte multiplyMaxByteAndMaxByte() {
76 return mul(Byte.MAX_VALUE, Byte.MAX_VALUE);
79 @Compare public byte multiplyMinByte() {
80 return mul(Byte.MIN_VALUE, (byte)2);
83 @Compare public byte multiplyMinByteAndMinByte() {
84 return mul(Byte.MIN_VALUE, Byte.MIN_VALUE);
87 @Compare public byte multiplyPrecision() {
88 return mul((byte)17638, (byte)1103);
91 @Compare public byte division() {
92 return div((byte)1, (byte)2);
95 @Compare public byte divisionReminder() {
96 return mod((byte)1, (byte)2);
99 private static int readShort(byte[] byteCodes, int offset) {
100 int signed = byteCodes[offset];
101 byte b0 = (byte)signed;
102 return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
105 private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
106 return readShort(byteCodes, offsetInstruction + 1);
109 @Compare public int readIntArgs255and156() {
110 final byte[] arr = new byte[] { (byte)0, (byte)255, (byte)156 };
112 assert arr[1] == -1 : "First byte: " + arr[1];
113 assert arr[2] == -100 : "Second byte: " + arr[2];
114 final int ret = readShortArg(arr, 0);
115 assert ret < 65000: "Value: " + ret;
119 @JavaScriptBody(args = { "arr" }, body = "arr[1] = 255; arr[2] = 156; return arr;")
120 private static byte[] fill255and156(byte[] arr) {
126 @Compare public int readIntArgs255and156JSArray() {
127 final byte[] arr = fill255and156(new byte[] { 0, 0, 0 });
129 final int ret = readShortArg(arr, 0);
130 assert ret < 65000: "Value: " + ret;
134 @Compare public int readIntArgsMinus1andMinus100() {
135 final byte[] arr = new byte[] { (byte)0, (byte)-1, (byte)-100 };
137 assert arr[1] == -1 : "First byte: " + arr[1];
138 assert arr[2] == -100 : "Second byte: " + arr[2];
140 return readShortArg(arr, 0);
144 public static Object[] create() {
145 return VMTest.create(ByteArithmeticTest.class);