use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class SymbolicHeap method getField.
/**
* @param className
* @param fieldName
* @param conc_receiver
* @param symb_receiver
* @param conc_value
* @return
*/
public StringValue getField(String className, String fieldName, Object conc_receiver, ReferenceExpression symb_receiver, String conc_value) {
Map<ReferenceExpression, Expression<?>> symb_field = getOrCreateSymbolicField(className, fieldName);
StringValue symb_value = (StringValue) symb_field.get(symb_receiver);
if (symb_value == null || !((String) symb_value.getConcreteValue()).equals(conc_value)) {
symb_value = ExpressionFactory.buildNewStringConstant(conc_value);
symb_field.remove(symb_receiver);
}
return symb_value;
}
use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class ExprToZ3Str2Visitor method visit.
@Override
public SmtExpr visit(StringMultipleComparison e, Void arg) {
Expression<String> leftOperand = e.getLeftOperand();
Expression<?> rightOperand = e.getRightOperand();
Operator op = e.getOperator();
ArrayList<Expression<?>> othersOperands = e.getOther();
SmtExpr left = leftOperand.accept(this, null);
SmtExpr right = rightOperand.accept(this, null);
List<SmtExpr> others = new LinkedList<SmtExpr>();
for (Expression<?> otherOperand : othersOperands) {
SmtExpr other = otherOperand.accept(this, null);
others.add(other);
}
if (left == null || right == null) {
return null;
}
for (SmtExpr expr : others) {
if (expr == null) {
return null;
}
}
if (!left.isSymbolic() && !right.isSymbolic()) {
boolean isSymbolic = false;
for (SmtExpr smtExpr : others) {
if (smtExpr.isSymbolic()) {
isSymbolic = true;
break;
}
}
if (!isSymbolic) {
long longValue = e.getConcreteValue();
return SmtExprBuilder.mkIntConstant(longValue);
}
}
switch(op) {
case STARTSWITH:
{
// discard index (over-approximate solution)
SmtExpr startsWithExpr = SmtExprBuilder.mkStartsWith(left, right);
SmtExpr ifThenElseExpr = SmtExprBuilder.mkITE(startsWithExpr, SmtExprBuilder.ONE_INT, SmtExprBuilder.ZERO_INT);
return ifThenElseExpr;
}
case EQUALS:
case EQUALSIGNORECASE:
case ENDSWITH:
case CONTAINS:
{
throw new IllegalArgumentException("Illegal StringMultipleComparison operator " + op);
}
case REGIONMATCHES:
case PATTERNMATCHES:
case APACHE_ORO_PATTERN_MATCHES:
{
long longValue = e.getConcreteValue();
SmtExpr intConst = SmtExprBuilder.mkIntConstant(longValue);
return intConst;
}
default:
throw new UnsupportedOperationException("Not implemented yet! " + op);
}
}
use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class ExprToCVC4Visitor method visit.
@Override
public SmtExpr visit(StringMultipleComparison e, Void v) {
Expression<String> leftOperand = e.getLeftOperand();
Expression<?> rightOperand = e.getRightOperand();
Operator op = e.getOperator();
ArrayList<Expression<?>> othersOperands = e.getOther();
SmtExpr left = leftOperand.accept(this, null);
SmtExpr right = rightOperand.accept(this, null);
List<SmtExpr> others = new LinkedList<SmtExpr>();
for (Expression<?> otherOperand : othersOperands) {
SmtExpr other = otherOperand.accept(this, null);
others.add(other);
}
if (isNull(left, right, others)) {
return null;
}
if (!isSymbolic(left, right, others)) {
long longValue = e.getConcreteValue();
SmtExpr intConst = SmtExprBuilder.mkIntConstant(longValue);
return intConst;
}
switch(op) {
case STARTSWITH:
{
SmtExpr indexExpr = others.get(0);
if (indexExpr.equals(SmtExprBuilder.ZERO_INT)) {
SmtIntConstant oneExpr = SmtExprBuilder.ONE_INT;
SmtIntConstant zeroExpr = SmtExprBuilder.ZERO_INT;
SmtExpr startsWithFormula = SmtExprBuilder.mkStrPrefixOf(right, left);
SmtExpr ifThenElseFormula = SmtExprBuilder.mkITE(startsWithFormula, oneExpr, zeroExpr);
return ifThenElseFormula;
} else {
long longValue = e.getConcreteValue();
SmtExpr intConst = SmtExprBuilder.mkIntConstant(longValue);
return intConst;
}
}
case EQUALS:
case EQUALSIGNORECASE:
case ENDSWITH:
case CONTAINS:
{
throw new IllegalArgumentException("Illegal StringMultipleComparison operator " + op);
}
case REGIONMATCHES:
case PATTERNMATCHES:
case APACHE_ORO_PATTERN_MATCHES:
{
long longValue = e.getConcreteValue();
SmtExpr intConst = SmtExprBuilder.mkIntConstant(longValue);
return intConst;
}
default:
throw new UnsupportedOperationException("Not implemented yet! " + op);
}
}
use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class ExprToCVC4Visitor method visit.
@Override
public SmtExpr visit(StringMultipleExpression e, Void v) {
Expression<String> leftOperand = e.getLeftOperand();
Expression<?> rightOperand = e.getRightOperand();
ArrayList<Expression<?>> othersOperands = e.getOther();
SmtExpr left = leftOperand.accept(this, null);
SmtExpr right = rightOperand.accept(this, null);
List<SmtExpr> others = new LinkedList<SmtExpr>();
for (Expression<?> otherExpr : othersOperands) {
SmtExpr other = otherExpr.accept(this, null);
others.add(other);
}
if (isNull(left, right, others)) {
return null;
}
if (!isSymbolic(left, right, others)) {
String stringValue = e.getConcreteValue();
SmtExpr strConstant = SmtExprBuilder.mkStringConstant(stringValue);
return strConstant;
}
Operator op = e.getOperator();
switch(op) {
case SUBSTRING:
{
SmtExpr startIndex = right;
SmtExpr endIndex = others.get(0);
SmtExpr offset = SmtExprBuilder.mkSub(endIndex, startIndex);
SmtExpr substring = SmtExprBuilder.mkStrSubstring(left, startIndex, offset);
return substring;
}
case REPLACEC:
{
long concreteTarget = (Long) rightOperand.getConcreteValue();
long concreteReplacement = (Long) othersOperands.get(0).getConcreteValue();
String targetString = String.valueOf((char) concreteTarget);
String replacementString = String.valueOf((char) concreteReplacement);
SmtExpr target = SmtExprBuilder.mkStringConstant(targetString);
SmtExpr replacement = SmtExprBuilder.mkStringConstant(replacementString);
SmtExpr replace = SmtExprBuilder.mkStrReplace(left, target, replacement);
return replace;
}
case REPLACECS:
{
SmtExpr target = right;
SmtExpr replacement = others.get(0);
SmtExpr replace = SmtExprBuilder.mkStrReplace(left, target, replacement);
return replace;
}
case REPLACEALL:
case REPLACEFIRST:
{
String stringValue = e.getConcreteValue();
SmtExpr strConstant = SmtExprBuilder.mkStringConstant(stringValue);
return strConstant;
}
default:
throw new UnsupportedOperationException("Not implemented yet! " + op);
}
}
use of org.evosuite.symbolic.expr.Expression in project evosuite by EvoSuite.
the class ReplaceAll method executeFunction.
@Override
public Object executeFunction() {
// receiver
ReferenceConstant symb_receiver = this.getSymbReceiver();
String conc_receiver = (String) this.getConcReceiver();
// regex argument
ReferenceExpression symb_regex = this.getSymbArgument(0);
String conc_regex = (String) this.getConcArgument(0);
// replacement argument
ReferenceExpression symb_replacement = this.getSymbArgument(1);
String conc_replacement = (String) this.getConcArgument(1);
// return value
String conc_ret_val = (String) this.getConcRetVal();
ReferenceExpression symb_ret_val = this.getSymbRetVal();
StringValue stringReceiverExpr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_receiver, symb_receiver, conc_receiver);
if (symb_regex instanceof ReferenceConstant && symb_replacement instanceof ReferenceConstant) {
ReferenceConstant non_null_symb_regex = (ReferenceConstant) symb_regex;
StringValue regexExpr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_regex, non_null_symb_regex, conc_regex);
ReferenceConstant non_null_symb_replacement = (ReferenceConstant) symb_replacement;
StringValue replacementExpr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_replacement, non_null_symb_replacement, conc_replacement);
if (symb_ret_val instanceof ReferenceConstant) {
ReferenceConstant non_null_symb_ret_val = (ReferenceConstant) symb_ret_val;
StringMultipleExpression symb_value = new StringMultipleExpression(stringReceiverExpr, Operator.REPLACEALL, regexExpr, new ArrayList<Expression<?>>(Collections.singletonList(replacementExpr)), conc_ret_val);
env.heap.putField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_ret_val, non_null_symb_ret_val, symb_value);
}
}
return symb_ret_val;
}
Aggregations