Skip to content

Commit 31f11d0

Browse files
committed
It run for BinarySearch
1 parent 24ad8b7 commit 31f11d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2743
-559
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
/* This file is part of KeY - https://key-project.org
2+
* KeY is licensed under the GNU General Public License Version 2
3+
* SPDX-License-Identifier: GPL-2.0-only */
14
package de.uka.ilkd.key.testgen;
25

36
public enum Format {
47
Plain, JUnit4, JUnit5, TestNG,
5-
}
8+
}

key.core.testgen/src/main/java/de/uka/ilkd/key/testgen/ModelGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
import de.uka.ilkd.key.settings.ProofDependentSMTSettings;
2222
import de.uka.ilkd.key.settings.ProofIndependentSMTSettings;
2323
import de.uka.ilkd.key.settings.ProofIndependentSettings;
24-
import de.uka.ilkd.key.testgen.settings.TestGenerationSettings;
2524
import de.uka.ilkd.key.smt.*;
2625
import de.uka.ilkd.key.smt.lang.SMTSort;
2726
import de.uka.ilkd.key.smt.model.Model;
2827
import de.uka.ilkd.key.smt.solvertypes.SolverType;
2928
import de.uka.ilkd.key.smt.solvertypes.SolverTypes;
29+
import de.uka.ilkd.key.testgen.settings.TestGenerationSettings;
3030

3131
import org.key_project.util.collection.ImmutableList;
3232
import org.key_project.util.collection.ImmutableSLList;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package de.uka.ilkd.key.testgen;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.StandardCopyOption;
7+
import java.util.Objects;
8+
9+
/**
10+
* @author Alexander Weigl
11+
* @version 1 (02.02.24)
12+
*/
13+
public record OutputEnvironment(Path targetFolder) {
14+
public Path getSourceDir() {
15+
return targetFolder.resolve("src");
16+
}
17+
18+
public Path getTestSourceDir() {
19+
return targetFolder.resolve("test");
20+
}
21+
22+
public Path getAntFile() {
23+
return targetFolder.resolve("build.xml");
24+
}
25+
26+
public Path getReadmeFile() {
27+
return targetFolder.resolve("README.md");
28+
}
29+
30+
public void init() throws IOException {
31+
Files.createDirectories(getSourceDir());
32+
Files.createDirectories(getTestSourceDir());
33+
34+
installAntFile();
35+
}
36+
37+
private void installAntFile() throws IOException {
38+
try (var buildXml = getClass().getResourceAsStream("/de/uka/ilkd/key/tcg/build.xml")) {
39+
Files.copy(Objects.requireNonNull(buildXml), getAntFile(), StandardCopyOption.REPLACE_EXISTING);
40+
}
41+
}
42+
}

key.core.testgen/src/main/java/de/uka/ilkd/key/testgen/ProofInfo.java

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* SPDX-License-Identifier: GPL-2.0-only */
44
package de.uka.ilkd.key.testgen;
55

6-
import java.util.Set;
7-
86
import de.uka.ilkd.key.java.Services;
97
import de.uka.ilkd.key.java.abstraction.KeYJavaType;
8+
import de.uka.ilkd.key.java.declaration.ParameterDeclaration;
9+
import de.uka.ilkd.key.java.declaration.VariableSpecification;
1010
import de.uka.ilkd.key.logic.JavaBlock;
1111
import de.uka.ilkd.key.logic.Term;
1212
import de.uka.ilkd.key.logic.label.TermLabelManager;
@@ -20,17 +20,22 @@
2020
import de.uka.ilkd.key.speclang.Contract;
2121
import de.uka.ilkd.key.speclang.Contract.OriginalVariables;
2222
import de.uka.ilkd.key.speclang.FunctionalOperationContract;
23-
23+
import org.jspecify.annotations.Nullable;
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
2626

