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