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 24 package compiler.compilercontrol.share.scenario; 25 26 import compiler.compilercontrol.share.method.MethodDescriptor; 27 import jdk.test.lib.Asserts; 28 import jdk.test.lib.Utils; 29 30 import java.util.List; 31 import java.util.Random; 32 import java.util.stream.Collectors; 33 import java.util.stream.Stream; 34 35 /** 36 * Generates random commands 37 */ 38 public class CommandGenerator { 39 private static final int MAX_COMMANDS = Integer.getInteger( 40 "compiler.compilercontrol.share.scenario.CommandGenerator.commands", 41 100); 42 private static final Random RANDOM = Utils.getRandomInstance(); 43 44 /** 45 * Generates random command 46 * 47 * @return command 48 */ generateCommand()49 public Command generateCommand() { 50 return Utils.getRandomElement(Command.values()); 51 } 52 53 /** 54 * Generates random number of random command 55 * 56 * @return a list of random commands 57 */ generateCommands()58 public List<Command> generateCommands() { 59 int amount = 1 + RANDOM.nextInt(MAX_COMMANDS - 1); 60 return generateCommands(amount); 61 } 62 63 /** 64 * Generates specified amount of random command 65 * 66 * @param amount amount of commands to generate 67 * @return a list of random commands 68 */ generateCommands(int amount)69 public List<Command> generateCommands(int amount) { 70 return Stream.generate(this::generateCommand) 71 .limit(amount) 72 .collect(Collectors.toList()); 73 } 74 75 /** 76 * Generates random compile command {@link CompileCommand} with specified 77 * command and method descriptor 78 * 79 * @param command a command type 80 * @param md a method descriptor 81 * @param type a type of the command, or null to generate any 82 * @return the generated compile command 83 */ generateCompileCommand(Command command, MethodDescriptor md, Scenario.Type type)84 public CompileCommand generateCompileCommand(Command command, 85 MethodDescriptor md, Scenario.Type type) { 86 if (type == null) { 87 type = Utils.getRandomElement(Scenario.Type.values()); 88 } 89 return type.createCompileCommand(command, md, generateCompiler()); 90 } 91 generateCompileCommand(Command command, MethodDescriptor md, Scenario.Type type, String argument)92 public CompileCommand generateCompileCommand(Command command, 93 MethodDescriptor md, Scenario.Type type, String argument) { 94 if (type == null) { 95 type = Utils.getRandomElement(Scenario.Type.values()); 96 } 97 return type.createCompileCommand(command, md, generateCompiler(), argument); 98 } 99 100 101 /** 102 * Generates type of compiler that should be used for the command, or null 103 * if any or all compilers should be used 104 * 105 * @return Compiler value, or null 106 */ generateCompiler()107 public Scenario.Compiler generateCompiler() { 108 Scenario.Compiler[] compilers = Scenario.Compiler.values(); 109 int compiler = RANDOM.nextInt(compilers.length + 1) - 1; 110 return (compiler != -1) ? compilers[compiler] : null; 111 } 112 113 /** 114 * Generates random diagnostic command 115 * {@link Scenario.Type} 116 */ generateJcmdType()117 public Scenario.JcmdType generateJcmdType() { 118 return Utils.getRandomElement(Scenario.JcmdType.values()); 119 } 120 } 121