27+
import java.util.Objects;
28+
import java.util.Set;
29+
2730
public record ProofInfo(Proof proof, Services services) {
2831
private static final Logger LOGGER = LoggerFactory.getLogger(ProofInfo.class);
2932

3033
public ProofInfo(Proof proof) {
3134
this(proof, proof.getServices());
3235
}
3336

37+
38+
@Nullable
3439
public IProgramMethod getMUT() {
3540
SpecificationRepository spec = services.getSpecificationRepository();
3641
IObserverFunction f = spec.getTargetOfProof(proof);
@@ -41,20 +46,56 @@ public IProgramMethod getMUT() {
4146
}
4247
}
4348

49+
@Nullable
50+
public String getMUTCall() {
51+
var m = getMUT();
52+
if (m == null) return null;
53+
54+
var name = m.getFullName();
55+
if (name == null) return null;
56+
57+
StringBuilder params = new StringBuilder();
58+
for (ParameterDeclaration p : m.getParameters()) {
59+
for (VariableSpecification v : p.getVariables()) {
60+
IProgramVariable var = v.getProgramVariable();
61+
params.append(",").append(var.name());
62+
}
63+
}
64+
if (!params.isEmpty()) {
65+
params = new StringBuilder(params.substring(1));
66+
}
67+
68+
String caller;
69+
if (m.isStatic()) {
70+
caller = getTypeOfClassUnderTest().getName();
71+
} else {
72+
caller = "self";
73+
}
74+
75+
if (m.getReturnType().equals(KeYJavaType.VOID_TYPE)) {
76+
return caller + "." + name + "(" + params + ");";
77+
} else {
78+
String returnType = m.getReturnType().getFullName();
79+
return returnType + " result = " + caller + "." + name + "(" + params + ");";
80+
}
81+
}
82+
83+
@Nullable
4484
public KeYJavaType getTypeOfClassUnderTest() {
4585
if (getMUT() == null) {
4686
return null;
4787
}
4888
return getMUT().getContainerType();
4989
}
5090

91+
@Nullable
5192
public KeYJavaType getReturnType() {
5293
return getMUT().getType();
5394
}
5495

5596
public Contract getContract() {
5697
ContractPO po = services.getSpecificationRepository().getPOForProof(proof);
57-
return po.getContract();
98+
return Objects.requireNonNull(po).getContract();
5899
}
59100

60101
public Term getPostCondition() {

key.core.testgen/src/main/java/de/uka/ilkd/key/testgen/ReflectionClassCreator.java

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
* SPDX-License-Identifier: GPL-2.0-only */
44
package de.uka.ilkd.key.testgen;
55

6-
import com.squareup.javapoet.*;
7-
import de.uka.ilkd.key.logic.sort.Sort;
8-
9-
import javax.lang.model.element.Modifier;
106
import java.util.HashMap;
117
import java.util.HashSet;
8+
import javax.lang.model.element.Modifier;
9+
10+
import de.uka.ilkd.key.logic.sort.Sort;
11+
12+
import com.squareup.javapoet.*;
1213

1314
/**
1415
* Creates the RFL.java file, that provides setter and getter methods using the reflection API as
@@ -28,10 +29,10 @@ public class ReflectionClassCreator {
2829

2930
// setter and getter methods will be created for these types.
3031
private static final String[] PRIMITIVE_TYPES =
31-
{"int", "long", "byte", "char", "boolean", "float", "double"};
32+
{ "int", "long", "byte", "char", "boolean", "float", "double" };
3233

3334
// Default values for primitive types
34-
private static final String[] PRIM_TYP_DEF_VAL = {"0", "0", "0", "' '", "false", "0", "0"};
35+
private static final String[] PRIM_TYP_DEF_VAL = { "0", "0", "0", "' '", "false", "0", "0" };
3536

3637
private final HashSet<Sort> usedObjectSorts;
3738
private final HashSet<String> usedObjectSortsStrings;
@@ -110,21 +111,23 @@ private HashSet<String> sortsToString() {
110111
private TypeSpec.Builder classDecl(boolean staticClass) {
111112
var tb = TypeSpec.classBuilder(NAME_OF_CLASS);
112113
tb.addModifiers(Modifier.PUBLIC);
113-
if (staticClass) tb.addModifiers(Modifier.STATIC);
114-
tb.addJavadoc("""
115-
// This file was generated by KeY Version %s (www.key-project.org).
116-
/** This class enables the test suite to read and write protected and private
117-
* fields of other classes. It can also simulate ghost fields using a hashmap.
118-
* Ghostfields are implicit fields that exist in the specification but not in the
119-
* actual Java class. Futhermore, this class also enables to create an object of
120-
* any class even if it has no default constructor. To create objects the
121-
* the objenesis library is required and must be provided when compiling and
122-
* executing the test suite. * @see http://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html
123-
* @see http://code.google.com/p/objenesis
124-
* @see http://objenesis.org/
125-
* @author gladisch").append(NEW_LINE);
126-
* @author mbender").append(NEW_LINE);
127-
*/""");
114+
if (staticClass)
115+
tb.addModifiers(Modifier.STATIC);
116+
tb.addJavadoc(
117+
"""
118+
// This file was generated by KeY Version %s (www.key-project.org).
119+
/** This class enables the test suite to read and write protected and private
120+
* fields of other classes. It can also simulate ghost fields using a hashmap.
121+
* Ghostfields are implicit fields that exist in the specification but not in the
122+
* actual Java class. Futhermore, this class also enables to create an object of
123+
* any class even if it has no default constructor. To create objects the
124+
* the objenesis library is required and must be provided when compiling and
125+
* executing the test suite. * @see http://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html
126+
* @see http://code.google.com/p/objenesis
127+
* @see http://objenesis.org/
128+
* @author gladisch").append(NEW_LINE);
129+
* @author mbender").append(NEW_LINE);
130+
*/""");
128131
return tb;
129132
}
130133

@@ -137,14 +140,15 @@ private void ghostMapDecls(TypeSpec.Builder clazz) {
137140
clazz.addField(String.class, "NoSuchFieldExceptionText", Modifier.PUBLIC, Modifier.STATIC);
138141
clazz.addField(Boolean.TYPE, "ghostMapActive", Modifier.PUBLIC, Modifier.STATIC);
139142
clazz.addField(ParameterizedTypeName.get(HashMap.class, Integer.class, Object.class),
140-
"ghostModelFields",
141-
Modifier.PUBLIC, Modifier.STATIC);
143+
"ghostModelFields",
144+
Modifier.PUBLIC, Modifier.STATIC);
142145
clazz.addMethod(MethodSpec.methodBuilder("getHash")
143146
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
144147
.addParameter(Class.class, "c")
145148
.addParameter(Object.class, "obj")
146149
.addParameter(String.class, "attr")
147-
.addStatement("return c.hashCode() * (obj!=null?obj.hashCode():1) * attr.hashCode();")
150+
.addStatement(
151+
"return c.hashCode() * (obj!=null?obj.hashCode():1) * attr.hashCode();")
148152
.build());
149153
}
150154

@@ -158,7 +162,8 @@ private void instanceMethod(TypeSpec.Builder clazz) {
158162
.returns(Object.class)
159163
.addParameter(Class.class, "c")
160164
.addException(Exception.class)
161-
.addJavadoc("The Objenesis library can create instances of classes that have no default constructor.")
165+
.addJavadoc(
166+
"The Objenesis library can create instances of classes that have no default constructor.")
162167
.addStatement("Object res=objenesis.newInstance(c);")
163168
.beginControlFlow("if (res==null)")
164169
.addStatement("throw new Exception($S+c);", "Couldn't create instance of class: ")
@@ -169,24 +174,25 @@ private void instanceMethod(TypeSpec.Builder clazz) {
169174

170175
private void staticInitializer(boolean ghostMapActive, TypeSpec.Builder clazz) {
171176
clazz.addStaticBlock(
172-
CodeBlock.builder()
173-
.addStatement("objenesis = new org.objenesis.ObjenesisStd();")
174-
.add("ghostMapActive = $L;", ghostMapActive)
175-
.add("ghostModelFields = new java.util.HashMap<Integer,Object>();")
176-
.add("NoSuchFieldExceptionText =")
177-
.addStatement("/*This exception occurs when ghost fields or model fields are used in " +
178-
"the code or if mock objects are used that have different fields, than the real objects." +
179-
"The tester should extend the handling of such fields in this generated utility class RFL.java.*/;)"
180-
).build()
181-
);
177+
CodeBlock.builder()
178+
.addStatement("objenesis = new org.objenesis.ObjenesisStd();")
179+
.add("ghostMapActive = $L;", ghostMapActive)
180+
.add("ghostModelFields = new java.util.HashMap<Integer,Object>();")
181+
.add("NoSuchFieldExceptionText =")
182+
.addStatement(
183+
"/*This exception occurs when ghost fields or model fields are used in " +
184+
"the code or if mock objects are used that have different fields, than the real objects."
185+
+
186+
"The tester should extend the handling of such fields in this generated utility class RFL.java.*/;)")
187+
.build());
182188
}
183189

184190
/**
185191
* All calls to create objects for the given sorts
186192
*/
187193
private void instances(final HashSet<String> sorts, TypeSpec.Builder clazz) {
188-
//r.append(NEW_LINE).append(" // ---The methods for object creation---").append(NEW_LINE)
189-
// .append(NEW_LINE);
194+
// r.append(NEW_LINE).append(" // ---The methods for object creation---").append(NEW_LINE)
195+
// .append(NEW_LINE);
190196
for (final String sort : sorts) {
191197
clazz.addMethod(newRef(sort));
192198
}
@@ -236,8 +242,8 @@ private MethodSpec newInstance(final String sort) {
236242
.returns(returnType)
237243
.addException(RuntimeException.class)
238244
.addStatement("try{ return ($N) newInstance($N.class); } " +
239-
"catch (java.lang.Throwable e) { throw new java.lang.RuntimeException(e); }",
240-
returnType)
245+
"catch (java.lang.Throwable e) { throw new java.lang.RuntimeException(e); }",
246+
returnType)
241247
.build();
242248
}
243249

@@ -287,15 +293,16 @@ private MethodSpec declareSetter(final String sort, final boolean prim) {
287293

288294
if (prim)
289295
ms.addStatement("f.set" + Character.toUpperCase(sort.charAt(0)) + sort.substring(1)
290-
+ "(obj, val);");
296+
+ "(obj, val);");
291297
else
292298
ms.addStatement("f.set(obj, val);");
293299

294300
ms.nextControlFlow("catch(NoSuchFieldException e)")
295301
.beginControlFlow("if(ghostMapActive)")
296302
.addStatement("ghostModelFields.put(getHash(c,obj,attr), val);")
297303
.nextControlFlow("else")
298-
.addStatement("throw new RuntimeException(e.toString() + NoSuchFieldExceptionText);")
304+
.addStatement(
305+
"throw new RuntimeException(e.toString() + NoSuchFieldExceptionText);")
299306
.endControlFlow()
300307
.nextControlFlow("catch(Exception e)")
301308
.addStatement("throw new RuntimeException(e);")
@@ -327,7 +334,7 @@ private MethodSpec declareGetter(final String sort, final String def, final bool
327334

328335
if (prim)
329336
ms.addStatement("return f.get" + Character.toUpperCase(sort.charAt(0))
330-
+ sort.substring(1) + "(obj);");
337+
+ sort.substring(1) + "(obj);");
331338
else
332339
ms.addStatement("return ($N) f.get(obj);", retType);
333340

0 commit comments

Comments
 (0)