Search in sources :

Example 1 with SolverTimeoutException

use of org.evosuite.symbolic.solver.SolverTimeoutException in project evosuite by EvoSuite.

the class StringAVM method applyAVM.

/**
 * <p>
 * strLocalSearch
 * </p>
 *
 * @return a boolean.
 */
public boolean applyAVM() throws SolverTimeoutException {
    ExpressionExecutor exprExecutor = new ExpressionExecutor();
    // try to remove each
    log.debug("Trying to remove characters");
    boolean improvement = false;
    checkpointVar(DistanceEstimator.getDistance(cnstr));
    // First chop characters from the back until distance doesn't improve
    String oldString = strVar.getConcreteValue();
    boolean improved = true;
    while (improved && oldString.length() > 0) {
        if (isFinished()) {
            throw new SolverTimeoutException();
        }
        String newStr = oldString.substring(0, oldString.length() - 1);
        strVar.setConcreteValue(newStr);
        log.debug("Current attempt: " + newStr);
        improved = false;
        double newDist = DistanceEstimator.getDistance(cnstr);
        // if (distImpr(newDist)) {
        if (newDist <= checkpointDistance) {
            log.debug("Distance improved or did not increase, keeping change");
            checkpointVar(newDist);
            improvement = true;
            improved = true;
            oldString = newStr;
            if (newDist == 0) {
                return true;
            }
        } else {
            log.debug("Distance did not improve, reverting change");
            restoreVar();
        }
    }
    // next try to replace each character using AVM
    log.debug("Trying to replace characters");
    // Backup is done internally
    if (doStringAVM(oldString)) {
        improvement = true;
        oldString = strVar.getConcreteValue();
    }
    if (checkpointDistance == 0.0) {
        return true;
    }
    // try to add at the end
    log.debug("Trying to add characters");
    checkpointVar(DistanceEstimator.getDistance(cnstr));
    // Finally add new characters at the end of the string
    improved = true;
    while (improved) {
        if (isFinished()) {
            throw new SolverTimeoutException();
        }
        improved = false;
        char charToInsert = Randomness.nextChar();
        String newStr = oldString + charToInsert;
        strVar.setConcreteValue(newStr);
        double newDist = DistanceEstimator.getDistance(cnstr);
        log.debug("Adding: " + newStr + ": " + newDist);
        if (distImpr(newDist)) {
            improvement = true;
            improved = true;
            checkpointVar(newDist);
            if (checkpointDistance == 0.0) {
                log.debug("Search seems successful, stopping at " + checkpointDistance + "/" + newDist);
                return true;
            }
            doCharacterAVM(newStr.length() - 1);
            oldString = strVar.getConcreteValue();
        } else {
            restoreVar();
        }
    }
    // try to insert delimiters (if any)
    Set<StringValue> delimiters = getTokenDelimiters(cnstr);
    for (StringValue delimiter : delimiters) {
        if (isFinished()) {
            throw new SolverTimeoutException();
        }
        improved = true;
        String delimiterStr = (String) delimiter.accept(exprExecutor, null);
        while (improved) {
            if (isFinished()) {
                throw new SolverTimeoutException();
            }
            improved = false;
            char charToInsert = Randomness.nextChar();
            String newStr = oldString + delimiterStr + charToInsert;
            strVar.setConcreteValue(newStr);
            double newDist = DistanceEstimator.getDistance(cnstr);
            log.debug("Adding: " + newStr + ": " + newDist);
            if (distImpr(newDist)) {
                improvement = true;
                improved = true;
                checkpointVar(newDist);
                if (checkpointDistance == 0.0) {
                    log.debug("Search seems successful, stopping at " + checkpointDistance + "/" + newDist);
                    return true;
                }
                doCharacterAVM(newStr.length() - 1);
                oldString = strVar.getConcreteValue();
            } else {
                restoreVar();
            }
        }
    }
    return improvement;
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) ExpressionExecutor(org.evosuite.symbolic.expr.ExpressionExecutor) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 2 with SolverTimeoutException

use of org.evosuite.symbolic.solver.SolverTimeoutException in project evosuite by EvoSuite.

the class CVC4ResultParser method parse.

