Search in sources :

Example 36 with SolverTimeoutException

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

the class TestStringSearch method testIndexOfC2.

@Test
public void testIndexOfC2() {
    String var1value = ":cc]#0l";
    StringVariable var1 = new StringVariable("var0", var1value);
    IntegerConstant colon_code = new IntegerConstant(58);
    IntegerConstant numeral_code = new IntegerConstant(35);
    IntegerConstant minus_one = new IntegerConstant(-1);
    StringBinaryToIntegerExpression index_of_colon = new StringBinaryToIntegerExpression(var1, Operator.INDEXOFC, colon_code, -1L);
    StringBinaryToIntegerExpression index_of_numeral = new StringBinaryToIntegerExpression(var1, Operator.INDEXOFC, numeral_code, -1L);
    /*
		 * Here we are trying to modify the string such that the first '#' comes
		 * before the first ':', and both are present
		 */
    IntegerConstraint constr1 = new IntegerConstraint(index_of_colon, Comparator.NE, minus_one);
    IntegerConstraint constr2 = new IntegerConstraint(index_of_numeral, Comparator.NE, minus_one);
    IntegerConstraint constr3 = new IntegerConstraint(index_of_numeral, Comparator.LT, index_of_colon);
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    constraints.add(constr1);
    constraints.add(constr2);
    constraints.add(constr3);
    EvoSuiteSolver solver = new EvoSuiteSolver();
    Map<String, Object> solution = null;
    try {
        /*
			 * The constraint is not trivial, as there are search plateaus. So
			 * it is ok if sometimes it fails (tried 10 times, failed 3).
			 */
        final int TRIES = 20;
        for (int i = 0; i < TRIES; i++) {
            solution = solve(solver, constraints);
            if (solution != null) {
                break;
            }
        }
        assertNotNull(solution);
        String result = solution.get("var0").toString();
        int colonPos = result.indexOf(':');
        int numeralPos = result.indexOf('#');
        assertTrue("Colon not found in " + result, colonPos >= 0);
        assertTrue("Numeral not found in " + result, numeralPos >= 0);
        assertTrue(colonPos > numeralPos);
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) ArrayList(java.util.ArrayList) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Test(org.junit.Test)

Example 37 with SolverTimeoutException

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

the class EvoSuiteSolver method solve.

@Override
public SolverResult solve(Collection<Constraint<?>> constraints) throws SolverTimeoutException, SolverEmptyQueryException {
    long timeout = Properties.DSE_CONSTRAINT_SOLVER_TIMEOUT_MILLIS;
    long startTimeMillis = System.currentTimeMillis();
    Set<Variable<?>> variables = getVariables(constraints);
    Map<String, Object> initialValues = getConcreteValues(variables);
    double distance = DistanceEstimator.getDistance(constraints);
    if (distance == 0.0) {
        log.info("Initial distance already is 0.0, skipping search");
        SolverResult satResult = SolverResult.newSAT(initialValues);
        return satResult;
    }
    for (int attempt = 0; attempt <= Properties.DSE_VARIABLE_RESETS; attempt++) {
        for (Variable<?> v : variables) {
            long currentTimeMillis = System.currentTimeMillis();
            long elapsed_solving_time = currentTimeMillis - startTimeMillis;
            if (elapsed_solving_time > timeout) {
                throw new SolverTimeoutException();
            }
            log.debug("Variable: " + v + ", " + variables);
            if (v instanceof IntegerVariable) {
                IntegerVariable integerVariable = (IntegerVariable) v;
                IntegerAVM avm = new IntegerAVM(integerVariable, constraints, startTimeMillis, timeout);
                avm.applyAVM();
            } else if (v instanceof RealVariable) {
                RealVariable realVariable = (RealVariable) v;
                RealAVM avm = new RealAVM(realVariable, constraints, startTimeMillis, timeout);
                avm.applyAVM();
            } else if (v instanceof StringVariable) {
                StringVariable strVariable = (StringVariable) v;
                StringAVM avm = new StringAVM(strVariable, constraints, startTimeMillis, timeout);
                avm.applyAVM();
            } else {
                throw new RuntimeException("Unknown variable type " + v.getClass().getName());
            }
            distance = DistanceEstimator.getDistance(constraints);
            if (distance <= 0.0) {
                log.info("Distance is 0, ending search");
                break;
            }
        }
        if (distance <= 0.0) {
            log.info("Distance is 0, ending search");
            break;
        } else {
            log.info("Randomizing variables");
            randomizeValues(variables, getConstants(constraints));
        }
    }
    // distance = DistanceEstimator.getDistance(constraints);
    if (distance <= 0) {
        log.debug("Distance is " + distance + ", found solution");
        Map<String, Object> new_model = getConcreteValues(variables);
        setConcreteValues(variables, initialValues);
        SolverResult satResult = SolverResult.newSAT(new_model);
        return satResult;
    } else {
        setConcreteValues(variables, initialValues);
        log.debug("Returning null, search was not successful");
        SolverResult unsatResult = SolverResult.newUNSAT();
        return unsatResult;
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) Variable(org.evosuite.symbolic.expr.Variable) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) SolverResult(org.evosuite.symbolic.solver.SolverResult) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) Constraint(org.evosuite.symbolic.expr.Constraint) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable)

