Search in sources :

Example 21 with StringVariable

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

the class EvoSuiteSolver method randomizeValues.

private static void randomizeValues(Set<Variable<?>> variables, Set<Object> constants) {
    Set<String> stringConstants = new HashSet<String>();
    Set<Long> longConstants = new HashSet<Long>();
    Set<Double> realConstants = new HashSet<Double>();
    for (Object o : constants) {
        if (o instanceof String)
            stringConstants.add((String) o);
        else if (o instanceof Double)
            realConstants.add((Double) o);
        else if (o instanceof Long)
            longConstants.add((Long) o);
        else
            assert (false) : "Unexpected constant type: " + o;
    }
    for (Variable<?> v : variables) {
        if (v instanceof StringVariable) {
            StringVariable sv = (StringVariable) v;
            if (!stringConstants.isEmpty() && Randomness.nextDouble() < Properties.DSE_CONSTANT_PROBABILITY) {
                sv.setConcreteValue(Randomness.choice(stringConstants));
            } else {
                sv.setConcreteValue(Randomness.nextString(Properties.STRING_LENGTH));
            }
        } else if (v instanceof IntegerVariable) {
            IntegerVariable iv = (IntegerVariable) v;
            if (!longConstants.isEmpty() && Randomness.nextDouble() < Properties.DSE_CONSTANT_PROBABILITY) {
                iv.setConcreteValue(Randomness.choice(longConstants));
            } else {
                iv.setConcreteValue((long) Randomness.nextInt(Properties.MAX_INT * 2) - Properties.MAX_INT);
            }
        } else if (v instanceof RealVariable) {
            RealVariable rv = (RealVariable) v;
            if (!realConstants.isEmpty() && Randomness.nextDouble() < Properties.DSE_CONSTANT_PROBABILITY) {
                rv.setConcreteValue(Randomness.choice(realConstants));
            } else {
                rv.setConcreteValue((long) Randomness.nextInt(Properties.MAX_INT * 2) - Properties.MAX_INT);
            }
        }
    }
}
Also used : IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) HashSet(java.util.HashSet)

Example 22 with StringVariable

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

the class Solver method setConcreteValues.

/**
 * Restore all concrete values of the variables using the concrete_values
 * mapping.
 *
 * @param variables
 * @param concrete_values
 */
protected static void setConcreteValues(Set<Variable<?>> variables, Map<String, Object> concrete_values) {
    for (Variable<?> v : variables) {
        String var_name = v.getName();
        if (!concrete_values.containsKey(var_name)) {
            continue;
        }
        Object concreteValue = concrete_values.get(var_name);
        if (v instanceof StringVariable) {
            StringVariable sv = (StringVariable) v;
            String concreteString = (String) concreteValue;
            sv.setConcreteValue(concreteString);
        } else if (v instanceof IntegerVariable) {
            IntegerVariable iv = (IntegerVariable) v;
            Long concreteInteger = (Long) concreteValue;
            iv.setConcreteValue(concreteInteger);
        } else if (v instanceof RealVariable) {
            RealVariable ir = (RealVariable) v;
            Double concreteReal = (Double) concreteValue;
            ir.setConcreteValue(concreteReal);
        } else {
            logger.warn("unknow variable type " + v.getClass().getName());
        }
    }
}
Also used : IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) RealVariable(org.evosuite.symbolic.expr.fp.RealVariable) StringVariable(org.evosuite.symbolic.expr.str.StringVariable)

Example 23 with StringVariable

use of org.evosuite.symbolic.expr.str.StringVariable 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 24 with StringVariable

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

the class StringAVMTests method testIssueWithOptional.

@Test
public void testIssueWithOptional() throws SolverTimeoutException {
    String name = "addd";
    StringVariable var = new StringVariable(name, "");
    String format = "a.?c";
    List<Constraint<?>> constraints = getPatternConstraint(var, format);
    long start_time = System.currentTimeMillis();
    long timeout = Properties.DSE_CONSTRAINT_SOLVER_TIMEOUT_MILLIS;
    StringAVM avm = new StringAVM(var, constraints, start_time, timeout);
    boolean succeded = avm.applyAVM();
    assertTrue(succeded);
}
Also used : StringConstraint(org.evosuite.symbolic.expr.StringConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) StringAVM(org.evosuite.symbolic.solver.avm.StringAVM) Test(org.junit.Test)

Example 25 with StringVariable

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

the class StringAVMTests method testInsertLeft.

@Test
public void testInsertLeft() throws SolverTimeoutException {
    String name = "foo";
    String start = "abc";
    StringVariable var = new StringVariable(name, start);
    String format = "\\d\\d\\d" + start;
    List<Constraint<?>> constraints = getPatternConstraint(var, format);
    long start_time = System.currentTimeMillis();
    long timeout = Properties.DSE_CONSTRAINT_SOLVER_TIMEOUT_MILLIS;
    StringAVM avm = new StringAVM(var, constraints, start_time, timeout);
    boolean succeded = avm.applyAVM();
    assertTrue(succeded);
    String result = var.getConcreteValue();
    assertTrue("Length=" + result.length(), result.length() == 6);
    assertTrue(result, result.endsWith(start));
}
Also used : StringConstraint(org.evosuite.symbolic.expr.StringConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) StringVariable(org.evosuite.symbolic.expr.str.StringVariable) StringAVM(org.evosuite.symbolic.solver.avm.StringAVM) Test(org.junit.Test)

Aggregations

StringVariable (org.evosuite.symbolic.expr.str.StringVariable)39 Constraint (org.evosuite.symbolic.expr.Constraint)34 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)29 StringConstraint (org.evosuite.symbolic.expr.StringConstraint)28 Test (org.junit.Test)28 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)25 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)24 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)24 ArrayList (java.util.ArrayList)22 StringConstant (org.evosuite.symbolic.expr.str.StringConstant)20 StringBinaryComparison (org.evosuite.symbolic.expr.bv.StringBinaryComparison)16 StringBinaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringBinaryToIntegerExpression)10 StringUnaryExpression (org.evosuite.symbolic.expr.str.StringUnaryExpression)5 Expression (org.evosuite.symbolic.expr.Expression)4 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)4 StringMultipleComparison (org.evosuite.symbolic.expr.bv.StringMultipleComparison)4 StringUnaryToIntegerExpression (org.evosuite.symbolic.expr.bv.StringUnaryToIntegerExpression)4 RealVariable (org.evosuite.symbolic.expr.fp.RealVariable)4 StringAVM (org.evosuite.symbolic.solver.avm.StringAVM)4 SolverResult (org.evosuite.symbolic.solver.SolverResult)3