use of org.evosuite.testcase.statements.PrimitiveStatement in project evosuite by EvoSuite.
the class AbstractMOSA method removeUnusedVariables.
/**
* When a test case is changed via crossover and/or mutation, it can contains some
* primitive variables that are not used as input (or to store the output) of method calls.
* Thus, this method removes all these "trash" statements.
* @param chromosome
* @return true or false depending on whether "unused variables" are removed
*/
public boolean removeUnusedVariables(T chromosome) {
int sizeBefore = chromosome.size();
TestCase t = ((TestChromosome) chromosome).getTestCase();
List<Integer> to_delete = new ArrayList<Integer>(chromosome.size());
boolean has_deleted = false;
int num = 0;
for (Statement s : t) {
VariableReference var = s.getReturnValue();
boolean delete = false;
delete = delete || s instanceof PrimitiveStatement;
delete = delete || s instanceof ArrayStatement;
delete = delete || s instanceof StringPrimitiveStatement;
if (!t.hasReferences(var) && delete) {
to_delete.add(num);
has_deleted = true;
}
num++;
}
Collections.sort(to_delete, Collections.reverseOrder());
for (Integer position : to_delete) {
t.remove(position);
}
int sizeAfter = chromosome.size();
if (has_deleted)
logger.debug("Removed {} unused statements", (sizeBefore - sizeAfter));
return has_deleted;
}
use of org.evosuite.testcase.statements.PrimitiveStatement 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.testcase.statements.PrimitiveStatement in project evosuite by EvoSuite.
the class SameTraceObserver method visit.
/* (non-Javadoc)
* @see org.evosuite.assertion.AssertionTraceObserver#visit(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, org.evosuite.testcase.VariableReference)
*/
/**
* {@inheritDoc}
*/
@Override
protected void visit(Statement statement, Scope scope, VariableReference var) {
// TODO: Only MethodStatement?
if (statement.isAssignmentStatement())
return;
if (statement instanceof PrimitiveStatement<?>)
return;
if (statement instanceof ArrayStatement)
return;
if (statement instanceof ConstructorStatement)
return;
try {
Object object = var.getObject(scope);
if (object == null)
return;
if (var.isPrimitive())
return;
if (var.isString() && Properties.INLINE)
// After inlining the value of assertions would be different
return;
SameTraceEntry entry = new SameTraceEntry(var);
for (VariableReference other : scope.getElements(var.getType())) {
if (other == var)
continue;
if (other.isPrimitive())
continue;
if (other.isWrapperType())
// Issues with inlining resulting in unstable assertions
continue;
Object otherObject = other.getObject(scope);
if (otherObject == null)
continue;
if (otherObject.getClass() != object.getClass())
continue;
try {
logger.debug("Comparison of {} with {}", var, other);
entry.addEntry(other, object == otherObject);
} catch (Throwable t) {
logger.debug("Exception during equals: " + t);
// ignore?
}
}
trace.addEntry(statement.getPosition(), var, entry);
} catch (CodeUnderTestException e) {
logger.debug("", e);
}
}
use of org.evosuite.testcase.statements.PrimitiveStatement in project evosuite by EvoSuite.
the class DSETestGenerator method updateTest.
@SuppressWarnings({ "rawtypes", "unchecked" })
private TestCase updateTest(TestCase test, Map<String, Object> values) {
TestCase newTest = test.clone();
for (Object key : values.keySet()) {
Object val = values.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.testcase.statements.PrimitiveStatement in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createUnobservedInitStmt.
@SuppressWarnings({ "rawtypes" })
@Override
public void createUnobservedInitStmt(CaptureLog log, int logRecNo) {
// NOTE: PLAIN INIT: has always one non-null param
// TODO: use primitives
final int oid = log.objectIds.get(logRecNo);
try {
final Object value = log.params.get(logRecNo)[0];
final PrimitiveStatement stringRep = new ImmutableStringPrimitiveStatement(testCase, (String) value);
final VariableReference stringRepRef = testCase.addStatement(stringRep);
final MethodStatement m = new MethodStatement(testCase, new GenericMethod(EvoSuiteXStream.class.getMethod("fromString", new Class<?>[] { String.class }), EvoSuiteXStream.class), null, Arrays.asList(stringRepRef));
this.oidToVarRefMap.put(oid, testCase.addStatement(m));
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
Aggregations