Search in sources :

Example 1 with StringMultipleExpression

use of org.evosuite.symbolic.expr.str.StringMultipleExpression in project evosuite by EvoSuite.

the class StringBuffer_SetLength method executeFunction.

@Override
public Object executeFunction() {
    ReferenceConstant symb_str_buffer = this.getSymbReceiver();
    StringBuffer conc_str_buffer = (StringBuffer) this.getConcReceiver();
    IntegerValue newSymbLength = this.getSymbIntegerArgument(0);
    int newConcLength = this.getConcIntArgument(0);
    // retrieve symbolic value from heap
    String conc_value = conc_str_buffer.toString();
    StringValue symb_value = env.heap.getField(Types.JAVA_LANG_STRING_BUFFER, SymbolicHeap.$STRING_BUFFER_CONTENTS, conc_str_buffer, symb_str_buffer, pre_conc_value);
    if (symb_value.containsSymbolicVariable() || newSymbLength.containsSymbolicVariable()) {
        StringValue new_symb_value = null;
        if (!newSymbLength.containsSymbolicVariable() && newConcLength == 0) {
            // StringBuffer contents equals to "" string
            new_symb_value = new StringConstant("");
        } else {
            // StringBuffer contents equ
            new_symb_value = new StringMultipleExpression(symb_value, Operator.SUBSTRING, new IntegerConstant(0), new ArrayList<Expression<?>>(Collections.singletonList(newSymbLength)), conc_value);
        }
        env.heap.putField(Types.JAVA_LANG_STRING_BUFFER, SymbolicHeap.$STRING_BUFFER_CONTENTS, conc_str_buffer, symb_str_buffer, new_symb_value);
    }
    // return void
    return null;
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) StringMultipleExpression(org.evosuite.symbolic.expr.str.StringMultipleExpression) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue) ArrayList(java.util.ArrayList) StringValue(org.evosuite.symbolic.expr.str.StringValue) StringConstant(org.evosuite.symbolic.expr.str.StringConstant) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 2 with StringMultipleExpression

use of org.evosuite.symbolic.expr.str.StringMultipleExpression in project evosuite by EvoSuite.

the class ReplaceFirst 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.REPLACEFIRST, 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;
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) StringMultipleExpression(org.evosuite.symbolic.expr.str.StringMultipleExpression) Expression(org.evosuite.symbolic.expr.Expression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) StringMultipleExpression(org.evosuite.symbolic.expr.str.StringMultipleExpression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 3 with StringMultipleExpression

use of org.evosuite.symbolic.expr.str.StringMultipleExpression in project evosuite by EvoSuite.

the class ExprToZ3Str2Visitor method visit.

@Override
public SmtExpr visit(StringMultipleExpression 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;
            }
        }
        if (!isSymbolic) {
            String stringValue = e.getConcreteValue();
            return mkStringConstant(stringValue);
        }
    }
    switch(op) {
        case REPLACECS:
            {
                SmtExpr string = left;
                SmtExpr target = right;
                SmtExpr replacement = others.get(0);
                SmtExpr substringExpr = SmtExprBuilder.mkReplace(string, target, replacement);
                return substringExpr;
            }
        case SUBSTRING:
            {
                SmtExpr string = left;
                SmtExpr fromExpr = right;
                SmtExpr toExpr = others.get(0);
                SmtExpr substringExpr = SmtExprBuilder.mkSubstring(string, fromExpr, toExpr);
                return substringExpr;
            }
        case REPLACEC:
        case REPLACEALL:
        case REPLACEFIRST:
            {
                String stringValue = e.getConcreteValue();
                return mkStringConstant(stringValue);
            }
        default:
            throw new UnsupportedOperationException("Not implemented yet! " + op);
    }
}
Also used : Operator(org.evosuite.symbolic.expr.Operator) Expression(org.evosuite.symbolic.expr.Expression) StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) IntegerBinaryExpression(org.evosuite.symbolic.expr.bv.IntegerBinaryExpression) StringBinaryExpression(org.evosuite.symbolic.expr.str.StringBinaryExpression) GetFieldExpression(org.evosuite.symbolic.expr.ref.GetFieldExpression) StringMultipleToIntegerExpression(org.evosuite.symbolic.expr.bv.StringMultipleToIntegerExpression) IntegerUnaryExpression(org.evosuite.symbolic.expr.bv.IntegerUnaryExpression) RealBinaryExpression(org.evosuite.symbolic.expr.fp.RealBinaryExpression) StringMultipleExpression(org.evosuite.symbolic.expr.str.StringMultipleExpression) RealUnaryToIntegerExpression(org.evosuite.symbolic.expr.bv.RealUnaryToIntegerExpression) RealUnaryExpression(org.evosuite.symbolic.expr.fp.RealUnaryExpression) StringUnaryExpression(org.evosuite.symbolic.expr.str.StringUnaryExpression) StringUnaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression) SmtExpr(org.evosuite.symbolic.solver.smt.SmtExpr) LinkedList(java.util.LinkedList)