Example 38 with SolverTimeoutException

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

the class TestIntegerSearch method testNEConstant.

@Test
public void testNEConstant() throws SolverEmptyQueryException {
    // TODO: Currently, the model returned by the search is null if the
    // constraint is already satisfied,
    // so in this example the concrete value has to be the target initially
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    constraints.add(new IntegerConstraint(new IntegerVariable("test1", 235082, -1000000, 1000000), Comparator.NE, new IntegerConstant(235082)));
    try {
        EvoSuiteSolver solver = new EvoSuiteSolver();
        SolverResult solverResult = solver.solve(constraints);
        assertTrue(solverResult.isSAT());
        Map<String, Object> model = solverResult.getModel();
        assertNotNull(model.get("test1"));
        assertTrue(235082 != ((Number) model.get("test1")).intValue());
    } 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) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) Test(org.junit.Test)

Example 39 with SolverTimeoutException

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

the class TestIntegerSearch method testLEArithmetic.

@Test
public void testLEArithmetic() throws SolverEmptyQueryException {
    int var1 = 3;
    int var2 = 1;
    int var3 = 1;
    assertTrue(var1 > var2 + var3);
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    constraints.add(new IntegerConstraint(new IntegerVariable("test1", var1, -1000000, 1000000), Comparator.LE, new IntegerBinaryExpression(new IntegerVariable("test2", var2, -1000000, 1000000), Operator.PLUS, new IntegerVariable("test3", var3, -1000000, 1000000), 0L)));
    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();
        if (model.containsKey("test3"))
            var3 = ((Number) model.get("test3")).intValue();
        assertTrue(var1 <= var2 + var3);
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) IntegerBinaryExpression(org.evosuite.symbolic.expr.bv.IntegerBinaryExpression) 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)

Example 40 with SolverTimeoutException

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

the class TestIntegerSearch method testGEArithmetic.

@Test
public void testGEArithmetic() throws SolverEmptyQueryException {
    int var1 = 0;
    int var2 = 1;
    int var3 = 1;
    assertTrue(var1 < var2 + var3);
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    constraints.add(new IntegerConstraint(new IntegerVariable("test1", var1, -1000000, 1000000), Comparator.GT, new IntegerBinaryExpression(new IntegerVariable("test2", var2, -1000000, 1000000), Operator.PLUS, new IntegerVariable("test3", var3, -1000000, 1000000), 0L)));
    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();
        if (model.containsKey("test3"))
            var3 = ((Number) model.get("test3")).intValue();
        assertTrue(var1 >= var2 + var3);
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) IntegerBinaryExpression(org.evosuite.symbolic.expr.bv.IntegerBinaryExpression) 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