/* * Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* This code generate an LTTest.java and LTTest.c files with corresponding *.cfg files which containt test defenition * for JNI testing. * */ import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Random; //Java and C file generator for JNI tests public class JniArmHFTestGenerator { private final String JAVA_CLASS_NAME = "LTTest"; private final String C_FNC_NAME_PREFIX = "Java_vm_jit_LongTransitions_" + JAVA_CLASS_NAME + "_"; private final String JAVA_PACKAGE_NAME = "vm.jit.LongTransitions"; private final String TEST_LIST_PREFIX = "vm/jit/LongTransitions"; private final String TEST_LIST_NAME = "vm.jit.long.testlist"; //Maximal line length for some SRC generation function private final int LINE_LENGTH = 70; //127 is maximal value for JAVA parameters list private final int PARAMS_COUNTER = 127; StringBuilder javaFileSrc; StringBuilder cFileSrc; //StringBuilder cfgFileSrc; private String javaTxtFileName; private String cTxtFileName; private File outputDir; private ArrayList javaFncNames = new ArrayList(); public static void main(String[] args) throws IOException { new JniArmHFTestGenerator().makeSomeTest(); } public void makeSomeTest() throws IOException { makeJavaIntro(JAVA_CLASS_NAME); makeCIntro(); int cnt = PARAMS_COUNTER; makeOneTest(cnt, ParamType.EASY_F, TestType.RANDOM, "nativeFnc1"); makeOneTest(cnt, ParamType.EASY_D, TestType.RANDOM, "nativeFnc2"); makeOneTest(cnt, ParamType.MIXED_FI1, TestType.RANDOM, "nativeFnc3"); makeOneTest(cnt, ParamType.MIXED_FB1, TestType.RANDOM, "nativeFnc4"); makeOneTest(cnt, ParamType.MIXED_FBI1, TestType.RANDOM, "nativeFnc5"); makeOneTest(cnt, ParamType.MIXED_DI1, TestType.RANDOM, "nativeFnc6"); makeOneTest(cnt, ParamType.MIXED_DB1, TestType.RANDOM, "nativeFnc7"); makeOneTest(cnt, ParamType.MIXED_DBI1, TestType.RANDOM, "nativeFnc8"); makeOneTest(cnt, ParamType.MIXED_DFBI1, TestType.RANDOM, "nativeFnc9"); makeOneTest(cnt, ParamType.MIXED_DF1, TestType.RANDOM, "nativeFnc10"); makeOneTest(cnt, ParamType.MIXED_DFI1, TestType.RANDOM, "nativeFnc11"); makeOneTest(cnt, ParamType.MIXED_DFB1, TestType.RANDOM, "nativeFnc12"); makeOneTest(cnt, ParamType.MIXED_FI2, TestType.RANDOM, "nativeFnc13"); makeOneTest(cnt, ParamType.MIXED_FB2, TestType.RANDOM, "nativeFnc14"); makeOneTest(cnt, ParamType.MIXED_FBI2, TestType.RANDOM, "nativeFnc15"); makeOneTest(cnt, ParamType.MIXED_DI2, TestType.RANDOM, "nativeFnc16"); makeOneTest(cnt, ParamType.MIXED_DB2, TestType.RANDOM, "nativeFnc17"); makeOneTest(cnt, ParamType.MIXED_DBI2, TestType.RANDOM, "nativeFnc18"); makeOneTest(cnt, ParamType.MIXED_DFBI2, TestType.RANDOM, "nativeFnc19"); makeOneTest(cnt, ParamType.MIXED_DF2, TestType.RANDOM, "nativeFnc20"); makeOneTest(cnt, ParamType.MIXED_DFI2, TestType.RANDOM, "nativeFnc21"); makeOneTest(cnt, ParamType.MIXED_DFB2, TestType.RANDOM, "nativeFnc22"); makeOneTest(cnt, ParamType.MIXED_FI3, TestType.RANDOM, "nativeFnc23"); makeOneTest(cnt, ParamType.MIXED_FB3, TestType.RANDOM, "nativeFnc24"); makeOneTest(cnt, ParamType.MIXED_FBI3, TestType.RANDOM, "nativeFnc25"); makeOneTest(cnt, ParamType.MIXED_DI3, TestType.RANDOM, "nativeFnc26"); makeOneTest(cnt, ParamType.MIXED_DB3, TestType.RANDOM, "nativeFnc27"); makeOneTest(cnt, ParamType.MIXED_DBI3, TestType.RANDOM, "nativeFnc28"); makeOneTest(cnt, ParamType.MIXED_DFBI3, TestType.RANDOM, "nativeFnc29"); makeOneTest(cnt, ParamType.MIXED_DF3, TestType.RANDOM, "nativeFnc30"); makeOneTest(cnt, ParamType.MIXED_DFI3, TestType.RANDOM, "nativeFnc31"); makeOneTest(cnt, ParamType.MIXED_DFB3, TestType.RANDOM, "nativeFnc32"); makeOneTest(cnt, ParamType.MIXED_FI4, TestType.RANDOM, "nativeFnc33"); makeOneTest(cnt, ParamType.MIXED_FB4, TestType.RANDOM, "nativeFnc34"); makeOneTest(cnt, ParamType.MIXED_FBI4, TestType.RANDOM, "nativeFnc35"); makeOneTest(cnt, ParamType.MIXED_DI4, TestType.RANDOM, "nativeFnc36"); makeOneTest(cnt, ParamType.MIXED_DB4, TestType.RANDOM, "nativeFnc37"); makeOneTest(cnt, ParamType.MIXED_DBI4, TestType.RANDOM, "nativeFnc38"); makeOneTest(cnt, ParamType.MIXED_DFBI4, TestType.RANDOM, "nativeFnc39"); makeOneTest(cnt, ParamType.MIXED_DF4, TestType.RANDOM, "nativeFnc40"); makeOneTest(cnt, ParamType.MIXED_DFI4, TestType.RANDOM, "nativeFnc41"); makeOneTest(cnt, ParamType.MIXED_DFB4, TestType.RANDOM, "nativeFnc42"); makeOneTest(cnt, ParamType.MIXED_FI5, TestType.RANDOM, "nativeFnc43"); makeOneTest(cnt, ParamType.MIXED_FB5, TestType.RANDOM, "nativeFnc44"); makeOneTest(cnt, ParamType.MIXED_FBI5, TestType.RANDOM, "nativeFnc45"); makeOneTest(cnt, ParamType.MIXED_DI5, TestType.RANDOM, "nativeFnc46"); makeOneTest(cnt, ParamType.MIXED_DB5, TestType.RANDOM, "nativeFnc47"); makeOneTest(cnt, ParamType.MIXED_DBI5, TestType.RANDOM, "nativeFnc48"); makeOneTest(cnt, ParamType.MIXED_DFBI5, TestType.RANDOM, "nativeFnc49"); makeOneTest(cnt, ParamType.MIXED_DF5, TestType.RANDOM, "nativeFnc50"); makeOneTest(cnt, ParamType.MIXED_DFI5, TestType.RANDOM, "nativeFnc51"); makeOneTest(cnt, ParamType.MIXED_DFB5, TestType.RANDOM, "nativeFnc52"); makeJavaInvoke(); makeJavaOutro(); writeToJavaFile(JAVA_CLASS_NAME); writeToCFile(JAVA_CLASS_NAME); //writeCmpFile(); writeTestListFile(); writeCfgFile(); } private void makeJavaInvoke() { appendToJava(" public static void main(String[] args) throws Exception"); appendToJava(" {"); appendToJava(" if ( System.getProperty(\"os.name\").matches(\".*[Ww][Ii][Nn].*\") )"); appendToJava(" {"); appendToJava(" System.out.println(\"TEST PASSED! Dummy execution on Windows* OS!\");"); appendToJava(" return;"); appendToJava(" }"); appendToJava(" deleteFiles();"); appendToJavaN(" PrintStream ps=new PrintStream(new File(\""); appendToJavaN(javaTxtFileName); appendToJava("\"));"); appendToJava(" if (args.length>0)"); appendToJava(" switch(args[0])"); appendToJava(" {"); for (String s : javaFncNames) { appendToJava(" case \"" + s + "\":"); appendToJavaN(" "); appendToJavaN(s); appendToJava("_invoke(ps);"); appendToJava(" break;"); } appendToJava(" default:"); appendToJava(" throw new Exception(\"FAIL: invalid args!\");"); appendToJava(" }"); appendToJava(" else"); appendToJava(" {"); appendToJava(" throw new Exception(\"FAIL: invalid args!\");"); appendToJava(" }"); } private void makeCIntro() { //appendToC(" //------------------------C----------------------------"); //appendToC("#include \""+className+".h\""); appendToC("#include "); appendToC("#include "); } private void makeJavaIntro(String className) { cFileSrc = new StringBuilder(); javaFileSrc = new StringBuilder(); File outputDirL1 = new File("vm" + File.separatorChar + "jit" + File.separatorChar + "LongTransitions"); //outputDir=new File(outputDirL1,className); outputDir = outputDirL1; outputDir.mkdirs(); javaTxtFileName = className + "_java.txt"; cTxtFileName = className + "_c.txt"; appendToJava("package " + JAVA_PACKAGE_NAME + ";"); appendToJava("import java.io.*;"); appendToJava("import java.util.Random;"); appendToJava("import jdk.test.lib.Utils;"); appendToJava("public class " + className); appendToJava("{"); appendToJava(" public static boolean flag = false;"); appendToJava(" static Random rnd;"); appendToJava(" static{"); appendToJava(" rnd=Utils.getRandomInstance();"); appendToJava(" System.loadLibrary(\"" + className + "\");"); appendToJava(" }"); //Different randoms appendToJava(" public static int getRndInt(){return rnd.nextInt(Integer.MAX_VALUE);}"); appendToJava(" public static float getRndFloat(){return rnd.nextFloat()*Float.MAX_VALUE;}"); appendToJava(" public static double getRndDouble(){return rnd.nextDouble()*Double.MAX_VALUE;}"); appendToJava(" public static byte getRndByte(){return (byte)rnd.nextInt(Byte.MAX_VALUE);}"); //deleteFiles method appendToJava(" private static void deleteFiles()"); appendToJava(" {"); appendToJavaN(" File f=new File(\""); appendToJavaN(javaTxtFileName); appendToJava("\");"); appendToJava(" if ( f.exists())"); appendToJava(" f.delete();"); appendToJavaN(" f=new File(\""); appendToJavaN(cTxtFileName); appendToJava("\");"); appendToJava(" if ( f.exists())"); appendToJava(" f.delete();"); appendToJava(" }"); } private void makeJavaOutro() { appendToJava(" flag=chkFile();"); appendToJava(" if(!flag)"); appendToJava(" throw new Exception(\"FAIL:Tests failed!\"); "); appendToJava(" }"); appendToJava(" private static boolean chkFile()"); appendToJava(" {"); appendToJava(" File javaFile=new File(\"" + javaTxtFileName + "\");"); appendToJava(" if (! javaFile.exists())"); appendToJava(" {"); appendToJava(" System.out.println(\"FAIL:Failed to open file " + javaTxtFileName + " - file not exists!\");"); appendToJava(" return false;"); appendToJava(" }"); appendToJava(" File cFile=new File(\"" + cTxtFileName + "\");"); appendToJava(" if (! cFile.exists())"); appendToJava(" {"); appendToJava(" System.out.println(\"FAIL:Failed to open file " + cTxtFileName + " - file not exists!\");"); appendToJava(" return false;"); appendToJava(" }"); appendToJava(" if ( cFile.length()!=javaFile.length() )"); appendToJava(" {"); appendToJava(" System.out.println(\"FAIL:File length not equal!\");"); appendToJava(" return false;"); appendToJava(" }"); appendToJava(" long byteCount=cFile.length();"); appendToJava(" try{"); appendToJava(" FileInputStream fisC=new FileInputStream(cFile);"); appendToJava(" FileInputStream fisJava=new FileInputStream(javaFile);"); appendToJava(" byte[] cData=new byte[fisC.available()];"); appendToJava(" fisC.read(cData);"); appendToJava(" byte[] javaData=new byte[fisJava.available()];"); appendToJava(" fisJava.read(javaData);"); appendToJava(" for ( int cnt=0;cnt value = new ArrayList(); ParamType(NumberType... type) { value.addAll(Arrays.asList(type)); } public ArrayList getValue() { return value; } } class GeneratorLogic { public String generateJava(String fncName, ParameterList parmOut, ParameterList parmType, int maxLength, String prefix) { StringBuilder sb = new StringBuilder(); sb.append(" {\n").append(prefix); int lineLength = 0; for (ParameterItem p : parmType.getList()) { sb.append(p.value1); sb.append(" "); sb.append(p.value2); sb.append("="); sb.append(p.value3); sb.append(";"); if (sb.length() - lineLength > maxLength) { sb.append("\n").append(prefix); lineLength = sb.length(); } } sb.append("\n"); sb.append(prefix); for (ParameterItem p : parmOut.getList()) { sb.append("ps.format(\""); sb.append(p.value2); sb.append("="); sb.append(p.value1); sb.append("\\n\","); sb.append(p.value2); sb.append(");"); if (sb.length() - lineLength > maxLength) { sb.append("\n").append(prefix); lineLength = sb.length(); } } sb.append("\n").append(prefix); sb.append(fncName); sb.append("("); for (ParameterItem p : parmOut.getList()) { sb.append(p.getDelim()); sb.append(p.value2); if (sb.length() - lineLength > maxLength) { sb.append("\n").append(prefix); lineLength = sb.length(); } } sb.append(");\n}\n"); return sb.toString(); } } //Test types enum TestType { RANDOM(new GeneratorLogic()); private GeneratorLogic genLogic; TestType(GeneratorLogic genLogic) { this.genLogic = genLogic; } public GeneratorLogic getLogic() { return genLogic; } } //One item for ParameterList (delimiter and value to build strings) class ParameterItem { private String delimiter; String value1; String value2; String value3; //private String endVal=""; public ParameterItem(String delimiter, String value) { this.delimiter = delimiter; this.value1 = value; value2 = ""; value3 = ""; } public ParameterItem(String delimiter, String value1, String value2) { this.delimiter = delimiter; this.value1 = value1; this.value2 = value2; value3 = ""; } public ParameterItem(String delimiter, String value1, String value2, String value3) { this.delimiter = delimiter; this.value1 = value1; this.value2 = value2; this.value3 = value3; } public String toString() { return delimiter + value1 + value2 + value3; } public String getDelim() { return delimiter; } } //Delimiters and values to generate calls, etc. class ParameterList { ArrayList list = new ArrayList(); private String initDelim; private String delim; boolean lineBreak; public ParameterList(String initialDelimiter, String delimiter, boolean lineBreak) { initDelim = initialDelimiter; delim = delimiter; this.lineBreak = lineBreak; } public ArrayList getList() { return list; } public void add(String value) { list.add(new ParameterItem(initDelim, value)); initDelim = delim; } public void add(String value1, String value2) { list.add(new ParameterItem(initDelim, value1, value2)); initDelim = delim; } public void add(String value1, String value2, String value3) { list.add(new ParameterItem(initDelim, value1, value2, value3)); initDelim = delim; } public ParameterItem get(int id) { return list.get(id); } public String toString() { StringBuilder sb = new StringBuilder(); for (ParameterItem p : list) { sb.append(p.toString()); if (lineBreak) { sb.append("\n"); } } return sb.toString(); } public String toFormatString(String prefix, int maxLength) { StringBuilder sb = new StringBuilder(); int charCount = 0; for (ParameterItem p : list) { sb.append(p.toString()); if (sb.length() - charCount > maxLength) { sb.append("\n").append(prefix); charCount = sb.length(); } } return sb.toString(); } public int size() { return list.size(); } public String getAllItemExclude(int excludeId) { StringBuilder sb = new StringBuilder(); int cnt = list.size(); for (int id = 0; id < cnt; ++id) { ParameterItem p = list.get(id); if (id == excludeId) { sb.append(p.getDelim()); sb.append("0"); } else { sb.append(p.toString()); } } return sb.toString(); } public String getAllItemExcludeNot(int excludeId) { StringBuilder sb = new StringBuilder(); int cnt = list.size(); for (int id = 0; id < cnt; ++id) { ParameterItem p = list.get(id); if (id != excludeId) { sb.append(p.getDelim()); sb.append("0"); // sb.append(p.getEnd()); } else { sb.append(p.toString()); } } return sb.toString(); } } class ParameterListGenerator { private ArrayList list = new ArrayList(); private Random random; private int typeCnt; ParameterList cParms = new ParameterList(",", ",", true); ParameterList rParms = new ParameterList("", ",", true); ParameterList jParms = new ParameterList("", ",", true); ParameterList cExec = new ParameterList("", "", true); ParameterList jExecParm = new ParameterList("", ",", true); ParameterList jExecOut = new ParameterList("", ",", true); public ParameterListGenerator(int length, ParamType paramType) { generateRndTest(paramType, length); } private void add(ParameterList list, String s1) { list.add(s1); } private void add(ParameterList list, String s1, String s2) { list.add(s1, s2); } private void add(ParameterList list, String s1, String s2, String s3) { list.add(s1, s2, s3); } private void generateRndTest(ParamType paramType, int length) { generateTypeList(paramType); typeCnt = list.size(); random = new Random(); random.setSeed(System.currentTimeMillis()); for (int cnt = 0; cnt < length; ++cnt) { int type = random.nextInt(typeCnt); String randomVal = list.get(type).getFnc(); String ctype = list.get(type).getСType(); String jtype = list.get(type).getJType(); String cconv = list.get(type).getСConv(); String jconv = list.get(type).getJConv(); String varName = "p" + cnt; add(jParms, jtype, varName); //jParmGen.add(jtype + varName); //cParms.add(ctype + varName); add(cParms, ctype, varName); //rParms.add(varName); add(rParms, varName); add(cExec, varName, cconv); add(jExecParm, jtype, varName, randomVal); //jExecOut.add(new PairString(jconv,varName,jtype)); add(jExecOut, jconv, varName, jtype); } } public ParameterList getCExec() { return cExec; } public ParameterList getCParm() { return cParms; } //Return generated java text public ParameterList getJExecParameters() { return jExecParm; } public ParameterList getJExecOut() { return jExecOut; } /* * Return string containing parameters for Java */ public ParameterList getJParm() { return jParms; } public ParameterList getRParm() { return rParms; } private void generateTypeList(ParamType paramType) { list.addAll(paramType.getValue()); } } class SrcGenerator { ParameterListGenerator parmListGen; GeneratorLogic genLogic; int iterationCounter; int parametersCounter; ParamType paramType; TestType testType; String fncName; //SrcGenerator g1=new SrcGenerator(3,1,ParamType.MIXED_DFIB,TestType.RANDOM,"callRandom"); int maxLength; public SrcGenerator(int parmCounter, int iterCounter, ParamType parType, TestType testType, String fncName, int maxLength) { this.maxLength = maxLength; parametersCounter = parmCounter; iterationCounter = iterCounter; paramType = parType; this.testType = testType; this.fncName = fncName; parmListGen = new ParameterListGenerator(parametersCounter, paramType); genLogic = testType.getLogic(); } public String getFncName() { return fncName; } public ParameterList getJParm() { return parmListGen.getJParm(); } public ParameterList getCParm() { return parmListGen.getCParm(); } public String getCExec() { StringBuilder sb = new StringBuilder(); for (ParameterItem p : parmListGen.getCExec().getList()) { sb.append("fprintf(file,\""); sb.append(p.value1); sb.append("="); sb.append(p.value2); sb.append("\\n\","); sb.append(p.value1); sb.append(");\n"); } return sb.toString(); } public String getCExec(int maxLength,String prefix) { int lineLength = 0; StringBuilder sb = new StringBuilder(); for (ParameterItem p : parmListGen.getCExec().getList()) { sb.append("fprintf(file,\""); sb.append(p.value1); sb.append("="); sb.append(p.value2); sb.append("\\n\","); sb.append(p.value1); sb.append(");"); if (sb.length() - lineLength > maxLength) { lineLength = sb.length(); sb.append("\n").append(prefix); } } return sb.toString(); } public String getJExec(String prefix) { return genLogic.generateJava(fncName, parmListGen.getJExecOut(), parmListGen.getJExecParameters(), maxLength, prefix); } }