Search in sources :

Example 11 with Expression

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

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

use of org.evosuite.symbolic.expr.Expression 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);
    }
}
Also used : StringComparison(org.evosuite.symbolic.expr.bv.StringComparison) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) Expression(org.evosuite.symbolic.expr.Expression) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 14 with Expression

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

the class TestStringSearch method testStartsWithFalseConstant.

@Test
public void testStartsWithFalseConstant() {
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    String var1 = "footest";
    String const2 = "test";
    StringVariable strVar = new StringVariable("test1", var1);
    StringConstant strConst = new StringConstant(const2);
    IntegerConstant offs_expr = new IntegerConstant(3);
    ArrayList<Expression<?>> other = new ArrayList<Expression<?>>();
    other.add(offs_expr);
    StringMultipleComparison strComp = new StringMultipleComparison(strVar, Operator.STARTSWITH, strConst, other, 0L);
    constraints.add(new StringConstraint(strComp, Comparator.EQ, new IntegerConstant(0)));
    EvoSuiteSolver skr = new EvoSuiteSolver();
    Map<String, Object> result;
    try {
        result = solve(skr, constraints);
        assertNotNull(result);
        assertNotNull(result.get("test1"));
        assertFalse((result.get("test1").toString()).startsWith(const2, 3));
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) StringMultipleComparison(org.evosuite.symbolic.expr.bv.StringMultipleComparison) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) ArrayList(java.util.ArrayList) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant) Expression(org.evosuite.symbolic.expr.Expression) StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) StringUnaryExpression(org.evosuite.symbolic.expr.str.StringUnaryExpression) StringConstant(org.evosuite.symbolic.expr.str.StringConstant) Test(org.junit.Test)

Example 15 with Expression

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

the class SymbolicHeap method array_load.

public IntegerValue array_load(ReferenceExpression symb_array, int conc_index, long conc_value) {
    Map<Integer, Expression<?>> symb_array_contents = getOrCreateSymbolicArray(symb_array);
    IntegerValue symb_value = (IntegerValue) symb_array_contents.get(conc_index);
    if (symb_value == null || ((Long) symb_value.getConcreteValue()).longValue() != conc_value) {
        symb_value = ExpressionFactory.buildNewIntegerConstant(conc_value);
        symb_array_contents.remove(conc_index);
    }
    return symb_value;
}
Also used : Expression(org.evosuite.symbolic.expr.Expression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue)

Aggregations

Expression (org.evosuite.symbolic.expr.Expression)24 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)13 StringValue (org.evosuite.symbolic.expr.str.StringValue)10 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)9 IntegerValue (org.evosuite.symbolic.expr.bv.IntegerValue)8 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)8 StringUnaryExpression (org.evosuite.symbolic.expr.str.StringUnaryExpression)8 ArrayList (java.util.ArrayList)6 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)6 StringMultipleComparison (org.evosuite.symbolic.expr.bv.StringMultipleComparison)6 RealValue (org.evosuite.symbolic.expr.fp.RealValue)6 StringMultipleExpression (org.evosuite.symbolic.expr.str.StringMultipleExpression)6 StringConstraint (org.evosuite.symbolic.expr.StringConstraint)5 PrimitiveExpression (org.evosuite.testcase.statements.PrimitiveExpression)5 LinkedList (java.util.LinkedList)4 Constraint (org.evosuite.symbolic.expr.Constraint)4 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)4 Operator (org.evosuite.symbolic.expr.Operator)4 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)4 IntegerUnaryExpression (org.evosuite.symbolic.expr.bv.IntegerUnaryExpression)4