1/*
2 * Copyright (c) 2015, 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
24package compiler.compilercontrol.share;
25
26import compiler.compilercontrol.share.method.MethodDescriptor;
27import compiler.compilercontrol.share.scenario.Command;
28import compiler.compilercontrol.share.scenario.CommandGenerator;
29import compiler.compilercontrol.share.scenario.CompileCommand;
30import compiler.compilercontrol.share.scenario.Scenario;
31import jdk.test.lib.Utils;
32
33import java.lang.reflect.Executable;
34import java.util.ArrayList;
35import java.util.List;
36
37public class MultiCommand extends AbstractTestBase {
38    private final List<CompileCommand> testCases;
39
40    public MultiCommand(List<CompileCommand> testCases) {
41        this.testCases = testCases;
42    }
43
44    /**
45     * Generates a test containing multiple random commands
46     *
47     * @param validOnly shows that all commands should be valid
48     * @return test instance to run
49     */
50    public static AbstractTestBase generateRandomTest(boolean validOnly) {
51        CommandGenerator cmdGen = new CommandGenerator();
52        List<Command> commands = cmdGen.generateCommands();
53        List<CompileCommand> testCases = new ArrayList<>();
54        for (Command cmd : commands) {
55            if (validOnly && cmd == Command.NONEXISTENT) {
56                // replace with a valid command
57                cmd = Command.EXCLUDE;
58            }
59            Executable exec = Utils.getRandomElement(METHODS).first;
60            MethodDescriptor md;
61            if (validOnly) {
62                md = AbstractTestBase.getValidMethodDescriptor(exec);
63            } else {
64                md = AbstractTestBase.METHOD_GEN.generateRandomDescriptor(exec);
65            }
66            CompileCommand cc = cmdGen.generateCompileCommand(cmd, md, null);
67            testCases.add(cc);
68        }
69        return new MultiCommand(testCases);
70    }
71
72    @Override
73    public void test() {
74        Scenario.Builder builder = Scenario.getBuilder();
75        builder.addFlag("-Xmixed");
76        builder.addFlag("-XX:+UnlockDiagnosticVMOptions");
77        builder.addFlag("-XX:CompilerDirectivesLimit=101");
78        for (CompileCommand cc : testCases) {
79            cc.print();
80            builder.add(cc);
81        }
82        Scenario scenario = builder.build();
83        scenario.execute();
84    }
85}
86