Search in sources :

Example 46 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.

the class LineCoverageSuiteFitness method updateCoveredGoals.

@Override
public boolean updateCoveredGoals() {
    if (!Properties.TEST_ARCHIVE) {
        return false;
    }
    for (Integer goalID : this.toRemoveLines) {
        TestFitnessFunction ff = this.lineGoals.remove(goalID);
        if (ff != null) {
            this.removedLines.add(goalID);
        } else {
            throw new IllegalStateException("goal to remove not found");
        }
    }
    this.toRemoveLines.clear();
    logger.info("Current state of archive: " + Archive.getArchiveInstance().toString());
    assert this.numLines == this.lineGoals.size() + this.removedLines.size();
    return true;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 47 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.

the class LineCoverageSuiteFitness method initializeControlDependencies.

/**
 * Add guidance to the fitness function by including branch distances on
 * all control dependencies
 */
private void initializeControlDependencies() {
    // In case we target more than one class (context, or inner classes)
    Set<String> targetClasses = new LinkedHashSet<>();
    for (TestFitnessFunction ff : lineGoals.values()) {
        targetClasses.add(ff.getTargetClass());
    }
    for (String className : targetClasses) {
        List<BytecodeInstruction> instructions = BytecodeInstructionPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getInstructionsIn(className);
        if (instructions == null) {
            logger.info("No instructions known for class {} (is it an enum?)", className);
            continue;
        }
        for (BytecodeInstruction bi : instructions) {
            if (bi.getBasicBlock() == null) {
                // Labels get no basic block. TODO - why?
                continue;
            }
            // The order of CDs may be nondeterminstic
            // TODO: A better solution would be to make the CD order deterministic rather than sorting here
            List<ControlDependency> cds = new ArrayList<>(bi.getControlDependencies());
            Collections.sort(cds);
            for (ControlDependency cd : cds) {
                if (cd.getBranchExpressionValue()) {
                    branchesToCoverTrue.add(cd.getBranch().getActualBranchId());
                } else {
                    branchesToCoverFalse.add(cd.getBranch().getActualBranchId());
                }
            }
        }
    }
    branchesToCoverBoth.addAll(branchesToCoverTrue);
    branchesToCoverBoth.retainAll(branchesToCoverFalse);
    branchesToCoverTrue.removeAll(branchesToCoverBoth);
    branchesToCoverFalse.removeAll(branchesToCoverBoth);
    logger.info("Covering branches true: " + branchesToCoverTrue);
    logger.info("Covering branches false: " + branchesToCoverFalse);
    logger.info("Covering branches both: " + branchesToCoverBoth);
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction) ControlDependency(org.evosuite.graphs.cfg.ControlDependency)

Example 48 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.

the class LineCoverageSuiteFitness method analyzeTraces.

/**
 * Iterate over all execution results and summarize statistics
 *
 * @param results
 * @param coveredLines
 * @return
 */
private boolean analyzeTraces(List<ExecutionResult> results, Set<Integer> coveredLines) {
    boolean hasTimeoutOrTestException = false;
    for (ExecutionResult result : results) {
        if (result.hasTimeout() || result.hasTestException()) {
            hasTimeoutOrTestException = true;
            continue;
        }
        TestChromosome test = new TestChromosome();
        test.setTestCase(result.test);
        test.setLastExecutionResult(result);
        test.setChanged(false);
        for (Integer goalID : this.lineGoals.keySet()) {
            TestFitnessFunction goal = this.lineGoals.get(goalID);
            // archive is updated by the TestFitnessFunction class
            double fit = goal.getFitness(test, result);
            if (fit == 0.0) {
                // helper to count the number of covered goals
                coveredLines.add(goalID);
                // goal to not be considered by the next iteration of the evolutionary algorithm
                this.toRemoveLines.add(goalID);
            }
        }
    }
    return hasTimeoutOrTestException;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 49 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.

the class MethodCoverageSuiteFitness method analyzeTraces.

/**
 * Iterate over all execution results and summarize statistics
 *
 * @param results
 * @param calledMethods
 * @return
 */
protected boolean analyzeTraces(List<ExecutionResult> results, Set<String> calledMethods) {
    boolean hasTimeoutOrTestException = false;
    for (ExecutionResult result : results) {
        if (result.hasTimeout() || result.hasTestException()) {
            hasTimeoutOrTestException = true;
            continue;
        }
        TestChromosome test = new TestChromosome();
        test.setTestCase(result.test);
        test.setLastExecutionResult(result);
        test.setChanged(false);
        for (String methodName : this.methodCoverageMap.keySet()) {
            TestFitnessFunction goal = this.methodCoverageMap.get(methodName);
            // archive is updated by the TestFitnessFunction class
            double fit = goal.getFitness(test, result);
            if (fit == 0.0) {
                // helper to count the number of covered goals
                calledMethods.add(methodName);
                // goal to not be considered by the next iteration of the evolutionary algorithm
                this.toRemoveMethods.add(methodName);
            }
        }
        // In case there were exceptions in a constructor
        handleConstructorExceptions(test, result, calledMethods);
    }
    return hasTimeoutOrTestException;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 50 with TestFitnessFunction

use of org.evosuite.testcase.TestFitnessFunction in project evosuite by EvoSuite.

the class MethodCoverageSuiteFitness method handleConstructorExceptions.

/**
 * If there is an exception in a super-constructor, then the corresponding
 * constructor might not be included in the execution trace
 *
 * @param suite
 * @param results
 * @param calledMethods
 */
protected void handleConstructorExceptions(TestChromosome test, ExecutionResult result, Set<String> calledMethods) {
    if (result.hasTimeout() || result.hasTestException() || result.noThrownExceptions())
        return;
    Integer exceptionPosition = result.getFirstPositionOfThrownException();
    Statement statement = result.test.getStatement(exceptionPosition);
    if (statement instanceof ConstructorStatement) {
        ConstructorStatement c = (ConstructorStatement) statement;
        String className = c.getConstructor().getName();
        String methodName = "<init>" + Type.getConstructorDescriptor(c.getConstructor().getConstructor());
        String name = className + "." + methodName;
        if (methodCoverageMap.containsKey(name) && !calledMethods.contains(name)) {
            TestFitnessFunction goal = methodCoverageMap.get(name);
            // only include methods being called
            test.getTestCase().addCoveredGoal(goal);
            calledMethods.add(name);
            this.toRemoveMethods.add(name);
            if (Properties.TEST_ARCHIVE) {
                Archive.getArchiveInstance().updateArchive(goal, test, 0.0);
            }
        }
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Aggregations

TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)73 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)26 TestCase (org.evosuite.testcase.TestCase)24 Test (org.junit.Test)22 TestChromosome (org.evosuite.testcase.TestChromosome)21 ArrayList (java.util.ArrayList)20 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)12 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)11 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)11 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)9 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)9 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)8 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)8 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)8 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)4 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)4 Properties (org.evosuite.Properties)3