use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class DeprecatedTestSuiteDSE method negateCondition.
/**
* Generate new constraint and ask solver for solution
*
* @param condition
* @param test
* @return
*/
// @SuppressWarnings("rawtypes")
// @SuppressWarnings("rawtypes")
@SuppressWarnings({ "unchecked", "rawtypes" })
private TestCase negateCondition(Set<Constraint<?>> reachingConstraints, Constraint<?> localConstraint, TestCase test) {
List<Constraint<?>> constraints = new LinkedList<Constraint<?>>();
constraints.addAll(reachingConstraints);
Constraint<?> targetConstraint = localConstraint.negate();
constraints.add(targetConstraint);
if (!targetConstraint.isSolveable()) {
logger.info("Found unsolvable constraint: " + targetConstraint);
// Could we treat this as a special case?
return null;
}
int size = constraints.size();
/*
* int counter = 0; for (Constraint cnstr : constraints) { logger.debug(
* "Cnstr " + (counter++) + " : " + cnstr + " dist: " +
* DistanceEstimator.getDistance(constraints)); }
*/
if (size > 0) {
logger.debug("Calculating cone of influence for " + size + " constraints");
constraints = reduce(constraints);
logger.info("Reduced constraints from " + size + " to " + constraints.size());
// for (Constraint<?> c : constraints) {
// logger.info(c.toString());
// }
}
nrCurrConstraints = constraints.size();
nrConstraints += nrCurrConstraints;
logger.info("Applying local search");
Solver solver = SolverFactory.getInstance().buildNewSolver();
DSEStats.getInstance().reportNewConstraints(constraints);
long startSolvingTime = System.currentTimeMillis();
SolverCache solverCache = SolverCache.getInstance();
SolverResult solverResult = solverCache.solve(solver, constraints);
long estimatedSolvingTime = System.currentTimeMillis() - startSolvingTime;
DSEStats.getInstance().reportNewSolvingTime(estimatedSolvingTime);
if (solverResult == null) {
logger.info("Found no solution");
/* Timeout, parseException, error, trivialSolution, etc. */
return null;
} else if (solverResult.isUNSAT()) {
logger.info("Found UNSAT solution");
DSEStats.getInstance().reportNewUNSAT();
return null;
} else {
Map<String, Object> model = solverResult.getModel();
DSEStats.getInstance().reportNewSAT();
TestCase newTest = test.clone();
for (Object key : model.keySet()) {
Object val = model.get(key);
if (val != null) {
logger.info("New value: " + key + ": " + val);
if (val instanceof Long) {
Long value = (Long) val;
String name = ((String) key).replace("__SYM", "");
// logger.warn("New long value for " + name + " is " +
// value);
PrimitiveStatement p = getStatement(newTest, name);
if (p.getValue().getClass().equals(Character.class))
p.setValue((char) value.intValue());
else if (p.getValue().getClass().equals(Long.class))
p.setValue(value);
else if (p.getValue().getClass().equals(Integer.class))
p.setValue(value.intValue());
else if (p.getValue().getClass().equals(Short.class))
p.setValue(value.shortValue());
else if (p.getValue().getClass().equals(Boolean.class))
p.setValue(value.intValue() > 0);
else if (p.getValue().getClass().equals(Byte.class))
p.setValue(value.byteValue() > 0);
else
logger.warn("New value is of an unsupported type: " + p.getValue().getClass() + val);
} else if (val instanceof String) {
String name = ((String) key).replace("__SYM", "");
PrimitiveStatement p = getStatement(newTest, name);
// val);
assert (p != null) : "Could not find variable " + name + " in test: " + newTest.toCode() + " / Orig test: " + test.toCode() + ", seed: " + Randomness.getSeed();
if (p.getValue().getClass().equals(Character.class))
p.setValue((char) Integer.parseInt(val.toString()));
else
p.setValue(val.toString());
} else if (val instanceof Double) {
Double value = (Double) val;
String name = ((String) key).replace("__SYM", "");
PrimitiveStatement p = getStatement(newTest, name);
// value);
assert (p != null) : "Could not find variable " + name + " in test: " + newTest.toCode() + " / Orig test: " + test.toCode() + ", seed: " + Randomness.getSeed();
if (p.getValue().getClass().equals(Double.class))
p.setValue(value);
else if (p.getValue().getClass().equals(Float.class))
p.setValue(value.floatValue());
else
logger.warn("New value is of an unsupported type: " + val);
} else {
logger.debug("New value is of an unsupported type: " + val);
}
} else {
logger.debug("New value is null");
}
}
return newTest;
}
}
use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class TestIntegerSearch method testNEConstant.
@Test
public void testNEConstant() throws SolverEmptyQueryException {
// TODO: Currently, the model returned by the search is null if the
// constraint is already satisfied,
// so in this example the concrete value has to be the target initially
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
constraints.add(new IntegerConstraint(new IntegerVariable("test1", 235082, -1000000, 1000000), Comparator.NE, new IntegerConstant(235082)));
try {
EvoSuiteSolver solver = new EvoSuiteSolver();
SolverResult solverResult = solver.solve(constraints);
assertTrue(solverResult.isSAT());
Map<String, Object> model = solverResult.getModel();
assertNotNull(model.get("test1"));
assertTrue(235082 != ((Number) model.get("test1")).intValue());
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class TestIntegerSearch method testLEArithmetic.
@Test
public void testLEArithmetic() throws SolverEmptyQueryException {
int var1 = 3;
int var2 = 1;
int var3 = 1;
assertTrue(var1 > var2 + var3);
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
constraints.add(new IntegerConstraint(new IntegerVariable("test1", var1, -1000000, 1000000), Comparator.LE, new IntegerBinaryExpression(new IntegerVariable("test2", var2, -1000000, 1000000), Operator.PLUS, new IntegerVariable("test3", var3, -1000000, 1000000), 0L)));
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();
if (model.containsKey("test3"))
var3 = ((Number) model.get("test3")).intValue();
assertTrue(var1 <= var2 + var3);
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class TestIntegerSearch method testGEArithmetic.
@Test
public void testGEArithmetic() throws SolverEmptyQueryException {
int var1 = 0;
int var2 = 1;
int var3 = 1;
assertTrue(var1 < var2 + var3);
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
constraints.add(new IntegerConstraint(new IntegerVariable("test1", var1, -1000000, 1000000), Comparator.GT, new IntegerBinaryExpression(new IntegerVariable("test2", var2, -1000000, 1000000), Operator.PLUS, new IntegerVariable("test3", var3, -1000000, 1000000), 0L)));
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();
if (model.containsKey("test3"))
var3 = ((Number) model.get("test3")).intValue();
assertTrue(var1 >= var2 + var3);
} catch (SolverTimeoutException e) {
fail();
}
}
use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.
the class TestIntegerSearch method testEQArithmetic.
@Test
public void testEQArithmetic() throws SolverEmptyQueryException {
int var1 = 0;
int var2 = 1;
int var3 = 1;
assertTrue(var1 != var2 + var3);
List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
constraints.add(new IntegerConstraint(new IntegerVariable("test1", var1, -1000000, 1000000), Comparator.EQ, new IntegerBinaryExpression(new IntegerVariable("test2", var2, -1000000, 1000000), Operator.PLUS, new IntegerVariable("test3", var3, -1000000, 1000000), 0L)));
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();
if (model.containsKey("test3"))
var3 = ((Number) model.get("test3")).intValue();
assertTrue(var1 == var2 + var3);
} catch (SolverTimeoutException e) {
fail();
}
}
Aggregations