vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java
author Martin Soch <Martin.Soch@oracle.com>
Thu, 07 Feb 2013 16:11:53 +0100
brancharithmetic
changeset 698 ff57af563cb8
parent 680 7ffb635a5c4f
child 699 6cad41d877d2
permissions -rw-r--r--
Fix issue with Long.MIN_VALUE in Number.toExactString
Moved the first part of tests from vm/.../NumberTests to vmtest/.../LongArithmeticTest
jaroslav@114
     1
/**
jaroslav@114
     2
 * Back 2 Browser Bytecode Translator
jaroslav@114
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@114
     4
 *
jaroslav@114
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@114
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@114
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@114
     8
 *
jaroslav@114
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@114
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@114
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@114
    12
 * GNU General Public License for more details.
jaroslav@114
    13
 *
jaroslav@114
    14
 * You should have received a copy of the GNU General Public License
jaroslav@114
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@114
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@114
    17
 */
jaroslav@114
    18
package org.apidesign.vm4brwsr;
jaroslav@114
    19
jaroslav@181
    20
import java.io.ByteArrayInputStream;
jaroslav@181
    21
import java.io.DataInputStream;
jaroslav@181
    22
import java.io.IOException;
jaroslav@181
    23
jaroslav@114
    24
/**
jaroslav@114
    25
 *
jaroslav@114
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@114
    27
 */
jaroslav@114
    28
public class Numbers {
jaroslav@114
    29
    private static Double autoboxDbl() {
jaroslav@114
    30
        return 3.3;
jaroslav@114
    31
    }
jaroslav@114
    32
    public static String autoboxDblToString() {
jaroslav@114
    33
        return autoboxDbl().toString().toString();
jaroslav@114
    34
    }
jaroslav@178
    35
    public static int rem(int a, int b) {
jaroslav@178
    36
        return a % b;
jaroslav@178
    37
    }
jaroslav@181
    38
jaroslav@181
    39
    static float deserFloat() throws IOException {
jaroslav@181
    40
        byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83};
jaroslav@181
    41
        ByteArrayInputStream is = new ByteArrayInputStream(arr);
jaroslav@181
    42
        DataInputStream dis = new DataInputStream(is);
jaroslav@181
    43
        float r = dis.readFloat();
jaroslav@181
    44
        return r;
jaroslav@181
    45
    }
jaroslav@185
    46
    static double deserDouble() throws IOException {
jaroslav@185
    47
        byte[] arr = {(byte)64, (byte)8, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0, (byte)0};
jaroslav@185
    48
        ByteArrayInputStream is = new ByteArrayInputStream(arr);
jaroslav@185
    49
        DataInputStream dis = new DataInputStream(is);
jaroslav@185
    50
        return dis.readDouble();
jaroslav@185
    51
    }
jaroslav@185
    52
    static long deserLong(byte[] arr) throws IOException {
jaroslav@185
    53
        ByteArrayInputStream is = new ByteArrayInputStream(arr);
jaroslav@185
    54
        DataInputStream dis = new DataInputStream(is);
jaroslav@185
    55
        return dis.readLong();
jaroslav@185
    56
    }
jaroslav@181
    57
    static int deserInt() throws IOException {
jaroslav@181
    58
        byte[] arr = {(byte) 71, (byte) 84, (byte) 52, (byte) 83};
jaroslav@181
    59
        ByteArrayInputStream is = new ByteArrayInputStream(arr);
jaroslav@181
    60
        DataInputStream dis = new DataInputStream(is);
jaroslav@181
    61
        return dis.readInt();
jaroslav@181
    62
    }
jaroslav@186
    63
jaroslav@186
    64
    static String intToString() {
jaroslav@186
    65
        return new Integer(5).toString().toString();
jaroslav@186
    66
    }
jaroslav@187
    67
    static String floatToString() {
jaroslav@187
    68
        return new Float(7.0).toString().toString();
jaroslav@187
    69
    }
Martin@615
    70
    
Martin@617
    71
    public static long shlL(byte[] arrValue, int nBits) throws IOException {
Martin@617
    72
        ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue);
Martin@617
    73
        DataInputStream disValue = new DataInputStream(isValue);
Martin@617
    74
        return (disValue.readLong() << nBits);
