Search in sources :

Example 1 with BranchCondition

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

the class DeprecatedTestSuiteDSE method calculateUncoveredBranches.

/**
 * Iterate over path constraints to identify those which map to branches
 * that are only covered one way
 */
private void calculateUncoveredBranches() {
    unsolvedBranchConditions.clear();
    if (Properties.DSE_NEGATE_ALL_CONDITIONS == true) {
        for (TestChromosome testChromosome : pathConditions.keySet()) {
            final List<BranchCondition> pathCondition = pathConditions.get(testChromosome);
            for (BranchCondition branch : pathCondition) {
                if (!unsolvableBranchConditions.contains(branch)) {
                    unsolvedBranchConditions.add(new TestBranchPair(testChromosome, pathCondition, branch));
                }
            }
        }
    } else {
        Map<String, Map<Comparator, Set<TestBranchPair>>> solvedConstraints = new HashMap<String, Map<Comparator, Set<TestBranchPair>>>();
        for (TestChromosome test : pathConditions.keySet()) {
            final List<BranchCondition> pathCondition = pathConditions.get(test);
            for (BranchCondition branch : pathCondition) {
                if (unsolvableBranchConditions.contains(branch))
                    continue;
                String index = getBranchIndex(branch);
                if (!solvedConstraints.containsKey(index))
                    solvedConstraints.put(index, new HashMap<Comparator, Set<TestBranchPair>>());
                Constraint<?> c = branch.getConstraint();
                if (!solvedConstraints.get(index).containsKey(c.getComparator()))
                    solvedConstraints.get(index).put(c.getComparator(), new HashSet<TestBranchPair>());
                solvedConstraints.get(index).get(c.getComparator()).add(new TestBranchPair(test, pathCondition, branch));
            }
        }
        for (String index : solvedConstraints.keySet()) {
            if (solvedConstraints.get(index).size() == 1) {
                Set<TestBranchPair> branches = solvedConstraints.get(index).values().iterator().next();
                unsolvedBranchConditions.addAll(branches);
            }
        }
        logger.info("Update set of unsolved branch conditions to " + unsolvedBranchConditions.size());
        if (Properties.DSE_RANK_BRANCH_CONDITIONS == false) {
            Randomness.shuffle((ArrayList<TestBranchPair>) unsolvedBranchConditions);
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) BranchCondition(org.evosuite.symbolic.BranchCondition) Comparator(org.evosuite.symbolic.expr.Comparator) TestChromosome(org.evosuite.testcase.TestChromosome) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with BranchCondition

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

the class DeprecatedTestSuiteDSE method applyDSE0.

/**
 * Attempt to negate individual branches until budget is used up, or there
 * are no further branches to negate
 *
 * @param individual
 */
public boolean applyDSE0(TestSuiteChromosome individual) {
    logger.info("[DSE] Current test suite: " + individual.toString());
    boolean wasSuccess = false;
    // expansion already happens as part of LS
    // TestSuiteChromosome expandedTests = expandTestSuite(individual);
    TestSuiteChromosome expandedTests = individual.clone();
    createPathConstraints(expandedTests);
    // fitness.getFitness(expandedTests);
    double originalFitness = getFitness(individual);
    while (hasNextBranchCondition() && !LocalSearchBudget.getInstance().isFinished()) {
        logger.info("Branches remaining: " + unsolvedBranchConditions.size());
        TestBranchPair next = getNextBranchCondition();
        BranchCondition branch = next.branch;
        List<BranchCondition> pathCondition = next.pathCondition;
        List<Constraint<?>> reachingConstraints = new LinkedList<Constraint<?>>();
        for (BranchCondition b : pathCondition) {
            reachingConstraints.addAll(b.getSupportingConstraints());
            if (b == branch) {
                break;
            }
            reachingConstraints.add(b.getConstraint());
        }
        List<Constraint<?>> constraints = new LinkedList<Constraint<?>>();
        TestCase newTest = negateCondition(new HashSet<Constraint<?>>(reachingConstraints), branch.getConstraint(), next.test.getTestCase());
        if (newTest != null) {
            logger.info("Found new test: " + newTest.toCode());
            TestChromosome newTestChromosome = new TestChromosome();
            newTestChromosome.setTestCase(newTest);
            expandedTests.addTest(newTestChromosome);
            if (Properties.DSE_KEEP_ALL_TESTS) {
                updatePathConstraints(newTestChromosome);
                calculateUncoveredBranches();
                individual.addTest(newTest);
                wasSuccess = true;
            } else {
                if (getFitness(expandedTests) < originalFitness) {
                    logger.info("New test improves fitness to {}", getFitness(expandedTests));
                    DSEStats.getInstance().reportNewTestUseful();
                    wasSuccess = true;
                    // no need to clone so we can keep executionresult
                    updatePathConstraints(newTestChromosome);
                    calculateUncoveredBranches(newTestChromosome);
                    individual.addTest(newTest);
                    originalFitness = getFitness(expandedTests);
                // TODO: Cancel on fitness 0 - would need to know if
                // ZeroFitness is a stopping condition
                } else {
                    logger.info("New test does not improve fitness");
                    DSEStats.getInstance().reportNewTestUnuseful();
                    expandedTests.deleteTest(newTest);
                }
            }
            success++;
        } else {
            unsolvableBranchConditions.add(branch);
            failed++;
            logger.info("Failed to find new test.");
        }
    }
    logger.info("Finished DSE");
    // Ensure fitness values are up to date.
    getFitness(individual);
    LocalSearchBudget.getInstance().countLocalSearchOnTestSuite();
    return wasSuccess;
}
Also used : Constraint(org.evosuite.symbolic.expr.Constraint) TestCase(org.evosuite.testcase.TestCase) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) BranchCondition(org.evosuite.symbolic.BranchCondition) TestChromosome(org.evosuite.testcase.TestChromosome) LinkedList(java.util.LinkedList)

Example 3 with BranchCondition

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

the class TestStringSearch2 method testSolveIndexOfConstant.

@Test
public void testSolveIndexOfConstant() throws SecurityException, NoSuchMethodException {
    DefaultTestCase tc = buildTestCase("V*X-:o%tp");
    List<BranchCondition> branch_conditions = executeTest(tc);
    Collection<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    for (int i = 0; i < branch_conditions.size() - 2; i++) {
        BranchCondition b = branch_conditions.get(i);
        constraints.addAll(b.getSupportingConstraints());
        constraints.add(b.getConstraint());
    }
    BranchCondition last_branch = branch_conditions.get(branch_conditions.size() - 2);
    constraints.addAll(last_branch.getSupportingConstraints());
    constraints.add(last_branch.getConstraint().negate());
    EvoSuiteSolver solver = new EvoSuiteSolver();
    Map<String, Object> solution;
    try {
        solution = solve(solver, constraints);
        assertNotNull(solution);
        System.out.println(solution);
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) Constraint(org.evosuite.symbolic.expr.Constraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) BranchCondition(org.evosuite.symbolic.BranchCondition) Constraint(org.evosuite.symbolic.expr.Constraint) Test(org.junit.Test)

Example 4 with BranchCondition

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

the class TestStringSearch2 method testCreatePathConstraint.

@Test
public void testCreatePathConstraint() throws SecurityException, NoSuchMethodException {
    DefaultTestCase tc = buildTestCase("urn:pBth:/A/B/C/doc.html#gilada");
    List<BranchCondition> branch_conditions = executeTest(tc);
    assertEquals(11, branch_conditions.size());
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) BranchCondition(org.evosuite.symbolic.BranchCondition) Test(org.junit.Test)

Example 5 with BranchCondition

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

the class TestStringSearch2 method testSolvePathConstraint.

@Test
public void testSolvePathConstraint() throws SecurityException, NoSuchMethodException {
    DefaultTestCase tc = buildTestCase("urn:pBth:/A/B/C/doc.html#gilada");
    List<BranchCondition> branch_conditions = executeTest(tc);
    Collection<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    for (int i = 0; i < branch_conditions.size() - 1; i++) {
        BranchCondition b = branch_conditions.get(i);
        constraints.addAll(b.getSupportingConstraints());
        constraints.add(b.getConstraint());
    }
    BranchCondition last_branch = branch_conditions.get(branch_conditions.size() - 1);
    constraints.addAll(last_branch.getSupportingConstraints());
    constraints.add(last_branch.getConstraint().negate());
    EvoSuiteSolver solver = new EvoSuiteSolver();
    Map<String, Object> solution;
    try {
        solution = solve(solver, constraints);
        assertNotNull(solution);
        System.out.println(solution);
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) Constraint(org.evosuite.symbolic.expr.Constraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) BranchCondition(org.evosuite.symbolic.BranchCondition) Constraint(org.evosuite.symbolic.expr.Constraint) Test(org.junit.Test)

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