use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class AbstractMOSA method removeUnusedVariables.
/**
* When a test case is changed via crossover and/or mutation, it can contains some
* primitive variables that are not used as input (or to store the output) of method calls.
* Thus, this method removes all these "trash" statements.
* @param chromosome
* @return true or false depending on whether "unused variables" are removed
*/
public boolean removeUnusedVariables(T chromosome) {
int sizeBefore = chromosome.size();
TestCase t = ((TestChromosome) chromosome).getTestCase();
List<Integer> to_delete = new ArrayList<Integer>(chromosome.size());
boolean has_deleted = false;
int num = 0;
for (Statement s : t) {
VariableReference var = s.getReturnValue();
boolean delete = false;
delete = delete || s instanceof PrimitiveStatement;
delete = delete || s instanceof ArrayStatement;
delete = delete || s instanceof StringPrimitiveStatement;
if (!t.hasReferences(var) && delete) {
to_delete.add(num);
has_deleted = true;
}
num++;
}
Collections.sort(to_delete, Collections.reverseOrder());
for (Integer position : to_delete) {
t.remove(position);
}
int sizeAfter = chromosome.size();
if (has_deleted)
logger.debug("Removed {} unused statements", (sizeBefore - sizeAfter));
return has_deleted;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class AbstractMOSA method deriveCoveredExceptions.
/**
* This method analyzes the execution results of a TestChromosome looking for generated exceptions.
* Such exceptions are converted in instances of the class {@link ExceptionCoverageTestFitness},
* which are additional covered goals when using as criterion {@link Properties.Criterion.EXCEPTION}
* @param t TestChromosome to analyze
* @return list of exception goals being covered by t
*/
public List<ExceptionCoverageTestFitness> deriveCoveredExceptions(T t) {
List<ExceptionCoverageTestFitness> covered_exceptions = new ArrayList<ExceptionCoverageTestFitness>();
TestChromosome testCh = (TestChromosome) t;
ExecutionResult result = testCh.getLastExecutionResult();
Map<String, Set<Class<?>>> implicitTypesOfExceptions = new LinkedHashMap<>();
Map<String, Set<Class<?>>> explicitTypesOfExceptions = new LinkedHashMap<>();
Map<String, Set<Class<?>>> declaredTypesOfExceptions = new LinkedHashMap<>();
for (Integer i : result.getPositionsWhereExceptionsWereThrown()) {
if (ExceptionCoverageHelper.shouldSkip(result, i)) {
continue;
}
Class<?> exceptionClass = ExceptionCoverageHelper.getExceptionClass(result, i);
// eg name+descriptor
String methodIdentifier = ExceptionCoverageHelper.getMethodIdentifier(result, i);
// was the exception originated by a direct call on the SUT?
boolean sutException = ExceptionCoverageHelper.isSutException(result, i);
if (sutException) {
boolean notDeclared = !ExceptionCoverageHelper.isDeclared(result, i);
if (notDeclared) {
/*
* we need to distinguish whether it is explicit (ie "throw" in the code, eg for validating
* input for pre-condition) or implicit ("likely" a real fault).
*/
boolean isExplicit = ExceptionCoverageHelper.isExplicit(result, i);
if (isExplicit) {
if (!explicitTypesOfExceptions.containsKey(methodIdentifier)) {
explicitTypesOfExceptions.put(methodIdentifier, new LinkedHashSet<Class<?>>());
}
explicitTypesOfExceptions.get(methodIdentifier).add(exceptionClass);
} else {
if (!implicitTypesOfExceptions.containsKey(methodIdentifier)) {
implicitTypesOfExceptions.put(methodIdentifier, new LinkedHashSet<Class<?>>());
}
implicitTypesOfExceptions.get(methodIdentifier).add(exceptionClass);
}
} else {
if (!declaredTypesOfExceptions.containsKey(methodIdentifier)) {
declaredTypesOfExceptions.put(methodIdentifier, new LinkedHashSet<Class<?>>());
}
declaredTypesOfExceptions.get(methodIdentifier).add(exceptionClass);
}
ExceptionCoverageTestFitness.ExceptionType type = ExceptionCoverageHelper.getType(result, i);
/*
* Add goal to list of fitness functions to solve
*/
ExceptionCoverageTestFitness goal = new ExceptionCoverageTestFitness(Properties.TARGET_CLASS, methodIdentifier, exceptionClass, type);
covered_exceptions.add(goal);
}
}
return covered_exceptions;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class AbstractMOSA method mutate.
/**
* Method used to mutate an offspring
*/
private void mutate(T offspring, T parent) {
offspring.mutate();
TestChromosome tch = (TestChromosome) offspring;
if (!offspring.isChanged()) {
// if offspring is not changed, we try
// to mutate it once again
offspring.mutate();
}
if (!hasMethodCall(offspring)) {
tch.setTestCase(((TestChromosome) parent).getTestCase().clone());
boolean changed = tch.mutationInsert();
if (changed) {
for (Statement s : tch.getTestCase()) s.isValid();
}
offspring.setChanged(changed);
}
notifyMutation(offspring);
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class AbstractMOSA method hasMethodCall.
/**
* This method checks whether the test has only primitive type statements. Indeed,
* crossover and mutation can lead to tests with no method calls (methods or constructors call),
* thus, when executed they will never cover something in the class under test.
*
* @param test to check
* @return true if the test has at least one method or constructor call (i.e., the test may
* cover something when executed; false otherwise
*/
private boolean hasMethodCall(T test) {
boolean flag = false;
TestCase tc = ((TestChromosome) test).getTestCase();
for (Statement s : tc) {
if (s instanceof MethodStatement) {
MethodStatement ms = (MethodStatement) s;
boolean isTargetMethod = ms.getDeclaringClassName().equals(Properties.TARGET_CLASS);
if (isTargetMethod)
return true;
}
if (s instanceof ConstructorStatement) {
ConstructorStatement ms = (ConstructorStatement) s;
boolean isTargetMethod = ms.getDeclaringClassName().equals(Properties.TARGET_CLASS);
if (isTargetMethod)
return true;
}
}
return flag;
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class StructuralGoalManager method updateCoveredGoals.
protected void updateCoveredGoals(FitnessFunction<T> f, T tc) {
// the next two lines are needed since that coverage information are used
// during EvoSuite post-processing
TestChromosome tch = (TestChromosome) tc;
tch.getTestCase().getCoveredGoals().add((TestFitnessFunction) f);
// update covered targets
boolean toArchive = false;
T best = coveredGoals.get(f);
if (best == null) {
toArchive = true;
coveredGoals.put(f, tc);
uncoveredGoals.remove(f);
currentGoals.remove(f);
} else {
double bestSize = best.size();
double size = tc.size();
if (size < bestSize && size > 1) {
toArchive = true;
coveredGoals.put(f, tc);
archive.get(best).remove(f);
if (archive.get(best).size() == 0)
archive.remove(best);
}
}
// update archive
if (toArchive) {
List<FitnessFunction<T>> coveredTargets = archive.get(tc);
if (coveredTargets == null) {
List<FitnessFunction<T>> list = new ArrayList<FitnessFunction<T>>();
list.add(f);
archive.put(tc, list);
} else {
coveredTargets.add(f);
}
}
}
Aggregations