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 |
}
|
lubomir@778
|
53 |
|
lubomir@778
|
54 |
private static float fadd(float x, float y) {
|
lubomir@778
|
55 |
return x + y;
|
lubomir@778
|
56 |
}
|
lubomir@778
|
57 |
|
lubomir@778
|
58 |
private static double dadd(double x, double y) {
|
lubomir@778
|
59 |
return x + y;
|
lubomir@778
|
60 |
}
|
Martin@700
|
61 |
|
Martin@351
|
62 |
@Compare public int addOverflow() {
|
Martin@351
|
63 |
return add(Integer.MAX_VALUE, 1);
|
Martin@351
|
64 |
}
|
Martin@351
|
65 |
|
Martin@351
|
66 |
@Compare public int subUnderflow() {
|
Martin@351
|
67 |
return sub(Integer.MIN_VALUE, 1);
|
Martin@351
|
68 |
}
|
Martin@351
|
69 |
|
Martin@351
|
70 |
@Compare public int addMaxIntAndMaxInt() {
|
Martin@351
|
71 |
return add(Integer.MAX_VALUE, Integer.MAX_VALUE);
|
Martin@351
|
72 |
}
|
Martin@351
|
73 |
|
Martin@351
|
74 |
@Compare public int subMinIntAndMinInt() {
|
Martin@351
|
75 |
return sub(Integer.MIN_VALUE, Integer.MIN_VALUE);
|
Martin@345
|
76 |
}
|
Martin@345
|
77 |
|
Martin@345
|
78 |
@Compare public int multiplyMaxInt() {
|
Martin@351
|
79 |
return mul(Integer.MAX_VALUE, 2);
|
Martin@351
|
80 |
}
|
Martin@351
|
81 |
|
Martin@351
|
82 |
@Compare public int multiplyMaxIntAndMaxInt() {
|
Martin@351
|
83 |
return mul(Integer.MAX_VALUE, Integer.MAX_VALUE);
|
Martin@351
|
84 |
}
|
Martin@351
|
85 |
|
Martin@351
|
86 |
@Compare public int multiplyMinInt() {
|
Martin@351
|
87 |
return mul(Integer.MIN_VALUE, 2);
|
Martin@351
|
88 |
}
|
Martin@351
|
89 |
|
Martin@351
|
90 |
@Compare public int multiplyMinIntAndMinInt() {
|
Martin@351
|
91 |
return mul(Integer.MIN_VALUE, Integer.MIN_VALUE);
|
Martin@351
|
92 |
}
|
Martin@351
|
93 |
|
Martin@351
|
94 |
@Compare public int multiplyPrecision() {
|
Martin@351
|
95 |
return mul(119106029, 1103515245);
|
Martin@345
|
96 |
}
|
Martin@345
|
97 |
|
Martin@352
|
98 |
@Compare public int division() {
|
Martin@352
|
99 |
return div(1, 2);
|
Martin@352
|
100 |
}
|
lubomir@778
|
101 |
|
Martin@352
|
102 |
@Compare public int divisionReminder() {
|
Martin@352
|
103 |
return mod(1, 2);
|
Martin@352
|
104 |
}
|
lubomir@737
|
105 |
|
lubomir@737
|
106 |
@Compare public int negativeDivision() {
|
lubomir@737
|
107 |
return div(-7, 3);
|
lubomir@737
|
108 |
}
|
lubomir@737
|
109 |
|
lubomir@737
|
110 |
@Compare public int negativeDivisionReminder() {
|
lubomir@737
|
111 |
return mod(-7, 3);
|
lubomir@737
|
112 |
}
|
lubomir@737
|
113 |
|
lubomir@778
|
114 |
@Compare public int conversionFromFloat() {
|
lubomir@778
|
115 |
return (int) fadd(-2, -0.6f);
|
lubomir@778
|
116 |
}
|
lubomir@778
|
117 |
|
lubomir@778
|
118 |
@Compare public int conversionFromDouble() {
|
lubomir@778
|
119 |
return (int) dadd(-2, -0.6);
|
lubomir@778
|
120 |
}
|
lubomir@778
|
121 |
|
lubomir@737
|
122 |
@Compare public boolean divByZeroThrowsArithmeticException() {
|
lubomir@737
|
123 |
try {
|
lubomir@737
|
124 |
div(1, 0);
|
lubomir@737
|
125 |
return false;
|
lubomir@737
|
126 |
} catch (final ArithmeticException e) {
|
lubomir@737
|
127 |
return true;
|
lubomir@737
|
128 |
}
|
lubomir@737
|
129 |
}
|
lubomir@737
|
130 |
|
lubomir@737
|
131 |
@Compare public boolean modByZeroThrowsArithmeticException() {
|
lubomir@737
|
132 |
try {
|
lubomir@737
|
133 |
mod(1, 0);
|
lubomir@737
|
134 |
return false;
|
lubomir@737
|
135 |
} catch (final ArithmeticException e) {
|
lubomir@737
|
136 |
return true;
|
lubomir@737
|
137 |
}
|
lubomir@737
|
138 |
}
|
lubomir@737
|
139 |
|
Martin@700
|
140 |
@Compare public int negate() {
|
Martin@700
|
141 |
return neg(123456);
|
Martin@700
|
142 |
}
|
Martin@700
|
143 |
|
Martin@700
|
144 |
@Compare public int negateMaxInt() {
|
Martin@700
|
145 |
return neg(Integer.MAX_VALUE);
|
Martin@700
|
146 |
}
|
Martin@700
|
147 |
|
Martin@700
|
148 |
@Compare public int negateMinInt() {
|
Martin@700
|
149 |
return neg(Integer.MIN_VALUE);
|
Martin@700
|
150 |
}
|
Martin@700
|
151 |
|
jaroslav@454
|
152 |
@Compare public int sumTwoDimensions() {
|
jaroslav@454
|
153 |
int[][] matrix = createMatrix(4, 3);
|
jaroslav@457
|
154 |
matrix[0][0] += 10;
|
jaroslav@457
|
155 |
return matrix[0][0];
|
jaroslav@454
|
156 |
}
|
jaroslav@454
|
157 |
|
jaroslav@454
|
158 |
static int[][] createMatrix(int x, int y) {
|
jaroslav@457
|
159 |
return new int[x][y];
|
jaroslav@454
|
160 |
}
|
jaroslav@454
|
161 |
|
Martin@345
|
162 |
@Factory
|
Martin@345
|
163 |
public static Object[] create() {
|
Martin@427
|
164 |
return VMTest.create(IntegerArithmeticTest.class);
|
Martin@345
|
165 |
}
|
Martin@345
|
166 |
}
|