Path: blob/master/test/hotspot/jtreg/compiler/compilercontrol/share/scenario/CommandGenerator.java
41161 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package compiler.compilercontrol.share.scenario;2425import compiler.compilercontrol.share.method.MethodDescriptor;26import jdk.test.lib.Asserts;27import jdk.test.lib.Utils;2829import java.util.List;30import java.util.Random;31import java.util.stream.Collectors;32import java.util.stream.Stream;3334/**35* Generates random commands36*/37public class CommandGenerator {38private static final int MAX_COMMANDS = Integer.getInteger(39"compiler.compilercontrol.share.scenario.CommandGenerator.commands",40100);41private static final Random RANDOM = Utils.getRandomInstance();4243/**44* Generates random command45*46* @return command47*/48public Command generateCommand() {49return Utils.getRandomElement(Command.values());50}5152/**53* Generates random number of random command54*55* @return a list of random commands56*/57public List<Command> generateCommands() {58int amount = 1 + RANDOM.nextInt(MAX_COMMANDS - 1);59return generateCommands(amount);60}6162/**63* Generates specified amount of random command64*65* @param amount amount of commands to generate66* @return a list of random commands67*/68public List<Command> generateCommands(int amount) {69return Stream.generate(this::generateCommand)70.limit(amount)71.collect(Collectors.toList());72}7374/**75* Generates random compile command {@link CompileCommand} with specified76* command and method descriptor77*78* @param command a command type79* @param md a method descriptor80* @param type a type of the command, or null to generate any81* @return the generated compile command82*/83public CompileCommand generateCompileCommand(Command command,84MethodDescriptor md, Scenario.Type type) {85if (type == null) {86type = Utils.getRandomElement(Scenario.Type.values());87}88return type.createCompileCommand(command, md, generateCompiler());89}9091public CompileCommand generateCompileCommand(Command command,92MethodDescriptor md, Scenario.Type type, String argument) {93if (type == null) {94type = Utils.getRandomElement(Scenario.Type.values());95}96return type.createCompileCommand(command, md, generateCompiler(), argument);97}9899100/**101* Generates type of compiler that should be used for the command, or null102* if any or all compilers should be used103*104* @return Compiler value, or null105*/106public Scenario.Compiler generateCompiler() {107Scenario.Compiler[] compilers = Scenario.Compiler.values();108int compiler = RANDOM.nextInt(compilers.length + 1) - 1;109return (compiler != -1) ? compilers[compiler] : null;110}111112/**113* Generates random diagnostic command114* {@link Scenario.Type}115*/116public Scenario.JcmdType generateJcmdType() {117return Utils.getRandomElement(Scenario.JcmdType.values());118}119}120121122