Search in sources :

Example 1 with SolverResult

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

the class ConcolicMutation method negateCondition.

/**
 * Generate new constraint and ask solver for solution
 *
 * @param pathCondition
 *
 * @param targetCondition
 *            a {@link org.evosuite.symbolic.BranchCondition} object.
 * @param test
 *            a {@link org.evosuite.testcase.TestCase} object.
 * @return a {@link org.evosuite.testcase.TestCase} object.
 */
// @SuppressWarnings({ "rawtypes", "unchecked" })
public static TestCase negateCondition(List<BranchCondition> pathCondition, BranchCondition targetCondition, TestCase test) {
    List<Constraint<?>> constraints = new LinkedList<Constraint<?>>();
    for (BranchCondition b : pathCondition) {
        constraints.addAll(b.getSupportingConstraints());
        if (b == targetCondition) {
            break;
        } else {
            constraints.add(b.getConstraint());
        }
    }
    final Constraint<?> targetConstraint = targetCondition.getConstraint().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();
    if (size > 0) {
        constraints = reduce(constraints);
    // logger.info("Reduced constraints from " + size + " to " +
    // constraints.size());
    // logger.info("Now solving: " + constraints);
    }
    Solver solver = SolverFactory.getInstance().buildNewSolver();
    SolverCache solverCache = SolverCache.getInstance();
    SolverResult solverResult = solverCache.solve(solver, constraints);
    if (solverResult != null) {
        // logger.info(values.toString());
        TestCase newTest = test.clone();
        Map<String, Object> model = solverResult.getModel();
        for (Object key : model.keySet()) {
            Object val = model.get(key);
            if (val != null) {
                if (val instanceof Long) {
                    Long value = (Long) val;
                    String name = ((String) key).replace("__SYM", "");
                    logger.debug("New value for " + name + " is " + value);
                    PrimitiveStatement<?> p = getStatement(newTest, name);
                    assert (p != null);
                    if (p instanceof BooleanPrimitiveStatement) {
                        BooleanPrimitiveStatement bp = (BooleanPrimitiveStatement) p;
                        bp.setValue(value.intValue() > 0);
                    } else if (p instanceof CharPrimitiveStatement) {
                        CharPrimitiveStatement cp = (CharPrimitiveStatement) p;
                        cp.setValue((char) value.intValue());
                    } else if (p instanceof BytePrimitiveStatement) {
                        BytePrimitiveStatement bp = (BytePrimitiveStatement) p;
                        bp.setValue((byte) value.intValue());
                    } else if (p instanceof ShortPrimitiveStatement) {
                        ShortPrimitiveStatement sp = (ShortPrimitiveStatement) p;
                        sp.setValue((short) value.intValue());
                    } else if (p instanceof LongPrimitiveStatement) {
                        LongPrimitiveStatement lp = (LongPrimitiveStatement) p;
                        lp.setValue(value);
                    } else {
                        assert (p instanceof IntPrimitiveStatement);
                        IntPrimitiveStatement ip = (IntPrimitiveStatement) p;
                        ip.setValue(value.intValue());
                    }
                } else {
                    logger.debug("New value is not long " + val);
                }
            } else {
                logger.debug("New value is null");
            }
        }
        return newTest;
    } else {
        logger.debug("Got null :-(");
        return null;
    }
}
Also used : BytePrimitiveStatement(org.evosuite.testcase.statements.numeric.BytePrimitiveStatement) 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) CharPrimitiveStatement(org.evosuite.testcase.statements.numeric.CharPrimitiveStatement) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) TestCase(org.evosuite.testcase.TestCase) ShortPrimitiveStatement(org.evosuite.testcase.statements.numeric.ShortPrimitiveStatement) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) LongPrimitiveStatement(org.evosuite.testcase.statements.numeric.LongPrimitiveStatement)

Example 2 with SolverResult

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

the class TestCVC4ResultParser method parseSingleLineStringSolution.

@Test
public void parseSingleLineStringSolution() throws SolverParseException, SolverErrorException, SolverTimeoutException {
    StringBuffer buff = new StringBuffer();
    buff.append("sat\n");
    buff.append("(model\n");
    buff.append("(define-fun var8 () String \"Hello World\")\n");
    buff.append(")\n");
    String result_str = buff.toString();
    CVC4ResultParser parser = new CVC4ResultParser();
    SolverResult solution = parser.parse(result_str);
    assertTrue(solution.isSAT());
    assertEquals("Hello World", solution.getValue("var8"));
}
Also used : SolverResult(org.evosuite.symbolic.solver.SolverResult) Test(org.junit.Test)

Example 3 with SolverResult

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

the class TestCVC4ResultParser method parseMultiLineSolution.

@Test
public void parseMultiLineSolution() throws SolverParseException, SolverErrorException, SolverTimeoutException {
    StringBuffer buff = new StringBuffer();
    buff.append("sat\n");
    buff.append("(model\n");
    buff.append("(define-fun var8 () String \"Hello\nBeautiful\nWorld\")\n");
    buff.append(")\n");
    String result_str = buff.toString();
    CVC4ResultParser parser = new CVC4ResultParser();
    SolverResult solution = parser.parse(result_str);
    assertTrue(solution.isSAT());
    assertEquals("Hello\nBeautiful\nWorld", solution.getValue("var8"));
}
Also used : SolverResult(org.evosuite.symbolic.solver.SolverResult) Test(org.junit.Test)

Example 4 with SolverResult

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

the class TestCVC4ResultParser method parseEmptyStringSolution.

@Test
public void parseEmptyStringSolution() throws SolverParseException, SolverErrorException, SolverTimeoutException {
    StringBuffer buff = new StringBuffer();
    buff.append("sat\n");
    buff.append("(model\n");
    buff.append("(define-fun var8 () String \"\")\n");
    buff.append(")\n");
    String result_str = buff.toString();
    CVC4ResultParser parser = new CVC4ResultParser();
    SolverResult solution = parser.parse(result_str);
    assertTrue(solution.isSAT());
    assertEquals("", solution.getValue("var8"));
}
Also used : SolverResult(org.evosuite.symbolic.solver.SolverResult) Test(org.junit.Test)

Example 5 with SolverResult

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

the class TestCVC4ResultParser method parseBlankStringSolution.

@Test
public void parseBlankStringSolution() throws SolverParseException, SolverErrorException, SolverTimeoutException {
    StringBuffer buff = new StringBuffer();
    buff.append("sat\n");
    buff.append("(model\n");
    buff.append("(define-fun var8 () String \" \")\n");
    buff.append(")\n");
    String result_str = buff.toString();
    CVC4ResultParser parser = new CVC4ResultParser();
    SolverResult solution = parser.parse(result_str);
    assertTrue(solution.isSAT());
    assertEquals(" ", solution.getValue("var8"));
}
Also used : SolverResult(org.evosuite.symbolic.solver.SolverResult) 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