1 /*
   2  * Copyright (c) 2014, 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 /*
  25  * @test CorrectnessTest
  26  * @bug 8038418
  27  * @library /testlibrary /testlibrary/whitebox
  28  * @compile execution/TypeConflict.java execution/TypeProfile.java
  29  *          execution/MethodHandleDelegate.java
  30  * @build CorrectnessTest
  31  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  32  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions
  33  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  34  *                   -XX:TypeProfileLevel=222 -XX:+UseTypeSpeculation
  35  *                   -XX:CompileCommand=exclude,execution/*::methodNotToCompile
  36  *                   -XX:CompileCommand=dontinline,scenarios/Scenario::collectReturnType
  37  *                   CorrectnessTest RETURN
  38  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions
  39  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  40  *                   -XX:TypeProfileLevel=222 -XX:+UseTypeSpeculation
  41  *                   -XX:CompileCommand=exclude,execution/*::methodNotToCompile
  42  *                   -XX:CompileCommand=dontinline,scenarios/Scenario::collectReturnType
  43  *                   CorrectnessTest PARAMETERS
  44  * @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockExperimentalVMOptions
  45  *                   -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  46  *                   -XX:TypeProfileLevel=222 -XX:+UseTypeSpeculation
  47  *                   -XX:CompileCommand=exclude,execution/*::methodNotToCompile
  48  *                   -XX:CompileCommand=dontinline,scenarios/Scenario::collectReturnType
  49  *                   CorrectnessTest ARGUMENTS
  50  * @summary Tests correctness of type usage with type profiling and speculations
  51  */
  52 
  53 import com.oracle.java.testlibrary.Asserts;
  54 import com.oracle.java.testlibrary.Platform;
  55 import execution.Execution;
  56 import execution.MethodHandleDelegate;
  57 import execution.TypeConflict;
  58 import execution.TypeProfile;
  59 import hierarchies.*;
  60 import scenarios.*;
  61 import sun.hotspot.WhiteBox;
  62 
  63 import java.lang.reflect.Constructor;
  64 import java.lang.reflect.Method;
  65 import java.util.ArrayList;
  66 import java.util.List;
  67 import java.util.function.BiFunction;
  68 
  69 public class CorrectnessTest {
  70     private static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
  71 
  72     public static void main(String[] args) {
  73         if (!Platform.isServer()) {
  74             System.out.println("ALL TESTS SKIPPED");
  75         }
  76         Asserts.assertGTE(args.length, 1);
  77         ProfilingType profilingType = ProfilingType.valueOf(args[0]);
  78         if (runTests(profilingType)) {
  79             System.out.println("ALL TESTS PASSED");
  80         } else {
  81             throw new RuntimeException("SOME TESTS FAILED");
  82         }
  83     }
  84 
  85     @SuppressWarnings("unchecked")
  86     public static boolean runTests(ProfilingType profilingType) {
  87         boolean result = true;
  88 
  89         List<Execution> executionList = new ArrayList<>();
  90         executionList.add(new TypeConflict());
  91         executionList.add(new TypeProfile());
  92         for (int i = 0, n = executionList.size(); i < n; i++) {
  93             executionList.add(new MethodHandleDelegate(executionList.get(i)));
  94         }
  95 
  96         List<TypeHierarchy> hierarchyList = new ArrayList<>();
  97         hierarchyList.add(new DefaultMethodInterface.Hierarchy());
  98         hierarchyList.add(new DefaultMethodInterface2.Hierarchy());
  99         hierarchyList.add(new Linear.Hierarchy());
 100         hierarchyList.add(new Linear2.Hierarchy());
 101         hierarchyList.add(new OneRank.Hierarchy());
 102         for (int i = 0, n = hierarchyList.size(); i < n; i++) {
 103             hierarchyList.add(new NullableType(hierarchyList.get(i)));
 104         }
 105 
 106         List<BiFunction<ProfilingType, TypeHierarchy, Scenario<?, ?>>> testCasesConstructors
 107                 = new ArrayList<>();
 108         testCasesConstructors.add(ArrayCopy::new);
 109         testCasesConstructors.add(ArrayReferenceStore::new);
 110         testCasesConstructors.add(ClassIdentity::new);
 111         testCasesConstructors.add(ClassInstanceOf::new);
 112         testCasesConstructors.add(ClassIsInstance::new);
 113         testCasesConstructors.add(ReceiverAtInvokes::new);
 114         testCasesConstructors.add(CheckCast::new);
 115 
 116         for (TypeHierarchy hierarchy : hierarchyList) {
 117             for (BiFunction<ProfilingType, TypeHierarchy, Scenario<?, ?>> constructor : testCasesConstructors) {
 118                 for (Execution execution : executionList) {
 119                     Scenario<?, ?> scenario = constructor.apply(profilingType, hierarchy);
 120                     if (scenario.isApplicable()) {
 121                         result &= executeTest(hierarchy, execution, scenario);
 122                     }
 123                 }
 124             }
 125         }
 126         return result;
 127     }
 128 
 129     /**
 130      * Executes test case
 131      *
 132      * @param hierarchy type hierarchy for the test
 133      * @param execution execution scenario
 134      * @param scenario  test scenario executed with given Execution
 135      */
 136     private static boolean executeTest(TypeHierarchy hierarchy, Execution execution, Scenario<?, ?> scenario) {
 137         boolean testCaseResult = false;
 138         String testName = hierarchy.getClass().getName() + " :: " + scenario.getName() + " @ " + execution.getName();
 139         clearAllMethodsState(scenario.getClass());
 140         try {
 141             execution.execute(scenario);
 142             testCaseResult = true;
 143         } catch (Exception e) {
 144             System.err.println(testName + " failed with exception " + e);
 145             e.printStackTrace();
 146         }
 147         System.out.println((testCaseResult ? "PASSED: " : "FAILED: ") + testName);
 148         return testCaseResult;
 149     }
 150 
 151     private static void clearAllMethodsState(Class aClass) {
 152         while (aClass != null) {
 153             for (Method m : aClass.getDeclaredMethods()) {
 154                 WHITE_BOX.clearMethodState(m);
 155             }
 156             aClass = aClass.getSuperclass();
 157         }
 158     }
 159 }