use of org.evosuite.symbolic.expr.Constraint in project evosuite by EvoSuite.
the class ConstraintNormalizer method createRealConstraint.
private static Constraint<?> createRealConstraint(IntegerConstraint c) {
if (c.getLeftOperand() instanceof RealComparison) {
RealComparison cmp = (RealComparison) c.getLeftOperand();
int value = ((Number) c.getRightOperand().getConcreteValue()).intValue();
Comparator op = c.getComparator();
Expression<Double> cmp_left = cmp.getLeftOperant();
Expression<Double> cmp_right = cmp.getRightOperant();
return createRealConstraint(cmp_left, op, cmp_right, value);
} else {
assert (c.getRightOperand() instanceof RealComparison);
RealComparison cmp = (RealComparison) c.getRightOperand();
Comparator op = c.getComparator();
Comparator swap_op = op.swap();
int value = ((Number) c.getLeftOperand().getConcreteValue()).intValue();
int swap_value = -value;
Expression<Double> cmp_left = cmp.getLeftOperant();
Expression<Double> cmp_right = cmp.getRightOperant();
return createRealConstraint(cmp_left, swap_op, cmp_right, swap_value);
}
}
use of org.evosuite.symbolic.expr.Constraint in project evosuite by EvoSuite.
the class RealAVM method afterCommaSearch.
/**
* Try to optimize the digits after the comma
*
* @param realVar
* @param cnstr
* @return
*/
private boolean afterCommaSearch(RealVariable realVar, Collection<Constraint<?>> cnstr) throws SolverTimeoutException {
boolean improvement = false;
// Assume that floats have 7 digits after comma and double 15. This is
// based on Flopsy
int maxPrecision = realVar.getMaxValue() > Float.MAX_VALUE ? 15 : 7;
for (int precision = 1; precision <= maxPrecision; precision++) {
chopOffPrecision(precision, maxPrecision == 7);
log.debug("Current precision: " + precision);
final double delta = Math.pow(10.0, -precision);
final double factor = 2;
if (doRealSearch(delta, factor))
improvement = true;
if (this.checkpointedDistance <= 0) {
break;
}
}
return improvement;
}
use of org.evosuite.symbolic.expr.Constraint in project evosuite by EvoSuite.
the class DSETestGenerator method buildQuery.
/**
* Creates a Solver query give a branch condition
*
* @param condition
* @return
*/
private List<Constraint<?>> buildQuery(PathCondition pc, int conditionIndex) {
// negate target branch condition
PathCondition negatedPathCondition = pc.negate(conditionIndex);
// get constraints for negated path condition
List<Constraint<?>> query = negatedPathCondition.getConstraints();
// Compute cone of influence reduction
List<Constraint<?>> simplified_query = reduce(query);
return simplified_query;
}
use of org.evosuite.symbolic.expr.Constraint in project evosuite by EvoSuite.
the class DeprecatedTestSuiteDSE method reduce.
/**
* Apply cone of influence reduction to constraints with respect to the last
* constraint in the list
*
* @param constraints
* @return
*/
private List<Constraint<?>> reduce(List<Constraint<?>> constraints) {
Constraint<?> target = constraints.get(constraints.size() - 1);
Set<Variable<?>> dependencies = getVariables(target);
LinkedList<Constraint<?>> coi = new LinkedList<Constraint<?>>();
if (dependencies.size() <= 0)
return coi;
coi.add(target);
for (int i = constraints.size() - 2; i >= 0; i--) {
Constraint<?> constraint = constraints.get(i);
Set<Variable<?>> variables = getVariables(constraint);
for (Variable<?> var : dependencies) {
if (variables.contains(var)) {
dependencies.addAll(variables);
coi.addFirst(constraint);
break;
}
}
}
return coi;
}
use of org.evosuite.symbolic.expr.Constraint in project evosuite by EvoSuite.
the class DeprecatedTestSuiteDSE method applyDSE0.
/**
* Attempt to negate individual branches until budget is used up, or there
* are no further branches to negate
*
* @param individual
*/
public boolean applyDSE0(TestSuiteChromosome individual) {
logger.info("[DSE] Current test suite: " + individual.toString());
boolean wasSuccess = false;
// expansion already happens as part of LS
// TestSuiteChromosome expandedTests = expandTestSuite(individual);
TestSuiteChromosome expandedTests = individual.clone();
createPathConstraints(expandedTests);
// fitness.getFitness(expandedTests);
double originalFitness = getFitness(individual);
while (hasNextBranchCondition() && !LocalSearchBudget.getInstance().isFinished()) {
logger.info("Branches remaining: " + unsolvedBranchConditions.size());
TestBranchPair next = getNextBranchCondition();
BranchCondition branch = next.branch;
List<BranchCondition> pathCondition = next.pathCondition;
List<Constraint<?>> reachingConstraints = new LinkedList<Constraint<?>>();
for (BranchCondition b : pathCondition) {
reachingConstraints.addAll(b.getSupportingConstraints());
if (b == branch) {
break;
}
reachingConstraints.add(b.getConstraint());
}
List<Constraint<?>> constraints = new LinkedList<Constraint<?>>();
TestCase newTest = negateCondition(new HashSet<Constraint<?>>(reachingConstraints), branch.getConstraint(), next.test.getTestCase());
if (newTest != null) {
logger.info("Found new test: " + newTest.toCode());
TestChromosome newTestChromosome = new TestChromosome();
newTestChromosome.setTestCase(newTest);
expandedTests.addTest(newTestChromosome);
if (Properties.DSE_KEEP_ALL_TESTS) {
updatePathConstraints(newTestChromosome);
calculateUncoveredBranches();
individual.addTest(newTest);
wasSuccess = true;
} else {
if (getFitness(expandedTests) < originalFitness) {
logger.info("New test improves fitness to {}", getFitness(expandedTests));
DSEStats.getInstance().reportNewTestUseful();
wasSuccess = true;
// no need to clone so we can keep executionresult
updatePathConstraints(newTestChromosome);
calculateUncoveredBranches(newTestChromosome);
individual.addTest(newTest);
originalFitness = getFitness(expandedTests);
// TODO: Cancel on fitness 0 - would need to know if
// ZeroFitness is a stopping condition
} else {
logger.info("New test does not improve fitness");
DSEStats.getInstance().reportNewTestUnuseful();
expandedTests.deleteTest(newTest);
}
}
success++;
} else {
unsolvableBranchConditions.add(branch);
failed++;
logger.info("Failed to find new test.");
}
}
logger.info("Finished DSE");
// Ensure fitness values are up to date.
getFitness(individual);
LocalSearchBudget.getInstance().countLocalSearchOnTestSuite();
return wasSuccess;
}
Aggregations