# HG changeset patch # User Martin Soch # Date 1358151438 -3600 # Node ID e4fb6c7ac42ac8333ffd73d62f90ce7c39aaaeb9 # Parent 7df624c2a0a1b872f2a9452bf79b4608de682536 Short arithmetic - added tests and conversion int->short. diff -r 7df624c2a0a1 -r e4fb6c7ac42a emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_Number.js --- a/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_Number.js Sun Jan 13 23:07:54 2013 +0100 +++ b/emul/src/main/resources/org/apidesign/vm4brwsr/emul/java_lang_Number.js Mon Jan 14 09:17:18 2013 +0100 @@ -4,3 +4,5 @@ __mul32 = function(x,y) { return (((x * (y >> 16)) << 16) + x * (y & 0xFFFF)) | 0; }; + +__toInt16 = function(x) { return (x << 16) >> 16; }; \ No newline at end of file diff -r 7df624c2a0a1 -r e4fb6c7ac42a vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Sun Jan 13 23:07:54 2013 +0100 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Mon Jan 14 09:17:18 2013 +0100 @@ -673,8 +673,10 @@ break; case opc_i2b: case opc_i2c: + out.append("{ /* number conversion */ }"); + break; case opc_i2s: - out.append("{ /* number conversion */ }"); + emit(out, "@1 = __toInt16(@1);", smapper.getI(0)); break; case opc_aconst_null: emit(out, "@1 = null;", smapper.pushA()); diff -r 7df624c2a0a1 -r e4fb6c7ac42a vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ShortArithmeticTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ShortArithmeticTest.java Mon Jan 14 09:17:18 2013 +0100 @@ -0,0 +1,102 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.tck; + +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class ShortArithmeticTest { + + private static short add(short x, short y) { + return (short)(x + y); + } + + private static short sub(short x, short y) { + return (short)(x - y); + } + + private static short mul(short x, short y) { + return (short)(x * y); + } + + private static short div(short x, short y) { + return (short)(x / y); + } + + private static short mod(short x, short y) { + return (short)(x % y); + } + + @Compare public short conversion() { + return (short)123456; + } + + @Compare public short addOverflow() { + return add(Short.MAX_VALUE, (short)1); + } + + @Compare public short subUnderflow() { + return sub(Short.MIN_VALUE, (short)1); + } + + @Compare public short addMaxShortAndMaxShort() { + return add(Short.MAX_VALUE, Short.MAX_VALUE); + } + + @Compare public short subMinShortAndMinShort() { + return sub(Short.MIN_VALUE, Short.MIN_VALUE); + } + + @Compare public short multiplyMaxShort() { + return mul(Short.MAX_VALUE, (short)2); + } + + @Compare public short multiplyMaxShortAndMaxShort() { + return mul(Short.MAX_VALUE, Short.MAX_VALUE); + } + + @Compare public short multiplyMinShort() { + return mul(Short.MIN_VALUE, (short)2); + } + + @Compare public short multiplyMinShortAndMinShort() { + return mul(Short.MIN_VALUE, Short.MIN_VALUE); + } + + @Compare public short multiplyPrecision() { + return mul((short)17638, (short)1103); + } + + @Compare public short division() { + return div((short)1, (short)2); + } + + @Compare public short divisionReminder() { + return mod((short)1, (short)2); + } + + @Factory + public static Object[] create() { + return VMTest.create(ShortArithmeticTest.class); + } +}