use of org.evosuite.symbolic.solver.SolverTimeoutException in project evosuite by EvoSuite.
the class StringAVM method applyAVM.
/**
* <p>
* strLocalSearch
* </p>
*
* @return a boolean.
*/
public boolean applyAVM() throws SolverTimeoutException {
ExpressionExecutor exprExecutor = new ExpressionExecutor();
// try to remove each
log.debug("Trying to remove characters");
boolean improvement = false;
checkpointVar(DistanceEstimator.getDistance(cnstr));
// First chop characters from the back until distance doesn't improve
String oldString = strVar.getConcreteValue();
boolean improved = true;
while (improved && oldString.length() > 0) {
if (isFinished()) {
throw new SolverTimeoutException();
}
String newStr = oldString.substring(0, oldString.length() - 1);
strVar.setConcreteValue(newStr);
log.debug("Current attempt: " + newStr);
improved = false;
double newDist = DistanceEstimator.getDistance(cnstr);
// if (distImpr(newDist)) {
if (newDist <= checkpointDistance) {
log.debug("Distance improved or did not increase, keeping change");
checkpointVar(newDist);
improvement = true;
improved = true;
oldString = newStr;
if (newDist == 0) {
return true;
}
} else {
log.debug("Distance did not improve, reverting change");
restoreVar();
}
}
// next try to replace each character using AVM
log.debug("Trying to replace characters");
// Backup is done internally
if (doStringAVM(oldString)) {
improvement = true;
oldString = strVar.getConcreteValue();
}
if (checkpointDistance == 0.0) {
return true;
}
// try to add at the end
log.debug("Trying to add characters");
checkpointVar(DistanceEstimator.getDistance(cnstr));
// Finally add new characters at the end of the string
improved = true;
while (improved) {
if (isFinished()) {
throw new SolverTimeoutException();
}
improved = false;
char charToInsert = Randomness.nextChar();
String newStr = oldString + charToInsert;
strVar.setConcreteValue(newStr);
double newDist = DistanceEstimator.getDistance(cnstr);
log.debug("Adding: " + newStr + ": " + newDist);
if (distImpr(newDist)) {
improvement = true;
improved = true;
checkpointVar(newDist);
if (checkpointDistance == 0.0) {
log.debug("Search seems successful, stopping at " + checkpointDistance + "/" + newDist);
return true;
}
doCharacterAVM(newStr.length() - 1);
oldString = strVar.getConcreteValue();
} else {
restoreVar();
}
}
// try to insert delimiters (if any)
Set<StringValue> delimiters = getTokenDelimiters(cnstr);
for (StringValue delimiter : delimiters) {
if (isFinished()) {
throw new SolverTimeoutException();
}
improved = true;
String delimiterStr = (String) delimiter.accept(exprExecutor, null);
while (improved) {
if (isFinished()) {
throw new SolverTimeoutException();
}
improved = false;
char charToInsert = Randomness.nextChar();
String newStr = oldString + delimiterStr + charToInsert;
strVar.setConcreteValue(newStr);
double newDist = DistanceEstimator.getDistance(cnstr);
log.debug("Adding: " + newStr + ": " + newDist);
if (distImpr(newDist)) {
improvement = true;
improved = true;
checkpointVar(newDist);
if (checkpointDistance == 0.0) {
log.debug("Search seems successful, stopping at " + checkpointDistance + "/" + newDist);
return true;
}
doCharacterAVM(newStr.length() - 1);
oldString = strVar.getConcreteValue();
} else {
restoreVar();
}
}
}
return improvement;
}
use of org.evosuite.symbolic.solver.SolverTimeoutException in project evosuite by EvoSuite.
the class CVC4ResultParser method parse.
public SolverResult parse(String cvc4ResultStr) throws SolverParseException, SolverErrorException, SolverTimeoutException {
if (cvc4ResultStr.startsWith("sat")) {
logger.debug("CVC4 outcome was SAT");
SolverResult satResult = parseModel(cvc4ResultStr);
return satResult;
} else if (cvc4ResultStr.startsWith("unsat")) {
logger.debug("CVC4 outcome was UNSAT");
SolverResult unsatResult = SolverResult.newUNSAT();
return unsatResult;
} else if (cvc4ResultStr.startsWith("unknown")) {
logger.debug("CVC4 outcome was UNKNOWN (probably due to timeout)");
throw new SolverTimeoutException();
} else if (cvc4ResultStr.startsWith("(error")) {
logger.debug("CVC4 output was the following " + cvc4ResultStr);
throw new SolverErrorException("An error (probably an invalid input) occurred while executing CVC4");
} else {
logger.debug("The following CVC4 output could not be parsed " + cvc4ResultStr);
throw new SolverParseException("CVC4 output is unknown. We are unable to parse it to a proper solution!", cvc4ResultStr);
}
}
use of org.evosuite.symbolic.solver.SolverTimeoutException in project evosuite by EvoSuite.
the class TestConstraintSolver method testCase2.
@Test
public void testCase2() throws SecurityException, NoSuchMethodException, SolverEmptyQueryException {
DefaultTestCase tc = buildTestCase2();
// build patch condition
List<BranchCondition> branch_conditions = executeTest(tc);
assertEquals(57, branch_conditions.size());
// keep only 2 top-most branch conditions
List<BranchCondition> sublist = new ArrayList<BranchCondition>();
sublist.add(branch_conditions.get(0));
sublist.add(branch_conditions.get(1));
// invoke seeker
try {
SolverResult solverResult = executeSolver(sublist);
assertNotNull(solverResult);
assertTrue(solverResult.isSAT());
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.solver.SolverTimeoutException in project evosuite by EvoSuite.
the class TestConstraintSolver2 method test.
@Test
public void test() throws SolverEmptyQueryException {
// 5000000000000L; TODO - ??
Properties.LOCAL_SEARCH_BUDGET = 100;
Properties.LOCAL_SEARCH_BUDGET_TYPE = LocalSearchBudgetType.FITNESS_EVALUATIONS;
Collection<Constraint<?>> constraints = buildConstraintSystem();
System.out.println("Constraints:");
for (Constraint<?> c : constraints) {
System.out.println(c.toString());
}
System.out.println("");
System.out.println("Initial: " + INIT_STRING);
EvoSuiteSolver solver = new EvoSuiteSolver();
try {
SolverResult solverResult = solver.solve(constraints);
assertTrue(solverResult.isSAT());
Map<String, Object> model = solverResult.getModel();
assertNotNull(model);
Object var0 = model.get("var0");
System.out.println("Expected: " + EXPECTED_STRING);
System.out.println("Found: " + var0);
assertEquals(EXPECTED_STRING, var0);
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.solver.SolverTimeoutException in project evosuite by EvoSuite.
the class TestIntegerSearch method testEQVariable.
@Test
public void testEQVariable() throws SolverEmptyQueryException {
int var1 = 0;
int var2 = 1;
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
constraints.add(new IntegerConstraint(new IntegerVariable("test1", var1, -1000000, 1000000), Comparator.EQ, new IntegerVariable("test2", var2, -1000000, 1000000)));
try {
EvoSuiteSolver solver = new EvoSuiteSolver();
SolverResult solverResult = solver.solve(constraints);
assertTrue(solverResult.isSAT());
Map<String, Object> model = solverResult.getModel();
if (model.containsKey("test1"))
var1 = ((Number) model.get("test1")).intValue();
if (model.containsKey("test2"))
var2 = ((Number) model.get("test2")).intValue();
assertEquals(var1, var2);
} catch (SolverTimeoutException e) {
fail();
}
}
Aggregations