Search in sources :

Example 6 with BranchCondition

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

the class DefaultTestCaseConcolicExecutor method execute.

public static Collection<Constraint<?>> execute(DefaultTestCase tc) {
    List<BranchCondition> pc = getPathCondition(tc);
    Collection<Constraint<?>> constraints = new LinkedList<Constraint<?>>();
    for (BranchCondition condition : pc) {
        constraints.addAll(condition.getSupportingConstraints());
        Constraint<?> constraint = condition.getConstraint();
        constraints.add(constraint);
    }
    return constraints;
}
Also used : Constraint(org.evosuite.symbolic.expr.Constraint) BranchCondition(org.evosuite.symbolic.BranchCondition) LinkedList(java.util.LinkedList)

Example 7 with BranchCondition

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

the class PathConditionCollector method addBranchCondition.

/**
 * Add a new constraint to a branch condition
 *
 * @param className
 *            the class name where the branch is
 * @param methName
 *            the method where the branch is
 * @param branchIndex
 *            the branch index
 * @param c
 *            the constraint for the branch condition
 */
public void addBranchCondition(String className, String methName, int branchIndex, IntegerConstraint c) {
    Constraint<?> normalizedConstraint = normalizeConstraint(c);
    LinkedList<Constraint<?>> branch_supporting_constraints = new LinkedList<Constraint<?>>(currentSupportingConstraints);
    BranchCondition new_branch = new BranchCondition(className, methName, branchIndex, normalizedConstraint, branch_supporting_constraints);
    branchConditions.add(new_branch);
    currentSupportingConstraints.clear();
}
Also used : IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) BranchCondition(org.evosuite.symbolic.BranchCondition) LinkedList(java.util.LinkedList)

Example 8 with BranchCondition

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

the class TestChromosome method mutationConcolic.

/**
 * Collect path constraints and negate one of them to derive new integer
 * inputs
 *
 * @return
 */
private boolean mutationConcolic() {
    logger.info("Applying DSE mutation");
    // concolicExecution = new ConcolicExecution();
    // Apply DSE to gather constraints
    List<BranchCondition> branches = ConcolicExecution.getSymbolicPath(this);
    logger.debug("Conditions: " + branches);
    if (branches.isEmpty())
        return false;
    boolean mutated = false;
    List<BranchCondition> targetBranches = new ArrayList<BranchCondition>();
    for (BranchCondition branch : branches) {
        if (TestCluster.isTargetClassName(branch.getClassName()))
            targetBranches.add(branch);
    }
    // Select random branch
    BranchCondition branch = null;
    if (targetBranches.isEmpty())
        branch = Randomness.choice(branches);
    else
        branch = Randomness.choice(targetBranches);
    logger.debug("Trying to negate branch " + branch.getInstructionIndex() + " - have " + targetBranches.size() + "/" + branches.size() + " target branches");
    // Try to solve negated constraint
    TestCase newTest = ConcolicMutation.negateCondition(branches, branch, test);
    // If successful, add resulting test to test suite
    if (newTest != null) {
        logger.debug("CONCOLIC: Created new test");
        // logger.info(newTest.toCode());
        // logger.info("Old test");
        // logger.info(test.toCode());
        this.test = newTest;
        this.setChanged(true);
        this.lastExecutionResult = null;
    } else {
        logger.debug("CONCOLIC: Did not create new test");
    }
    return mutated;
}
Also used : ArrayList(java.util.ArrayList) BranchCondition(org.evosuite.symbolic.BranchCondition)

Example 9 with BranchCondition

use of org.evosuite.symbolic.BranchCondition 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

BranchCondition (org.evosuite.symbolic.BranchCondition)9 Constraint (org.evosuite.symbolic.expr.Constraint)6 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)4 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 Test (org.junit.Test)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)2 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)2 TestCase (org.evosuite.testcase.TestCase)2 TestChromosome (org.evosuite.testcase.TestChromosome)2 HashMap (java.util.HashMap)1 Set (java.util.Set)1 PathCondition (org.evosuite.symbolic.PathCondition)1 Comparator (org.evosuite.symbolic.expr.Comparator)1 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)1 Solver (org.evosuite.symbolic.solver.Solver)1 SolverCache (org.evosuite.symbolic.solver.SolverCache)1 SolverResult (org.evosuite.symbolic.solver.SolverResult)1