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);
}
}
}
}
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());
}
}
}
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"));
}
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);
}
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));
}
Aggregations