rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 737 vmtest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java@b2731af0357d
child 778 6f8683517f1f
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
Martin@582
     1
/**
Martin@582
     2
 * Back 2 Browser Bytecode Translator
Martin@582
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Martin@582
     4
 *
Martin@582
     5
 * This program is free software: you can redistribute it and/or modify
Martin@582
     6
 * it under the terms of the GNU General Public License as published by
Martin@582
     7
 * the Free Software Foundation, version 2 of the License.
Martin@582
     8
 *
Martin@582
     9
 * This program is distributed in the hope that it will be useful,
Martin@582
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Martin@582
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Martin@582
    12
 * GNU General Public License for more details.
Martin@582
    13
 *
Martin@582
    14
 * You should have received a copy of the GNU General Public License
Martin@582
    15
 * along with this program. Look for COPYING file in the top folder.
Martin@582
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
Martin@582
    17
 */
Martin@582
    18
package org.apidesign.bck2brwsr.tck;
Martin@582
    19
Martin@582
    20
import org.apidesign.bck2brwsr.vmtest.Compare;
Martin@582
    21
import org.apidesign.bck2brwsr.vmtest.VMTest;
Martin@582
    22
import org.testng.annotations.Factory;
Martin@582
    23
Martin@582
    24
/**
Martin@582
    25
 *
Martin@582
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
Martin@582
    27
 */
Martin@582
    28
