rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 11 Aug 2014 20:10:46 +0200
branchjdk8
changeset 1666 4e349118658f
parent 1665 868ad94dc3ec
parent 1663 03f1dd008e83
permissions -rw-r--r--
Merging the latest changes on default branch
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 {
lubomir@778
    29
Martin@582
    30
    private static long add(long x, long y) {
Martin@582
    31
        return (x + y);
Martin@582
    32
    }
lubomir@778
    33
Martin@582
    34
    private static long sub(long x, long y) {
Martin@582
    35
        return (x - y);
Martin@582
    36
    }
lubomir@778
    37
Martin@582
    38
    private static long mul(long x, long y) {
Martin@582
    39
        return (x * y);
Martin@582
    40
    }
lubomir@778
    41
Martin@582
    42
    private static long div(long x, long y) {
Martin@582
    43
        return (x / y);
Martin@582
    44
    }
lubomir@778
    45
Martin@582
    46
    private static long mod(long x, long y) {
Martin@582
    47
        return (x % y);
Martin@582
    48
    }
lubomir@778
    49
Martin@698
    50
    private static long neg(long x) {
Martin@699
    51
        return (-x);
Martin@699
    52
    }
lubomir@778
    53
Martin@699
    54
    private static long shl(long x, int b) {
Martin@699
    55
        return (x << b);
Martin@699
    56
    }
lubomir@778
    57
Martin@699
    58
    private static long shr(long x, int b) {
Martin@699
    59
        return (x >> b);
Martin@699
    60
    }
lubomir@778
    61
Martin@699
    62
    private static long ushr(long x, int b) {
Martin@699
    63
        return (x >>> b);
Martin@699
    64
    }
lubomir@778
    65
Martin@699
    66
    private static long and(long x, long y) {
Martin@699
    67
        return (x & y);
Martin@699
    68
    }
lubomir@778
    69
Martin@699
    70
    private static long or(long x, long y) {
Martin@699
    71
        return (x | y);
Martin@699
    72
    }
lubomir@778
    73
Martin@699
    74
    private static long xor(long x, long y) {
Martin@699
    75
        return (x ^ y);
Martin@699
    76
    }
lubomir@778
    77
lubomir@778
    78
    private static float fadd(float x, float y) {
lubomir@778
    79
        return x + y;
lubomir@778
    80
    }
lubomir@778
    81
lubomir@778
    82
    private static double dadd(double x, double y) {
lubomir@778
    83
        return x + y;
lubomir@778
    84
    }
lubomir@778
    85
Martin@699
    86
    public static int compare(long x, long y, int zero) {
Martin@699
    87
        final int xyResult = compareL(x, y, zero);
Martin@699
    88
        final int yxResult = compareL(y, x, zero);
Martin@699
    89
Martin@699
    90
        return ((xyResult + yxResult) == 0) ? xyResult : -2;
Martin@699
    91
    }
Martin@699
    92
Martin@699
    93
    private static int compareL(long x, long y, int zero) {
Martin@699
    94
        int result = -2;
Martin@699
    95
        int trueCount = 0;
Martin@699
    96
Martin@699
    97
        x += zero;
Martin@699
    98
        if (x == y) {
Martin@699
    99
            result = 0;
Martin@699
   100
            ++trueCount;
Martin@699
   101
        }
Martin@699
   102
Martin@699
   103
        x += zero;
Martin@699
   104
        if (x < y) {
Martin@699
   105
            result = -1;
Martin@699
   106
            ++trueCount;
Martin@699
   107
        }
Martin@699
   108
Martin@699
   109
        x += zero;
Martin@699
   110
        if (x > y) {
Martin@699
   111
            result = 1;
Martin@699
   112
            ++trueCount;
Martin@699
   113
        }
Martin@699
   114
Martin@699
   115
        return (trueCount == 1) ? result : -2;
Martin@698
   116
    }
jaroslav@1663
   117
    
jaroslav@1663
   118
    @Compare public int parameterSlotCount() {
jaroslav@1663
   119
        long argCounts = 281479271874563L;
jaroslav@1663
   120
        int x = unpack(argCounts, 2);
jaroslav@1663
   121
        return x;
jaroslav@1663
   122
    }
jaroslav@1663
   123
    private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d
jaroslav@1663
   124
        assert(word <= 3);
jaroslav@1663
   125
        final long val = packed >> ((3-word) * 16);
jaroslav@1663
   126
        return (char)val;
jaroslav@1663
   127
    }
Martin@582
   128
    @Compare public long conversion() {
Martin@582
   129
        return Long.MAX_VALUE;
Martin@582
   130
    }
lubomir@778
   131
Martin@698
   132
    @Compare public long negate1() {
Martin@698
   133
        return neg(0x00fa37d7763e0ca1l);
Martin@698
   134
    }
