rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ByteArithmeticTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 28 Apr 2013 10:26:08 +0200
branchmodel
changeset 1034 28dc692f3b11
parent 864 rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ByteArithmeticTest.java@8ece59d31fcf
permissions -rw-r--r--
Moving the VM compatibility tests to emul.compact
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.tck;
    19 
    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;
    24 
    25 /**
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 public class ByteArithmeticTest {
    30     
    31     private static byte add(byte x, byte y) {
    32         return (byte)(x + y);
    33     }
    34     
    35     private static byte sub(byte x, byte y) {
    36         return (byte)(x - y);
    37     }
    38     
    39     private static byte mul(byte x, byte y) {
    40         return (byte)(x * y);
    41     }
    42     
    43     private static byte div(byte x, byte y) {
    44         return (byte)(x / y);
    45     }
    46     
    47     private static byte mod(byte x, byte y) {
    48         return (byte)(x % y);
    49     }
    50     
    51     @Compare public byte conversion() {
    52         return (byte)123456;
    53     }
    54     
    55     @Compare public byte addOverflow() {
    56         return add(Byte.MAX_VALUE, (byte)1);
    57     }
    58     
    59     @Compare public byte subUnderflow() {
    60         return sub(Byte.MIN_VALUE, (byte)1);
    61     }
    62     
    63     @Compare public byte addMaxByteAndMaxByte() {
    64         return add(Byte.MAX_VALUE, Byte.MAX_VALUE);
    65     }
    66     
    67     @Compare public byte subMinByteAndMinByte() {
    68         return sub(Byte.MIN_VALUE, Byte.MIN_VALUE);
    69     }
    70     
    71     @Compare public byte multiplyMaxByte() {
    72         return mul(Byte.MAX_VALUE, (byte)2);
    73     }
    74     
    75     @Compare public byte multiplyMaxByteAndMaxByte() {
    76         return mul(Byte.MAX_VALUE, Byte.MAX_VALUE);
    77     }
    78     
    79     @Compare public byte multiplyMinByte() {
    80         return mul(Byte.MIN_VALUE, (byte)2);
    81     }
    82     
    83     @Compare public byte multiplyMinByteAndMinByte() {
    84         return mul(Byte.MIN_VALUE, Byte.MIN_VALUE);
    85     }
    86     
    87     @Compare public byte multiplyPrecision() {
    88         return mul((byte)17638, (byte)1103);
    89     }
    90     
    91     @Compare public byte division() {
    92         return div((byte)1, (byte)2);
    93     }
    94     
    95     @Compare public byte divisionReminder() {
    96         return mod((byte)1, (byte)2);
    97     }
    98 
    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);
   103     }
   104     
   105     private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
   106         return readShort(byteCodes, offsetInstruction + 1);
   107     }
   108     
   109     @Compare public int readIntArgs255and156() {
   110         final byte[] arr = new byte[] { (byte)0, (byte)255, (byte)156 };
   111         
   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;
   116         return ret;
   117     }
   118     
   119     @JavaScriptBody(args = { "arr" }, body = "arr[1] = 255; arr[2] = 156; return arr;")
   120     private static byte[] fill255and156(byte[] arr) {
   121         arr[1] = (byte)255;
   122         arr[2] = (byte)156;
   123         return arr;
   124     }
   125 
   126     @Compare public int readIntArgs255and156JSArray() {
   127         final byte[] arr = fill255and156(new byte[] { 0, 0, 0 });
   128         
   129         final int ret = readShortArg(arr, 0);
   130         assert ret < 65000: "Value: " + ret;
   131         return ret;
   132     }
   133     
   134     @Compare public int readIntArgsMinus1andMinus100() {
   135         final byte[] arr = new byte[] { (byte)0, (byte)-1, (byte)-100 };
   136         
   137         assert arr[1] == -1 : "First byte: " + arr[1];
   138         assert arr[2] == -100 : "Second byte: " + arr[2];
   139         
   140         return readShortArg(arr, 0);
   141     }
   142     
   143     @Factory
   144     public static Object[] create() {
   145         return VMTest.create(ByteArithmeticTest.class);
   146     }
   147 }