Path: blob/master/test/hotspot/jtreg/vmTestbase/jit/graph/test5.java
40948 views
/*1* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package jit.graph;2425import jdk.test.lib.Utils;26import nsk.share.TestFailure;2728import java.lang.reflect.InvocationTargetException;29import java.util.Random;30import java.util.Vector;3132class test5 {33private final int[] MethodID = {Globals.MethodID_Array[7],34Globals.MethodID_Array[8],35Globals.MethodID_Array[9],36Globals.MethodID_Array[10]};37private static Random loopNumGen = new Random(Utils.SEED);3839private final int maxLoops = 12;4041private long factorial(int n) {42if (n > 1) {43return (n * factorial(n - 1));44} else {45return (1);46}47}4849private long fibonacci(long num1, long num2, int n) {50if (n <= 0) {51return (num2);52} else {53return (fibonacci(num2, num1 + num2, n - 1));54}55}5657private long combination(int n, int r) {58if ((r == 0) || (n == r)) {59return 1;60} else {61return (combination(n - 1, r) + combination(n - 1, r - 1));62}63}6465private int[] pascalsTriangle(int[] source, int n) {66if (n > 0) {67int sourceLength = source.length;68int[] temp = new int[sourceLength + 1];69temp[0] = 1;70temp[sourceLength] = 1;7172int j = 1;73for (int i = 0; i < (sourceLength - 1); i++) {74temp[j++] = source[i] + source[i + 1];75}7677return pascalsTriangle(temp, n - 1);78} else {79return source;80}81}8283private boolean verifyArray(int[] ArrayToBeVerified, int[] MasterArray) {84if (ArrayToBeVerified.length != MasterArray.length) {85return false;86}8788for (int i = 0; i < MasterArray.length; i++) {89if (MasterArray[i] != ArrayToBeVerified[i]) {90return false;91}92}93return true;94}9596private int[] verifyPascal(int n) {97int[] pascalOut = new int[n + 1];98int[][] dataArray = new int[n + 1][n + 1];99100for (int i = 0; i <= n; i++) {101for (int j = 0; j <= n; j++) {102if (j == 0) {103dataArray[i][0] = 1;104} else if (j == i) {105dataArray[i][i] = 1;106} else if (j < i) {107dataArray[i][j] = dataArray[i - 1][j - 1] + dataArray[i - 1][j];108}109}110}111112// could be a little more efficient, but not that important113int j = n;114for (int i = 0; i <= n; i++) {115pascalOut[i] = dataArray[j][i];116}117return pascalOut;118}119120private long verifyFact(int n) {121long answer = 1;122for (int i = 2; i <= n; i++) {123answer *= i;124}125return answer;126}127128private long verifyFibo(int n) {129long num1 = 1;130long num2 = 1;131132for (int i = 0; i < n; i++) {133long temp = num1 + num2;134num1 = num2;135num2 = temp;136}137138return num2;139}140141private long verifyComb(int n, int r) {142return (verifyFact(n) / (verifyFact(n - r) * verifyFact(r)));143}144145public void factTest(Vector summation, Vector ID, Long functionDepth, Integer staticFunctionDepth)146throws InvocationTargetException {147Globals.appendSumToSummationVector(MethodID[0], summation);148149if (CGT.shouldFinish()) {150return;151}152153if (Globals.VERBOSE) {154System.out.println("test5.factTest");155}156157if ((functionDepth.longValue() <= 0) && (staticFunctionDepth.intValue() <= 0)) {158return;159}160MethodData methodCallStr;161Long numFcalls;162Integer staticFcalls;163164if (staticFunctionDepth.intValue() > 0) {165numFcalls = functionDepth;166staticFcalls = Integer.valueOf(staticFunctionDepth.intValue() - 1);167methodCallStr = Globals.returnNextStaticMethod(MethodID[0]);168} else {169numFcalls = Long.valueOf(functionDepth.longValue() - 1);170staticFcalls = staticFunctionDepth;171methodCallStr = Globals.nextRandomMethod();172}173174int localNumLoops = loopNumGen.nextInt(maxLoops);175long facFunctionValue = factorial(localNumLoops);176long facVerValue = verifyFact(localNumLoops);177if (facFunctionValue != facVerValue) {178System.out.println("Factorial Computed Incorrectly");179System.out.println("Specific Factorial Requested " + localNumLoops + "!");180throw new TestFailure("Expected: " + facVerValue + " Actual " + facFunctionValue);181}182183Globals.addFunctionIDToVector(methodCallStr.id, ID);184Globals.callMethod(methodCallStr, summation, ID, numFcalls, staticFcalls);185}186187public void fiboTest(Vector summation, Vector ID, Long functionDepth, Integer staticFunctionDepth)188throws InvocationTargetException {189Globals.appendSumToSummationVector(MethodID[1], summation);190191if (CGT.shouldFinish()) {192return;193}194195if (Globals.VERBOSE) {196System.out.println("test5.fiboTest");197}198199if ((functionDepth.longValue() <= 0) && (staticFunctionDepth.intValue() <= 0)) {200return;201}202MethodData methodCallStr;203Long numFcalls;204Integer staticFcalls;205if (staticFunctionDepth.intValue() > 0) {206numFcalls = functionDepth;207staticFcalls = Integer.valueOf(staticFunctionDepth.intValue() - 1);208methodCallStr = Globals.returnNextStaticMethod(MethodID[1]);209} else {210numFcalls = Long.valueOf(functionDepth.longValue() - 1);211staticFcalls = staticFunctionDepth;212methodCallStr = Globals.nextRandomMethod();213}214int localNumLoops = loopNumGen.nextInt(maxLoops * 3);215long fiboFunctionValue = fibonacci(1, 1, localNumLoops);216long fiboVerValue = verifyFibo(localNumLoops);217if (fiboFunctionValue != fiboVerValue) {218System.out.println("Fibonacci Series Computed Incorrectly");219System.out.println("Specific Digit Requested " + localNumLoops);220throw new TestFailure("Expected: " + fiboVerValue + " Actual " + fiboFunctionValue);221}222223Globals.addFunctionIDToVector(methodCallStr.id, ID);224Globals.callMethod(methodCallStr, summation, ID, numFcalls, staticFcalls);225}226227228public void combTest(Vector summation, Vector ID, Long functionDepth, Integer staticFunctionDepth)229throws InvocationTargetException {230Globals.appendSumToSummationVector(MethodID[2], summation);231232if (CGT.shouldFinish()) {233return;234}235236if (Globals.VERBOSE) {237System.out.println("test5.combTest");238}239240if ((functionDepth.longValue() <= 0) && (staticFunctionDepth.intValue() <= 0)) {241return;242}243MethodData methodCallStr;244Long numFcalls;245Integer staticFcalls;246if (staticFunctionDepth.intValue() > 0) {247numFcalls = functionDepth;248staticFcalls = Integer.valueOf(staticFunctionDepth.intValue() - 1);249methodCallStr = Globals.returnNextStaticMethod(MethodID[2]);250} else {251numFcalls = Long.valueOf(functionDepth.longValue() - 1);252staticFcalls = staticFunctionDepth;253methodCallStr = Globals.nextRandomMethod();254}255int n = loopNumGen.nextInt(maxLoops);256int k = (n > 0) ? loopNumGen.nextInt(n) : 0;257long combFunctionValue = combination(n, k);258long combVerValue = verifyComb(n, k);259if (combFunctionValue != combVerValue) {260System.out.println("Combination Computed Incorrectly");261System.out.println("N = " + n + "K = " + k);262throw new TestFailure("Expected: " + combVerValue + " Actual " + combFunctionValue);263}264265Globals.addFunctionIDToVector(methodCallStr.id, ID);266Globals.callMethod(methodCallStr, summation, ID, numFcalls, staticFcalls);267}268269270public void pascalTest(Vector summation, Vector ID, Long functionDepth, Integer staticFunctionDepth)271throws InvocationTargetException {272Globals.appendSumToSummationVector(MethodID[3], summation);273274if (CGT.shouldFinish()) {275return;276}277278if (Globals.VERBOSE) {279System.out.println("test5.pascalTest");280}281282int[] x = new int[1 << 30];283x[1 << 24] = 1;284285if ((functionDepth.longValue() <= 0) && (staticFunctionDepth.intValue() <= 0)) {286return;287}288MethodData methodCallStr;289Long numFcalls;290Integer staticFcalls;291if (staticFunctionDepth.intValue() > 0) {292numFcalls = functionDepth;293staticFcalls = Integer.valueOf(staticFunctionDepth.intValue() - 1);294methodCallStr = Globals.returnNextStaticMethod(MethodID[3]);295} else {296numFcalls = Long.valueOf(functionDepth.longValue() - 1);297staticFcalls = staticFunctionDepth;298methodCallStr = Globals.nextRandomMethod();299}300int num = loopNumGen.nextInt(maxLoops);301302int[] pascalFunctionValue = pascalsTriangle(new int[]{1}, num);303int[] pascalVerValue = verifyPascal(num);304if (!verifyArray(pascalFunctionValue, pascalVerValue)) {305StringBuilder temp = new StringBuilder("Expected: ");306for (int aPascalVerValue : pascalVerValue) {307temp.append(aPascalVerValue)308.append(", ");309}310temp.append(" Actual ");311for (int aPascalFunctionValue : pascalFunctionValue) {312temp.append(aPascalFunctionValue)313.append(", ");314}315System.out.println("Pascal Tringle Row Computed Incorrectly");316System.out.println("Row Number " + num);317throw new TestFailure(temp.toString());318}319320Globals.addFunctionIDToVector(methodCallStr.id, ID);321Globals.callMethod(methodCallStr, summation, ID, numFcalls, staticFcalls);322}323}324325326