Search in sources :

Example 1 with PathCondition

use of org.evosuite.symbolic.PathCondition in project evosuite by EvoSuite.

the class DSETestGenerator method buildQuery.

/**
 * Creates a Solver query give a branch condition
 *
 * @param condition
 * @return
 */
private List<Constraint<?>> buildQuery(PathCondition pc, int conditionIndex) {
    // negate target branch condition
    PathCondition negatedPathCondition = pc.negate(conditionIndex);
    // get constraints for negated path condition
    List<Constraint<?>> query = negatedPathCondition.getConstraints();
    // Compute cone of influence reduction
    List<Constraint<?>> simplified_query = reduce(query);
    return simplified_query;
}
Also used : PathCondition(org.evosuite.symbolic.PathCondition) Constraint(org.evosuite.symbolic.expr.Constraint)

Example 2 with PathCondition

use of org.evosuite.symbolic.PathCondition in project evosuite by EvoSuite.

the class DSETestGenerator method generateNewTest.

/**
 * Applies DSE to the passed test using as symbolic variables only those
 * that are declared in the set of statement indexes. The objective is used
 * to detect if the DSE has improved the fitness.
 *
 * @param test
 *            the test case to be used as parameterised unit test
 *
 * @param statementIndexes
 *            a set with statement indexes with primitive value declarations
 *            that can be used as symbolic variables. This set must be
 *            non-empty.
 *
 * @param objective
 *            the local search objective to measure fitness improvement.
 */
public TestChromosome generateNewTest(final TestChromosome test, Set<Integer> statementIndexes, LocalSearchObjective<TestChromosome> objective) {
    logger.info("APPLYING DSE EEEEEEEEEEEEEEEEEEEEEEE");
    logger.info(test.getTestCase().toCode());
    logger.info("Starting concolic execution");
    // Backup copy
    // test.getMutationHistory().clear();
    // I am not sure what is the purpose of this
    test.clone();
    DefaultTestCase clone_test_case = (DefaultTestCase) test.getTestCase().clone();
    List<BranchCondition> branchConditions = ConcolicExecution.executeConcolic(clone_test_case);
    final PathCondition collectedPathCondition = new PathCondition(branchConditions);
    logger.info("Done concolic execution");
    if (collectedPathCondition.isEmpty()) {
        return null;
    }
    for (BranchCondition c : collectedPathCondition.getBranchConditions()) {
        logger.info(" -> " + c.getConstraint());
    }
    Set<VariableReference> symbolicVariables = new HashSet<VariableReference>();
    for (Integer position : statementIndexes) {
        final VariableReference variableReference = test.getTestCase().getStatement(position).getReturnValue();
        symbolicVariables.add(variableReference);
    }
    logger.info("Checking {} conditions", collectedPathCondition.size());
    List<Integer> conditionIndexesNotCoveredTwoWays = computeConditionIndexesNotCoveredTwoWays(test, collectedPathCondition);
    // 
    for (int conditionIndex = 0; conditionIndex < collectedPathCondition.size(); conditionIndex++) {
        BranchCondition condition = collectedPathCondition.get(conditionIndex);
        if (LocalSearchBudget.getInstance().isFinished()) {
            logger.debug("Local search budget used up: " + Properties.LOCAL_SEARCH_BUDGET_TYPE);
            break;
        }
        logger.debug("Local search budget not yet used up");
        if (!conditionIndexesNotCoveredTwoWays.contains(conditionIndex)) {
            // skip branches covered two ways
            continue;
        }
        logger.info("Current condition: " + conditionIndex + "/" + collectedPathCondition.size() + ": " + condition.getConstraint());
        // Determine if this a branch condition depending on the target
        // statement
        Constraint<?> currentConstraint = condition.getConstraint();
        if (!isRelevant(currentConstraint, symbolicVariables)) {
            // if(!isRelevant(currentConstraint, test.getTestCase(),
            // statement)) {
            logger.info("Is not relevant for " + symbolicVariables);
            continue;
        }
        logger.info("Is relevant for " + symbolicVariables);
        List<Constraint<?>> query = buildQuery(collectedPathCondition, conditionIndex);
        logger.info("Trying to solve: ");
        for (Constraint<?> c : query) {
            logger.info("  " + c);
        }
        DSEStats.getInstance().reportNewConstraints(query);
        // Get solution
        Solver solver = SolverFactory.getInstance().buildNewSolver();
        long startSolvingTime = System.currentTimeMillis();
        SolverCache solverCache = SolverCache.getInstance();
        SolverResult solverResult = solverCache.solve(solver, query);
        long estimatedSolvingTime = System.currentTimeMillis() - startSolvingTime;
        DSEStats.getInstance().reportNewSolvingTime(estimatedSolvingTime);
        if (solverResult == null) {
            logger.info("Found no result");
        } else if (solverResult.isUNSAT()) {
            logger.info("Found UNSAT result");
            DSEStats.getInstance().reportNewUNSAT();
        } else {
            logger.info("Found SAT result");
            DSEStats.getInstance().reportNewSAT();
            Map<String, Object> model = solverResult.getModel();
            TestCase oldTest = test.getTestCase();
            ExecutionResult oldResult = test.getLastExecutionResult().clone();
            TestCase newTest = updateTest(oldTest, model);
            logger.info("New test: " + newTest.toCode());
            test.setTestCase(newTest);
            // test.clearCachedMutationResults(); // TODO Mutation
            test.clearCachedResults();
            if (objective.hasImproved(test)) {
                DSEStats.getInstance().reportNewTestUseful();
                logger.info("Solution improves fitness, finishing DSE");
                /* new test was created */
                return test;
            } else {
                DSEStats.getInstance().reportNewTestUnuseful();
                test.setTestCase(oldTest);
                // FIXXME: How can this be null?
                if (oldResult != null)
                    test.setLastExecutionResult(oldResult);
            // TODO Mutation
            }
        }
    }
    /* no new test was created */
    return null;
}
Also used : PathCondition(org.evosuite.symbolic.PathCondition) Solver(org.evosuite.symbolic.solver.Solver) VariableReference(org.evosuite.testcase.variable.VariableReference) Constraint(org.evosuite.symbolic.expr.Constraint) SolverResult(org.evosuite.symbolic.solver.SolverResult) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) BranchCondition(org.evosuite.symbolic.BranchCondition) SolverCache(org.evosuite.symbolic.solver.SolverCache) Constraint(org.evosuite.symbolic.expr.Constraint) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

PathCondition (org.evosuite.symbolic.PathCondition)2 Constraint (org.evosuite.symbolic.expr.Constraint)2 HashSet (java.util.HashSet)1 Map (java.util.Map)1 BranchCondition (org.evosuite.symbolic.BranchCondition)1 Solver (org.evosuite.symbolic.solver.Solver)1 SolverCache (org.evosuite.symbolic.solver.SolverCache)1 SolverResult (org.evosuite.symbolic.solver.SolverResult)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 TestCase (org.evosuite.testcase.TestCase)1 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)1 VariableReference (org.evosuite.testcase.variable.VariableReference)1