lubomir@778
   135
Martin@698
   136
    @Compare public long negate2() {
Martin@698
   137
        return neg(0x80fa37d7763e0ca1l);
Martin@698
   138
    }
Martin@698
   139
Martin@698
   140
    @Compare public long negate3() {
Martin@698
   141
        return neg(0xfffffffffffffeddl);
Martin@698
   142
    }
Martin@698
   143
Martin@582
   144
    @Compare public long addOverflow() {
Martin@582
   145
        return add(Long.MAX_VALUE, 1l);
Martin@582
   146
    }
Martin@698
   147
Martin@582
   148
    @Compare public long subUnderflow() {
Martin@582
   149
        return sub(Long.MIN_VALUE, 1l);
Martin@582
   150
    }
Martin@698
   151
Martin@582
   152
    @Compare public long addMaxLongAndMaxLong() {
Martin@582
   153
        return add(Long.MAX_VALUE, Long.MAX_VALUE);
Martin@582
   154
    }
lubomir@778
   155
Martin@582
   156
    @Compare public long subMinLongAndMinLong() {
Martin@582
   157
        return sub(Long.MIN_VALUE, Long.MIN_VALUE);
Martin@582
   158
    }
lubomir@778
   159
Martin@698
   160
    @Compare public long subMinLongAndMaxLong() {
Martin@698
   161
        return sub(Long.MIN_VALUE, Long.MAX_VALUE);
Martin@698
   162
    }
Martin@698
   163
Martin@582
   164
    @Compare public long multiplyMaxLong() {
Martin@582
   165
        return mul(Long.MAX_VALUE, 2l);
Martin@582
   166
    }
lubomir@778
   167
Martin@582
   168
    @Compare public long multiplyMaxLongAndMaxLong() {
Martin@582
   169
        return mul(Long.MAX_VALUE, Long.MAX_VALUE);
Martin@582
   170
    }
lubomir@778
   171
Martin@582
   172
    @Compare public long multiplyMinLong() {
Martin@582
   173
        return mul(Long.MIN_VALUE, 2l);
Martin@582
   174
    }
lubomir@778
   175
Martin@582
   176
    @Compare public long multiplyMinLongAndMinLong() {
Martin@582
   177
        return mul(Long.MIN_VALUE, Long.MIN_VALUE);
Martin@582
   178
    }
lubomir@778
   179
Martin@582
   180
    @Compare public long multiplyPrecision() {
Martin@698
   181
        return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
Martin@582
   182
    }
lubomir@778
   183
Martin@698
   184
    @Compare public long divideSmallPositiveNumbers() {
Martin@698
   185
        return div(0xabcdef, 0x123);
Martin@582
   186
    }
Martin@698
   187
Martin@698
   188
    @Compare public long divideSmallNegativeNumbers() {
Martin@698
   189
        return div(-0xabcdef, -0x123);
Martin@582
   190
    }
Martin@698
   191
Martin@698
   192
    @Compare public long divideSmallMixedNumbers() {
Martin@698
   193
        return div(0xabcdef, -0x123);
Martin@698
   194
    }
Martin@698
   195
Martin@698
   196
    @Compare public long dividePositiveNumbersOneDigitDenom() {
Martin@698
   197
        return div(0xabcdef0102ffffl, 0x654);
Martin@698
   198
    }
Martin@698
   199
Martin@698
   200
    @Compare public long divideNegativeNumbersOneDigitDenom() {
Martin@698
   201
        return div(-0xabcdef0102ffffl, -0x654);
Martin@698
   202
    }
Martin@698
   203
Martin@698
   204
    @Compare public long divideMixedNumbersOneDigitDenom() {
Martin@698
   205
        return div(-0xabcdef0102ffffl, 0x654);
Martin@698
   206
    }
Martin@698
   207
Martin@698
   208
    @Compare public long dividePositiveNumbersMultiDigitDenom() {
Martin@698
   209
        return div(0x7ffefc003322aabbl, 0x89ab1000l);
Martin@698
   210
    }
Martin@698
   211
Martin@698
   212
    @Compare public long divideNegativeNumbersMultiDigitDenom() {
Martin@698
   213
        return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
Martin@698
   214
    }
Martin@698
   215
Martin@698
   216
    @Compare public long divideMixedNumbersMultiDigitDenom() {
Martin@698
   217
        return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
Martin@698
   218
    }
Martin@698
   219
Martin@698
   220
    @Compare public long divideWithOverflow() {
Martin@698
   221
        return div(0x8000fffe0000l, 0x8000ffffl);
Martin@698
   222
    }
Martin@698
   223
Martin@698
   224
    @Compare public long divideWithCorrection() {
Martin@698
   225
        return div(0x7fff800000000000l, 0x800000000001l);
Martin@698
   226
    }
