Search in sources :

Example 41 with StringValue

use of org.evosuite.symbolic.expr.str.StringValue 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)

Example 42 with StringValue

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

the class ToLowerCase method executeFunction.

@Override
public Object executeFunction() {
    // object receiver
    ReferenceConstant symb_str = this.getSymbReceiver();
    String conc_str = (String) this.getConcReceiver();
    // return value
    String conc_ret_val = (String) this.getConcRetVal();
    ReferenceConstant symb_ret_val = (ReferenceConstant) this.getSymbRetVal();
    StringValue string_expr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_str, symb_str, conc_str);
    StringUnaryExpression symb_value = new StringUnaryExpression(string_expr, Operator.TOLOWERCASE, conc_ret_val);
    env.heap.putField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_ret_val, symb_ret_val, symb_value);
    return this.getSymbRetVal();
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) StringUnaryExpression(org.evosuite.symbolic.expr.str.StringUnaryExpression) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 43 with StringValue

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

the class ToString method executeFunction.

@Override
public Object executeFunction() {
    // object receiver
    ReferenceConstant symb_str = this.getSymbReceiver();
    String conc_str = (String) this.getConcReceiver();
    // return value
    String conc_ret_val = (String) this.getConcRetVal();
    ReferenceConstant symb_ret_val = (ReferenceConstant) this.getSymbRetVal();
    StringValue string_expr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_str, symb_str, conc_str);
    env.heap.putField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_ret_val, symb_ret_val, string_expr);
    return this.getSymbRetVal();
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 44 with StringValue

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

the class Trim method executeFunction.

@Override
public Object executeFunction() {
    // object receiver
    ReferenceConstant symb_str = this.getSymbReceiver();
    String conc_str = (String) this.getConcReceiver();
    // return value
    String conc_ret_val = (String) this.getConcRetVal();
    ReferenceConstant symb_ret_val = (ReferenceConstant) this.getSymbRetVal();
    StringValue string_expr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_str, symb_str, conc_str);
    StringUnaryExpression symb_value = new StringUnaryExpression(string_expr, Operator.TRIM, conc_ret_val);
    env.heap.putField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_ret_val, symb_ret_val, symb_value);
    return this.getSymbRetVal();
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) StringUnaryExpression(org.evosuite.symbolic.expr.str.StringUnaryExpression) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 45 with StringValue

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

the class SymbolicObserver method readInstanceField.

private ReferenceExpressionPair readInstanceField(VariableReference source, Field field, Scope scope) {
    String owner = field.getDeclaringClass().getName().replace('.', '/');
    String name = field.getName();
    Class<?> fieldClazz = field.getType();
    String source_name = source.getName();
    ReferenceConstant symb_receiver = (ReferenceConstant) symb_references.get(source_name);
    try {
        Object conc_receiver = source.getObject(scope);
        if (fieldClazz.equals(int.class)) {
            int conc_value = field.getInt(conc_receiver);
            IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newIntegerRef = newIntegerReference(conc_value, expr);
            return new ReferenceExpressionPair(newIntegerRef, expr);
        } else if (fieldClazz.equals(char.class)) {
            char conc_value = field.getChar(conc_receiver);
            IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newCharacterRef = newCharacterReference(conc_value, expr);
            return new ReferenceExpressionPair(newCharacterRef, expr);
        } else if (fieldClazz.equals(long.class)) {
            long conc_value = field.getLong(conc_receiver);
            IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newLongRef = newLongReference(conc_value, expr);
            return new ReferenceExpressionPair(newLongRef, expr);
        } else if (fieldClazz.equals(short.class)) {
            short conc_value = field.getShort(conc_receiver);
            IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newShortRef = newShortReference(conc_value, expr);
            return new ReferenceExpressionPair(newShortRef, expr);
        } else if (fieldClazz.equals(byte.class)) {
            byte conc_value = field.getByte(conc_receiver);
            IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newByteRef = newByteReference(conc_value, expr);
            return new ReferenceExpressionPair(newByteRef, expr);
        } else if (fieldClazz.equals(boolean.class)) {
            boolean conc_value = field.getBoolean(conc_receiver);
            IntegerValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value ? 1 : 0);
            ReferenceConstant newBooleanRef = newBooleanReference(conc_value, expr);
            return new ReferenceExpressionPair(newBooleanRef, expr);
        } else if (fieldClazz.equals(float.class)) {
            float conc_value = field.getFloat(conc_receiver);
            RealValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newFloatRef = newFloatReference(conc_value, expr);
            return new ReferenceExpressionPair(newFloatRef, expr);
        } else if (fieldClazz.equals(double.class)) {
            double conc_value = field.getDouble(conc_receiver);
            RealValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, conc_value);
            ReferenceConstant newDoubleRef = newDoubleReference(conc_value, expr);
            return new ReferenceExpressionPair(newDoubleRef, expr);
        } else {
            Object conc_value = field.get(conc_receiver);
            if (conc_value instanceof String) {
                String string = (String) conc_value;
                StringValue expr = env.heap.getField(owner, name, conc_receiver, symb_receiver, string);
                ReferenceConstant newStringRef = newStringReference(string, expr);
                return new ReferenceExpressionPair(newStringRef, expr);
            } else {
                ReferenceExpression ref = env.heap.getReference(conc_value);
                if (conc_value != null && isWrapper(conc_value)) {
                    ReferenceConstant nonNullRef = (ReferenceConstant) ref;
                    Expression<?> expr = findOrCreate(conc_value, nonNullRef);
                    return new ReferenceExpressionPair(ref, expr);
                } else {
                    return new ReferenceExpressionPair(ref, null);
                }
            }
        }
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (CodeUnderTestException e) {
        throw new RuntimeException(e);
    }
}
Also used : RealValue(org.evosuite.symbolic.expr.fp.RealValue) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) PrimitiveExpression(org.evosuite.testcase.statements.PrimitiveExpression) Expression(org.evosuite.symbolic.expr.Expression) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Aggregations

StringValue (org.evosuite.symbolic.expr.str.StringValue)45 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)39 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)16 Expression (org.evosuite.symbolic.expr.Expression)10 IntegerValue (org.evosuite.symbolic.expr.bv.IntegerValue)8 StringBinaryComparison (org.evosuite.symbolic.expr.bv.StringBinaryComparison)8 RealValue (org.evosuite.symbolic.expr.fp.RealValue)4 StringConstant (org.evosuite.symbolic.expr.str.StringConstant)4 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)4 PrimitiveExpression (org.evosuite.testcase.statements.PrimitiveExpression)4 ArrayList (java.util.ArrayList)3 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)3 StringReaderExpr (org.evosuite.symbolic.expr.reader.StringReaderExpr)3 StringMultipleExpression (org.evosuite.symbolic.expr.str.StringMultipleExpression)3 StringUnaryExpression (org.evosuite.symbolic.expr.str.StringUnaryExpression)3 StringReader (java.io.StringReader)2 Matcher (java.util.regex.Matcher)2 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)2 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)2 StringMultipleComparison (org.evosuite.symbolic.expr.bv.StringMultipleComparison)2