Search in sources :

Example 6 with Constraint

use of org.evosuite.symbolic.expr.Constraint in project evosuite by EvoSuite.

the class ConstraintNormalizer method createRealConstraint.

private static Constraint<?> createRealConstraint(IntegerConstraint c) {
    if (c.getLeftOperand() instanceof RealComparison) {
        RealComparison cmp = (RealComparison) c.getLeftOperand();
        int value = ((Number) c.getRightOperand().getConcreteValue()).intValue();
        Comparator op = c.getComparator();
        Expression<Double> cmp_left = cmp.getLeftOperant();
        Expression<Double> cmp_right = cmp.getRightOperant();
        return createRealConstraint(cmp_left, op, cmp_right, value);
    } else {
        assert (c.getRightOperand() instanceof RealComparison);
        RealComparison cmp = (RealComparison) c.getRightOperand();
        Comparator op = c.getComparator();
        Comparator swap_op = op.swap();
        int value = ((Number) c.getLeftOperand().getConcreteValue()).intValue();
        int swap_value = -value;
        Expression<Double> cmp_left = cmp.getLeftOperant();
        Expression<Double> cmp_right = cmp.getRightOperant();
        return createRealConstraint(cmp_left, swap_op, cmp_right, swap_value);
    }
}
Also used : RealComparison(org.evosuite.symbolic.expr.bv.RealComparison) RealConstraint(org.evosuite.symbolic.expr.RealConstraint) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) Comparator(org.evosuite.symbolic.expr.Comparator)

Example 7 with Constraint

use of org.evosuite.symbolic.expr.Constraint in project evosuite by EvoSuite.

the class RealAVM method afterCommaSearch.

/**
 * Try to optimize the digits after the comma
 *
 * @param realVar
 * @param cnstr
 * @return
 */
private boolean afterCommaSearch(RealVariable realVar, Collection<Constraint<?>> cnstr) throws SolverTimeoutException {
    boolean improvement = false;
    // Assume that floats have 7 digits after comma and double 15. This is
    // based on Flopsy
    int maxPrecision = realVar.getMaxValue() > Float.MAX_VALUE ? 15 : 7;
    for (int precision = 1; precision <= maxPrecision; precision++) {
        chopOffPrecision(precision, maxPrecision == 7);
        log.debug("Current precision: " + precision);
        final double delta = Math.pow(10.0, -precision);
        final double factor = 2;
        if (doRealSearch(delta, factor))
            improvement = true;
        if (this.checkpointedDistance <= 0) {
            break;
        }
    }
    return improvement;
}
Also used : Constraint(org.evosuite.symbolic.expr.Constraint)

Example 8 with Constraint

use of org.evosuite.symbolic.expr.Constraint 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 9 with Constraint

use of org.evosuite.symbolic.expr.Constraint in project evosuite by EvoSuite.

the class DeprecatedTestSuiteDSE method reduce.

/**
 * Apply cone of influence reduction to constraints with respect to the last
 * constraint in the list
 *
 * @param constraints
 * @return
 */
private List<Constraint<?>> reduce(List<Constraint<?>> constraints) {
    Constraint<?> target = constraints.get(constraints.size() - 1);
    Set<Variable<?>> dependencies = getVariables(target);
    LinkedList<Constraint<?>> coi = new LinkedList<Constraint<?>>();
    if (dependencies.size() <= 0)
        return coi;
    coi.add(target);
    for (int i = constraints.size() - 2; i >= 0; i--) {
        Constraint<?> constraint = constraints.get(i);
        Set<Variable<?>> variables = getVariables(constraint);
        for (Variable<?> var : dependencies) {
            if (variables.contains(var)) {
                dependencies.addAll(variables);
                coi.addFirst(constraint);
                break;
            }
        }
    }
    return coi;
}
Also used : Variable(org.evosuite.symbolic.expr.Variable) Constraint(org.evosuite.symbolic.expr.Constraint) LinkedList(java.util.LinkedList) Constraint(org.evosuite.symbolic.expr.Constraint)

Example 10 with Constraint

use of org.evosuite.symbolic.expr.Constraint 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)

Aggregations

Constraint (org.evosuite.symbolic.expr.Constraint)210 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)101 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)83 Test (org.junit.Test)81 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)76 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)75 ArrayList (java.util.ArrayList)68 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)42 SolverResult (org.evosuite.symbolic.solver.SolverResult)36 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)34 StringConstraint (org.evosuite.symbolic.expr.StringConstraint)33 TestCaseStringAppendString (com.examples.with.different.packagename.solver.TestCaseStringAppendString)28 TestCaseStringIndexOfString (com.examples.with.different.packagename.solver.TestCaseStringIndexOfString)28 TestCaseStringLastIndexOfString (com.examples.with.different.packagename.solver.TestCaseStringLastIndexOfString)28 RealConstraint (org.evosuite.symbolic.expr.RealConstraint)26 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)26 RealVariable (org.evosuite.symbolic.expr.fp.RealVariable)23 StringConstant (org.evosuite.symbolic.expr.str.StringConstant)22 StringBinaryComparison (org.evosuite.symbolic.expr.bv.StringBinaryComparison)17 RealConstant (org.evosuite.symbolic.expr.fp.RealConstant)16