Search in sources :

Example 6 with StringBinaryToIntegerExpression

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

the class TestStringSearch method testIndexOfC2.

@Test
public void testIndexOfC2() {
    String var1value = ":cc]#0l";
    StringVariable var1 = new StringVariable("var0", var1value);
    IntegerConstant colon_code = new IntegerConstant(58);
    IntegerConstant numeral_code = new IntegerConstant(35);
    IntegerConstant minus_one = new IntegerConstant(-1);
    StringBinaryToIntegerExpression index_of_colon = new StringBinaryToIntegerExpression(var1, Operator.INDEXOFC, colon_code, -1L);
    StringBinaryToIntegerExpression index_of_numeral = new StringBinaryToIntegerExpression(var1, Operator.INDEXOFC, numeral_code, -1L);
    /*
		 * Here we are trying to modify the string such that the first '#' comes
		 * before the first ':', and both are present
		 */
    IntegerConstraint constr1 = new IntegerConstraint(index_of_colon, Comparator.NE, minus_one);
    IntegerConstraint constr2 = new IntegerConstraint(index_of_numeral, Comparator.NE, minus_one);
    IntegerConstraint constr3 = new IntegerConstraint(index_of_numeral, Comparator.LT, index_of_colon);
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    constraints.add(constr1);
    constraints.add(constr2);
    constraints.add(constr3);
    EvoSuiteSolver solver = new EvoSuiteSolver();
    Map<String, Object> solution = null;
    try {
        /*
			 * The constraint is not trivial, as there are search plateaus. So
			 * it is ok if sometimes it fails (tried 10 times, failed 3).
			 */
        final int TRIES = 20;
        for (int i = 0; i < TRIES; i++) {
            solution = solve(solver, constraints);
            if (solution != null) {
                break;
            }
        }
        assertNotNull(solution);
        String result = solution.get("var0").toString();
        int colonPos = result.indexOf(':');
        int numeralPos = result.indexOf('#');
        assertTrue("Colon not found in " + result, colonPos >= 0);
        assertTrue("Numeral not found in " + result, numeralPos >= 0);
        assertTrue(colonPos > numeralPos);
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) 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) StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) ArrayList(java.util.ArrayList) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Test(org.junit.Test)

Example 7 with StringBinaryToIntegerExpression

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

the class CharAt method executeFunction.

@Override
public Object executeFunction() {
    String conc_str = (String) this.getConcReceiver();
    ReferenceConstant symb_str = this.getSymbReceiver();
    StringValue string_expr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_str, symb_str, conc_str);
    IntegerValue index_expr = this.getSymbIntegerArgument(0);
    char res = this.getConcCharRetVal();
    if (string_expr.containsSymbolicVariable() || index_expr.containsSymbolicVariable()) {
        StringBinaryToIntegerExpression strBExpr = new StringBinaryToIntegerExpression(string_expr, Operator.CHARAT, index_expr, (long) res);
        return strBExpr;
    } else {
        return this.getSymbIntegerRetVal();
    }
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue) StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 8 with StringBinaryToIntegerExpression

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

the class DistanceCalculator method getDistanceIndexOfCEqualsK.

private static long getDistanceIndexOfCEqualsK(IntegerConstraint n, long leftVal, long rightVal) {
    if (n.getLeftOperand() instanceof StringBinaryToIntegerExpression && n.getComparator() == Comparator.EQ && n.getRightOperand() instanceof IntegerConstant) {
        IntegerConstant right_constant = (IntegerConstant) n.getRightOperand();
        StringBinaryToIntegerExpression left_string_expr = (StringBinaryToIntegerExpression) n.getLeftOperand();
        if (left_string_expr.getOperator() == Operator.INDEXOFC) {
            Expression<?> theSymbolicString = left_string_expr.getLeftOperand();
            Expression<?> theSymbolicChar = left_string_expr.getRightOperand();
            Expression<?> theSymbolicIndex = right_constant;
            // check theString.lenght>0
            ExpressionExecutor exprExecutor = new ExpressionExecutor();
            String theConcreteString = (String) theSymbolicString.accept(exprExecutor, null);
            Long theConcreteIndex = (Long) theSymbolicIndex.accept(exprExecutor, null);
            if (theConcreteIndex > theConcreteString.length() - 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 : StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 9 with StringBinaryToIntegerExpression

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

the class DistanceCalculator method getDistanceIndexOfCFound.

private static long getDistanceIndexOfCFound(IntegerConstraint n, long leftVal, long rightVal) {
    ExpressionExecutor exprExecutor = new ExpressionExecutor();
    if (n.getLeftOperand() instanceof StringBinaryToIntegerExpression && n.getComparator() == Comparator.NE && n.getRightOperand() instanceof IntegerConstant) {
        IntegerConstant right_constant = (IntegerConstant) n.getRightOperand();
        StringBinaryToIntegerExpression left_string_expr = (StringBinaryToIntegerExpression) n.getLeftOperand();
        if (left_string_expr.getOperator() == Operator.INDEXOFC && right_constant.getConcreteValue() == -1L) {
            Expression<?> theSymbolicString = left_string_expr.getLeftOperand();
            Expression<?> theSymbolicChar = left_string_expr.getRightOperand();
            // check theString.lenght>0
            String theConcreteString = (String) theSymbolicString.accept(exprExecutor, null);
            if (theConcreteString.length() == 0) {
                // no char can be modified to satisfy the constraint
                return Long.MAX_VALUE;
            } else {
                char theConcreteChar = (char) ((Long) theSymbolicChar.accept(exprExecutor, null)).longValue();
                char[] charArray = theConcreteString.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 : StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 10 with StringBinaryToIntegerExpression

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

the class TestStringSearch method testChopOffIndexOfC.

@Test
public void testChopOffIndexOfC() {
    String var1value = "D<E\u001Exqaa:saksajij1§n";
    StringVariable var1 = new StringVariable("var1", var1value);
    IntegerConstant colon_code = new IntegerConstant(58);
    IntegerConstant minus_one = new IntegerConstant(-1);
    int colon_int_code = (int) ':';
    int concrete_value = var1value.indexOf(colon_int_code);
    StringBinaryToIntegerExpression index_of_colon = new StringBinaryToIntegerExpression(var1, Operator.INDEXOFC, colon_code, (long) concrete_value);
    IntegerConstraint constr1 = new IntegerConstraint(index_of_colon, Comparator.EQ, minus_one);
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    constraints.add(constr1);
    EvoSuiteSolver solver = new EvoSuiteSolver();
    Map<String, Object> solution;
    try {
        solution = solve(solver, constraints);
        assertNotNull(solution);
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) 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) StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) ArrayList(java.util.ArrayList) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Test(org.junit.Test)

Aggregations

StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)12 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)9 Constraint (org.evosuite.symbolic.expr.Constraint)7 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)7 ArrayList (java.util.ArrayList)6 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)6 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)6 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)6 Test (org.junit.Test)6 StringConstraint (org.evosuite.symbolic.expr.StringConstraint)4 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)3 StringValue (org.evosuite.symbolic.expr.str.StringValue)3 StringUnaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression)2 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)1 IntegerValue (org.evosuite.symbolic.expr.bv.IntegerValue)1 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)1 StringConstant (org.evosuite.symbolic.expr.str.StringConstant)1 SolverResult (org.evosuite.symbolic.solver.SolverResult)1