public SolverResult parse(String cvc4ResultStr) throws SolverParseException, SolverErrorException, SolverTimeoutException {
    if (cvc4ResultStr.startsWith("sat")) {
        logger.debug("CVC4 outcome was SAT");
        SolverResult satResult = parseModel(cvc4ResultStr);
        return satResult;
    } else if (cvc4ResultStr.startsWith("unsat")) {
        logger.debug("CVC4 outcome was UNSAT");
        SolverResult unsatResult = SolverResult.newUNSAT();
        return unsatResult;
    } else if (cvc4ResultStr.startsWith("unknown")) {
        logger.debug("CVC4 outcome was UNKNOWN (probably due to timeout)");
        throw new SolverTimeoutException();
    } else if (cvc4ResultStr.startsWith("(error")) {
        logger.debug("CVC4 output was the following " + cvc4ResultStr);
        throw new SolverErrorException("An error (probably an invalid input) occurred while executing CVC4");
    } else {
        logger.debug("The following CVC4 output could not be parsed " + cvc4ResultStr);
        throw new SolverParseException("CVC4 output is unknown. We are unable to parse it to a proper solution!", cvc4ResultStr);
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) SolverErrorException(org.evosuite.symbolic.solver.SolverErrorException) SolverParseException(org.evosuite.symbolic.solver.SolverParseException) SolverResult(org.evosuite.symbolic.solver.SolverResult)

Example 3 with SolverTimeoutException

use of org.evosuite.symbolic.solver.SolverTimeoutException in project evosuite by EvoSuite.

the class TestConstraintSolver method testCase2.

@Test
public void testCase2() throws SecurityException, NoSuchMethodException, SolverEmptyQueryException {
    DefaultTestCase tc = buildTestCase2();
    // build patch condition
    List<BranchCondition> branch_conditions = executeTest(tc);
    assertEquals(57, branch_conditions.size());
    // keep only 2 top-most branch conditions
    List<BranchCondition> sublist = new ArrayList<BranchCondition>();
    sublist.add(branch_conditions.get(0));
    sublist.add(branch_conditions.get(1));
    // invoke seeker
    try {
        SolverResult solverResult = executeSolver(sublist);
        assertNotNull(solverResult);
        assertTrue(solverResult.isSAT());
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) ArrayList(java.util.ArrayList) SolverResult(org.evosuite.symbolic.solver.SolverResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 4 with SolverTimeoutException

use of org.evosuite.symbolic.solver.SolverTimeoutException in project evosuite by EvoSuite.

the class TestConstraintSolver2 method test.

@Test
public void test() throws SolverEmptyQueryException {
    // 5000000000000L; TODO - ??
    Properties.LOCAL_SEARCH_BUDGET = 100;
    Properties.LOCAL_SEARCH_BUDGET_TYPE = LocalSearchBudgetType.FITNESS_EVALUATIONS;
    Collection<Constraint<?>> constraints = buildConstraintSystem();
    System.out.println("Constraints:");
    for (Constraint<?> c : constraints) {
        System.out.println(c.toString());
    }
    System.out.println("");
    System.out.println("Initial: " + INIT_STRING);
    EvoSuiteSolver solver = new EvoSuiteSolver();
    try {
        SolverResult solverResult = solver.solve(constraints);
        assertTrue(solverResult.isSAT());
        Map<String, Object> model = solverResult.getModel();
        assertNotNull(model);
        Object var0 = model.get("var0");
        System.out.println("Expected: " + EXPECTED_STRING);
        System.out.println("Found: " + var0);
        assertEquals(EXPECTED_STRING, var0);
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) SolverResult(org.evosuite.symbolic.solver.SolverResult) Test(org.junit.Test)

Example 5 with SolverTimeoutException

use of org.evosuite.symbolic.solver.SolverTimeoutException in project evosuite by EvoSuite.

the class TestIntegerSearch method testEQVariable.

@Test
public void testEQVariable() throws SolverEmptyQueryException {
    int var1 = 0;
    int var2 = 1;
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    constraints.add(new IntegerConstraint(new IntegerVariable("test1", var1, -1000000, 1000000), Comparator.EQ, new IntegerVariable("test2", var2, -1000000, 1000000)));
    try {
        EvoSuiteSolver solver = new EvoSuiteSolver();
        SolverResult solverResult = solver.solve(constraints);
        assertTrue(solverResult.isSAT());
        Map<String, Object> model = solverResult.getModel();
        if (model.containsKey("test1"))
            var1 = ((Number) model.get("test1")).intValue();
        if (model.containsKey("test2"))
            var2 = ((Number) model.get("test2")).intValue();
        assertEquals(var1, var2);
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) ArrayList(java.util.ArrayList) SolverResult(org.evosuite.symbolic.solver.SolverResult) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) Test(org.junit.Test)

Aggregations

SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)79 Constraint (org.evosuite.symbolic.expr.Constraint)75 Test (org.junit.Test)75 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)74 ArrayList (java.util.ArrayList)68 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)48 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)35 SolverResult (org.evosuite.symbolic.solver.SolverResult)34 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)25 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)24 RealConstraint (org.evosuite.symbolic.expr.RealConstraint)22 StringConstraint (org.evosuite.symbolic.expr.StringConstraint)22 RealVariable (org.evosuite.symbolic.expr.fp.RealVariable)22 StringConstant (org.evosuite.symbolic.expr.str.StringConstant)18 RealConstant (org.evosuite.symbolic.expr.fp.RealConstant)15 StringBinaryComparison (org.evosuite.symbolic.expr.bv.StringBinaryComparison)13 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)10 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)8 StringUnaryExpression (org.evosuite.symbolic.expr.str.StringUnaryExpression)5 Expression (org.evosuite.symbolic.expr.Expression)4