use of org.evosuite.symbolic.expr.bv.IntegerVariable in project evosuite by EvoSuite.
the class Z3Solver method buildSmtQuery.
private static SmtCheckSatQuery buildSmtQuery(Collection<Constraint<?>> constraints, Set<Variable<?>> variables) {
List<SmtConstantDeclaration> constantDeclarations = new LinkedList<SmtConstantDeclaration>();
for (Variable<?> v : variables) {
String varName = v.getName();
if (v instanceof IntegerVariable) {
SmtConstantDeclaration intVar = SmtExprBuilder.mkIntConstantDeclaration(varName);
constantDeclarations.add(intVar);
} else if (v instanceof RealVariable) {
SmtConstantDeclaration realVar = SmtExprBuilder.mkRealConstantDeclaration(varName);
constantDeclarations.add(realVar);
} else if (v instanceof StringVariable) {
// ignore string variables
} else {
throw new RuntimeException("Unknown variable type " + v.getClass().getCanonicalName());
}
}
List<SmtAssertion> assertions = new LinkedList<SmtAssertion>();
for (Constraint<?> c : constraints) {
ConstraintToZ3Visitor v = new ConstraintToZ3Visitor();
SmtExpr bool_expr = c.accept(v, null);
if (bool_expr != null && bool_expr.isSymbolic()) {
SmtAssertion newAssertion = new SmtAssertion(bool_expr);
assertions.add(newAssertion);
}
}
SmtCheckSatQuery smtCheckSatQuery = new SmtCheckSatQuery(constantDeclarations, assertions);
return smtCheckSatQuery;
}
use of org.evosuite.symbolic.expr.bv.IntegerVariable in project evosuite by EvoSuite.
the class EvoSuiteSolver method solve.
@Override
public SolverResult solve(Collection<Constraint<?>> constraints) throws SolverTimeoutException, SolverEmptyQueryException {
long timeout = Properties.DSE_CONSTRAINT_SOLVER_TIMEOUT_MILLIS;
long startTimeMillis = System.currentTimeMillis();
Set<Variable<?>> variables = getVariables(constraints);
Map<String, Object> initialValues = getConcreteValues(variables);
double distance = DistanceEstimator.getDistance(constraints);
if (distance == 0.0) {
log.info("Initial distance already is 0.0, skipping search");
SolverResult satResult = SolverResult.newSAT(initialValues);
return satResult;
}
for (int attempt = 0; attempt <= Properties.DSE_VARIABLE_RESETS; attempt++) {
for (Variable<?> v : variables) {
long currentTimeMillis = System.currentTimeMillis();
long elapsed_solving_time = currentTimeMillis - startTimeMillis;
if (elapsed_solving_time > timeout) {
throw new SolverTimeoutException();
}
log.debug("Variable: " + v + ", " + variables);
if (v instanceof IntegerVariable) {
IntegerVariable integerVariable = (IntegerVariable) v;
IntegerAVM avm = new IntegerAVM(integerVariable, constraints, startTimeMillis, timeout);
avm.applyAVM();
} else if (v instanceof RealVariable) {
RealVariable realVariable = (RealVariable) v;
RealAVM avm = new RealAVM(realVariable, constraints, startTimeMillis, timeout);
avm.applyAVM();
} else if (v instanceof StringVariable) {
StringVariable strVariable = (StringVariable) v;
StringAVM avm = new StringAVM(strVariable, constraints, startTimeMillis, timeout);
avm.applyAVM();
} else {
throw new RuntimeException("Unknown variable type " + v.getClass().getName());
}
distance = DistanceEstimator.getDistance(constraints);
if (distance <= 0.0) {
log.info("Distance is 0, ending search");
break;
}
}
if (distance <= 0.0) {
log.info("Distance is 0, ending search");
break;
} else {
log.info("Randomizing variables");
randomizeValues(variables, getConstants(constraints));
}
}
// distance = DistanceEstimator.getDistance(constraints);
if (distance <= 0) {
log.debug("Distance is " + distance + ", found solution");
Map<String, Object> new_model = getConcreteValues(variables);
setConcreteValues(variables, initialValues);
SolverResult satResult = SolverResult.newSAT(new_model);
return satResult;
} else {
setConcreteValues(variables, initialValues);
log.debug("Returning null, search was not successful");
SolverResult unsatResult = SolverResult.newUNSAT();
return unsatResult;
}
}
use of org.evosuite.symbolic.expr.bv.IntegerVariable in project evosuite by EvoSuite.
the class EvoSuiteSolver method randomizeValues.
private static void randomizeValues(Set<Variable<?>> variables, Set<Object> constants) {
Set<String> stringConstants = new HashSet<String>();
Set<Long> longConstants = new HashSet<Long>();
Set<Double> realConstants = new HashSet<Double>();
for (Object o : constants) {
if (o instanceof String)
stringConstants.add((String) o);
else if (o instanceof Double)
realConstants.add((Double) o);
else if (o instanceof Long)
longConstants.add((Long) o);
else
assert (false) : "Unexpected constant type: " + o;
}
for (Variable<?> v : variables) {
if (v instanceof StringVariable) {
StringVariable sv = (StringVariable) v;
if (!stringConstants.isEmpty() && Randomness.nextDouble() < Properties.DSE_CONSTANT_PROBABILITY) {
sv.setConcreteValue(Randomness.choice(stringConstants));
} else {
sv.setConcreteValue(Randomness.nextString(Properties.STRING_LENGTH));
}
} else if (v instanceof IntegerVariable) {
IntegerVariable iv = (IntegerVariable) v;
if (!longConstants.isEmpty() && Randomness.nextDouble() < Properties.DSE_CONSTANT_PROBABILITY) {
iv.setConcreteValue(Randomness.choice(longConstants));
} else {
iv.setConcreteValue((long) Randomness.nextInt(Properties.MAX_INT * 2) - Properties.MAX_INT);
}
} else if (v instanceof RealVariable) {
RealVariable rv = (RealVariable) v;
if (!realConstants.isEmpty() && Randomness.nextDouble() < Properties.DSE_CONSTANT_PROBABILITY) {
rv.setConcreteValue(Randomness.choice(realConstants));
} else {
rv.setConcreteValue((long) Randomness.nextInt(Properties.MAX_INT * 2) - Properties.MAX_INT);
}
}
}
}
use of org.evosuite.symbolic.expr.bv.IntegerVariable in project evosuite by EvoSuite.
the class SymbolicObserver method after.
private void after(BytePrimitiveStatement statement, Scope scope) {
byte valueOf = statement.getValue();
VariableReference varRef = statement.getReturnValue();
String varRefName = varRef.getName();
IntegerVariable integerVariable = buildIntegerVariable(varRefName, valueOf, Byte.MIN_VALUE, Byte.MAX_VALUE);
symb_expressions.put(varRefName, integerVariable);
Byte byte_instance;
try {
byte_instance = (Byte) varRef.getObject(scope);
} catch (CodeUnderTestException e) {
throw new EvosuiteError(e);
}
ReferenceConstant byteRef = newByteReference(byte_instance, integerVariable);
symb_references.put(varRefName, byteRef);
}
use of org.evosuite.symbolic.expr.bv.IntegerVariable in project evosuite by EvoSuite.
the class SymbolicObserver method after.
private void after(LongPrimitiveStatement statement, Scope scope) {
long valueOf = statement.getValue();
VariableReference varRef = statement.getReturnValue();
String varRefName = varRef.getName();
IntegerVariable integerVariable = buildIntegerVariable(varRefName, valueOf, Long.MIN_VALUE, Long.MAX_VALUE);
symb_expressions.put(varRefName, integerVariable);
Long long_instance;
try {
long_instance = (Long) varRef.getObject(scope);
} catch (CodeUnderTestException e) {
throw new EvosuiteError(e);
}
ReferenceConstant longRef = newLongReference(long_instance, integerVariable);
symb_references.put(varRefName, longRef);
}
Aggregations