Search in sources :

Example 11 with StringConstraint

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

the class TestStringSearch method testStartsWithFalseConstant.

@Test
public void testStartsWithFalseConstant() {
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    String var1 = "footest";
    String const2 = "test";
    StringVariable strVar = new StringVariable("test1", var1);
    StringConstant strConst = new StringConstant(const2);
    IntegerConstant offs_expr = new IntegerConstant(3);
    ArrayList<Expression<?>> other = new ArrayList<Expression<?>>();
    other.add(offs_expr);
    StringMultipleComparison strComp = new StringMultipleComparison(strVar, Operator.STARTSWITH, strConst, other, 0L);
    constraints.add(new StringConstraint(strComp, Comparator.EQ, new IntegerConstant(0)));
    EvoSuiteSolver skr = new EvoSuiteSolver();
    Map<String, Object> result;
    try {
        result = solve(skr, constraints);
        assertNotNull(result);
        assertNotNull(result.get("test1"));
        assertFalse((result.get("test1").toString()).startsWith(const2, 3));
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) StringMultipleComparison(org.evosuite.symbolic.expr.bv.StringMultipleComparison) 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) Expression(org.evosuite.symbolic.expr.Expression) StringBinaryToIntegerExpression(org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression) StringUnaryExpression(org.evosuite.symbolic.expr.str.StringUnaryExpression) StringConstant(org.evosuite.symbolic.expr.str.StringConstant) Test(org.junit.Test)

Example 12 with StringConstraint

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

the class TestStringSearch method testEndsWithFalseConstant.

@Test
public void testEndsWithFalseConstant() {
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    String var1 = "footest";
    String const2 = "test";
    StringVariable strVar = new StringVariable("test1", var1);
    StringConstant strConst = new StringConstant(const2);
    StringBinaryComparison strComp = new StringBinaryComparison(strVar, Operator.ENDSWITH, strConst, 0L);
    constraints.add(new StringConstraint(strComp, Comparator.EQ, new IntegerConstant(0)));
    EvoSuiteSolver skr = new EvoSuiteSolver();
    Map<String, Object> result;
    try {
        result = solve(skr, constraints);
        assertNotNull(result);
        assertNotNull(result.get("test1"));
        assertFalse((result.get("test1").toString()).endsWith(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 13 with StringConstraint

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

the class DistanceEstimator method getDistance.

/**
 * <p>
 * getDistance
 * </p>
 *
 * @param constraints
 *            a {@link java.util.Collection} object.
 * @return normalized distance in [0,1]
 */
public static double getDistance(Collection<Constraint<?>> constraints) {
    double result = 0;
    DistanceCalculator distanceCalculator = new DistanceCalculator();
    try {
        for (Constraint<?> c : constraints) {
            if (c instanceof StringConstraint) {
                StringConstraint string_constraint = (StringConstraint) c;
                try {
                    double strD = (double) string_constraint.accept(distanceCalculator, null);
                    result += normalize(strD);
                    log.debug("S: " + string_constraint + " strDist " + strD);
                } catch (Throwable t) {
                    log.debug("S: " + string_constraint + " strDist " + t);
                    result += 1.0;
                }
            } else if (c instanceof IntegerConstraint) {
                IntegerConstraint integer_constraint = (IntegerConstraint) c;
                long intD = (long) integer_constraint.accept(distanceCalculator, null);
                result += normalize(intD);
                log.debug("C: " + integer_constraint + " intDist " + intD);
            } else if (c instanceof RealConstraint) {
                RealConstraint real_constraint = (RealConstraint) c;
                double realD = (double) real_constraint.accept(distanceCalculator, null);
                result += normalize(realD);
                log.debug("C: " + real_constraint + " realDist " + realD);
            } else {
                throw new IllegalArgumentException("DistanceCalculator: got an unknown constraint: " + c);
            }
        }
        log.debug("Resulting distance: " + result);
        return Math.abs(result);
    } catch (Exception e) {
        return Double.MAX_VALUE;
    }
}
Also used : DistanceCalculator(org.evosuite.symbolic.expr.DistanceCalculator) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) RealConstraint(org.evosuite.symbolic.expr.RealConstraint)

Example 14 with StringConstraint

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

Example 15 with StringConstraint

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

the class StringAVMTests method getPatternConstraint.

private List<Constraint<?>> getPatternConstraint(StringVariable var, String format) {
    StringConstant symb_regex = ExpressionFactory.buildNewStringConstant(format);
    StringBinaryComparison strComp = new StringBinaryComparison(symb_regex, Operator.PATTERNMATCHES, var, 0L);
    StringConstraint constraint = new StringConstraint(strComp, Comparator.NE, new IntegerConstant(0));
    List<Constraint<?>> constraints = Collections.<Constraint<?>>singletonList(constraint);
    return constraints;
}
Also used : StringConstraint(org.evosuite.symbolic.expr.StringConstraint) StringConstraint(org.evosuite.symbolic.expr.StringConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) StringBinaryComparison(org.evosuite.symbolic.expr.bv.StringBinaryComparison) StringConstant(org.evosuite.symbolic.expr.str.StringConstant) IntegerConstant(org.evosuite.symbolic.expr.bv.IntegerConstant)

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