use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.
the class ConstraintNormalizer method createStringConstraint.
private static Constraint<?> createStringConstraint(IntegerConstraint c) {
if (c.getLeftOperand() instanceof StringComparison) {
StringComparison string_comparison = (StringComparison) c.getLeftOperand();
@SuppressWarnings("unchecked") Expression<Long> number_expr = (Expression<Long>) c.getRightOperand();
IntegerConstant constant = new IntegerConstant(number_expr.getConcreteValue());
return new StringConstraint(string_comparison, c.getComparator(), constant);
} else {
assert c.getRightOperand() instanceof StringComparison;
StringComparison string_comparison = (StringComparison) c.getRightOperand();
@SuppressWarnings("unchecked") Expression<Long> number_expr = (Expression<Long>) c.getLeftOperand();
IntegerConstant constant = new IntegerConstant(number_expr.getConcreteValue());
return new StringConstraint(string_comparison, c.getComparator(), constant);
}
}
use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.
the class ExpressionFactory method buildAddNormalized.
private static IntegerValue buildAddNormalized(IntegerValue left, IntegerValue right, long con) {
// can only optimize if left is a literal
if (!(left instanceof IntegerConstant))
return new IntegerBinaryExpression(left, Operator.PLUS, right, con);
/*
* (add 0 x) --> x
*/
if (((IntegerConstant) left).getConcreteValue() == 0) {
return right;
}
/*
* (add a b) --> result of a+b
*/
if (right instanceof IntegerConstant) {
long a = left.getConcreteValue();
long b = right.getConcreteValue();
return buildNewIntegerConstant(a + b);
}
/*
* (add a (add b x)) --> (add (a+b) x)
*/
if (right instanceof IntegerBinaryExpression && ((IntegerBinaryExpression) right).getOperator() == Operator.PLUS) {
IntegerBinaryExpression add = (IntegerBinaryExpression) right;
if (add.getLeftOperand() instanceof IntegerConstant) {
long a = left.getConcreteValue();
long b = add.getLeftOperand().getConcreteValue();
IntegerConstant a_plus_b = buildNewIntegerConstant(a + b);
return new IntegerBinaryExpression(a_plus_b, Operator.PLUS, add.getRightOperand(), con);
}
}
return new IntegerBinaryExpression(left, Operator.PLUS, right, con);
}
use of org.evosuite.symbolic.expr.bv.IntegerConstant in project evosuite by EvoSuite.
the class HeapVM method INSTANCEOF.
/**
* Dynamic type check:
*
* <pre>
* (variable instanceof TypeName)
* </pre>
*
* null is not treated as (is not an instance of) any reference type. This
* requires non-standard treatment of null. Note the different treatment in
* {@link #CHECKCAST}.
*
* <p>
* If the jvm has not loaded the class/interface named TypeName before, then
* we load it. TODO: Is this a problem?
*
* http://java.sun.com/docs/books/jvms/second_edition/html/Instructions2.
* doc6.html#instanceof
*/
@Override
public void INSTANCEOF(Object conc_ref, String typeName) {
/* pop symbolic arguments */
ReferenceExpression symb_ref = env.topFrame().operandStack.popRef();
/* check reference initialization */
env.heap.initializeReference(conc_ref, symb_ref);
Type type = Type.getType(typeName);
Class<?> myClazz = classLoader.getClassForType(type);
boolean instanceOf = myClazz.isInstance(conc_ref);
IntegerConstant ret;
if (instanceOf) {
ret = ExpressionFactory.ICONST_1;
} else {
ret = ExpressionFactory.ICONST_0;
}
/* push symbolic arguments */
env.topFrame().operandStack.pushBv32(ret);
}
use of org.evosuite.symbolic.expr.bv.IntegerConstant 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.bv.IntegerConstant in project evosuite by EvoSuite.
the class DistanceCalculator method getDistanceStringReaderLength.
private static long getDistanceStringReaderLength(IntegerConstraint n, long leftVal, long rightVal) {
ExpressionExecutor exprExecutor = new ExpressionExecutor();
Expression<?> left = n.getLeftOperand();
Expression<?> right = n.getRightOperand();
if (left instanceof StringReaderExpr && right instanceof IntegerConstant) {
StringReaderExpr stringReaderExpr = (StringReaderExpr) left;
IntegerConstant intValue = (IntegerConstant) right;
String conc_string = (String) stringReaderExpr.getString().accept(exprExecutor, null);
int new_length = stringReaderExpr.getReaderPosition();
int conc_string_length = conc_string.length();
if ((intValue.getConcreteValue() == 0L) && n.getComparator().equals(Comparator.LT)) {
if (conc_string_length <= new_length)
return 0L;
else {
// return distance to length(string)<=new_length
return conc_string_length - new_length;
}
}
if ((intValue.getConcreteValue() == 0L) && n.getComparator().equals(Comparator.GE)) {
if (conc_string_length > new_length)
return 0L;
else {
// return distance to length(string)>new_length
return new_length - conc_string_length + 1;
}
}
if ((intValue.getConcreteValue() == -1L) && (n.getComparator().equals(Comparator.EQ) || n.getComparator().equals(Comparator.NE))) {
if (n.getComparator().equals(Comparator.EQ)) {
if (conc_string_length <= new_length)
return 0L;
else {
// return distance to length(string)<=new_length
return conc_string_length - new_length;
}
} else if (n.getComparator().equals(Comparator.NE)) {
if (conc_string_length > new_length)
return 0L;
else {
// return distance to length(string)>new_length
return new_length - conc_string_length + 1;
}
}
}
}
// TODO Auto-generated method stub
return -1L;
}
Aggregations