Search in sources :

Example 1 with StringConstant

use of org.evosuite.symbolic.expr.str.StringConstant in project evosuite by EvoSuite.

the class Matcher_Matches method executeFunction.

@Override
public Object executeFunction() {
    Matcher conc_matcher = (Matcher) this.getConcReceiver();
    ReferenceConstant symb_matcher = (ReferenceConstant) this.getSymbReceiver();
    boolean res = this.getConcBooleanRetVal();
    String conc_regex = conc_matcher.pattern().pattern();
    StringValue symb_input = (StringValue) env.heap.getField(Types.JAVA_UTIL_REGEX_MATCHER, SymbolicHeap.$MATCHER_INPUT, conc_matcher, symb_matcher);
    if (symb_input != null && symb_input.containsSymbolicVariable()) {
        int concrete_value = res ? 1 : 0;
        StringConstant symb_regex = ExpressionFactory.buildNewStringConstant(conc_regex);
        StringBinaryComparison strComp = new StringBinaryComparison(symb_regex, Operator.PATTERNMATCHES, symb_input, (long) concrete_value);
        return strComp;
    } else {
        return this.getSymbIntegerRetVal();
    }
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) Matcher(java.util.regex.Matcher) StringBinaryComparison(org.evosuite.symbolic.expr.bv.StringBinaryComparison) StringValue(org.evosuite.symbolic.expr.str.StringValue) StringConstant(org.evosuite.symbolic.expr.str.StringConstant)

Example 2 with StringConstant

use of org.evosuite.symbolic.expr.str.StringConstant in project evosuite by EvoSuite.

the class StringBuffer_SetLength method executeFunction.

@Override
public Object executeFunction() {
    ReferenceConstant symb_str_buffer = this.getSymbReceiver();
    StringBuffer conc_str_buffer = (StringBuffer) this.getConcReceiver();
    IntegerValue newSymbLength = this.getSymbIntegerArgument(0);
    int newConcLength = this.getConcIntArgument(0);
    // retrieve symbolic value from heap
    String conc_value = conc_str_buffer.toString();
    StringValue symb_value = env.heap.getField(Types.JAVA_LANG_STRING_BUFFER, SymbolicHeap.$STRING_BUFFER_CONTENTS, conc_str_buffer, symb_str_buffer, pre_conc_value);
    if (symb_value.containsSymbolicVariable() || newSymbLength.containsSymbolicVariable()) {
        StringValue new_symb_value = null;
        if (!newSymbLength.containsSymbolicVariable() && newConcLength == 0) {
            // StringBuffer contents equals to "" string
            new_symb_value = new StringConstant("");
        } else {
            // StringBuffer contents equ
            new_symb_value = new StringMultipleExpression(symb_value, Operator.SUBSTRING, new IntegerConstant(0), new ArrayList<Expression<?>>(Collections.singletonList(newSymbLength)), conc_value);
        }
        env.heap.putField(Types.JAVA_LANG_STRING_BUFFER, SymbolicHeap.$STRING_BUFFER_CONTENTS, conc_str_buffer, symb_str_buffer, new_symb_value);
    }
    // return void
    return null;
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) StringMultipleExpression(org.evosuite.symbolic.expr.str.StringMultipleExpression) IntegerValue(org.evosuite.symbolic.expr.bv.IntegerValue) ArrayList(java.util.ArrayList) StringValue(org.evosuite.symbolic.expr.str.StringValue) StringConstant(org.evosuite.symbolic.expr.str.StringConstant) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 3 with StringConstant

use of org.evosuite.symbolic.expr.str.StringConstant in project evosuite by EvoSuite.

the class Matches method executeFunction.

@Override
public Object executeFunction() {
    // receiver
    ReferenceConstant symb_receiver = this.getSymbReceiver();
    String conc_receiver = (String) this.getConcReceiver();
    // argument
    String conc_argument = (String) this.getConcArgument(0);
    StringValue right_expr = env.heap.getField(Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_receiver, symb_receiver, conc_receiver);
    // return val
    boolean res = this.getConcBooleanRetVal();
    if (right_expr.containsSymbolicVariable()) {
        StringConstant left_expr = ExpressionFactory.buildNewStringConstant(conc_argument);
        int conV = res ? 1 : 0;
        StringBinaryComparison strBExpr = new StringBinaryComparison(left_expr, Operator.PATTERNMATCHES, right_expr, (long) conV);
        return strBExpr;
    }
    return this.getSymbIntegerRetVal();
}
Also used : ReferenceConstant(org.evosuite.symbolic.expr.ref.ReferenceConstant) StringBinaryComparison(org.evosuite.symbolic.expr.bv.StringBinaryComparison) StringValue(org.evosuite.symbolic.expr.str.StringValue) StringConstant(org.evosuite.symbolic.expr.str.StringConstant)

