Byte arithmetic - added tests + conversion from int->byte. arithmetic
authorMartin Soch <Martin.Soch@oracle.com>
Mon, 14 Jan 2013 09:47:38 +0100
brancharithmetic
changeset 440aa50464da62d
parent 439 e4fb6c7ac42a
child 441 7569db829afa
Byte arithmetic - added tests + conversion from int->byte.
emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_Number.js
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ByteArithmeticTest.java
     1.1 --- a/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_Number.js	Mon Jan 14 09:17:18 2013 +0100
     1.2 +++ b/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_Number.js	Mon Jan 14 09:47:38 2013 +0100
     1.3 @@ -5,4 +5,5 @@
     1.4      return (((x * (y >> 16)) << 16) + x * (y & 0xFFFF)) | 0;
     1.5  };
     1.6  
     1.7 +__toInt8 = function(x)  { return (x << 24) >> 24; };
     1.8  __toInt16 = function(x) { return (x << 16) >> 16; };
     1.9 \ No newline at end of file
     2.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Mon Jan 14 09:17:18 2013 +0100
     2.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Mon Jan 14 09:47:38 2013 +0100
     2.3 @@ -672,6 +672,8 @@
     2.4                           smapper.popD(), smapper.pushL());
     2.5                      break;
     2.6                  case opc_i2b:
     2.7 +                    emit(out, "@1 = __toInt8(@1);", smapper.getI(0));
     2.8 +                    break;
     2.9                  case opc_i2c:
    2.10                      out.append("{ /* number conversion */ }");
    2.11                      break;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ByteArithmeticTest.java	Mon Jan 14 09:47:38 2013 +0100
     3.3 @@ -0,0 +1,102 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.bck2brwsr.tck;
    3.22 +
    3.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
    3.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    3.25 +import org.testng.annotations.Factory;
    3.26 +
    3.27 +/**
    3.28 + *
    3.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.30 + */
    3.31 +public class ByteArithmeticTest {
    3.32 +    
    3.33 +    private static byte add(byte x, byte y) {
    3.34 +        return (byte)(x + y);
    3.35 +    }
    3.36 +    
    3.37 +    private static byte sub(byte x, byte y) {
    3.38 +        return (byte)(x - y);
    3.39 +    }
    3.40 +    
    3.41 +    private static byte mul(byte x, byte y) {
    3.42 +        return (byte)(x * y);
    3.43 +    }
    3.44 +    
    3.45 +    private static byte div(byte x, byte y) {
    3.46 +        return (byte)(x / y);
    3.47 +    }
    3.48 +    
    3.49 +    private static byte mod(byte x, byte y) {
    3.50 +        return (byte)(x % y);
    3.51 +    }
    3.52 +    
    3.53 +    @Compare public byte conversion() {
    3.54 +        return (byte)123456;
    3.55 +    }
    3.56 +    
    3.57 +    @Compare public byte addOverflow() {
    3.58 +        return add(Byte.MAX_VALUE, (byte)1);
    3.59 +    }
    3.60 +    
    3.61 +    @Compare public byte subUnderflow() {
    3.62 +        return sub(Byte.MIN_VALUE, (byte)1);
    3.63 +    }
    3.64 +    
    3.65 +    @Compare public byte addMaxByteAndMaxByte() {
    3.66 +        return add(Byte.MAX_VALUE, Byte.MAX_VALUE);
    3.67 +    }
    3.68 +    
    3.69 +    @Compare public byte subMinByteAndMinByte() {
    3.70 +        return sub(Byte.MIN_VALUE, Byte.MIN_VALUE);
    3.71 +    }
    3.72 +    
    3.73 +    @Compare public byte multiplyMaxByte() {
    3.74 +        return mul(Byte.MAX_VALUE, (byte)2);
    3.75 +    }
    3.76 +    
    3.77 +    @Compare public byte multiplyMaxByteAndMaxByte() {
    3.78 +        return mul(Byte.MAX_VALUE, Byte.MAX_VALUE);
    3.79 +    }
    3.80 +    
    3.81 +    @Compare public byte multiplyMinByte() {
    3.82 +        return mul(Byte.MIN_VALUE, (byte)2);
    3.83 +    }
    3.84 +    
    3.85 +    @Compare public byte multiplyMinByteAndMinByte() {
    3.86 +        return mul(Byte.MIN_VALUE, Byte.MIN_VALUE);
    3.87 +    }
    3.88 +    
    3.89 +    @Compare public byte multiplyPrecision() {
    3.90 +        return mul((byte)17638, (byte)1103);
    3.91 +    }
    3.92 +    
    3.93 +    @Compare public byte division() {
    3.94 +        return div((byte)1, (byte)2);
    3.95 +    }
    3.96 +    
    3.97 +    @Compare public byte divisionReminder() {
    3.98 +        return mod((byte)1, (byte)2);
    3.99 +    }
   3.100 +    
   3.101 +    @Factory
   3.102 +    public static Object[] create() {
   3.103 +        return VMTest.create(ByteArithmeticTest.class);
   3.104 +    }
   3.105 +}