Example 4 with StringMultipleExpression

use of org.evosuite.symbolic.expr.str.StringMultipleExpression 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);
    }
}
Also used : Operator(org.evosuite.symbolic.expr.Operator) IntegerBinaryExpression(org.evosuite.symbolic.expr.bv.IntegerBinaryExpression) StringBinaryExpression(org.evosuite.symbolic.expr.str.StringBinaryExpression) GetFieldExpression(org.evosuite.symbolic.expr.ref.GetFieldExpression) StringMultipleToIntegerExpression(org.evosuite.symbolic.expr.bv.StringMultipleToIntegerExpression) IntegerUnaryExpression(org.evosuite.symbolic.expr.bv.IntegerUnaryExpression) StringMultipleExpression(org.evosuite.symbolic.expr.str.StringMultipleExpression) RealUnaryExpression(org.evosuite.symbolic.expr.fp.RealUnaryExpression) StringUnaryExpression(org.evosuite.symbolic.expr.str.StringUnaryExpression) Expression(org.evosuite.symbolic.expr.Expression) StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) RealBinaryExpression(org.evosuite.symbolic.expr.fp.RealBinaryExpression) RealUnaryToIntegerExpression(org.evosuite.symbolic.expr.bv.RealUnaryToIntegerExpression) StringUnaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression) SmtExpr(org.evosuite.symbolic.solver.smt.SmtExpr) LinkedList(java.util.LinkedList)

Example 5 with StringMultipleExpression

use of org.evosuite.symbolic.expr.str.StringMultipleExpression 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;
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) StringMultipleExpression(org.evosuite.symbolic.expr.str.StringMultipleExpression) Expression(org.evosuite.symbolic.expr.Expression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) StringMultipleExpression(org.evosuite.symbolic.expr.str.StringMultipleExpression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Aggregations

StringMultipleExpression (org.evosuite.symbolic.expr.str.StringMultipleExpression)5 Expression (org.evosuite.symbolic.expr.Expression)4 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)3 StringValue (org.evosuite.symbolic.expr.str.StringValue)3 LinkedList (java.util.LinkedList)2 Operator (org.evosuite.symbolic.expr.Operator)2 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)2 IntegerUnaryExpression (org.evosuite.symbolic.expr.bv.IntegerUnaryExpression)2 RealUnaryToIntegerExpression (org.evosuite.symbolic.expr.bv.RealUnaryToIntegerExpression)2 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)2 StringMultipleToIntegerExpression (org.evosuite.symbolic.expr.bv.StringMultipleToIntegerExpression)2 StringUnaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression)2 RealBinaryExpression (org.evosuite.symbolic.expr.fp.RealBinaryExpression)2 RealUnaryExpression (org.evosuite.symbolic.expr.fp.RealUnaryExpression)2 GetFieldExpression (org.evosuite.symbolic.expr.ref.GetFieldExpression)2 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)2 StringBinaryExpression (org.evosuite.symbolic.expr.str.StringBinaryExpression)2 StringUnaryExpression (org.evosuite.symbolic.expr.str.StringUnaryExpression)2 SmtExpr (org.evosuite.symbolic.solver.smt.SmtExpr)2 ArrayList (java.util.ArrayList)1