vmtest/src/test/java/org/apidesign/bck2brwsr/tck/IntegerArithmeticTest.java
author Martin Soch <Martin.Soch@oracle.com>
Thu, 07 Feb 2013 17:41:41 +0100
brancharithmetic
changeset 700 b9bf26ea0118
parent 457 b0e82dcf51fb
child 737 b2731af0357d
permissions -rw-r--r--
Fixed negation for Integer.MIN_VALUE
Martin@345
     1
/**
Martin@345
     2
 * Back 2 Browser Bytecode Translator
Martin@345
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Martin@345
     4
 *
Martin@345
     5
 * This program is free software: you can redistribute it and/or modify
Martin@345
     6
 * it under the terms of the GNU General Public License as published by
Martin@345
     7
 * the Free Software Foundation, version 2 of the License.
Martin@345
     8
 *
Martin@345
     9
 * This program is distributed in the hope that it will be useful,
Martin@345
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Martin@345
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Martin@345
    12
 * GNU General Public License for more details.
Martin@345
    13
 *
Martin@345
    14
 * You should have received a copy of the GNU General Public License
Martin@345
    15
 * along with this program. Look for COPYING file in the top folder.
Martin@345
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
Martin@345
    17
 */
Martin@427
    18
package org.apidesign.bck2brwsr.tck;
Martin@345
    19
Martin@427
    20
import org.apidesign.bck2brwsr.vmtest.Compare;
Martin@427
    21
import org.apidesign.bck2brwsr.vmtest.VMTest;
Martin@345
    22
import org.testng.annotations.Factory;
Martin@345
    23
Martin@345
    24
/**
Martin@345
    25
 *
Martin@345
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
Martin@345
    27
 */
Martin@345
    28
public class IntegerArithmeticTest {
Martin@345
    29
    
Martin@351
    30
    private static int add(int x, int y) {
Martin@351
    31
        return x + y;
Martin@345
    32
    }
Martin@345
    33
    
Martin@351
    34
    private static int sub(int x, int y) {
Martin@351
    35
        return x - y;
Martin@345
    36
    }
Martin@345
    37
    
Martin@351
    38
    private static int mul(int x, int y) {
Martin@351
    39
        return x * y;
Martin@351
    40
    }
Martin@351
    41
    
Martin@352
    42
    private static int div(int x, int y) {
Martin@352
    43
        return x / y;
Martin@352
    44
    }
Martin@352
    45
    
Martin@352
    46
    private static int mod(int x, int y) {
Martin@352
    47
        return x % y;
Martin@352
    48
    }
Martin@352
    49
    
Martin@700
    50
    private static int neg(int x) {
Martin@700
    51
        return (-x);
Martin@700
    52
    }
Martin@700
    53
    
Martin@351
    54
    @Compare public int addOverflow() {
Martin@351
    55
        return add(Integer.MAX_VALUE, 1);
Martin@351
    56
    }
Martin@351
    57
    
Martin@351
    58
    @Compare public int subUnderflow() {
Martin@351
    59
        return sub(Integer.MIN_VALUE, 1);
Martin@351
    60
    }
Martin@351
    61
    
Martin@351
    62
    @Compare public int addMaxIntAndMaxInt() {
Martin@351
    63
        return add(Integer.MAX_VALUE, Integer.MAX_VALUE);
Martin@351
    64
    }
Martin@351
    65
    
Martin@351
    66
    @Compare public int subMinIntAndMinInt() {
Martin@351
    67
        return sub(Integer.MIN_VALUE, Integer.MIN_VALUE);
Martin@345
    68
    }
Martin@345
    69
    
Martin@345
    70
    @Compare public int multiplyMaxInt() {
Martin@351
    71
        return mul(Integer.MAX_VALUE, 2);
Martin@351
    72
    }
Martin@351
    73
    
Martin@351
    74
    @Compare public int multiplyMaxIntAndMaxInt() {
Martin@351
    75
        return mul(Integer.MAX_VALUE, Integer.MAX_VALUE);
Martin@351
    76
    }
Martin@351
    77
    
Martin@351
    78
    @Compare public int multiplyMinInt() {
Martin@351
    79
        return mul(Integer.MIN_VALUE, 2);
Martin@351
    80
    }
Martin@351
    81
    
Martin@351
    82
    @Compare public int multiplyMinIntAndMinInt() {
Martin@351
    83
        return mul(Integer.MIN_VALUE, Integer.MIN_VALUE);
Martin@351
    84
    }
Martin@351
    85
    
Martin@351
    86
    @Compare public int multiplyPrecision() {
Martin@351
    87
        return mul(119106029, 1103515245);
Martin@345
    88
    }
Martin@345
    89
    
Martin@352
    90
    @Compare public int division() {
Martin@352
    91
        return div(1, 2);
Martin@352
    92
    }
Martin@352
    93
    
Martin@352
    94
    @Compare public int divisionReminder() {
Martin@352
    95
        return mod(1, 2);
Martin@352
    96
    }
Martin@352
    97
    
Martin@700
    98
    @Compare public int negate() {
Martin@700
    99
        return neg(123456);
Martin@700
   100
    }
Martin@700
   101
    
Martin@700
   102
    @Compare public int negateMaxInt() {
Martin@700
   103
        return neg(Integer.MAX_VALUE);
Martin@700
   104
    }
Martin@700
   105
    
Martin@700
   106
    @Compare public int negateMinInt() {
Martin@700
   107
        return neg(Integer.MIN_VALUE);
Martin@700
   108
    }
Martin@700
   109
    
jaroslav@454
   110
    @Compare public int sumTwoDimensions() {
jaroslav@454
   111
        int[][] matrix = createMatrix(4, 3);
jaroslav@457
   112
        matrix[0][0] += 10;
jaroslav@457
   113
        return matrix[0][0];
jaroslav@454
   114
    }
jaroslav@454
   115
    
jaroslav@454
   116
    static int[][] createMatrix(int x, int y) {
jaroslav@457
   117
        return new int[x][y];
jaroslav@454
   118
    }
jaroslav@454
   119
    
Martin@345
   120
    @Factory
Martin@345
   121
    public static Object[] create() {
Martin@427
   122
        return VMTest.create(IntegerArithmeticTest.class);
Martin@345
   123
    }
Martin@345
   124
}