use of org.evosuite.symbolic.expr.bv.StringBinaryComparison 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();
}
}
use of org.evosuite.symbolic.expr.bv.StringBinaryComparison 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();
}
}
use of org.evosuite.symbolic.expr.bv.StringBinaryComparison 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();
}
}
use of org.evosuite.symbolic.expr.bv.StringBinaryComparison in project evosuite by EvoSuite.
the class TestStringSearch method testEndsWithTrueConstant.
@Test
public void testEndsWithTrueConstant() {
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
String var1 = "foo";
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.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()).endsWith(const2));
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.expr.bv.StringBinaryComparison in project evosuite by EvoSuite.
the class TestStringSearch method testInversionOfRegex.
@Test
public void testInversionOfRegex() {
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
String var = "a+";
String regex = "aaa";
// so we need to solve it
assertFalse(var.matches(regex));
String variableName = "test1";
StringVariable strVar = new StringVariable(variableName, var);
StringConstant strConst = new StringConstant(regex);
StringBinaryComparison strComp = new StringBinaryComparison(strConst, Operator.PATTERNMATCHES, strVar, 0L);
// the constraint should evaluate to true
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(variableName));
String solution = result.get(variableName).toString();
assertTrue(solution.matches(regex));
/*
* as the regex defines only one possible matching string, then the
* solution has to be equal to the regex
*/
assertEquals(regex, solution);
} catch (SolverTimeoutException e) {
fail();
}
// now let's invert them
strVar = new StringVariable(variableName, regex);
strConst = new StringConstant(var);
// the inversion should match immediately
assertTrue(regex.matches(var));
// recreate the same type of constraint
strComp = new StringBinaryComparison(strConst, Operator.PATTERNMATCHES, strVar, 0L);
constraints.clear();
constraints.add(new StringConstraint(strComp, Comparator.NE, new IntegerConstant(0)));
try {
result = solve(skr, constraints);
assertNotNull(result);
} catch (SolverTimeoutException e) {
fail();
}
}
Aggregations