Search in sources :

Example 1 with StringReaderExpr

use of org.evosuite.symbolic.expr.reader.StringReaderExpr in project evosuite by EvoSuite.

the class Reader_Read method executeFunction.

@Override
public Object executeFunction() {
    Reader conc_reader = (Reader) this.getConcReceiver();
    if (conc_reader instanceof StringReader) {
        ReferenceConstant symb_str_reader = this.getSymbReceiver();
        StringReader conc_str_reader = (StringReader) conc_reader;
        StringReaderExpr stringReaderExpr = (StringReaderExpr) env.heap.getField(Types.JAVA_IO_STRING_READER, SymbolicHeap.$STRING_READER_VALUE, conc_str_reader, symb_str_reader);
        if (stringReaderExpr != null && stringReaderExpr.containsSymbolicVariable()) {
            StringValue symb_string = stringReaderExpr.getString();
            String conc_string = symb_string.getConcreteValue();
            int currPosition = stringReaderExpr.getReaderPosition();
            if (currPosition < conc_string.length()) {
                // update symbolic string reader
                currPosition++;
                int conc_string_reader_value;
                if (currPosition >= conc_string.length()) {
                    conc_string_reader_value = -1;
                } else {
                    conc_string_reader_value = conc_string.charAt(currPosition);
                }
                StringReaderExpr newStringReaderExpr = new StringReaderExpr((long) conc_string_reader_value, symb_string, currPosition);
                env.heap.putField(Types.JAVA_IO_STRING_READER, SymbolicHeap.$STRING_READER_VALUE, conc_str_reader, symb_str_reader, newStringReaderExpr);
            }
            // returns STRING_READER(string,currPosition)
            return stringReaderExpr;
        }
    }
    return this.getSymbIntegerRetVal();
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) StringReaderExpr(org.evosuite.symbolic.expr.reader.StringReaderExpr) StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 2 with StringReaderExpr

use of org.evosuite.symbolic.expr.reader.StringReaderExpr in project evosuite by EvoSuite.

the class StringReader_Read method executeFunction.

@Override
public Object executeFunction() {
    ReferenceConstant symb_str_reader = this.getSymbReceiver();
    StringReader conc_str_reader = (StringReader) this.getConcReceiver();
    StringReaderExpr stringReaderExpr = (StringReaderExpr) env.heap.getField(Types.JAVA_IO_STRING_READER, SymbolicHeap.$STRING_READER_VALUE, conc_str_reader, symb_str_reader);
    if (stringReaderExpr != null && stringReaderExpr.containsSymbolicVariable()) {
        StringValue symb_string = stringReaderExpr.getString();
        String conc_string = symb_string.getConcreteValue();
        int currPosition = stringReaderExpr.getReaderPosition();
        if (currPosition < conc_string.length()) {
            // update symbolic string reader
            currPosition++;
            int conc_string_reader_value;
            if (currPosition >= conc_string.length()) {
                conc_string_reader_value = -1;
            } else {
                conc_string_reader_value = conc_string.charAt(currPosition);
            }
            StringReaderExpr newStringReaderExpr = new StringReaderExpr((long) conc_string_reader_value, symb_string, currPosition);
            env.heap.putField(Types.JAVA_IO_STRING_READER, SymbolicHeap.$STRING_READER_VALUE, conc_str_reader, symb_str_reader, newStringReaderExpr);
        }
        // returns STRING_READER(string,currPosition)
        return stringReaderExpr;
    }
    return this.getSymbIntegerRetVal();
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) StringReaderExpr(org.evosuite.symbolic.expr.reader.StringReaderExpr) StringReader(java.io.StringReader) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 3 with StringReaderExpr

use of org.evosuite.symbolic.expr.reader.StringReaderExpr in project evosuite by EvoSuite.

the class StringReader_Init method executeFunction.

@Override
public Object executeFunction() {
    // symbolic receiver (new object)
    ReferenceConstant symb_str_reader = (ReferenceConstant) this.getSymbReceiver();
    // string argument
    String conc_str = (String) this.getConcArgument(0);
    ReferenceExpression symb_str = this.getSymbArgument(0);
    if (symb_str instanceof ReferenceConstant) {
        ReferenceConstant non_null_symb_string = (ReferenceConstant) symb_str;
        assert conc_str != null;
        StringValue strExpr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_str, non_null_symb_string, conc_str);
        if (strExpr.containsSymbolicVariable()) {
            int conc_string_reader_value;
            if (conc_str.length() == 0) {
                conc_string_reader_value = -1;
            } else {
                conc_string_reader_value = conc_str.charAt(0);
            }
            StringReaderExpr newStringReaderExpr = new StringReaderExpr((long) conc_string_reader_value, strExpr);
            // update symbolic heap
            env.heap.putField(Types.JAVA_IO_STRING_READER, SymbolicHeap.$STRING_READER_VALUE, null, symb_str_reader, newStringReaderExpr);
        }
    }
    // constructor returns void
    return null;
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) StringReaderExpr(org.evosuite.symbolic.expr.reader.StringReaderExpr) ReferenceExpression(org.evosuite.symbolic.expr.ref.ReferenceExpression) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 4 with StringReaderExpr

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

Aggregations

StringReaderExpr (org.evosuite.symbolic.expr.reader.StringReaderExpr)4 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)3 StringValue (org.evosuite.symbolic.expr.str.StringValue)3 StringReader (java.io.StringReader)2 Reader (java.io.Reader)1 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)1 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)1