Search in sources :

Example 1 with StringUnaryToIntegerExpression

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

the class Length method executeFunction.

@Override
public Object executeFunction() {
    ReferenceConstant symb_str = this.getSymbReceiver();
    String conc_str = (String) this.getConcReceiver();
    int res = this.getConcIntRetVal();
    StringValue string_expr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_str, symb_str, conc_str);
    if (string_expr.containsSymbolicVariable()) {
        StringUnaryToIntegerExpression strUnExpr = new StringUnaryToIntegerExpression(string_expr, Operator.LENGTH, (long) res);
        return strUnExpr;
    }
    return this.getSymbIntegerRetVal();
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) StringUnaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression) StringValue(org.evosuite.symbolic.expr.str.StringValue)

Example 2 with StringUnaryToIntegerExpression

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

the class TestIsInteger method testIsInteger.

@Test
public void testIsInteger() throws SolverEmptyQueryException {
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    constraints.add(new IntegerConstraint(new StringUnaryToIntegerExpression(new StringVariable("var0", "hello"), Operator.IS_INTEGER, 0L), Comparator.NE, new IntegerConstant(0)));
    EvoSuiteSolver solver = new EvoSuiteSolver();
    try {
        SolverResult result = solver.solve(constraints);
        assertTrue(result.isSAT());
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) 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) StringUnaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant) Test(org.junit.Test)

Example 3 with StringUnaryToIntegerExpression

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

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

the class TestSolverStringFunctions method testNegativeLength.

public static Map<String, Object> testNegativeLength(Solver solver) throws SecurityException, NoSuchMethodException, SolverTimeoutException {
    IntegerConstraint newIntegerConstraint = new IntegerConstraint(new StringUnaryToIntegerExpression(new StringVariable("var0", "01234"), Operator.LENGTH, (long) 5), Comparator.LT, new IntegerConstant(0));
    Collection<Constraint<?>> constraints = Collections.<Constraint<?>>singleton(newIntegerConstraint);
    Map<String, Object> solution = solve(solver, constraints);
    return solution;
}
Also used : IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) StringUnaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) TestCaseStringAppendString(com.examples.with.different.packagename.solver.TestCaseStringAppendString) TestCaseStringLastIndexOfString(com.examples.with.different.packagename.solver.TestCaseStringLastIndexOfString) TestCaseStringIndexOfString(com.examples.with.different.packagename.solver.TestCaseStringIndexOfString) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 5 with StringUnaryToIntegerExpression

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

the class TestConstraintSolver1 method buildConstraintSystem.

private static Collection<Constraint<?>> buildConstraintSystem() {
    StringVariable var0 = new StringVariable("var0", INIT_STRING);
    StringUnaryToIntegerExpression length = new StringUnaryToIntegerExpression(var0, Operator.LENGTH, (long) INIT_STRING.length());
    IntegerConstant const3 = new IntegerConstant(3);
    StringBinaryToIntegerExpression charAt3 = new StringBinaryToIntegerExpression(var0, Operator.CHARAT, const3, (long) INIT_STRING.charAt(3));
    IntegerConstant const4 = new IntegerConstant(4);
    StringBinaryToIntegerExpression charAt4 = new StringBinaryToIntegerExpression(var0, Operator.CHARAT, const4, (long) INIT_STRING.charAt(4));
    IntegerConstant const5 = new IntegerConstant(INIT_STRING.length());
    IntegerConstant const95 = new IntegerConstant(EXPECTED_STRING.charAt(3));
    IntegerConstant const43 = new IntegerConstant(EXPECTED_STRING.charAt(4));
    IntegerConstraint constr1 = new IntegerConstraint(length, Comparator.EQ, const5);
    IntegerConstraint constr2 = new IntegerConstraint(charAt3, Comparator.EQ, const95);
    IntegerConstraint constr3 = new IntegerConstraint(charAt4, Comparator.EQ, const43);
    return Arrays.<Constraint<?>>asList(constr1, constr2, constr3);
}
Also used : IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) StringUnaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Aggregations

StringUnaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression)6 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)5 Constraint (org.evosuite.symbolic.expr.Constraint)4 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)4 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)4 ArrayList (java.util.ArrayList)2 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)2 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)2 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)2 Test (org.junit.Test)2 TestCaseStringAppendString (com.examples.with.different.packagename.solver.TestCaseStringAppendString)1 TestCaseStringIndexOfString (com.examples.with.different.packagename.solver.TestCaseStringIndexOfString)1 TestCaseStringLastIndexOfString (com.examples.with.different.packagename.solver.TestCaseStringLastIndexOfString)1 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)1 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)1 StringValue (org.evosuite.symbolic.expr.str.StringValue)1 SolverResult (org.evosuite.symbolic.solver.SolverResult)1