Search in sources :

Example 1 with StringConstraint

use of org.evosuite.symbolic.expr.StringConstraint in project evosuite by EvoSuite.

the class ConstraintNormalizer method createStringConstraint.

private static Constraint<?> createStringConstraint(IntegerConstraint c) {
    if (c.getLeftOperand() instanceof StringComparison) {
        StringComparison string_comparison = (StringComparison) c.getLeftOperand();
        @SuppressWarnings("unchecked") Expression<Long> number_expr = (Expression<Long>) c.getRightOperand();
        IntegerConstant constant = new IntegerConstant(number_expr.getConcreteValue());
        return new StringConstraint(string_comparison, c.getComparator(), constant);
    } else {
        assert c.getRightOperand() instanceof StringComparison;
        StringComparison string_comparison = (StringComparison) c.getRightOperand();
        @SuppressWarnings("unchecked") Expression<Long> number_expr = (Expression<Long>) c.getLeftOperand();
        IntegerConstant constant = new IntegerConstant(number_expr.getConcreteValue());
        return new StringConstraint(string_comparison, c.getComparator(), constant);
    }
}
Also used : StringComparison(org.evosuite.symbolic.expr.bv.StringComparison) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) Expression(org.evosuite.symbolic.expr.Expression) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

Example 2 with StringConstraint

use of org.evosuite.symbolic.expr.StringConstraint in project evosuite by EvoSuite.

the class StringAVM method getTokenDelimiters.

private static Set<StringValue> getTokenDelimiters(Collection<Constraint<?>> constraints) {
    Set<StringValue> delimiters = new HashSet<StringValue>();
    for (Constraint<?> constraint : constraints) {
        if (constraint instanceof StringConstraint) {
            StringConstraint stringConstraint = (StringConstraint) constraint;
            if (stringConstraint.getLeftOperand() instanceof HasMoreTokensExpr) {
                HasMoreTokensExpr hasMoreTokensExpr = (HasMoreTokensExpr) stringConstraint.getLeftOperand();
                StringValue delimiter = hasMoreTokensExpr.getTokenizerExpr().getDelimiter();
                delimiters.add(delimiter);
            }
        }
    }
    return delimiters;
}
Also used : StringConstraint(org.evosuite.symbolic.expr.StringConstraint) HasMoreTokensExpr(org.evosuite.symbolic.expr.token.HasMoreTokensExpr) StringValue(org.evosuite.symbolic.expr.str.StringValue) HashSet(java.util.HashSet)

Example 3 with StringConstraint

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

Example 4 with StringConstraint

use of org.evosuite.symbolic.expr.StringConstraint in project evosuite by EvoSuite.

the class TestStringSearch method testRegexMatchesTrue.

@Test
public void testRegexMatchesTrue() {
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    String var1 = "test";
    String const2 = "TEST";
    StringVariable strVar = new StringVariable("test1", var1);
    StringConstant strConst = new StringConstant(const2);
    StringBinaryComparison strComp = new StringBinaryComparison(strVar, Operator.PATTERNMATCHES, strConst, 0L);
    constraints.add(new StringConstraint(strComp, Comparator.NE, new IntegerConstant(0)));
    EvoSuiteSolver skr = new EvoSuiteSolver();
    Map<String, Object> result;
    try {
        result = solve(skr, constraints);
        assertNotNull(result);
        assertNotNull(result.get("test1"));
        assertTrue(result.get("test1").toString().matches(const2));
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) 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) ArrayList(java.util.ArrayList) 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)

Example 5 with StringConstraint

use of org.evosuite.symbolic.expr.StringConstraint in project evosuite by EvoSuite.

the class TestStringSearch method testEqualsIgnoreCaseTrueConstant.

@Test
public void testEqualsIgnoreCaseTrueConstant() {
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    String var1 = "foo";
    String const2 = "Fest";
    StringVariable strVar = new StringVariable("test1", var1);
    StringConstant strConst = new StringConstant(const2);
    StringBinaryComparison strComp = new StringBinaryComparison(strVar, Operator.EQUALSIGNORECASE, strConst, 0L);
    constraints.add(new StringConstraint(strComp, Comparator.NE, new IntegerConstant(0)));
    EvoSuiteSolver skr = new EvoSuiteSolver();
    Map<String, Object> result;
    try {
        result = solve(skr, constraints);
        assertNotNull(result);
        assertNotNull(result.get("test1"));
        assertTrue(const2.equalsIgnoreCase(result.get("test1").toString()));
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) 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) ArrayList(java.util.ArrayList) 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

StringConstraint (org.evosuite.symbolic.expr.StringConstraint)24 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)22 Constraint (org.evosuite.symbolic.expr.Constraint)21 StringConstant (org.evosuite.symbolic.expr.str.StringConstant)21 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)20 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)18 Test (org.junit.Test)18 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)17 StringBinaryComparison (org.evosuite.symbolic.expr.bv.StringBinaryComparison)17 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)17 ArrayList (java.util.ArrayList)16 Expression (org.evosuite.symbolic.expr.Expression)5 StringUnaryExpression (org.evosuite.symbolic.expr.str.StringUnaryExpression)5 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)4 StringMultipleComparison (org.evosuite.symbolic.expr.bv.StringMultipleComparison)4 HashSet (java.util.HashSet)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 DistanceCalculator (org.evosuite.symbolic.expr.DistanceCalculator)1 RealConstraint (org.evosuite.symbolic.expr.RealConstraint)1