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);
}
}
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);
}
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();
}
}
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();
}
}
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();
}
}
Aggregations