Search in sources :

Example 31 with SolverResult

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

the class DeprecatedTestSuiteDSE method negateCondition.

/**
 * Generate new constraint and ask solver for solution
 *
 * @param condition
 * @param test
 * @return
 */
// @SuppressWarnings("rawtypes")
// @SuppressWarnings("rawtypes")
@SuppressWarnings({ "unchecked", "rawtypes" })
private TestCase negateCondition(Set<Constraint<?>> reachingConstraints, Constraint<?> localConstraint, TestCase test) {
    List<Constraint<?>> constraints = new LinkedList<Constraint<?>>();
    constraints.addAll(reachingConstraints);
    Constraint<?> targetConstraint = localConstraint.negate();
    constraints.add(targetConstraint);
    if (!targetConstraint.isSolveable()) {
        logger.info("Found unsolvable constraint: " + targetConstraint);
        // Could we treat this as a special case?
        return null;
    }
    int size = constraints.size();
    /*
		 * int counter = 0; for (Constraint cnstr : constraints) { logger.debug(
		 * "Cnstr " + (counter++) + " : " + cnstr + " dist: " +
		 * DistanceEstimator.getDistance(constraints)); }
		 */
    if (size > 0) {
        logger.debug("Calculating cone of influence for " + size + " constraints");
        constraints = reduce(constraints);
        logger.info("Reduced constraints from " + size + " to " + constraints.size());
    // for (Constraint<?> c : constraints) {
    // logger.info(c.toString());
    // }
    }
    nrCurrConstraints = constraints.size();
    nrConstraints += nrCurrConstraints;
    logger.info("Applying local search");
    Solver solver = SolverFactory.getInstance().buildNewSolver();
    DSEStats.getInstance().reportNewConstraints(constraints);
    long startSolvingTime = System.currentTimeMillis();
    SolverCache solverCache = SolverCache.getInstance();
    SolverResult solverResult = solverCache.solve(solver, constraints);
    long estimatedSolvingTime = System.currentTimeMillis() - startSolvingTime;
    DSEStats.getInstance().reportNewSolvingTime(estimatedSolvingTime);
    if (solverResult == null) {
        logger.info("Found no solution");
        /* Timeout, parseException, error, trivialSolution, etc. */
        return null;
    } else if (solverResult.isUNSAT()) {
        logger.info("Found UNSAT solution");
        DSEStats.getInstance().reportNewUNSAT();
        return null;
    } else {
        Map<String, Object> model = solverResult.getModel();
        DSEStats.getInstance().reportNewSAT();
        TestCase newTest = test.clone();
        for (Object key : model.keySet()) {
            Object val = model.get(key);
            if (val != null) {
                logger.info("New value: " + key + ": " + val);
                if (val instanceof Long) {
                    Long value = (Long) val;
                    String name = ((String) key).replace("__SYM", "");
                    // logger.warn("New long value for " + name + " is " +
                    // value);
                    PrimitiveStatement p = getStatement(newTest, name);
                    if (p.getValue().getClass().equals(Character.class))
                        p.setValue((char) value.intValue());
                    else if (p.getValue().getClass().equals(Long.class))
                        p.setValue(value);
                    else if (p.getValue().getClass().equals(Integer.class))
                        p.setValue(value.intValue());
                    else if (p.getValue().getClass().equals(Short.class))
                        p.setValue(value.shortValue());
                    else if (p.getValue().getClass().equals(Boolean.class))
                        p.setValue(value.intValue() > 0);
                    else if (p.getValue().getClass().equals(Byte.class))
                        p.setValue(value.byteValue() > 0);
                    else
                        logger.warn("New value is of an unsupported type: " + p.getValue().getClass() + val);
                } else if (val instanceof String) {
                    String name = ((String) key).replace("__SYM", "");
                    PrimitiveStatement p = getStatement(newTest, name);
                    // val);
                    assert (p != null) : "Could not find variable " + name + " in test: " + newTest.toCode() + " / Orig test: " + test.toCode() + ", seed: " + Randomness.getSeed();
                    if (p.getValue().getClass().equals(Character.class))
                        p.setValue((char) Integer.parseInt(val.toString()));
                    else
                        p.setValue(val.toString());
                } else if (val instanceof Double) {
                    Double value = (Double) val;
                    String name = ((String) key).replace("__SYM", "");
                    PrimitiveStatement p = getStatement(newTest, name);
                    // value);
                    assert (p != null) : "Could not find variable " + name + " in test: " + newTest.toCode() + " / Orig test: " + test.toCode() + ", seed: " + Randomness.getSeed();
                    if (p.getValue().getClass().equals(Double.class))
                        p.setValue(value);
                    else if (p.getValue().getClass().equals(Float.class))
                        p.setValue(value.floatValue());
                    else
                        logger.warn("New value is of an unsupported type: " + val);
                } else {
                    logger.debug("New value is of an unsupported type: " + val);
                }
            } else {
                logger.debug("New value is null");
            }
        }
        return newTest;
    }
}
Also used : Solver(org.evosuite.symbolic.solver.Solver) Constraint(org.evosuite.symbolic.expr.Constraint) SolverResult(org.evosuite.symbolic.solver.SolverResult) SolverCache(org.evosuite.symbolic.solver.SolverCache) LinkedList(java.util.LinkedList) Constraint(org.evosuite.symbolic.expr.Constraint) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) TestCase(org.evosuite.testcase.TestCase) HashMap(java.util.HashMap) Map(java.util.Map)

Example 32 with SolverResult

use of org.evosuite.symbolic.solver.SolverResult 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 33 with SolverResult

use of org.evosuite.symbolic.solver.SolverResult 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 34 with SolverResult

use of org.evosuite.symbolic.solver.SolverResult 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)

Example 35 with SolverResult

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

the class TestIntegerSearch method testEQArithmetic.

@Test
public void testEQArithmetic() 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.EQ, 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

SolverResult (org.evosuite.symbolic.solver.SolverResult)50 Test (org.junit.Test)37 Constraint (org.evosuite.symbolic.expr.Constraint)36 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)34 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)31 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)27 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)26 ArrayList (java.util.ArrayList)24 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)14 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)7 HashMap (java.util.HashMap)6 Variable (org.evosuite.symbolic.expr.Variable)4 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 Solver (org.evosuite.symbolic.solver.Solver)3 SolverCache (org.evosuite.symbolic.solver.SolverCache)3 SolverEmptyQueryException (org.evosuite.symbolic.solver.SolverEmptyQueryException)3 SolverErrorException (org.evosuite.symbolic.solver.SolverErrorException)3