Martin@698
   227
Martin@698
   228
    @Compare public long moduloSmallPositiveNumbers() {
Martin@698
   229
        return mod(0xabcdef, 0x123);
Martin@698
   230
    }
Martin@698
   231
Martin@698
   232
    @Compare public long moduloSmallNegativeNumbers() {
Martin@698
   233
        return mod(-0xabcdef, -0x123);
Martin@698
   234
    }
Martin@698
   235
Martin@698
   236
    @Compare public long moduloSmallMixedNumbers() {
Martin@698
   237
        return mod(0xabcdef, -0x123);
Martin@698
   238
    }
Martin@698
   239
Martin@698
   240
    @Compare public long moduloPositiveNumbersOneDigitDenom() {
Martin@698
   241
        return mod(0xabcdef0102ffffl, 0x654);
Martin@698
   242
    }
Martin@698
   243
Martin@698
   244
    @Compare public long moduloNegativeNumbersOneDigitDenom() {
Martin@698
   245
        return mod(-0xabcdef0102ffffl, -0x654);
Martin@698
   246
    }
Martin@698
   247
Martin@698
   248
    @Compare public long moduloMixedNumbersOneDigitDenom() {
Martin@698
   249
        return mod(-0xabcdef0102ffffl, 0x654);
Martin@698
   250
    }
Martin@698
   251
Martin@698
   252
    @Compare public long moduloPositiveNumbersMultiDigitDenom() {
Martin@698
   253
        return mod(0x7ffefc003322aabbl, 0x89ab1000l);
Martin@698
   254
    }
Martin@698
   255
Martin@698
   256
    @Compare public long moduloNegativeNumbersMultiDigitDenom() {
Martin@698
   257
        return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
Martin@698
   258
    }
Martin@698
   259
Martin@698
   260
    @Compare public long moduloMixedNumbersMultiDigitDenom() {
Martin@698
   261
        return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
Martin@698
   262
    }
Martin@698
   263
Martin@698
   264
    @Compare public long moduloWithOverflow() {
Martin@698
   265
        return mod(0x8000fffe0000l, 0x8000ffffl);
Martin@698
   266
    }
Martin@698
   267
Martin@698
   268
    @Compare public long moduloWithCorrection() {
Martin@698
   269
        return mod(0x7fff800000000000l, 0x800000000001l);
Martin@698
   270
    }
lubomir@737
   271
lubomir@778
   272
    @Compare public long conversionFromFloatPositive() {
lubomir@778
   273
        return (long) fadd(2, 0.6f);
lubomir@778
   274
    }
lubomir@778
   275
lubomir@778
   276
    @Compare public long conversionFromFloatNegative() {
lubomir@778
   277
        return (long) fadd(-2, -0.6f);
lubomir@778
   278
    }
lubomir@778
   279
lubomir@778
   280
    @Compare public long conversionFromDoublePositive() {
lubomir@778
   281
        return (long) dadd(0x20ffff0000L, 0.6);
lubomir@778
   282
    }
lubomir@778
   283
lubomir@778
   284
    @Compare public long conversionFromDoubleNegative() {
lubomir@778
   285
        return (long) dadd(-0x20ffff0000L, -0.6);
lubomir@778
   286
    }
lubomir@778
   287
lubomir@737
   288
    @Compare public boolean divByZeroThrowsArithmeticException() {
lubomir@737
   289
        try {
lubomir@737
   290
            div(1, 0);
lubomir@737
   291
            return false;
lubomir@737
   292
        } catch (final ArithmeticException e) {
lubomir@737
   293
            return true;
lubomir@737
   294
        }
lubomir@737
   295
    }
lubomir@737
   296
lubomir@737
   297
    @Compare public boolean modByZeroThrowsArithmeticException() {
lubomir@737
   298
        try {
lubomir@737
   299
            mod(1, 0);
lubomir@737
   300
            return false;
lubomir@737
   301
        } catch (final ArithmeticException e) {
lubomir@737
   302
            return true;
lubomir@737
   303
        }
lubomir@737
   304
    }
lubomir@737
   305
Martin@699
   306
    @Compare public long shiftL1() {
Martin@699
   307
        return shl(0x00fa37d7763e0ca1l, 5);
Martin@699
   308
    }
lubomir@778
   309
Martin@699
   310
    @Compare public long shiftL2() {
Martin@699
   311
        return shl(0x00fa37d7763e0ca1l, 32);
Martin@699
   312
    }
lubomir@778
   313
Martin@699
   314
    @Compare public long shiftL3() {
Martin@699
   315
        return shl(0x00fa37d7763e0ca1l, 45);
Martin@699
   316
    }
Martin@1352
   317
    
