use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class TestIntegerSearch method testLTVariable.
@Test
public void testLTVariable() throws SolverEmptyQueryException {
int var1 = 2;
int var2 = 1;
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
constraints.add(new IntegerConstraint(new IntegerVariable("test1", var1, -1000000, 1000000), Comparator.LT, 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();
assertTrue(var1 < var2);
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class TestRealConstraint 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: " + String.valueOf(INIT_DOUBLE));
EvoSuiteSolver seeker = new EvoSuiteSolver();
try {
SolverResult result = seeker.solve(constraints);
if (result.isUNSAT()) {
fail("search was unsuccessfull");
} else {
Map<String, Object> model = result.getModel();
Object var0 = model.get("var0");
System.out.println("Expected: " + EXPECTED_DOUBLE);
System.out.println("Found: " + var0);
assertEquals(EXPECTED_DOUBLE, var0);
}
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class TestConstraintSolver1 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());
}
EvoSuiteSolver seeker = new EvoSuiteSolver();
try {
SolverResult solverResult = seeker.solve(constraints);
assertTrue(solverResult.isSAT());
Map<String, Object> model = solverResult.getModel();
System.out.println(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.SolverResult in project evosuite by EvoSuite.
the class TestConstraintSolver3 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 result = solver.solve(constraints);
if (result.isUNSAT()) {
fail("search was unsuccessfull");
} else {
Map<String, Object> model = result.getModel();
Object var0 = model.get("var0");
System.out.println("Expected: " + EXPECTED_INTEGER);
System.out.println("Found: " + var0);
assertEquals(String.valueOf(EXPECTED_INTEGER), var0);
}
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class DSETestGenerator method generateNewTest.
/**
* Applies DSE to the passed test using as symbolic variables only those
* that are declared in the set of statement indexes. The objective is used
* to detect if the DSE has improved the fitness.
*
* @param test
* the test case to be used as parameterised unit test
*
* @param statementIndexes
* a set with statement indexes with primitive value declarations
* that can be used as symbolic variables. This set must be
* non-empty.
*
* @param objective
* the local search objective to measure fitness improvement.
*/
public TestChromosome generateNewTest(final TestChromosome test, Set<Integer> statementIndexes, LocalSearchObjective<TestChromosome> objective) {
logger.info("APPLYING DSE EEEEEEEEEEEEEEEEEEEEEEE");
logger.info(test.getTestCase().toCode());
logger.info("Starting concolic execution");
// Backup copy
// test.getMutationHistory().clear();
// I am not sure what is the purpose of this
test.clone();
DefaultTestCase clone_test_case = (DefaultTestCase) test.getTestCase().clone();
List<BranchCondition> branchConditions = ConcolicExecution.executeConcolic(clone_test_case);
final PathCondition collectedPathCondition = new PathCondition(branchConditions);
logger.info("Done concolic execution");
if (collectedPathCondition.isEmpty()) {
return null;
}
for (BranchCondition c : collectedPathCondition.getBranchConditions()) {
logger.info(" -> " + c.getConstraint());
}
Set<VariableReference> symbolicVariables = new HashSet<VariableReference>();
for (Integer position : statementIndexes) {
final VariableReference variableReference = test.getTestCase().getStatement(position).getReturnValue();
symbolicVariables.add(variableReference);
}
logger.info("Checking {} conditions", collectedPathCondition.size());
List<Integer> conditionIndexesNotCoveredTwoWays = computeConditionIndexesNotCoveredTwoWays(test, collectedPathCondition);
//
for (int conditionIndex = 0; conditionIndex < collectedPathCondition.size(); conditionIndex++) {
BranchCondition condition = collectedPathCondition.get(conditionIndex);
if (LocalSearchBudget.getInstance().isFinished()) {
logger.debug("Local search budget used up: " + Properties.LOCAL_SEARCH_BUDGET_TYPE);
break;
}
logger.debug("Local search budget not yet used up");
if (!conditionIndexesNotCoveredTwoWays.contains(conditionIndex)) {
// skip branches covered two ways
continue;
}
logger.info("Current condition: " + conditionIndex + "/" + collectedPathCondition.size() + ": " + condition.getConstraint());
// Determine if this a branch condition depending on the target
// statement
Constraint<?> currentConstraint = condition.getConstraint();
if (!isRelevant(currentConstraint, symbolicVariables)) {
// if(!isRelevant(currentConstraint, test.getTestCase(),
// statement)) {
logger.info("Is not relevant for " + symbolicVariables);
continue;
}
logger.info("Is relevant for " + symbolicVariables);
List<Constraint<?>> query = buildQuery(collectedPathCondition, conditionIndex);
logger.info("Trying to solve: ");
for (Constraint<?> c : query) {
logger.info(" " + c);
}
DSEStats.getInstance().reportNewConstraints(query);
// Get solution
Solver solver = SolverFactory.getInstance().buildNewSolver();
long startSolvingTime = System.currentTimeMillis();
SolverCache solverCache = SolverCache.getInstance();
SolverResult solverResult = solverCache.solve(solver, query);
long estimatedSolvingTime = System.currentTimeMillis() - startSolvingTime;
DSEStats.getInstance().reportNewSolvingTime(estimatedSolvingTime);
if (solverResult == null) {
logger.info("Found no result");
} else if (solverResult.isUNSAT()) {
logger.info("Found UNSAT result");
DSEStats.getInstance().reportNewUNSAT();
} else {
logger.info("Found SAT result");
DSEStats.getInstance().reportNewSAT();
Map<String, Object> model = solverResult.getModel();
TestCase oldTest = test.getTestCase();
ExecutionResult oldResult = test.getLastExecutionResult().clone();
TestCase newTest = updateTest(oldTest, model);
logger.info("New test: " + newTest.toCode());
test.setTestCase(newTest);
// test.clearCachedMutationResults(); // TODO Mutation
test.clearCachedResults();
if (objective.hasImproved(test)) {
DSEStats.getInstance().reportNewTestUseful();
logger.info("Solution improves fitness, finishing DSE");
/* new test was created */
return test;
} else {
DSEStats.getInstance().reportNewTestUnuseful();
test.setTestCase(oldTest);
// FIXXME: How can this be null?
if (oldResult != null)
test.setLastExecutionResult(oldResult);
// TODO Mutation
}
}
}
/* no new test was created */
return null;
}
Aggregations