rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/ByteArithmeticTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 19 Nov 2014 19:32:00 +0100
changeset 1723 3a1f262311cf
parent 1034 rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ByteArithmeticTest.java@28dc692f3b11
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Separating compact profile tests into own project, so launcher.http can depend on compact profile JAR
Martin@440
     1
/**
Martin@440
     2
 * Back 2 Browser Bytecode Translator
Martin@440
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Martin@440
     4
 *
Martin@440
     5
 * This program is free software: you can redistribute it and/or modify
Martin@440
     6
 * it under the terms of the GNU General Public License as published by
Martin@440
     7
 * the Free Software Foundation, version 2 of the License.
Martin@440
     8
 *
Martin@440
     9
 * This program is distributed in the hope that it will be useful,
Martin@440
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Martin@440
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Martin@440
    12
 * GNU General Public License for more details.
Martin@440
    13
 *
Martin@440
    14
 * You should have received a copy of the GNU General Public License
Martin@440
    15
 * along with this program. Look for COPYING file in the top folder.
Martin@440
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
Martin@440
    17
 */
Martin@440
    18
package org.apidesign.bck2brwsr.tck;
Martin@440
    19
jaroslav@864
    20
import org.apidesign.bck2brwsr.core.JavaScriptBody;
Martin@440
    21
import org.apidesign.bck2brwsr.vmtest.Compare;
Martin@440
    22
import org.apidesign.bck2brwsr.vmtest.VMTest;
Martin@440
    23
import org.testng.annotations.Factory;
Martin@440
    24
Martin@440
    25
/**
Martin@440
    26
 *
Martin@440
    27
 * @author Jaroslav Tulach <jtulach@netbeans.org>
Martin@440
    28
 */
Martin@440
    29
public class ByteArithmeticTest {
Martin@440
    30
    
Martin@440
    31
    private static byte add(byte x, byte y) {
Martin@440
    32
        return (byte)(x + y);
Martin@440
    33
    }
Martin@440
    34
    
Martin@440
    35
    private static byte sub(byte x, byte y) {
Martin@440
    36
        return (byte)(x - y);
Martin@440
    37
    }
Martin@440
    38
    
Martin@440
    39
    private static byte mul(byte x, byte y) {
Martin@440
    40
        return (byte)(x * y);
Martin@440
    41
    }
Martin@440
    42
    
Martin@440
    43
    private static byte div(byte x, byte y) {
Martin@440
    44
        return (byte)(x / y);
Martin@440
    45
    }
Martin@440
    46
    
Martin@440
    47
    private static byte mod(byte x, byte y) {
Martin@440
    48
        return (byte)(x % y);
Martin@440
    49
    }
Martin@440
    50
    
Martin@440
    51
    @Compare public byte conversion() {
Martin@440
    52
        return (byte)123456;
Martin@440
    53
    }
Martin@440
    54
    
Martin@440
    55
    @Compare public byte addOverflow() {
Martin@440
    56
        return add(Byte.MAX_VALUE, (byte)1);
Martin@440
    57
    }
Martin@440
    58
    
Martin@440
    59
    @Compare public byte subUnderflow() {
Martin@440
    60
        return sub(Byte.MIN_VALUE, (byte)1);
Martin@440
    61
    }
Martin@440
    62
    
Martin@440
    63
    @Compare public byte addMaxByteAndMaxByte() {
Martin@440
    64
        return add(Byte.MAX_VALUE, Byte.MAX_VALUE);
Martin@440
    65
    }
Martin@440
    66
    
Martin@440
    67
    @Compare public byte subMinByteAndMinByte() {
Martin@440
    68
        return sub(Byte.MIN_VALUE, Byte.MIN_VALUE);
Martin@440
    69
    }
Martin@440
    70
    
Martin@440
    71
    @Compare public byte multiplyMaxByte() {
Martin@440
    72
        return mul(Byte.MAX_VALUE, (byte)2);
Martin@440
    73
    }
Martin@440
    74
    
Martin@440
    75
    @Compare public byte multiplyMaxByteAndMaxByte() {
Martin@440
    76
        return mul(Byte.MAX_VALUE, Byte.MAX_VALUE);
Martin@440
    77
    }
Martin@440
    78
    
Martin@440
    79
    @Compare public byte multiplyMinByte() {
Martin@440
    80
        return mul(Byte.MIN_VALUE, (byte)2);
Martin@440
    81
    }
Martin@440
    82
    
Martin@440
    83
    @Compare public byte multiplyMinByteAndMinByte() {
Martin@440
    84
        return mul(Byte.MIN_VALUE, Byte.MIN_VALUE);
Martin@440
    85
    }
Martin@440
    86
    
Martin@440
    87
    @Compare public byte multiplyPrecision() {
Martin@440
    88
        return mul((byte)17638, (byte)1103);
Martin@440
    89
    }
Martin@440
    90
    
Martin@440
    91
    @Compare public byte division() {
Martin@440
    92
        return div((byte)1, (byte)2);
Martin@440
    93
    }
Martin@440
    94
    
Martin@440
    95
    @Compare public byte divisionReminder() {
Martin@440
    96
        return mod((byte)1, (byte)2);
Martin@440
    97
    }
jaroslav@864
    98
jaroslav@864
    99
    private static int readShort(byte[] byteCodes, int offset) {
jaroslav@864
   100
        int signed = byteCodes[offset];
jaroslav@864
   101
        byte b0 = (byte)signed;
jaroslav@864
   102
        return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
jaroslav@864
   103
    }
jaroslav@864
   104
    
jaroslav@864
   105
    private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
jaroslav@864
   106
        return readShort(byteCodes, offsetInstruction + 1);
jaroslav@864
   107
    }