Martin@617
    75
    }
Martin@617
    76
    
Martin@617
    77
    public static long shrL(byte[] arrValue, int nBits) throws IOException {
Martin@617
    78
        ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue);
Martin@617
    79
        DataInputStream disValue = new DataInputStream(isValue);
Martin@617
    80
        return (disValue.readLong() >> nBits);
Martin@617
    81
    }
Martin@627
    82
    
Martin@629
    83
    public static long ushrL(byte[] arrValue, int nBits) throws IOException {
Martin@629
    84
        ByteArrayInputStream isValue = new ByteArrayInputStream(arrValue);
Martin@629
    85
        DataInputStream disValue = new DataInputStream(isValue);
Martin@629
    86
        return (disValue.readLong() >>> nBits);
Martin@629
    87
    }
Martin@629
    88
    
Martin@627
    89
    public static long andL(byte[] arrX, byte[] arrY) throws IOException {
Martin@627
    90
        ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
Martin@627
    91
        DataInputStream disX = new DataInputStream(isX);
Martin@627
    92
        ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
Martin@627
    93
        DataInputStream disY = new DataInputStream(isY);
Martin@627
    94
        return (disX.readLong() & disY.readLong());
Martin@627
    95
    }
Martin@627
    96
    
Martin@627
    97
    public static long orL(byte[] arrX, byte[] arrY) throws IOException {
Martin@627
    98
        ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
Martin@627
    99
        DataInputStream disX = new DataInputStream(isX);
Martin@627
   100
        ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
Martin@627
   101
        DataInputStream disY = new DataInputStream(isY);
Martin@627
   102
        return (disX.readLong() | disY.readLong());
Martin@627
   103
    }
Martin@628
   104
    
Martin@628
   105
    public static long xorL(byte[] arrX, byte[] arrY) throws IOException {
Martin@628
   106
        ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
Martin@628
   107
        DataInputStream disX = new DataInputStream(isX);
Martin@628
   108
        ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
Martin@628
   109
        DataInputStream disY = new DataInputStream(isY);
Martin@628
   110
        return (disX.readLong() ^ disY.readLong());
Martin@628
   111
    }
lubomir@680
   112
lubomir@680
   113
    public static int compareL(byte[] arrX, byte[] arrY,
lubomir@680
   114
                               int zero) throws IOException {
lubomir@680
   115
        ByteArrayInputStream isX = new ByteArrayInputStream(arrX);
lubomir@680
   116
        DataInputStream disX = new DataInputStream(isX);
lubomir@680
   117
        ByteArrayInputStream isY = new ByteArrayInputStream(arrY);
lubomir@680
   118
        DataInputStream disY = new DataInputStream(isY);
lubomir@680
   119
        final long x = disX.readLong();
lubomir@680
   120
        final long y = disY.readLong();
lubomir@680
   121
lubomir@680
   122
        final int xyResult = compareL(x, y, zero);
lubomir@680
   123
        final int yxResult = compareL(y, x, zero);
lubomir@680
   124
lubomir@680
   125
        return ((xyResult + yxResult) == 0) ? xyResult : -2;
lubomir@680
   126
    }
lubomir@680
   127
lubomir@680
   128
    private static int compareL(long x, long y, int zero) {
lubomir@680
   129
        int result = -2;
lubomir@680
   130
        int trueCount = 0;
lubomir@680
   131
lubomir@680
   132
        x += zero;
lubomir@680
   133
        if (x == y) {
lubomir@680
   134
            result = 0;
lubomir@680
   135
            ++trueCount;
lubomir@680
   136
        }
lubomir@680
   137
lubomir@680
   138
        x += zero;
lubomir@680
   139
        if (x < y) {
lubomir@680
   140
            result = -1;
lubomir@680
   141
            ++trueCount;
lubomir@680
   142
        }
lubomir@680
   143
lubomir@680
   144
        x += zero;
lubomir@680
   145
        if (x > y) {
lubomir@680
   146
            result = 1;
lubomir@680
   147
            ++trueCount;
lubomir@680
   148
        }
lubomir@680
   149
lubomir@680
   150
        return (trueCount == 1) ? result : -2;
lubomir@680
   151
    }
jaroslav@114
   152
}