public class LongArithmeticTest {
Martin@582
    29
    
Martin@582
    30
    private static long add(long x, long y) {
Martin@582
    31
        return (x + y);
Martin@582
    32
    }
Martin@582
    33
    
Martin@582
    34
    private static long sub(long x, long y) {
Martin@582
    35
        return (x - y);
Martin@582
    36
    }
Martin@582
    37
    
Martin@582
    38
    private static long mul(long x, long y) {
Martin@582
    39
        return (x * y);
Martin@582
    40
    }
Martin@582
    41
    
Martin@582
    42
    private static long div(long x, long y) {
Martin@582
    43
        return (x / y);
Martin@582
    44
    }
Martin@582
    45
    
Martin@582
    46
    private static long mod(long x, long y) {
Martin@582
    47
        return (x % y);
Martin@582
    48
    }
Martin@582
    49
    
Martin@698
    50
    private static long neg(long x) {
Martin@699
    51
        return (-x);
Martin@699
    52
    }
Martin@699
    53
    
Martin@699
    54
    private static long shl(long x, int b) {
Martin@699
    55
        return (x << b);
Martin@699
    56
    }
Martin@699
    57
    
Martin@699
    58
    private static long shr(long x, int b) {
Martin@699
    59
        return (x >> b);
Martin@699
    60
    }
Martin@699
    61
    
Martin@699
    62
    private static long ushr(long x, int b) {
Martin@699
    63
        return (x >>> b);
Martin@699
    64
    }
Martin@699
    65
    
Martin@699
    66
    private static long and(long x, long y) {
Martin@699
    67
        return (x & y);
Martin@699
    68
    }
Martin@699
    69
    
Martin@699
    70
    private static long or(long x, long y) {
Martin@699
    71
        return (x | y);
Martin@699
    72
    }
Martin@699
    73
    
Martin@699
    74
    private static long xor(long x, long y) {
Martin@699
    75
        return (x ^ y);
Martin@699
    76
    }
Martin@699
    77
    
Martin@699
    78
    public static int compare(long x, long y, int zero) {
Martin@699
    79
        final int xyResult = compareL(x, y, zero);
Martin@699
    80
        final int yxResult = compareL(y, x, zero);
Martin@699
    81
Martin@699
    82
        return ((xyResult + yxResult) == 0) ? xyResult : -2;
Martin@699
    83
    }
Martin@699
    84
Martin@699
    85
    private static int compareL(long x, long y, int zero) {
Martin@699
    86
        int result = -2;
Martin@699
    87
        int trueCount = 0;
Martin@699
    88
Martin@699
    89
        x += zero;
Martin@699
    90
        if (x == y) {
Martin@699
    91
            result = 0;
Martin@699
    92
            ++trueCount;
Martin@699
    93
        }
Martin@699
    94
Martin@699
    95
        x += zero;
Martin@699
    96
        if (x < y) {
Martin@699
    97
            result = -1;
Martin@699
    98
            ++trueCount;
Martin@699
    99
        }
Martin@699
   100
Martin@699
   101
        x += zero;
Martin@699
   102
        if (x > y) {
Martin@699
   103
            result = 1;
Martin@699
   104
            ++trueCount;
Martin@699
   105
        }
Martin@699
   106
Martin@699
   107
        return (trueCount == 1) ? result : -2;
Martin@698
   108
    }
Martin@698
   109
    
Martin@582
   110
    @Compare public long conversion() {
Martin@582
   111
        return Long.MAX_VALUE;
Martin@582
   112
    }
Martin@582
   113
    
Martin@698
   114
    @Compare public long negate1() {
Martin@698
   115
        return neg(0x00fa37d7763e0ca1l);
Martin@698
   116
    }
Martin@698
   117
    
Martin@698
   118
    @Compare public long negate2() {
Martin@698
   119
        return neg(0x80fa37d7763e0ca1l);
Martin@698
   120
    }
Martin@698
   121
Martin@698
   122
    @Compare public long negate3() {
Martin@698
   123
        return neg(0xfffffffffffffeddl);
Martin@698
   124
    }
Martin@698
   125
Martin@582
   126
    @Compare public long addOverflow() {
Martin@582
   127
        return add(Long.MAX_VALUE, 1l);
Martin@582
   128
    }
Martin@698
   129
Martin@582
   130
    @Compare public long subUnderflow() {
Martin@582
   131
        return sub(Long.MIN_VALUE, 1l);
Martin@582
   132
    }
Martin@698
   133
Martin@582
   134
    @Compare public long addMaxLongAndMaxLong() {
Martin@582
   135
        return add(Long.MAX_VALUE, Long.MAX_VALUE);
Martin@582
   136
    }
Martin@582
   137
    
Martin@582
   138
    @Compare public long subMinLongAndMinLong() {
Martin@582
   139
        return sub(Long.MIN_VALUE, Long.MIN_VALUE);
Martin@582
   140
    }
Martin@582
   141
    
Martin@698
   142
    @Compare public long subMinLongAndMaxLong() {
Martin@698
   143
        return sub(Long.MIN_VALUE, Long.MAX_VALUE);
Martin@698
   144
    }
Martin@698
   145
Martin@582
   146
    @Compare public long multiplyMaxLong() {
Martin@582
   147
        return mul(Long.MAX_VALUE, 2l);
Martin@582
   148
    }
Martin@582
   149
    
Martin@582
   150
    @Compare public long multiplyMaxLongAndMaxLong() {
Martin@582
   151
        return mul(Long.MAX_VALUE, Long.MAX_VALUE);
Martin@582
   152
    }
Martin@582
   153
    
Martin@582
   154
    @Compare public long multiplyMinLong() {
Martin@582
   155
        return mul(Long.MIN_VALUE, 2l);
Martin@582
   156
    }
Martin@582
   157
    
Martin@582
   158
    @Compare public long multiplyMinLongAndMinLong() {
Martin@582
   159
        return mul(Long.MIN_VALUE, Long.MIN_VALUE);
Martin@582
   160
    }
Martin@582
   161
    
Martin@582
   162
    @Compare public long multiplyPrecision() {
Martin@698
   163
        return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
Martin@582
   164
    }
Martin@582
   165
    
Martin@698
   166
    @Compare public long divideSmallPositiveNumbers() {
Martin@698
   167
        return div(0xabcdef, 0x123);
Martin@582
   168
    }
Martin@698
   169
Martin@698
   170
    @Compare public long divideSmallNegativeNumbers() {
Martin@698
   171
        return div(-0xabcdef, -0x123);
Martin@582
   172
    }
Martin@698
   173
Martin@698
   174
    @Compare public long divideSmallMixedNumbers() {
Martin@698
   175
        return div(0xabcdef, -0x123);
Martin@698
   176
    }
Martin@698
   177
Martin@698
   178
    @Compare public long dividePositiveNumbersOneDigitDenom() {
Martin@698
   179
        return div(0xabcdef0102ffffl, 0x654);
Martin@698
   180
    }
Martin@698
   181
Martin@698
   182
    @Compare public long divideNegativeNumbersOneDigitDenom() {
Martin@698
   183
        return div(-0xabcdef0102ffffl, -0x654);
Martin@698
   184
    }
Martin@698
   185
Martin@698
   186
    @Compare public long divideMixedNumbersOneDigitDenom() {
Martin@698
   187
        return div(-0xabcdef0102ffffl, 0x654);
Martin@698
   188
    }
Martin@698
   189
Martin@698
   190
    @Compare public long dividePositiveNumbersMultiDigitDenom() {
Martin@698
   191
        return div(0x7ffefc003322aabbl, 0x89ab1000l);
Martin@698
   192
    }
Martin@698
   193
Martin@698
   194
    @Compare public long divideNegativeNumbersMultiDigitDenom() {
Martin@698
   195
        return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
Martin@698
   196
    }
Martin@698
   197
Martin@698
   198
    @Compare public long divideMixedNumbersMultiDigitDenom() {
Martin@698
   199
        return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
Martin@698
   200
    }
Martin@698
   201
Martin@698
   202
    @Compare public long divideWithOverflow() {
Martin@698
   203
        return div(0x8000fffe0000l, 0x8000ffffl);
Martin@698
   204
    }
Martin@698
   205
Martin@698
   206
    @Compare public long divideWithCorrection() {
Martin@698
   207
        return div(0x7fff800000000000l, 0x800000000001l);
Martin@698
   208
    }
Martin@698
   209
Martin@698
   210
    @Compare public long moduloSmallPositiveNumbers() {
Martin@698
   211
        return mod(0xabcdef, 0x123);
Martin@698
   212
    }
Martin@698
   213
Martin@698
   214
    @Compare public long moduloSmallNegativeNumbers() {
Martin@698
   215
        return mod(-0xabcdef, -0x123);
Martin@698
   216
    }
Martin@698
   217
Martin@698
   218
    @Compare public long moduloSmallMixedNumbers() {
Martin@698
   219
        return mod(0xabcdef, -0x123);
Martin@698
   220
    }
Martin@698
   221
Martin@698
   222
    @Compare public long moduloPositiveNumbersOneDigitDenom() {
Martin@698
   223
        return mod(0xabcdef0102ffffl, 0x654);
Martin@698
   224
    }
Martin@698
   225
Martin@698
   226
    @Compare public long moduloNegativeNumbersOneDigitDenom() {
Martin@698
   227
        return mod(-0xabcdef0102ffffl, -0x654);
Martin@698
   228
    }
Martin@698
   229
Martin@698
   230
    @Compare public long moduloMixedNumbersOneDigitDenom() {
Martin@698
   231
        return mod(-0xabcdef0102ffffl, 0x654);
Martin@698
   232
    }
Martin@698
   233
Martin@698
   234
    @Compare public long moduloPositiveNumbersMultiDigitDenom() {
Martin@698
   235
        return mod(0x7ffefc003322aabbl, 0x89ab1000l);
Martin@698
   236
    }
Martin@698
   237
Martin@698
   238
    @Compare public long moduloNegativeNumbersMultiDigitDenom() {
Martin@698
   239
        return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
Martin@698
   240
    }
Martin@698
   241
Martin@698
   242
    @Compare public long moduloMixedNumbersMultiDigitDenom() {
Martin@698
   243
        return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
Martin@698
   244
    }
Martin@698
   245
Martin@698
   246
    @Compare public long moduloWithOverflow() {
Martin@698
   247
        return mod(0x8000fffe0000l, 0x8000ffffl);
Martin@698
   248
    }
Martin@698
   249
Martin@698
   250
    @Compare public long moduloWithCorrection() {
Martin@698
   251
        return mod(0x7fff800000000000l, 0x800000000001l);
Martin@698
   252
    }
lubomir@737
   253
lubomir@737
   254
    @Compare public boolean divByZeroThrowsArithmeticException() {
lubomir@737
   255
        try {
lubomir@737
   256
            div(1, 0);
lubomir@737
   257
            return false;
lubomir@737
   258
        } catch (final ArithmeticException e) {
lubomir@737
   259
            return true;
lubomir@737
   260
        }
lubomir@737
   261
    }
lubomir@737
   262
lubomir@737
   263
    @Compare public boolean modByZeroThrowsArithmeticException() {
lubomir@737
   264
        try {
lubomir@737
   265
            mod(1, 0);
lubomir@737
   266
            return false;
lubomir@737
   267
        } catch (final ArithmeticException e) {
lubomir@737
   268
            return true;
lubomir@737
   269
        }
lubomir@737
   270
    }
lubomir@737
   271
Martin@699
   272
    @Compare public long shiftL1() {
Martin@699
   273
        return shl(0x00fa37d7763e0ca1l, 5);
Martin@699
   274
    }
Martin@699
   275
    
Martin@699
   276
    @Compare public long shiftL2() {
Martin@699
   277
        return shl(0x00fa37d7763e0ca1l, 32);
Martin@699
   278
    }
Martin@699
   279
    
Martin@699
   280
    @Compare public long shiftL3() {
Martin@699
   281
        return shl(0x00fa37d7763e0ca1l, 45);
Martin@699
   282
    }
Martin@699
   283
    
Martin@699
   284
    @Compare public long shiftR1() {
Martin@699
   285
        return shr(0x00fa37d7763e0ca1l, 5);
Martin@699
   286
    }
Martin@699
   287
    
Martin@699
   288
    @Compare public long shiftR2() {
Martin@699
   289
        return shr(0x00fa37d7763e0ca1l, 32);
Martin@699
   290
    }
Martin@699
   291
    
Martin@699
   292
    @Compare public long shiftR3() {
Martin@699
   293
        return shr(0x00fa37d7763e0ca1l, 45);
Martin@699
   294
    }
Martin@699
   295
    
Martin@699
   296
    @Compare public long uShiftR1() {
Martin@699
   297
        return ushr(0x00fa37d7763e0ca1l, 5);
Martin@699
   298
    }
Martin@699
   299
    
Martin@699
   300
    @Compare public long uShiftR2() {
Martin@699
   301
        return ushr(0x00fa37d7763e0ca1l, 45);
Martin@699
   302
    }
Martin@699
   303
    
Martin@699
   304
    @Compare public long uShiftR3() {
Martin@699
   305
        return ushr(0xf0fa37d7763e0ca1l, 5);
Martin@699
   306
    }
Martin@699
   307
    
Martin@699
   308
    @Compare public long uShiftR4() {
Martin@699
   309
        return ushr(0xf0fa37d7763e0ca1l, 45);
Martin@699
   310
    }
Martin@699
   311
    
Martin@699
   312
    @Compare public long and1() {
Martin@699
   313
        return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
Martin@699
   314
    }
Martin@699
   315
    
Martin@699
   316
    @Compare public long or1() {
Martin@699
   317
        return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
Martin@699
   318
    }
Martin@699
   319
    
Martin@699
   320
    @Compare public long xor1() {
Martin@699
   321
        return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
Martin@699
   322
    }
Martin@699
   323
    
Martin@699
   324
    @Compare public long xor2() {
Martin@699
   325
        return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
Martin@699
   326
    }
Martin@699
   327
    
Martin@699
   328
    @Compare public long xor3() {
Martin@699
   329
        return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
Martin@699
   330
    }
Martin@699
   331
    
Martin@699
   332
    @Compare public int compareSameNumbers() {
Martin@699
   333
        return compare(0x0000000000000000l, 0x0000000000000000l, 0);
Martin@699
   334
    }
Martin@699
   335
Martin@699
   336
    @Compare public int comparePositiveNumbers() {
Martin@699
   337
        return compare(0x0000000000200000l, 0x0000000010000000l, 0);
Martin@699
   338
    }
Martin@699
   339
Martin@699
   340
    @Compare public int compareNegativeNumbers() {
Martin@699
   341
        return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
Martin@699
   342
    }
Martin@699
   343
Martin@699
   344
    @Compare public int compareMixedNumbers() {
Martin@699
   345
        return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
Martin@699
   346
    }
Martin@699
   347
    
Martin@582
   348
    @Factory
Martin@582
   349
    public static Object[] create() {
Martin@582
   350
        return VMTest.create(LongArithmeticTest.class);
Martin@582
   351
    }
Martin@582
   352
}