jaroslav@864
   108
    
jaroslav@864
   109
    @Compare public int readIntArgs255and156() {
jaroslav@864
   110
        final byte[] arr = new byte[] { (byte)0, (byte)255, (byte)156 };
jaroslav@864
   111
        
jaroslav@864
   112
        assert arr[1] == -1 : "First byte: " + arr[1];
jaroslav@864
   113
        assert arr[2] == -100 : "Second byte: " + arr[2];
jaroslav@864
   114
        final int ret = readShortArg(arr, 0);
jaroslav@864
   115
        assert ret < 65000: "Value: " + ret;
jaroslav@864
   116
        return ret;
jaroslav@864
   117
    }
jaroslav@864
   118
    
jaroslav@864
   119
    @JavaScriptBody(args = { "arr" }, body = "arr[1] = 255; arr[2] = 156; return arr;")
jaroslav@864
   120
    private static byte[] fill255and156(byte[] arr) {
jaroslav@864
   121
        arr[1] = (byte)255;
jaroslav@864
   122
        arr[2] = (byte)156;
jaroslav@864
   123
        return arr;
jaroslav@864
   124
    }
jaroslav@864
   125
jaroslav@864
   126
    @Compare public int readIntArgs255and156JSArray() {
jaroslav@864
   127
        final byte[] arr = fill255and156(new byte[] { 0, 0, 0 });
jaroslav@864
   128
        
jaroslav@864
   129
        final int ret = readShortArg(arr, 0);
jaroslav@864
   130
        assert ret < 65000: "Value: " + ret;
jaroslav@864
   131
        return ret;
jaroslav@864
   132
    }
jaroslav@864
   133
    
jaroslav@864
   134
    @Compare public int readIntArgsMinus1andMinus100() {
jaroslav@864
   135
        final byte[] arr = new byte[] { (byte)0, (byte)-1, (byte)-100 };
jaroslav@864
   136
        
jaroslav@864
   137
        assert arr[1] == -1 : "First byte: " + arr[1];
jaroslav@864
   138
        assert arr[2] == -100 : "Second byte: " + arr[2];
jaroslav@864
   139
        
jaroslav@864
   140
        return readShortArg(arr, 0);
jaroslav@864
   141
    }
Martin@440
   142
    
Martin@440
   143
    @Factory
Martin@440
   144
    public static Object[] create() {
Martin@440
   145
        return VMTest.create(ByteArithmeticTest.class);
Martin@440
   146
    }
Martin@440
   147
}