Example 4 with StringConstant

use of org.evosuite.symbolic.expr.str.StringConstant in project evosuite by EvoSuite.

the class TestIntegerSearch method testEvosuiteExample5.

@Test
public void testEvosuiteExample5() throws SolverEmptyQueryException {
    // TestSuiteDSE.setStart();
    // Cnstr 0 : var6__SYM(84) != (y charAt 0) dist: 8.0
    // Cnstr 1 : var6__SYM(84) != 115 dist: 8.0
    // Cnstr 2 : var6__SYM(84) == 108 dist: 8.0
    int var1 = 84;
    int const1 = 115;
    int const2 = 108;
    String const3 = "y";
    IntegerConstant iconst1 = new IntegerConstant(const1);
    IntegerConstant iconst2 = new IntegerConstant(const2);
    StringConstant strConst = new StringConstant(const3);
    IntegerVariable ivar1 = new IntegerVariable("test1", var1, Integer.MIN_VALUE, Integer.MAX_VALUE);
    StringBinaryToIntegerExpression sBExpr = new StringBinaryToIntegerExpression(strConst, Operator.CHARAT, new IntegerConstant(0), (long) "y".charAt(0));
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    constraints.add(new IntegerConstraint(ivar1, Comparator.NE, sBExpr));
    constraints.add(new IntegerConstraint(ivar1, Comparator.NE, iconst1));
    constraints.add(new IntegerConstraint(ivar1, Comparator.EQ, iconst2));
    try {
        EvoSuiteSolver solver = new EvoSuiteSolver();
        SolverResult solverResult = solver.solve(constraints);
        assertTrue(solverResult.isSAT());
        Map<String, Object> model = solverResult.getModel();
        var1 = ((Number) model.get("test1")).intValue();
        assertTrue(var1 == 108);
    } 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) StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) ArrayList(java.util.ArrayList) SolverResult(org.evosuite.symbolic.solver.SolverResult) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) StringConstant(org.evosuite.symbolic.expr.str.StringConstant) Test(org.junit.Test)

Example 5 with StringConstant

use of org.evosuite.symbolic.expr.str.StringConstant in project evosuite by EvoSuite.

the class TestPatternSearch method testMatcherMatches.

@Test
public void testMatcherMatches() throws SolverEmptyQueryException {
    String input = "random_value";
    String format = "(\\d+)-(\\d\\d)-(\\d)";
    // String format = "^(\\d+)-(\\d\\d)-(\\d)$";
    StringVariable var0 = new StringVariable("var0", input);
    StringConstant symb_regex = ExpressionFactory.buildNewStringConstant(format);
    StringBinaryComparison strComp = new StringBinaryComparison(symb_regex, Operator.PATTERNMATCHES, var0, 0L);
    StringConstraint constraint = new StringConstraint(strComp, Comparator.NE, new IntegerConstant(0));
    List<Constraint<?>> constraints = Collections.<Constraint<?>>singletonList(constraint);
    try {
        EvoSuiteSolver solver = new EvoSuiteSolver();
        SolverResult result = solver.solve(constraints);
        assertTrue(result.isSAT());
        Map<String, Object> model = result.getModel();
        String var0_value = (String) model.get("var0");
        Pattern pattern = Pattern.compile(format);
        Matcher matcher = pattern.matcher(var0_value);
        assertTrue(matcher.matches());
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) Pattern(java.util.regex.Pattern) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) Matcher(java.util.regex.Matcher) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) SolverResult(org.evosuite.symbolic.solver.SolverResult) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant) StringBinaryComparison(org.evosuite.symbolic.expr.bv.StringBinaryComparison) StringConstant(org.evosuite.symbolic.expr.str.StringConstant) Test(org.junit.Test)

Aggregations

StringConstant (org.evosuite.symbolic.expr.str.StringConstant)26 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)23 Constraint (org.evosuite.symbolic.expr.Constraint)22 StringConstraint (org.evosuite.symbolic.expr.StringConstraint)21 StringBinaryComparison (org.evosuite.symbolic.expr.bv.StringBinaryComparison)20 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)20 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)19 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)18 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)18 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)18 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)5 StringUnaryExpression (org.evosuite.symbolic.expr.str.StringUnaryExpression)5 Expression (org.evosuite.symbolic.expr.Expression)4 StringMultipleComparison (org.evosuite.symbolic.expr.bv.StringMultipleComparison)4 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)4 StringValue (org.evosuite.symbolic.expr.str.StringValue)4 Matcher (java.util.regex.Matcher)2 SolverResult (org.evosuite.symbolic.solver.SolverResult)2 Pattern (java.util.regex.Pattern)1