vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
brancharithmetic
changeset 680 7ffb635a5c4f
parent 676 eecf6077ec4e
child 698 ff57af563cb8
     1.1 --- a/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java	Tue Feb 05 16:40:01 2013 +0100
     1.2 +++ b/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java	Wed Feb 06 12:46:35 2013 +0100
     1.3 @@ -159,4 +159,44 @@
     1.4          DataInputStream disY = new DataInputStream(isY);
     1.5          return (disX.readLong() ^ disY.readLong());
     1.6      }
     1.7 +
     1.8 +    public static int compareL(byte[] arrX, byte[] arrY,
     1.9 +                               int zero) throws IOException {
    1.10 +        ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
    1.11 +        DataInputStream disX = new DataInputStream(isX);
    1.12 +        ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
    1.13 +        DataInputStream disY = new DataInputStream(isY);
    1.14 +        final long x = disX.readLong();
    1.15 +        final long y = disY.readLong();
    1.16 +
    1.17 +        final int xyResult = compareL(x, y, zero);
    1.18 +        final int yxResult = compareL(y, x, zero);
    1.19 +
    1.20 +        return ((xyResult + yxResult) == 0) ? xyResult : -2;
    1.21 +    }
    1.22 +
    1.23 +    private static int compareL(long x, long y, int zero) {
    1.24 +        int result = -2;
    1.25 +        int trueCount = 0;
    1.26 +
    1.27 +        x += zero;
    1.28 +        if (x == y) {
    1.29 +            result = 0;
    1.30 +            ++trueCount;
    1.31 +        }
    1.32 +
    1.33 +        x += zero;
    1.34 +        if (x < y) {
    1.35 +            result = -1;
    1.36 +            ++trueCount;
    1.37 +        }
    1.38 +
    1.39 +        x += zero;
    1.40 +        if (x > y) {
    1.41 +            result = 1;
    1.42 +            ++trueCount;
    1.43 +        }
    1.44 +
    1.45 +        return (trueCount == 1) ? result : -2;
    1.46 +    }
    1.47  }