# HG changeset patch # User Lubomir Nerad # Date 1360151195 -3600 # Node ID 7ffb635a5c4f7d7de67f7b0b1641b506f4379363 # Parent ee595aae8353c73ba6a032011e00adf925b7f8b4 Fixed compare64 implementation diff -r ee595aae8353 -r 7ffb635a5c4f emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js --- a/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Tue Feb 05 19:14:39 2013 +0100 +++ b/emul/mini/src/main/resources/org/apidesign/vm4brwsr/emul/lang/java_lang_Number.js Wed Feb 06 12:46:35 2013 +0100 @@ -188,10 +188,10 @@ }; Number.prototype.compare64 = function(x) { - if (this.hi == x.hi) { - return (this == x) ? 0 : ((this < x) ? -1 : 1); + if (this.high32() === x.high32()) { + return (this < x) ? -1 : ((this > x) ? 1 : 0); } - return (this.hi < x.hi) ? -1 : 1; + return (this.high32() < x.high32()) ? -1 : 1; }; Number.prototype.neg64 = function() { diff -r ee595aae8353 -r 7ffb635a5c4f vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Tue Feb 05 19:14:39 2013 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Wed Feb 06 12:46:35 2013 +0100 @@ -649,7 +649,47 @@ new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xff, (byte)0x00, (byte)0x12, (byte)0x3e } ); } - + + @Test public void longCompareSameNumbers() throws Exception { + assertExec("Long compare same numbers", + Numbers.class, "compareL__I_3B_3BI", + 0.0, + new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, + new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, + 0 + ); + } + + @Test public void longComparePositiveNumbers() throws Exception { + assertExec("Long compare positive numbers", + Numbers.class, "compareL__I_3B_3BI", + -1.0, + new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x20, (byte)0x00, (byte)0x00 }, + new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x10, (byte)0x00, (byte)0x00, (byte)0x00 }, + 0 + ); + } + + @Test public void longCompareNegativeNumbers() throws Exception { + assertExec("Long compare negative numbers", + Numbers.class, "compareL__I_3B_3BI", + 1.0, + new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }, + new byte[] { (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, + 0 + ); + } + + @Test public void longCompareMixedNumbers() throws Exception { + assertExec("Long compare mixed numbers", + Numbers.class, "compareL__I_3B_3BI", + -1.0, + new byte[] { (byte)0x80, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, + new byte[] { (byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff }, + 0 + ); + } + private static CharSequence codeSeq; private static Invocable code; diff -r ee595aae8353 -r 7ffb635a5c4f vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java --- a/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Tue Feb 05 19:14:39 2013 +0100 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Wed Feb 06 12:46:35 2013 +0100 @@ -159,4 +159,44 @@ DataInputStream disY = new DataInputStream(isY); return (disX.readLong() ^ disY.readLong()); } + + public static int compareL(byte[] arrX, byte[] arrY, + int zero) throws IOException { + ByteArrayInputStream isX = new ByteArrayInputStream(arrX); + DataInputStream disX = new DataInputStream(isX); + ByteArrayInputStream isY = new ByteArrayInputStream(arrY); + DataInputStream disY = new DataInputStream(isY); + final long x = disX.readLong(); + final long y = disY.readLong(); + + final int xyResult = compareL(x, y, zero); + final int yxResult = compareL(y, x, zero); + + return ((xyResult + yxResult) == 0) ? xyResult : -2; + } + + private static int compareL(long x, long y, int zero) { + int result = -2; + int trueCount = 0; + + x += zero; + if (x == y) { + result = 0; + ++trueCount; + } + + x += zero; + if (x < y) { + result = -1; + ++trueCount; + } + + x += zero; + if (x > y) { + result = 1; + ++trueCount; + } + + return (trueCount == 1) ? result : -2; + } }