Search in sources :

Example 1 with StringMultipleToIntegerExpression

use of org.evosuite.symbolic.expr.bv.StringMultipleToIntegerExpression in project evosuite by EvoSuite.

the class DistanceCalculator method getDistanceIndexOfCIEqualsK.

private static long getDistanceIndexOfCIEqualsK(IntegerConstraint n, long leftVal, long rightVal) {
    ExpressionExecutor exprExecutor = new ExpressionExecutor();
    if (n.getLeftOperand() instanceof StringMultipleToIntegerExpression && n.getComparator() == Comparator.EQ && n.getRightOperand() instanceof IntegerConstant) {
        IntegerConstant right_constant = (IntegerConstant) n.getRightOperand();
        StringMultipleToIntegerExpression left_string_expr = (StringMultipleToIntegerExpression) n.getLeftOperand();
        if (left_string_expr.getOperator() == Operator.INDEXOFCI) {
            Expression<?> theSymbolicString = left_string_expr.getLeftOperand();
            Expression<?> theSymbolicChar = left_string_expr.getRightOperand();
            Expression<?> theSymbolicIndex = right_constant;
            Expression<?> theOffset = left_string_expr.getOther().get(0);
            Long theConcreteOffset = (Long) theOffset.accept(exprExecutor, null);
            // check theString.lenght>0
            String theConcreteString = (String) theSymbolicString.accept(exprExecutor, null);
            Long theConcreteIndex = (Long) theSymbolicIndex.accept(exprExecutor, null);
            if (theConcreteIndex > theConcreteString.length() - theConcreteOffset - 1) {
                // there is no char at the index to modify
                return Long.MAX_VALUE;
            } else if (theConcreteIndex != -1) {
                int theIndex = theConcreteIndex.intValue();
                char theConcreteChar = (char) ((Long) theSymbolicChar.accept(exprExecutor, null)).longValue();
                char theCurrentChar = theConcreteString.charAt(theIndex);
                return Math.abs(theCurrentChar - theConcreteChar);
            }
        }
    }
    return -1;
}
Also used : StringMultipleToIntegerExpression(org.evosuite.symbolic.expr.bv.StringMultipleToIntegerExpression) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 2 with StringMultipleToIntegerExpression

use of org.evosuite.symbolic.expr.bv.StringMultipleToIntegerExpression in project evosuite by EvoSuite.

the class DistanceCalculator method getDistanceIndexOfCIFound.

private static long getDistanceIndexOfCIFound(IntegerConstraint n, long leftVal, long rightVal) {
    ExpressionExecutor exprExecutor = new ExpressionExecutor();
    if (n.getLeftOperand() instanceof StringMultipleToIntegerExpression && n.getComparator() == Comparator.NE && n.getRightOperand() instanceof IntegerConstant) {
        IntegerConstant right_constant = (IntegerConstant) n.getRightOperand();
        StringMultipleToIntegerExpression left_string_expr = (StringMultipleToIntegerExpression) n.getLeftOperand();
        if (left_string_expr.getOperator() == Operator.INDEXOFCI && right_constant.getConcreteValue() == -1L) {
            Expression<?> theSymbolicString = left_string_expr.getLeftOperand();
            Expression<?> theSymbolicChar = left_string_expr.getRightOperand();
            Expression<?> theOffset = left_string_expr.getOther().get(0);
            // check theString.lenght>0
            String theConcreteString = (String) theSymbolicString.accept(exprExecutor, null);
            Long theConcreteOffset = (Long) theOffset.accept(exprExecutor, null);
            if (theConcreteOffset > theConcreteString.length() - 1) {
                // satisfy the constraint
                return Long.MAX_VALUE;
            } else {
                char theConcreteChar = (char) ((Long) theSymbolicChar.accept(exprExecutor, null)).longValue();
                char[] charArray = theConcreteString.substring(theConcreteOffset.intValue(), theConcreteString.length()).toCharArray();
                int min_distance_to_char = Integer.MAX_VALUE;
                for (char c : charArray) {
                    if (Math.abs(c - theConcreteChar) < min_distance_to_char) {
                        min_distance_to_char = Math.abs(c - theConcreteChar);
                    }
                }
                return min_distance_to_char;
            }
        }
    }
    return -1;
}
Also used : StringMultipleToIntegerExpression(org.evosuite.symbolic.expr.bv.StringMultipleToIntegerExpression) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Aggregations

IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)2 StringMultipleToIntegerExpression (org.evosuite.symbolic.expr.bv.StringMultipleToIntegerExpression)2