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