< prev index next >

test/compiler/jvmci/compilerToVM/GetStackTraceElementTest.java

Print this page
rev 9261 : 8138815: improve tests CompilerToVM::getStackTraceElement


  24 
  25 /**
  26  * @test
  27  * @bug 8136421
  28  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  29  * @library /testlibrary /../../test/lib /
  30  * @compile ../common/CompilerToVMHelper.java
  31  * @run main ClassFileInstaller jdk.vm.ci.hotspot.CompilerToVMHelper
  32  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  33  *      -Xbootclasspath/a:. compiler.jvmci.compilerToVM.GetStackTraceElementTest
  34  */
  35 
  36 package compiler.jvmci.compilerToVM;
  37 
  38 import compiler.jvmci.common.CTVMUtilities;
  39 import java.lang.reflect.Executable;
  40 import java.lang.reflect.Method;
  41 import java.lang.reflect.Modifier;
  42 import java.util.HashMap;
  43 import java.util.Map;


  44 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  45 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  46 import jdk.test.lib.Asserts;
  47 
  48 public class GetStackTraceElementTest {
  49 
  50     public static void main(String[] args) {
  51         Map<Executable, int[]> testCases = createTestCases();
  52         testCases.forEach(GetStackTraceElementTest::runSanityTest);
  53     }
  54 
  55     private static void runSanityTest(Executable aMethod, int[] bcis) {
  56         HotSpotResolvedJavaMethod method = CTVMUtilities
  57                 .getResolvedMethod(aMethod);
  58         String className = aMethod.getDeclaringClass().getName();
  59         int lastDot = className.lastIndexOf('.');
  60         int firstDol = className.contains("$")
  61                 ? className.indexOf('$')
  62                 : className.length();
  63         String fileName = className.substring(lastDot + 1, firstDol) + ".java";



  64         for (int bci : bcis) {
  65             StackTraceElement ste = CompilerToVMHelper
  66                     .getStackTraceElement(method, bci);
  67             Asserts.assertNotNull(ste);
  68             Asserts.assertEQ(ste.getClassName(), className);
  69             Asserts.assertEQ(ste.getFileName(), fileName);
  70             Asserts.assertEQ(ste.getMethodName(), aMethod.getName());
  71             Asserts.assertEQ(ste.isNativeMethod(), Modifier
  72                     .isNative(aMethod.getModifiers()));
















  73         }
  74 
  75     }
  76 








  77     private static Map<Executable, int[]> createTestCases() {
  78         Map<Executable, int[]> testCases = new HashMap<>();
  79 
  80         try {
  81             Class<?> aClass = DummyClass.class;
  82             Method aMethod = aClass.getDeclaredMethod("dummyInstanceFunction");
  83             int[] bci = new int[] {0, 2, 3, 6, 7, 8, 11, 13, 15, 16, 17, 18};
  84             testCases.put(aMethod, bci);
  85 
  86             aMethod = aClass.getDeclaredMethod("dummyEmptyFunction");
  87             bci = new int[] {0};
  88             testCases.put(aMethod, bci);







  89         } catch (NoSuchMethodException e) {
  90             throw new Error("TEST BUG : test method not found", e);
  91         }
  92         return testCases;
  93     }
  94 
  95     private class DummyClass {
  96         public int dummyInstanceFunction() {
  97             String str1 = "123123123";
  98             double x = 3.14;
  99             int y = Integer.parseInt(str1);
 100 
 101             return y / (int)x;
 102         }
 103 
 104         public void dummyEmptyFunction() {}


 105     }
 106 }


  24 
  25 /**
  26  * @test
  27  * @bug 8136421
  28  * @requires (os.simpleArch == "x64" | os.simpleArch == "sparcv9") & os.arch != "aarch64"
  29  * @library /testlibrary /../../test/lib /
  30  * @compile ../common/CompilerToVMHelper.java
  31  * @run main ClassFileInstaller jdk.vm.ci.hotspot.CompilerToVMHelper
  32  * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
  33  *      -Xbootclasspath/a:. compiler.jvmci.compilerToVM.GetStackTraceElementTest
  34  */
  35 
  36 package compiler.jvmci.compilerToVM;
  37 
  38 import compiler.jvmci.common.CTVMUtilities;
  39 import java.lang.reflect.Executable;
  40 import java.lang.reflect.Method;
  41 import java.lang.reflect.Modifier;
  42 import java.util.HashMap;
  43 import java.util.Map;
  44 
  45 import compiler.jvmci.common.testcases.TestCase;
  46 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  47 import jdk.vm.ci.hotspot.CompilerToVMHelper;
  48 import jdk.test.lib.Asserts;
  49 
  50 public class GetStackTraceElementTest {
  51 
  52     public static void main(String[] args) {
  53         Map<Executable, int[]> testCases = createTestCases();
  54         testCases.forEach(GetStackTraceElementTest::runSanityTest);
  55     }
  56 
  57     private static void runSanityTest(Executable aMethod, int[] bcis) {
  58         HotSpotResolvedJavaMethod method = CTVMUtilities
  59                 .getResolvedMethod(aMethod);
  60         String className = aMethod.getDeclaringClass().getName();
  61         String methodName = aMethod.getName().equals(className)
  62                 ? "<init>"
  63                 : aMethod.getName();
  64         String fileName = getFileName(className);
  65         Map<Integer, Integer> bciWithLineNumber = CTVMUtilities
  66                 .getBciToLineNumber(aMethod);
  67         boolean isNative = Modifier.isNative(aMethod.getModifiers());
  68         int lineNumber = -1;
  69         for (int bci : bcis) {
  70             StackTraceElement ste = CompilerToVMHelper
  71                     .getStackTraceElement(method, bci);
  72             Asserts.assertNotNull(ste, aMethod + " : got null StackTraceElement"
  73                     + " at bci " + bci);
  74             Asserts.assertEQ(className, ste.getClassName(), aMethod
  75                     + " : unexpected class name");
  76             Asserts.assertEQ(fileName, ste.getFileName(), aMethod
  77                     + " : unexpected filename");
  78             Asserts.assertEQ(methodName, ste.getMethodName(), aMethod
  79                     + " : unexpected method name");
  80             Asserts.assertEQ(isNative, ste.isNativeMethod(), aMethod
  81                     + " : unexpected 'isNative' value");
  82             if (bciWithLineNumber.size() > 0) {
  83                 if (bciWithLineNumber.containsKey(bci)) {
  84                     lineNumber = bciWithLineNumber.get(bci);
  85                 }
  86                 Asserts.assertEQ(lineNumber, ste.getLineNumber(), aMethod
  87                         + " : unexpected line number");
  88             } else {
  89                 // native and abstract function
  90                 Asserts.assertLT(0, ste.getLineNumber(),
  91                         aMethod + " : unexpected line number for abstract "
  92                                 + "or native method");
  93             }
  94         }
  95 
  96     }
  97 
  98     private static String getFileName(String className) {
  99         int lastDot = className.lastIndexOf('.');
 100         int firstDol = className.contains("$")
 101                 ? className.indexOf('$')
 102                 : className.length();
 103         return className.substring(lastDot + 1, firstDol) + ".java";
 104     }
 105 
 106     private static Map<Executable, int[]> createTestCases() {
 107         Map<Executable, int[]> testCases = new HashMap<>();
 108 
 109         try {
 110             Class<?> aClass = DummyClass.class;
 111             Method aMethod = aClass.getDeclaredMethod("dummyInstanceFunction");
 112             int[] bci = new int[] {0, 2, 3, 6, 7, 8, 11, 13, 15, 16, 17, 18};
 113             testCases.put(aMethod, bci);
 114 
 115             aMethod = aClass.getDeclaredMethod("dummyEmptyFunction");
 116             bci = new int[] {0};
 117             testCases.put(aMethod, bci);
 118 
 119             aMethod = aClass.getDeclaredMethod("nativeFunction");
 120             bci = new int[] {0};
 121             testCases.put(aMethod, bci);
 122 
 123             TestCase.getAllExecutables()
 124                     .forEach(c -> testCases.put(c, new int[] {0}));
 125         } catch (NoSuchMethodException e) {
 126             throw new Error("TEST BUG : test method not found", e);
 127         }
 128         return testCases;
 129     }
 130 
 131     private class DummyClass {
 132         public int dummyInstanceFunction() {
 133             String str1 = "123123123";
 134             double x = 3.14;
 135             int y = Integer.parseInt(str1);
 136 
 137             return y / (int)x;
 138         }
 139 
 140         public void dummyEmptyFunction() {}
 141 
 142         public native void nativeFunction();
 143     }
 144 }
< prev index next >