Search in sources :

Example 41 with IntegerConstant

use of org.evosuite.symbolic.expr.bv.IntegerConstant 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 42 with IntegerConstant

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

the class DistanceCalculator method getDistanceStringIsInteger.

private static long getDistanceStringIsInteger(IntegerConstraint n, long leftVal, long rightVal) {
    if (n.getLeftOperand() instanceof StringUnaryToIntegerExpression && n.getComparator() == Comparator.NE && n.getRightOperand() instanceof IntegerConstant) {
        IntegerConstant right_constant = (IntegerConstant) n.getRightOperand();
        StringUnaryToIntegerExpression left_string_expr = (StringUnaryToIntegerExpression) n.getLeftOperand();
        if (right_constant.getConcreteValue().longValue() != 0L) {
            return -1;
        }
        if (left_string_expr.getOperator() != Operator.IS_INTEGER) {
            return -1;
        }
        String string = left_string_expr.getOperand().getConcreteValue();
        if (string.length() > 0) {
            char[] charArray = string.toCharArray();
            int maxDistance = 0;
            for (int i = 0; i < charArray.length; i++) {
                char c = charArray[i];
                int distance;
                if (!Character.isDigit(c)) {
                    if (c < '0') {
                        distance = '0' - c;
                    } else if (c > '9') {
                        distance = c - '9';
                    } else {
                        throw new RuntimeException("This branch is unreachable!");
                    }
                    if (maxDistance < distance) {
                        maxDistance = distance;
                    }
                }
            }
            return maxDistance;
        } else {
            return Long.MAX_VALUE;
        }
    }
    return -1;
}
Also used : StringUnaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 43 with IntegerConstant

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

Example 44 with IntegerConstant

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

the class TestIntegerSearch method testNEConstant.

@Test
public void testNEConstant() throws SolverEmptyQueryException {
    // TODO: Currently, the model returned by the search is null if the
    // constraint is already satisfied,
    // so in this example the concrete value has to be the target initially
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    constraints.add(new IntegerConstraint(new IntegerVariable("test1", 235082, -1000000, 1000000), Comparator.NE, new IntegerConstant(235082)));
    try {
        EvoSuiteSolver solver = new EvoSuiteSolver();
        SolverResult solverResult = solver.solve(constraints);
        assertTrue(solverResult.isSAT());
        Map<String, Object> model = solverResult.getModel();
        assertNotNull(model.get("test1"));
        assertTrue(235082 != ((Number) model.get("test1")).intValue());
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) ArrayList(java.util.ArrayList) SolverResult(org.evosuite.symbolic.solver.SolverResult) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) Test(org.junit.Test)

Example 45 with IntegerConstant

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

the class TestStringEqualsIgnoreCase method testStringEqualsIgnoreCase.

@Test
public void testStringEqualsIgnoreCase() throws SecurityException, NoSuchMethodException, SolverTimeoutException {
    IntegerConstant zero = new IntegerConstant(0);
    StringVariable stringVar0 = new StringVariable("var0", "");
    StringConstant strConst = new StringConstant("bar");
    StringBinaryComparison cmp1 = new StringBinaryComparison(stringVar0, Operator.EQUALS, strConst, 0L);
    StringConstraint constr1 = new StringConstraint(cmp1, Comparator.EQ, zero);
    StringBinaryComparison cmp2 = new StringBinaryComparison(stringVar0, Operator.EQUALSIGNORECASE, strConst, 1L);
    StringConstraint constr2 = new StringConstraint(cmp2, Comparator.NE, zero);
    Collection<Constraint<?>> constraints = Arrays.<Constraint<?>>asList(constr1, constr2);
    EvoSuiteSolver solver = new EvoSuiteSolver();
    Map<String, Object> solution = solve(solver, constraints);
    assertNotNull(solution);
    String var0 = (String) solution.get("var0");
    assertNotNull(var0);
    assertTrue(!var0.equals("bar"));
    assertTrue(var0.equalsIgnoreCase("bar"));
}
Also used : StringConstraint(org.evosuite.symbolic.expr.StringConstraint) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) StringBinaryComparison(org.evosuite.symbolic.expr.bv.StringBinaryComparison) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) StringConstant(org.evosuite.symbolic.expr.str.StringConstant) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant) Test(org.junit.Test)

Aggregations

IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)65 Constraint (org.evosuite.symbolic.expr.Constraint)42 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)42 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)36 Test (org.junit.Test)36 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)35 ArrayList (java.util.ArrayList)34 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)29 StringConstraint (org.evosuite.symbolic.expr.StringConstraint)26 StringConstant (org.evosuite.symbolic.expr.str.StringConstant)23 StringBinaryComparison (org.evosuite.symbolic.expr.bv.StringBinaryComparison)17 SolverResult (org.evosuite.symbolic.solver.SolverResult)14 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)13 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)12 IntegerValue (org.evosuite.symbolic.expr.bv.IntegerValue)7 Expression (org.evosuite.symbolic.expr.Expression)6 StringMultipleComparison (org.evosuite.symbolic.expr.bv.StringMultipleComparison)5 StringUnaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression)5 StringUnaryExpression (org.evosuite.symbolic.expr.str.StringUnaryExpression)5 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)3