Implementation of "<<" for Long works, added tests for "+". arithmetic
authorMartin Soch <Martin.Soch@oracle.com>
Thu, 31 Jan 2013 13:42:14 +0100
brancharithmetic
changeset 6169cbf1f2ad7ee
parent 615 e3f671b50e93
child 617 960e219425e2
Implementation of "<<" for Long works, added tests for "+".
emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js
vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java
vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
     1.1 --- a/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js	Wed Jan 30 22:23:21 2013 +0100
     1.2 +++ b/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js	Thu Jan 31 13:42:14 2013 +0100
     1.3 @@ -15,7 +15,7 @@
     1.4      return low;
     1.5    }
     1.6    var l = new Number(low);
     1.7 -  l.hi = this;
     1.8 +  l.hi = this | 0;
     1.9    return l;
    1.10  };
    1.11  
    1.12 @@ -28,7 +28,7 @@
    1.13  };
    1.14  Number.prototype.toLong = function() {
    1.15      var hi = (this > __m32) ? (Math.floor(this / (__m32+1))) | 0 : 0;
    1.16 -    return hi.next32(this % (__m32+1));
    1.17 +    return hi.next32(Math.floor(this % (__m32+1)));
    1.18  };
    1.19  
    1.20  Number.prototype.toExactString = function() {
    1.21 @@ -88,14 +88,15 @@
    1.22  };
    1.23  
    1.24  Number.prototype.shl64 = function(x) {
    1.25 -    if (x > 32) {
    1.26 -        var hi = (this << (x - 32)) & 0xFFFFFFFF;
    1.27 +    if (x >= 32) {
    1.28 +        var hi = (this << (x - 32)) | 0;
    1.29          return hi.next32(0);
    1.30      } else {
    1.31 -        var hi = (this.high32() << x) & 0xFFFFFFFF;
    1.32 +        var hi = (this.high32() << x) | 0;
    1.33          var low_reminder = this >> (32 - x);
    1.34          hi |= low_reminder;
    1.35          var low = this << x;
    1.36 +        low += (low < 0) ? (__m32+1) : 0;
    1.37          return hi.next32(low);
    1.38      }
    1.39  };
     2.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Wed Jan 30 22:23:21 2013 +0100
     2.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java	Thu Jan 31 13:42:14 2013 +0100
     2.3 @@ -151,6 +151,26 @@
     2.4          );
     2.5      }
     2.6      
     2.7 +    @Test public void longAddOverflow() throws Exception {
     2.8 +        final long res = Long.MAX_VALUE + 1l;
     2.9 +        assertExec("Addition 1+MAX",
    2.10 +            Numbers.class, "addL__J_3B_3B", 
    2.11 +            Double.valueOf(res),
    2.12 +                new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff },
    2.13 +                new byte[] { (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)1 }
    2.14 +        );
    2.15 +    }
    2.16 +    
    2.17 +    @Test public void longAddMaxAndMax() throws Exception {
    2.18 +        final long res = Long.MAX_VALUE + Long.MAX_VALUE;
    2.19 +        assertExec("Addition MAX+MAX",
    2.20 +            Numbers.class, "addL__J_3B_3B", 
    2.21 +            Double.valueOf(res),
    2.22 +            new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff },
    2.23 +            new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }
    2.24 +        );
    2.25 +    }
    2.26 +    
    2.27      private static CharSequence codeSeq;
    2.28      private static Invocable code;
    2.29  
    2.30 @@ -173,10 +193,11 @@
    2.31          if (expRes instanceof Double && ret instanceof Double) {
    2.32              double expD = ((Double)expRes).doubleValue();
    2.33              double retD = ((Double)ret).doubleValue();
    2.34 -            assertEquals(retD, expD, 0.000004, msg + " was " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
    2.35 +            assertEquals(retD, expD, 0.000004, msg + " "
    2.36 +                    + StaticMethodTest.dumpJS(codeSeq));
    2.37              return;
    2.38          }
    2.39 -        assertEquals(ret, expRes, msg + "was: " + ret + "\n" + StaticMethodTest.dumpJS(codeSeq));
    2.40 +        assertEquals(ret, expRes, msg + " " + StaticMethodTest.dumpJS(codeSeq));
    2.41      }
    2.42      
    2.43  }
     3.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java	Wed Jan 30 22:23:21 2013 +0100
     3.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java	Thu Jan 31 13:42:14 2013 +0100
     3.3 @@ -72,8 +72,12 @@
     3.4          return Long.MAX_VALUE;
     3.5      }
     3.6      
     3.7 -    public static long addL(long x, long y) {
     3.8 -        return (x + y);
     3.9 +    public static long addL(byte[] arrX, byte[] arrY) throws IOException {
    3.10 +        ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
    3.11 +        DataInputStream disX = new DataInputStream(isX);
    3.12 +        ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
    3.13 +        DataInputStream disY = new DataInputStream(isY);
    3.14 +        return (disX.readLong() + disY.readLong());
    3.15      }
    3.16      
    3.17      public static long subL(long x, long y) {