|
3 | 3 | * SPDX-License-Identifier: GPL-2.0-only */ |
4 | 4 | package de.uka.ilkd.key.testgen; |
5 | 5 |
|
| 6 | +import de.uka.ilkd.key.api.ProofManagementApi; |
6 | 7 | import de.uka.ilkd.key.control.KeYEnvironment; |
7 | 8 | import de.uka.ilkd.key.proof.Proof; |
8 | 9 | import de.uka.ilkd.key.proof.io.ProblemLoaderException; |
9 | 10 | import de.uka.ilkd.key.smt.solvertypes.SolverTypes; |
| 11 | +import de.uka.ilkd.key.speclang.Contract; |
10 | 12 | import de.uka.ilkd.key.testgen.settings.TestGenerationSettings; |
11 | 13 | import de.uka.ilkd.key.testgen.smt.testgen.TestGenerationLog; |
12 | 14 | import org.slf4j.Logger; |
13 | 15 | import org.slf4j.LoggerFactory; |
| 16 | +import picocli.CommandLine; |
14 | 17 |
|
15 | 18 | import java.io.File; |
16 | | -import java.util.Objects; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.LinkedList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.concurrent.Callable; |
17 | 23 |
|
18 | | -public class TGMain { |
| 24 | +@CommandLine.Command(name = "tcgen", mixinStandardHelpOptions = true, |
| 25 | + description = "Generator of Testcases based on Proof Attempts") |
| 26 | +public class TGMain implements Callable<Integer> { |
19 | 27 | private final static Logger LOGGER = LoggerFactory.getLogger("main"); |
20 | 28 |
|
21 | 29 | public static void main(String[] args) throws ProblemLoaderException, InterruptedException { |
| 30 | + int exitCode = new CommandLine(new TGMain()).execute(args); |
| 31 | + System.exit(exitCode); |
| 32 | + } |
| 33 | + |
| 34 | + @CommandLine.Parameters(description = "KeY or Java file.", arity = "1..*") |
| 35 | + private List<File> files = new LinkedList<>(); |
| 36 | + |
| 37 | + @CommandLine.Option(names = {"-s", "--symbex"}, |
| 38 | + description = "apply symbex", negatable = true) |
| 39 | + private boolean symbex; |
| 40 | + |
| 41 | + @CommandLine.Option(names = {"-c", "--contract"}, |
| 42 | + arity = "*", |
| 43 | + description = "name of the contract to be loaded in the Java environment") |
| 44 | + private List<String> contractNames = new ArrayList<>(); |
| 45 | + |
| 46 | + @CommandLine.Option(names = {"--all-contracts"}, |
| 47 | + description = "name of the contract to be loaded in the Java environment") |
| 48 | + private boolean allContracts = false; |
| 49 | + |
| 50 | + |
| 51 | + @CommandLine.Option(names = {"-o", "--output"}, description = "Output folder") |
| 52 | + private File outputFolder = new File("out"); |
| 53 | + |
| 54 | + @CommandLine.Option(names = {"-r", "--rfl"}, description = "Use Reflection class", negatable = true) |
| 55 | + private boolean useReflection = false; |
| 56 | + |
| 57 | + @CommandLine.Option(names = {"-f", "--format"}, description = "Use Reflection class") |
| 58 | + private Format format = Format.JUnit4; |
| 59 | + |
| 60 | + |
| 61 | + @CommandLine.Option(names = {"--max-unwinds"}, description = "max unwinds") |
| 62 | + private int maxUnwinds = 10; |
| 63 | + @CommandLine.Option(names = {"--dups"}, description = "remove duplicates", negatable = true) |
| 64 | + private boolean removeDuplicates; |
| 65 | + |
| 66 | + @Override |
| 67 | + public Integer call() throws Exception { |
22 | 68 | if (SolverTypes.Z3_CE_SOLVER.checkForSupport()) { |
23 | 69 | LOGGER.error("Z3 not found! Bye."); |
24 | | - System.exit(1); |
25 | | - return; |
| 70 | + return 1; |
26 | 71 | } else { |
27 | 72 | LOGGER.info("Z3 found; Version {}", SolverTypes.Z3_CE_SOLVER.getInstalledVersion()); |
28 | 73 | } |
29 | 74 |
|
30 | | - |
31 | 75 | var settings = new TestGenerationSettings(); |
32 | | - String fileName = null; |
33 | | - for (int i = 0; i < args.length; i++) { |
34 | | - var a = args[i]; |
35 | | - if (a.startsWith("--")) { |
36 | | - switch (a) { |
37 | | - case "--output" -> settings.setOutputPath(args[++i]); |
38 | | - case "--rfl" -> settings.setRFL(true); |
39 | | - case "--format" -> settings.setFormat(Format.valueOf(args[++i])); |
| 76 | + TestGenerationLog log = new SysoutTestGenerationLog(); |
| 77 | + settings.setOutputPath(outputFolder.getAbsolutePath()); |
| 78 | + settings.setRFL(useReflection); |
| 79 | + settings.setFormat(format); |
| 80 | + settings.setApplySymbolicExecution(symbex); |
| 81 | + settings.setMaxUnwinds(maxUnwinds); |
| 82 | + settings.setRemoveDuplicates(removeDuplicates); |
| 83 | + |
| 84 | + for (File file : files) { |
| 85 | + List<Proof> proofs = new LinkedList<>(); |
| 86 | + var env = KeYEnvironment.load(file); |
| 87 | + Proof proof = env.getLoadedProof(); |
| 88 | + if (proof == null) { // non-key file |
| 89 | + var print = contractNames.isEmpty(); |
| 90 | + final var api = new ProofManagementApi(env); |
| 91 | + var contracts = api.getProofContracts(); |
| 92 | + for (Contract contract : contracts) { |
| 93 | + final var name = contract.getName(); |
| 94 | + if (print) { |
| 95 | + LOGGER.info("Contract found: {}", name); |
| 96 | + } |
| 97 | + |
| 98 | + if (allContracts || contractNames.contains(name)) { |
| 99 | + proofs.add(api.startProof(contract).getProof()); |
| 100 | + } |
40 | 101 | } |
41 | | - } else { |
42 | | - fileName = a; |
| 102 | + } else { // key file |
| 103 | + proofs = List.of(proof); |
43 | 104 | } |
44 | | - } |
45 | 105 |
|
46 | | - var env = KeYEnvironment.load(new File(Objects.requireNonNull(fileName))); |
47 | | - TestGenerationLog log = new SysoutTestGenerationLog(); |
48 | | - Proof proof = env.getLoadedProof(); |
49 | | - TestgenFacade.generateTestcases(env, proof, settings, log); |
| 106 | + for (Proof p : proofs) { |
| 107 | + TestgenFacade.generateTestcases(env, p, settings, log); |
| 108 | + p.dispose(); |
| 109 | + } |
| 110 | + |
| 111 | + env.dispose(); |
| 112 | + } |
| 113 | + return 0; |
50 | 114 | } |
51 | 115 |
|
52 | 116 | private static class SysoutTestGenerationLog implements TestGenerationLog { |
|
0 commit comments