Search in sources :

Example 71 with TestChromosome

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;
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) TestCase(org.evosuite.testcase.TestCase) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) ArrayList(java.util.ArrayList) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 72 with TestChromosome

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;
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap) ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 73 with TestChromosome

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);
}
Also used : ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 74 with TestChromosome

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;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) TestCase(org.evosuite.testcase.TestCase) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 75 with TestChromosome

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);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

TestChromosome (org.evosuite.testcase.TestChromosome)128 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)47 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)33 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)22 ArrayList (java.util.ArrayList)17 TestCase (org.evosuite.testcase.TestCase)17 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)16 HashSet (java.util.HashSet)15 Properties (org.evosuite.Properties)15 Test (org.junit.Test)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)14 HashMap (java.util.HashMap)11 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)10 AbstractTestSuiteChromosome (org.evosuite.testsuite.AbstractTestSuiteChromosome)8 LinkedHashMap (java.util.LinkedHashMap)7 Foo (com.examples.with.different.packagename.symbolic.Foo)6 LinkedHashSet (java.util.LinkedHashSet)6 Set (java.util.Set)6 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 Map (java.util.Map)5