Martin@1352
   318
    @Compare public long shiftL4() {
Martin@1352
   319
        return shl(0x00fa37d7763e0ca1l, 0);
Martin@1352
   320
    }
Martin@1352
   321
    
Martin@1352
   322
    @Compare public long shiftL5() {
Martin@1352
   323
        return shl(0x00fa37d7763e0ca1l, 70);
Martin@1352
   324
    }
lubomir@778
   325
Martin@699
   326
    @Compare public long shiftR1() {
Martin@699
   327
        return shr(0x00fa37d7763e0ca1l, 5);
Martin@699
   328
    }
lubomir@778
   329
Martin@699
   330
    @Compare public long shiftR2() {
Martin@699
   331
        return shr(0x00fa37d7763e0ca1l, 32);
Martin@699
   332
    }
lubomir@778
   333
Martin@699
   334
    @Compare public long shiftR3() {
Martin@699
   335
        return shr(0x00fa37d7763e0ca1l, 45);
Martin@699
   336
    }
Martin@1352
   337
    
Martin@1352
   338
    @Compare public long shiftR4() {
Martin@1352
   339
        return shr(0x00fa37d7763e0ca1l, 0);
Martin@1352
   340
    }
Martin@1352
   341
    
Martin@1352
   342
    @Compare public long shiftR5() {
Martin@1352
   343
        return shr(0x00fa37d7763e0ca1l, 70);
Martin@1352
   344
    }
lubomir@778
   345
Martin@699
   346
    @Compare public long uShiftR1() {
Martin@699
   347
        return ushr(0x00fa37d7763e0ca1l, 5);
Martin@699
   348
    }
lubomir@778
   349
Martin@699
   350
    @Compare public long uShiftR2() {
Martin@699
   351
        return ushr(0x00fa37d7763e0ca1l, 45);
Martin@699
   352
    }
Martin@1352
   353
    
Martin@1352
   354
    @Compare public long uShiftR3() {
Martin@1352
   355
        return ushr(0x00fa37d7763e0ca1l, 0);
Martin@1352
   356
    }
Martin@1352
   357
    
Martin@1352
   358
    @Compare public long uShiftR4() {
Martin@1352
   359
        return ushr(0x00fa37d7763e0ca1l, 70);
Martin@1352
   360
    }
lubomir@778
   361
Martin@1352
   362
    @Compare public long uShiftR5() {
Martin@699
   363
        return ushr(0xf0fa37d7763e0ca1l, 5);
Martin@699
   364
    }
lubomir@778
   365
Martin@1352
   366
    @Compare public long uShiftR6() {
Martin@699
   367
        return ushr(0xf0fa37d7763e0ca1l, 45);
Martin@699
   368
    }
Martin@1352
   369
    
Martin@1352
   370
    @Compare public long uShiftR7() {
Martin@1352
   371
        return ushr(0xf0fa37d7763e0ca1l, 0);
Martin@1352
   372
    }
Martin@1352
   373
    
Martin@1352
   374
    @Compare public long uShiftR8() {
Martin@1352
   375
        return ushr(0xf0fa37d7763e0ca1l, 70);
Martin@1352
   376
    }
lubomir@778
   377
Martin@699
   378
    @Compare public long and1() {
Martin@699
   379
        return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
Martin@699
   380
    }
lubomir@778
   381
Martin@699
   382
    @Compare public long or1() {
Martin@699
   383
        return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
Martin@699
   384
    }
lubomir@778
   385
Martin@699
   386
    @Compare public long xor1() {
Martin@699
   387
        return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
Martin@699
   388
    }
lubomir@778
   389
Martin@699
   390
    @Compare public long xor2() {
Martin@699
   391
        return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
Martin@699
   392
    }
lubomir@778
   393
Martin@699
   394
    @Compare public long xor3() {
Martin@699
   395
        return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
Martin@699
   396
    }
lubomir@778
   397
Martin@699
   398
    @Compare public int compareSameNumbers() {
Martin@699
   399
        return compare(0x0000000000000000l, 0x0000000000000000l, 0);
Martin@699
   400
    }
Martin@699
   401
Martin@699
   402
    @Compare public int comparePositiveNumbers() {
Martin@699
   403
        return compare(0x0000000000200000l, 0x0000000010000000l, 0);
Martin@699
   404
    }
Martin@699
   405
Martin@699
   406
    @Compare public int compareNegativeNumbers() {
Martin@699
   407
        return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
Martin@699
   408
    }
Martin@699
   409
Martin@699
   410
    @Compare public int compareMixedNumbers() {
Martin@699
   411
        return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
Martin@699
   412
    }
Martin@699
   413
    
Martin@582
   414
    @Factory
Martin@582
   415
    public static Object[] create() {
Martin@582
   416
        return VMTest.create(LongArithmeticTest.class);
Martin@582
   417
    }
Martin@582
   418
}