Search in sources :

Example 1 with IntegerConstraint

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

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

the class JumpVM method TABLESWITCH.

/* switch */
/**
 * <b>switch</b> statement that has consecutively numbered cases. I.e.,
 * there are no holes (missing targets) between the lowest and highest
 * target. Hence the compiler does not need to translate the case values to
 * offsets.
 *
 * <p>
 * We treat the switch statement as a nested if in order lowest to highest
 * index as follows.
 *
 * <pre>
 * if (x==lowest) ..
 * else
 * {
 *   if (x==second_lowest) ..
 *   else
 *   {
 *     if ..
 * </pre>
 *
 * http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.
 * doc14.html#tableswitch
 */
@Override
public void TABLESWITCH(String className, String methName, int branchIndex, int goalConcrete, int min, int max) {
    final IntegerValue value = env.topFrame().operandStack.popBv32();
    Vector<IntegerConstraint> constraints = new Vector<IntegerConstraint>();
    // process each time in the same order: lowest to highest target
    for (int i = min; i <= max; i++) {
        IntegerConstant literal = ExpressionFactory.buildNewIntegerConstant(i);
        IntegerConstraint constraint;
        if (goalConcrete == i) {
            constraint = ConstraintFactory.eq(value, literal);
            constraints.add(constraint);
            break;
        } else {
            constraint = ConstraintFactory.neq(value, literal);
            constraints.add(constraint);
        }
    }
    for (int i = 0; i < constraints.size() - 1; i++) {
        IntegerConstraint cnstrt = constraints.get(i);
        if (cnstrt.getLeftOperand().containsSymbolicVariable() || cnstrt.getRightOperand().containsSymbolicVariable())
            pc.addSupportingConstraint(cnstrt);
    }
    // add branch condition iif local constraint is concrete
    IntegerConstraint cnstr = constraints.get(constraints.size() - 1);
    if (cnstr.getLeftOperand().containsSymbolicVariable() || cnstr.getRightOperand().containsSymbolicVariable())
        pc.addBranchCondition(className, methName, branchIndex, cnstr);
}
Also used : IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue) Vector(java.util.Vector) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 3 with IntegerConstraint

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

Example 4 with IntegerConstraint

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

the class TestIntegerSearch method testGTVariable.

@Test
public void testGTVariable() 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.GT, 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();
        assertTrue(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)

Example 5 with IntegerConstraint

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

the class TestIntegerSearch method testLTArithmetic.

@Test
public void testLTArithmetic() throws SolverEmptyQueryException {
    int var1 = 2;
    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.LT, 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

IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)43 Constraint (org.evosuite.symbolic.expr.Constraint)37 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)30 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)30 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)28 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)25 SolverResult (org.evosuite.symbolic.solver.SolverResult)25 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)24 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)9 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)8 StringConstraint (org.evosuite.symbolic.expr.StringConstraint)7 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)7 IntegerValue (org.evosuite.symbolic.expr.bv.IntegerValue)6 StringUnaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression)4 RealConstraint (org.evosuite.symbolic.expr.RealConstraint)3 LinkedList (java.util.LinkedList)2 Vector (java.util.Vector)2 Comparator (org.evosuite.symbolic.expr.Comparator)2 TestCaseStringAppendString (com.examples.with.different.packagename.solver.TestCaseStringAppendString)1