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