Skip to content

Commit

Permalink
test: test StatementAdd amplificator (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb authored and monperrus committed Dec 7, 2016
1 parent fff239f commit 241ff9c
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ static void setSeedRandom(long seed) {
random = new Random(seed);
}

static Random getRandom() {
public static Random getRandom() {
return random;
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/fr/inria/diversify/dspot/amp/StatementAdd.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public List<CtMethod> apply(CtMethod method) {
.collect(Collectors.toList()).stream())
.collect(Collectors.toList());

// use the existing invocation to add new invocation

// use the potential parameters to generate new invocation

invocations.stream()
.filter(invocation -> !CtTypeUtils.isPrimitive(invocation.getType()) || !CtTypeUtils.isString(invocation.getType()))
.forEach(invocation -> {
Expand All @@ -69,6 +73,8 @@ public List<CtMethod> apply(CtMethod method) {
}
});

// use the return value of the first generation to generate

return ampMethods;
}

Expand Down Expand Up @@ -161,7 +167,7 @@ protected List<CtInvocation> getInvocation(CtMethod method) {
List<CtInvocation> statements = Query.getElements(method, new TypeFilter(CtInvocation.class));
return statements.stream()
.filter(invocation -> invocation.getParent() instanceof CtBlock)
.filter(stmt -> stmt.getExecutable().getDeclaringType().getQualifiedName().startsWith(filter))
.filter(stmt -> stmt.getExecutable().getDeclaringType().getQualifiedName().startsWith(filter)) // filter on the name for amplify a specific type
.collect(Collectors.toList());
}

Expand All @@ -171,7 +177,7 @@ protected void initMethods(CtType testClass) {
Set<CtMethod> allMethods = cl.getAllMethods();
return allMethods.stream();
})
.filter(mth -> !mth.getModifiers().contains(ModifierKind.ABSTRACT))
.filter(mth -> !mth.getModifiers().contains(ModifierKind.ABSTRACT))//TODO abstract
.filter(mth -> !mth.getModifiers().contains(ModifierKind.PRIVATE))
.filter(mth -> mth.getBody() != null)
.filter(mth -> !mth.getBody().getStatements().isEmpty())
Expand Down
31 changes: 14 additions & 17 deletions src/main/java/fr/inria/diversify/dspot/value/ValueType.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fr.inria.diversify.dspot.value;


import fr.inria.diversify.dspot.amp.AmplifierHelper;
import fr.inria.diversify.dspot.value.objectInstanciationTree.ObjectInstantiation;
import fr.inria.diversify.dspot.value.objectInstanciationTree.StaticMethodValue;
import fr.inria.diversify.utils.CtTypeUtils;
Expand All @@ -21,12 +22,9 @@
public class ValueType {
protected static Factory factory;
protected static ValueFactory valueFactory;
protected static Random random = new Random();
protected String dynamicType;
protected List<Value> values;



public ValueType(String dynamicType) {
this.dynamicType = dynamicType;
this.values = new ArrayList<>();
Expand All @@ -40,7 +38,7 @@ public void addValue(Value value) {

public Value getRandomValue(boolean generateIsEmpty) {
while (!values.isEmpty()) {
int index = random.nextInt(values.size());
int index = AmplifierHelper.getRandom().nextInt(values.size());
Value value = values.get(index);
if(value.isOk()) {
return value;
Expand Down Expand Up @@ -69,6 +67,8 @@ public String getType() {
return dynamicType;
}


//TODO
protected Value generateRandomValue() {
// if(dynamicType.equals("null")) {
// }
Expand All @@ -80,7 +80,7 @@ protected Value generateRandomValue() {
}
Object primitiveValue = null;
if(type == Boolean.class) {
primitiveValue = random.nextBoolean();
primitiveValue = AmplifierHelper.getRandom().nextBoolean();
}
if(type == Character.class) {
primitiveValue = '1';
Expand All @@ -89,19 +89,19 @@ protected Value generateRandomValue() {
// value = '1';
}
if(type == Short.class) {
primitiveValue = (short)random.nextInt(100);
primitiveValue = (short)AmplifierHelper.getRandom().nextInt(100);
}
if(type == Integer.class) {
primitiveValue = random.nextInt(100);
if(type == Integer.class || type == int.class) {
primitiveValue = AmplifierHelper.getRandom().nextInt(100);
}
if(type == Long.class) {
primitiveValue = (long)random.nextInt(100);
primitiveValue = (long)AmplifierHelper.getRandom().nextInt(100);
}
if(type == Float.class) {
primitiveValue = (float)random.nextDouble();
primitiveValue = (float)AmplifierHelper.getRandom().nextDouble();
}
if(type == Double.class) {
primitiveValue = random.nextDouble();
primitiveValue = AmplifierHelper.getRandom().nextDouble();
}
if(type == String.class) {
primitiveValue = "foo";
Expand Down Expand Up @@ -129,10 +129,6 @@ protected Value generateRandomValue() {
return null;
}

public boolean hasValue() {
return !values.isEmpty();
}

protected CtExecutableReference getSingletonMethodAccess() {
try {
CtClass cl = factory.Class().get(dynamicType);
Expand Down Expand Up @@ -182,8 +178,9 @@ protected CtExecutableReference getEmptyConstructor() {
return null;
}

} catch (Exception e) {}
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private boolean isPrivate(CtConstructor constructor) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/fr/inria/diversify/utils/CtTypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static boolean isSerializable(CtTypeReference type) {

public static boolean isPrimitive(CtTypeReference type) {
try {
return type.unbox().isPrimitive();
// return type.unbox().isPrimitive();
return type.isPrimitive();
} catch (Exception e) {
e.printStackTrace();
return false;
Expand Down
1 change: 1 addition & 0 deletions src/test/java/fr/inria/diversify/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Date: 25/11/16
* Time: 11:16
*/
@Deprecated
public class Utils {
private static String confFile = "src/test/resources/sample.properties";
private static InputProgram inputProgram;
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/fr/inria/diversify/dspot/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fr.inria.diversify.dspot;

import fr.inria.diversify.factories.DiversityCompiler;
import fr.inria.diversify.runner.InputProgram;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.factory.Factory;

import java.io.IOException;

/**
* Created by Benjamin DANGLOT
* benjamin.danglot@inria.fr
* on 12/7/16
*/
public class Utils {

public static CtMethod getCtMethod(CtClass testClass, final String simpleNameMethod) {
return (CtMethod) testClass.getMethods().stream().filter(method -> simpleNameMethod.equals(((CtMethod) method).getSimpleName())).findFirst().get();
}

public static Factory getFactory(InputProgram inputProgram) throws IOException, InterruptedException {
DiversityCompiler diversityCompiler = DSpotUtils.initDiversityCompiler(inputProgram, true);
Factory factory = diversityCompiler.getFactory();
factory.getEnvironment().setNoClasspath(true);
return factory;
}

public static InputProgram getInputProgram() {
InputProgram inputProgram = new InputProgram();
inputProgram.setProgramDir("src/test/resources/");
inputProgram.setRelativeSourceCodeDir("src");
inputProgram.setRelativeTestSourceCodeDir("test");
inputProgram.setTransformationPerRun(1);
inputProgram.setJavaVersion(8);
return inputProgram;
}
}
44 changes: 44 additions & 0 deletions src/test/java/fr/inria/diversify/dspot/amp/StatementAddTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package fr.inria.diversify.dspot.amp;

import fr.inria.diversify.dspot.Utils;
import fr.inria.diversify.dspot.value.ValueFactory;
import fr.inria.diversify.runner.InputProgram;
import org.junit.Test;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.factory.Factory;

import java.util.*;

import static org.junit.Assert.assertEquals;

/**
* Created by Benjamin DANGLOT
* benjamin.danglot@inria.fr
* on 11/30/16
*/
public class StatementAddTest {

@Test
public void testStatementAdd() throws Exception {
final String packageName = "statementadd";
InputProgram inputProgram = Utils.getInputProgram();

final Factory factory = Utils.getFactory(inputProgram);
inputProgram.setFactory(factory);
AmplifierHelper.setSeedRandom(23L);
ValueFactory valueFactory = new ValueFactory(inputProgram);
StatementAdd amplificator = new StatementAdd(inputProgram.getFactory(), valueFactory, packageName);
amplificator.reset(null, factory.Class().get(packageName + ".ClassTargetAmplify"));

CtMethod<?> ctMethod = Utils.getCtMethod(factory.Class().get(packageName + ".TestClassTargetAmplify"), "test");
// CtMethod<?> ctMethod = factory.Class().get(packageName + ".TestClassTargetAmplify").getMethods().stream().findAny().get();
List<CtMethod> amplifiedMethods = amplificator.apply(ctMethod);

assertEquals(4, amplifiedMethods.size());

amplifiedMethods.forEach(System.out::println);
}



}
20 changes: 20 additions & 0 deletions src/test/resources/src/statementadd/ClassParameterAmplify.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package statementadd;

/**
* Created by bdanglot on 11/30/16.
*/
public class ClassParameterAmplify {

public void method1() {
System.out.println("");
}

private void method2() {
System.out.println("");
}

protected void method3() {
System.out.println("");
}

}
20 changes: 20 additions & 0 deletions src/test/resources/src/statementadd/ClassTargetAmplify.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package statementadd;

/**
* Created by bdanglot on 11/30/16.
*/
public class ClassTargetAmplify {

public ClassParameterAmplify methodWithReturn() {
return new ClassParameterAmplify();
}

public void methodPrimitif(Integer i) {
System.out.println(i);
}

public void method() {
System.out.println("");
}

}
16 changes: 16 additions & 0 deletions src/test/resources/test/statementadd/TestClassTargetAmplify.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package statementadd;

import org.junit.Test;

/**
* Created by bdanglot on 11/30/16.
*/
public class TestClassTargetAmplify {

@Test
public void test() throws Exception {
ClassTargetAmplify clazz = new ClassTargetAmplify();
clazz.methodWithReturn();
}

}

0 comments on commit 241ff9c

Please sign in to comment.