Skip to content

Commit

Permalink
Merge 7dcd9fa into b6bfc59
Browse files Browse the repository at this point in the history
  • Loading branch information
Pouria Derakhshanfar committed Mar 4, 2020
2 parents b6bfc59 + 7dcd9fa commit 213bb82
Show file tree
Hide file tree
Showing 36 changed files with 1,449 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Thumbs.db
**/test/**/botsing/model/generation/local/
**/test/**/cling/local/
**/test/**/coupling/analyze/local/
**/test/**/cbc/local/


# Generated website
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class PostProcessUtility {
private static final Logger LOG = LoggerFactory.getLogger(PostProcessUtility.class);

public static void postProcessTests(TestSuiteChromosome testSuite, List<TestFitnessFactory<? extends TestFitnessFunction>> fitnessFactories) {

LOG.info("test size before post-process: {}",testSuite.size());
if (Properties.INLINE) {
ConstantInliner inliner = new ConstantInliner();
inliner.inline(testSuite);
Expand All @@ -49,6 +49,7 @@ public static void postProcessTests(TestSuiteChromosome testSuite, List<TestFitn
throw new Error("EvoSuite bug: minimization lead fitness from " + before + " to " + after);
}
}
LOG.info("test size after post-process: {}",testSuite.size());

if (Properties.ASSERTIONS) {
LOG.info("Generating assertions");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public class AbstractMOSA<T extends Chromosome> extends org.evosuite.ga.metaheur
private static final Logger LOG = LoggerFactory.getLogger(AbstractMOSA.class);
protected Mutation<T> mutation;

/** Boolean vector to indicate whether each test goal is covered or not. **/
protected Set<FitnessFunction<T>> uncoveredGoals = new LinkedHashSet<FitnessFunction<T>>();
protected Set<FitnessFunction<T>> coveredGoals = new LinkedHashSet<FitnessFunction<T>>();

/** Map used to store the covered test goals (keys of the map) and the corresponding covering test cases (values of the map) **/
protected Map<FitnessFunction<T>, T> archive = new LinkedHashMap<FitnessFunction<T>, T>();
FitnessFunctions fitnessCollector;
Expand Down Expand Up @@ -131,7 +135,14 @@ protected void evolve() {}
@Override
public void generateSolution() {}

@Override
protected Set<FitnessFunction<T>> getCoveredGoals() {
return this.coveredGoals;
}


@Override
protected Set<FitnessFunction<T>> getUncoveredGoals() {
return this.uncoveredGoals;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public class MOSA<T extends Chromosome> extends AbstractMOSA<T> {
private static final Logger LOG = LoggerFactory.getLogger(MOSA.class);


/** Boolean vector to indicate whether each test goal is covered or not. **/
protected Set<FitnessFunction<T>> uncoveredGoals = new LinkedHashSet<FitnessFunction<T>>();


protected CrowdingDistance<T> distance = new CrowdingDistance<T>();

Expand Down Expand Up @@ -134,7 +133,7 @@ public void generateSolution() {
// Calculate dominance ranks and crowding distance
this.rankingFunction.computeRankingAssignment(this.population, this.uncoveredGoals);
for (int i = 0; i < this.rankingFunction.getNumberOfSubfronts(); i++) {
this.distance.fastEpsilonDominanceAssignment(this.rankingFunction.getSubfront(i), this.getUncoveredGoals());
this.distance.fastEpsilonDominanceAssignment(this.rankingFunction.getSubfront(i), this.uncoveredGoals);
}


Expand All @@ -149,6 +148,7 @@ public void generateSolution() {
for(StoppingCondition stoppingCondition : this.stoppingConditions){
if(stoppingCondition.isFinished()){
LOG.info("Stopping reason: {}", stoppingCondition.toString());
LOG.info("Number of covered goals are: {}", this.coveredGoals.size());
}

}
Expand Down Expand Up @@ -210,6 +210,7 @@ private void updateArchive(T solution, FitnessFunction<T> covered) {
} else {
archive.put(covered, solution);
this.uncoveredGoals.remove(covered);
this.coveredGoals.add(covered);
LOG.debug("New covered goal: {}",covered);
}
}
Expand Down Expand Up @@ -297,4 +298,11 @@ protected int getNumberOfCoveredGoals() {
LOG.debug("# Covered Goals = " + n_covered_goals);
return n_covered_goals;
}

@Override
protected List<T> getNonDominatedSolutions(List<T> solutions) {
return super.getNonDominatedSolutions(solutions);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package eu.stamp.botsing.commons;

import org.evosuite.Properties;
import org.evosuite.coverage.TestFitnessFactory;
import org.evosuite.result.TestGenerationResult;
import org.evosuite.setup.TestCluster;
import org.evosuite.testcase.TestChromosome;
import org.evosuite.testcase.TestFitnessFunction;
import org.evosuite.testcase.factories.AllMethodsTestChromosomeFactory;
import org.evosuite.testsuite.TestSuiteChromosome;
import org.evosuite.utils.generic.GenericClass;
import org.evosuite.utils.generic.GenericMethod;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.lang.reflect.Method;
import java.util.ArrayList;

import static eu.stamp.botsing.commons.PostProcessUtility.writeJUnitFailingTests;
import static eu.stamp.botsing.commons.PostProcessUtility.writeJUnitTestsAndCreateResult;


public class PostProcessUtilityTest {

private TestSuiteChromosome testSuite;

@Before
public void generateTestSuite() throws NoSuchMethodException {
Object obj = new String();
GenericClass gc = Mockito.mock(GenericClass.class);
Mockito.when(gc.hasWildcardOrTypeVariables()).thenReturn(false);
Method m = obj.getClass().getMethod("equals", Object.class);
GenericMethod call = Mockito.mock(GenericMethod.class,Mockito.RETURNS_DEEP_STUBS);
Mockito.doReturn(obj.getClass()).when(call).getDeclaringClass();
Mockito.when(call.getName()).thenReturn("equals");
Mockito.when(call.getOwnerClass()).thenReturn(gc);
Mockito.when(call.isMethod()).thenReturn(true);
Mockito.when(call.getOwnerType()).thenReturn(String.class);
Mockito.when(call.getMethod()).thenReturn(m);
Mockito.when(call.getParameterTypes()).thenReturn(m.getParameterTypes());
Mockito.when(call.getReturnType()).thenReturn(Boolean.TYPE);
Mockito.when(call.isPublic()).thenReturn(true);
Mockito.doReturn(boolean.class).when(call).getRawGeneratedType();


Mockito.when(call.copy()).thenReturn(call);
TestCluster.getInstance().addTestCall(call);

AllMethodsTestChromosomeFactory testChromosomeFactory = new AllMethodsTestChromosomeFactory();
TestChromosome test =testChromosomeFactory.getChromosome();
testSuite = new TestSuiteChromosome();
testSuite.addTest(test);
}

@Test
public void testWithoutSearchObjective(){
TestFitnessFactory testFitnessFactory = Mockito.mock(TestFitnessFactory.class);
ArrayList testFitnessFactoryList = new ArrayList();
testFitnessFactoryList.add(testFitnessFactory);
PostProcessUtility.postProcessTests(testSuite, testFitnessFactoryList);
// Minimization suppose to remove the test because there is no search objective
assert(testSuite.getTests().size() == 0);
}

@Test
public void testWithSearchObjective(){
TestFitnessFunction testFF = Mockito.mock(TestFitnessFunction.class);
Mockito.when(testFF.isCovered(testSuite.getTestChromosome(0))).thenReturn(true);
ArrayList<TestFitnessFunction> coverageGoals = new ArrayList<>();
coverageGoals.add(testFF);

Properties.MINIMIZE = false;
TestFitnessFactory testFitnessFactory = Mockito.mock(TestFitnessFactory.class);
Mockito.when(testFitnessFactory.getCoverageGoals()).thenReturn(coverageGoals);
Mockito.when(testFitnessFactory.getCoverageGoals()).thenReturn(coverageGoals);
ArrayList testFitnessFactoryList = new ArrayList();
testFitnessFactoryList.add(testFitnessFactory);
PostProcessUtility.postProcessTests(testSuite, testFitnessFactoryList);
assert(testSuite.getTests().size() == 1);
}

@Test
public void testWriteTest(){
TestGenerationResult writingTest = writeJUnitTestsAndCreateResult(testSuite, Properties.JUNIT_SUFFIX);
String interestingPart = writingTest.getTestSuiteCode().split("public void test0\\(\\) throws Throwable \\{\n")[1];
interestingPart = interestingPart.replaceAll(" ","");
assert (interestingPart.startsWith(testSuite.getTests().get(0).toCode()));

writeJUnitFailingTests();

Properties.CHECK_CONTRACTS = true;
writeJUnitFailingTests();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package eu.stamp.botsing.coupling;

import org.junit.Test;

public class CallerTest {

@Test
public void test0(){
Caller caller = new Caller(10,20);
caller.firstNumberIsBigger();
}
}
Binary file modified botsing-reproduction/evosuite-client-botsing-1.1.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public enum TestGenerationStrategy {
public enum FitnessFunction {
WeightedSum,
SimpleSum,
TestLen,
IntegrationIndexedAccess,
IntegrationSingleObjective;
FitnessFunction() {
Expand All @@ -77,6 +78,7 @@ public enum FitnessFunction {
public enum SearchAlgorithm {
Single_Objective_GGA,
Guided_MOSA,
NSGA_II,
DynaMOSA;

SearchAlgorithm() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,21 @@ private TestFitnessFunction getFF(CrashProperties.FitnessFunction givenFFName, S
return new IntegrationTestingFF(crash);
case IntegrationIndexedAccess:
return new ITFFForIndexedAccess(crash);
case TestLen:
return new TestLenFF();
default:
return new WeightedSum(crash);
}
}


public static double normalize(double value) throws IllegalArgumentException {
if (value < 0d) {
throw new IllegalArgumentException("Values to normalize cannot be negative");
}
if (Double.isInfinite(value)) {
return 1.0;
}
return value / (1.0 + value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package eu.stamp.botsing.fitnessfunction;

import org.evosuite.testcase.TestChromosome;
import org.evosuite.testcase.TestFitnessFunction;
import org.evosuite.testcase.execution.ExecutionResult;

public class TestLenFF extends TestFitnessFunction {
@Override
public double getFitness(TestChromosome testChromosome, ExecutionResult executionResult) {
double testLen = testChromosome.getTestCase().size();
// 1 - Normalize(len)
return (1 - FitnessFunctionHelper.normalize(testLen));
}

@Override
public int compareTo(TestFitnessFunction other) {
if (other == null){
return 1;
}

if (other instanceof TestLenFF){
return 0;
}

return compareClassName(other);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
return prime * result + (this.getClass().hashCode());
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
return getClass() == obj.getClass();
}

@Override
public String getTargetClass() {
return null;
}

@Override
public String getTargetMethod() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,16 @@ public double getFitness(TestChromosome testChromosome, ExecutionResult executio
}

@Override
public int compareTo(TestFitnessFunction testFitnessFunction) {
// TODO Add this when we have multple fitness functions
return 0;
public int compareTo(TestFitnessFunction other) {
if (other == null){
return 1;
}

if (other instanceof WeightedSum){
return 0;
}

return compareClassName(other);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.evosuite.testcase.execution.MethodCall;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static eu.stamp.botsing.fitnessfunction.FitnessFunctionHelper.normalize;

import java.util.*;

Expand Down Expand Up @@ -258,18 +259,4 @@ private List<BranchCoverageTestFitness> setupDependencies(String className , Str
return branchCoverages;
}


private double normalize(double value) throws IllegalArgumentException {
if (value < 0d) {
throw new IllegalArgumentException("Values to normalize cannot be negative");
}
if (Double.isInfinite(value)) {
return 1.0;
}
return value / (1.0 + value);
}




}

0 comments on commit 213bb82

Please sign in to comment.