Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/jit/graph/test5.java
40948 views
1
/*
2
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package jit.graph;
25
26
import jdk.test.lib.Utils;
27
import nsk.share.TestFailure;
28
29
import java.lang.reflect.InvocationTargetException;
30
import java.util.Random;
31
import java.util.Vector;
32
33
class test5 {
34
private final int[] MethodID = {Globals.MethodID_Array[7],
35
Globals.MethodID_Array[8],
36
Globals.MethodID_Array[9],
37
Globals.MethodID_Array[10]};
38
private static Random loopNumGen = new Random(Utils.SEED);
39
40
private final int maxLoops = 12;
41
42
private long factorial(int n) {
43
if (n > 1) {
44
return (n * factorial(n - 1));
45
} else {
46
return (1);
47
}
48
}
49
50
private long fibonacci(long num1, long num2, int n) {
51
if (n <= 0) {
52
return (num2);
53
} else {
54
return (fibonacci(num2, num1 + num2, n - 1));
55
}
56
}
57
58
private long combination(int n, int r) {
59
if ((r == 0) || (n == r)) {
60
return 1;
61
} else {
62
return (combination(n - 1, r) + combination(n - 1, r - 1));
63
}
64
}
65
66
private int[] pascalsTriangle(int[] source, int n) {
67
if (n > 0) {
68
int sourceLength = source.length;
69
int[] temp = new int[sourceLength + 1];
70
temp[0] = 1;
71
temp[sourceLength] = 1;
72
73
int j = 1;
74
for (int i = 0; i < (sourceLength - 1); i++) {
75
temp[j++] = source[i] + source[i + 1];
76
}
77
78
return pascalsTriangle(temp, n - 1);
79
} else {
80
return source;
81
}
82
}
83
84
private boolean verifyArray(int[] ArrayToBeVerified, int[] MasterArray) {
85
if (ArrayToBeVerified.length != MasterArray.length) {
86
return false;
87
}
88
89
for (int i = 0; i < MasterArray.length; i++) {
90
if (MasterArray[i] != ArrayToBeVerified[i]) {
91
return false;
92
}
93
}
94
return true;
95
}
96
97
private int[] verifyPascal(int n) {
98
int[] pascalOut = new int[n + 1];
99
int[][] dataArray = new int[n + 1][n + 1];
100
101
for (int i = 0; i <= n; i++) {
102
for (int j = 0; j <= n; j++) {
103
if (j == 0) {
104
dataArray[i][0] = 1;
105
} else if (j == i) {
106
dataArray[i][i] = 1;
107
} else if (j < i) {
108
dataArray[i][j] = dataArray[i - 1][j - 1] + dataArray[i - 1][j];
109
}
110
}
111
}
112
113
// could be a little more efficient, but not that important
114
int j = n;
115
for (int i = 0; i <= n; i++) {
116
pascalOut[i] = dataArray[j][i];
117
}
118
return pascalOut;
119
}
120
121
private long verifyFact(int n) {
122
long answer = 1;
123
for (int i = 2; i <= n; i++) {
124
answer *= i;
125
}
126
return answer;
127
}
128
129
private long verifyFibo(int n) {
130
long num1 = 1;
131
long num2 = 1;
132
133
for (int i = 0; i < n; i++) {
134
long temp = num1 + num2;
135
num1 = num2;
136
num2 = temp;
137
}
138
139
return num2;
140
}
141
142
private long verifyComb(int n, int r) {
143
return (verifyFact(n) / (verifyFact(n - r) * verifyFact(r)));
144
}
145
146
public void factTest(Vector summation, Vector ID, Long functionDepth, Integer staticFunctionDepth)
147
throws InvocationTargetException {
148
Globals.appendSumToSummationVector(MethodID[0], summation);
149
150
if (CGT.shouldFinish()) {
151
return;
152
}
153
154
if (Globals.VERBOSE) {
155
System.out.println("test5.factTest");
156
}
157
158
if ((functionDepth.longValue() <= 0) && (staticFunctionDepth.intValue() <= 0)) {
159
return;
160
}
161
MethodData methodCallStr;
162
Long numFcalls;
163
Integer staticFcalls;
164
165
if (staticFunctionDepth.intValue() > 0) {
166
numFcalls = functionDepth;
167
staticFcalls = Integer.valueOf(staticFunctionDepth.intValue() - 1);
168
methodCallStr = Globals.returnNextStaticMethod(MethodID[0]);
169
} else {
170
numFcalls = Long.valueOf(functionDepth.longValue() - 1);
171
staticFcalls = staticFunctionDepth;
172
methodCallStr = Globals.nextRandomMethod();
173
}
174
175
int localNumLoops = loopNumGen.nextInt(maxLoops);
176
long facFunctionValue = factorial(localNumLoops);
177
long facVerValue = verifyFact(localNumLoops);
178
if (facFunctionValue != facVerValue) {
179
System.out.println("Factorial Computed Incorrectly");
180
System.out.println("Specific Factorial Requested " + localNumLoops + "!");
181
throw new TestFailure("Expected: " + facVerValue + " Actual " + facFunctionValue);
182
}
183
184
Globals.addFunctionIDToVector(methodCallStr.id, ID);
185
Globals.callMethod(methodCallStr, summation, ID, numFcalls, staticFcalls);
186
}
187
188
public void fiboTest(Vector summation, Vector ID, Long functionDepth, Integer staticFunctionDepth)
189
throws InvocationTargetException {
190
Globals.appendSumToSummationVector(MethodID[1], summation);
191
192
if (CGT.shouldFinish()) {
193
return;
194
}
195
196
if (Globals.VERBOSE) {
197
System.out.println("test5.fiboTest");
198
}
199
200
if ((functionDepth.longValue() <= 0) && (staticFunctionDepth.intValue() <= 0)) {
201
return;
202
}
203
MethodData methodCallStr;
204
Long numFcalls;
205
Integer staticFcalls;
206
if (staticFunctionDepth.intValue() > 0) {
207
numFcalls = functionDepth;
208
staticFcalls = Integer.valueOf(staticFunctionDepth.intValue() - 1);
209
methodCallStr = Globals.returnNextStaticMethod(MethodID[1]);
210
} else {
211
numFcalls = Long.valueOf(functionDepth.longValue() - 1);
212
staticFcalls = staticFunctionDepth;
213
methodCallStr = Globals.nextRandomMethod();
214
}
215
int localNumLoops = loopNumGen.nextInt(maxLoops * 3);
216
long fiboFunctionValue = fibonacci(1, 1, localNumLoops);
217
long fiboVerValue = verifyFibo(localNumLoops);
218
if (fiboFunctionValue != fiboVerValue) {
219
System.out.println("Fibonacci Series Computed Incorrectly");
220
System.out.println("Specific Digit Requested " + localNumLoops);
221
throw new TestFailure("Expected: " + fiboVerValue + " Actual " + fiboFunctionValue);
222
}
223
224
Globals.addFunctionIDToVector(methodCallStr.id, ID);
225
Globals.callMethod(methodCallStr, summation, ID, numFcalls, staticFcalls);
226
}
227
228
229
public void combTest(Vector summation, Vector ID, Long functionDepth, Integer staticFunctionDepth)
230
throws InvocationTargetException {
231
Globals.appendSumToSummationVector(MethodID[2], summation);
232
233
if (CGT.shouldFinish()) {
234
return;
235
}
236
237
if (Globals.VERBOSE) {
238
System.out.println("test5.combTest");
239
}
240
241
if ((functionDepth.longValue() <= 0) && (staticFunctionDepth.intValue() <= 0)) {
242
return;
243
}
244
MethodData methodCallStr;
245
Long numFcalls;
246
Integer staticFcalls;
247
if (staticFunctionDepth.intValue() > 0) {
248
numFcalls = functionDepth;
249
staticFcalls = Integer.valueOf(staticFunctionDepth.intValue() - 1);
250
methodCallStr = Globals.returnNextStaticMethod(MethodID[2]);
251
} else {
252
numFcalls = Long.valueOf(functionDepth.longValue() - 1);
253
staticFcalls = staticFunctionDepth;
254
methodCallStr = Globals.nextRandomMethod();
255
}
256
int n = loopNumGen.nextInt(maxLoops);
257
int k = (n > 0) ? loopNumGen.nextInt(n) : 0;
258
long combFunctionValue = combination(n, k);
259
long combVerValue = verifyComb(n, k);
260
if (combFunctionValue != combVerValue) {
261
System.out.println("Combination Computed Incorrectly");
262
System.out.println("N = " + n + "K = " + k);
263
throw new TestFailure("Expected: " + combVerValue + " Actual " + combFunctionValue);
264
}
265
266
Globals.addFunctionIDToVector(methodCallStr.id, ID);
267
Globals.callMethod(methodCallStr, summation, ID, numFcalls, staticFcalls);
268
}
269
270
271
public void pascalTest(Vector summation, Vector ID, Long functionDepth, Integer staticFunctionDepth)
272
throws InvocationTargetException {
273
Globals.appendSumToSummationVector(MethodID[3], summation);
274
275
if (CGT.shouldFinish()) {
276
return;
277
}
278
279
if (Globals.VERBOSE) {
280
System.out.println("test5.pascalTest");
281
}
282
283
int[] x = new int[1 << 30];
284
x[1 << 24] = 1;
285
286
if ((functionDepth.longValue() <= 0) && (staticFunctionDepth.intValue() <= 0)) {
287
return;
288
}
289
MethodData methodCallStr;
290
Long numFcalls;
291
Integer staticFcalls;
292
if (staticFunctionDepth.intValue() > 0) {
293
numFcalls = functionDepth;
294
staticFcalls = Integer.valueOf(staticFunctionDepth.intValue() - 1);
295
methodCallStr = Globals.returnNextStaticMethod(MethodID[3]);
296
} else {
297
numFcalls = Long.valueOf(functionDepth.longValue() - 1);
298
staticFcalls = staticFunctionDepth;
299
methodCallStr = Globals.nextRandomMethod();
300
}
301
int num = loopNumGen.nextInt(maxLoops);
302
303
int[] pascalFunctionValue = pascalsTriangle(new int[]{1}, num);
304
int[] pascalVerValue = verifyPascal(num);
305
if (!verifyArray(pascalFunctionValue, pascalVerValue)) {
306
StringBuilder temp = new StringBuilder("Expected: ");
307
for (int aPascalVerValue : pascalVerValue) {
308
temp.append(aPascalVerValue)
309
.append(", ");
310
}
311
temp.append(" Actual ");
312
for (int aPascalFunctionValue : pascalFunctionValue) {
313
temp.append(aPascalFunctionValue)
314
.append(", ");
315
}
316
System.out.println("Pascal Tringle Row Computed Incorrectly");
317
System.out.println("Row Number " + num);
318
throw new TestFailure(temp.toString());
319
}
320
321
Globals.addFunctionIDToVector(methodCallStr.id, ID);
322
Globals.callMethod(methodCallStr, summation, ID, numFcalls, staticFcalls);
323
}
324
}
325
326