use of org.evosuite.testcase.statements.PrimitiveStatement in project evosuite by EvoSuite.
the class EvoTestCaseCodeGenerator method createPlainInitStmt.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void createPlainInitStmt(CaptureLog log, int logRecNo) {
// NOTE: PLAIN INIT: has always one non-null param
// TODO: use primitives
final int oid = log.objectIds.get(logRecNo);
if (this.oidToVarRefMap.containsKey(oid)) {
// TODO this might happen because of Integer.valueOf(), for example. . Is this approach ok?
return;
}
final String type = log.getTypeName(oid);
final Object value = log.params.get(logRecNo)[0];
final VariableReference varRef;
if (value instanceof Class) {
// final PrimitiveStatement cps = ClassPrimitiveStatement.getPrimitiveStatement(testCase, getClassForName(type));
final PrimitiveStatement cps = new ClassPrimitiveStatement(testCase, getClassForName(type));
cps.setValue(value);
varRef = testCase.addStatement(cps);
} else {
final PrimitiveStatement primitiveValue = PrimitiveStatement.getPrimitiveStatement(testCase, getClassForName(type));
primitiveValue.setValue(value);
varRef = testCase.addStatement(primitiveValue);
}
this.oidToVarRefMap.put(oid, varRef);
}
use of org.evosuite.testcase.statements.PrimitiveStatement in project evosuite by EvoSuite.
the class ConcreteValueObserver method afterStatement.
/* (non-Javadoc)
* @see org.evosuite.testcase.ExecutionObserver#afterStatement(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, java.lang.Throwable)
*/
@Override
public void afterStatement(Statement statement, Scope scope, Throwable exception) {
int numStatement = statement.getPosition();
VariableReference returnValue = statement.getReturnValue();
if (!returnValue.isPrimitive()) {
// Only interested in primitive values
return;
}
TestCase test = super.getCurrentTest();
if (test.getStatement(returnValue.getStPosition()) instanceof PrimitiveStatement<?>) {
// Don't need to collect primitive statement values
return;
}
try {
Object object = statement.getReturnValue().getObject(scope);
concreteValues.put(numStatement, object);
} catch (CodeUnderTestException e) {
// Ignore
}
}
use of org.evosuite.testcase.statements.PrimitiveStatement in project evosuite by EvoSuite.
the class ConstantInliner method removeUnusedVariables.
/**
* Remove all unreferenced variables
*
* @param t
* The test case
* @return True if something was deleted
*/
public boolean removeUnusedVariables(TestCase t) {
List<Integer> toDelete = new ArrayList<Integer>();
boolean hasDeleted = false;
int num = 0;
for (Statement s : t) {
if (s instanceof PrimitiveStatement) {
VariableReference var = s.getReturnValue();
if (!t.hasReferences(var)) {
toDelete.add(num);
hasDeleted = true;
}
}
num++;
}
Collections.sort(toDelete, Collections.reverseOrder());
for (Integer position : toDelete) {
t.remove(position);
}
return hasDeleted;
}
use of org.evosuite.testcase.statements.PrimitiveStatement in project evosuite by EvoSuite.
the class BooleanLocalSearch method doSearch.
/* (non-Javadoc)
* @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
*/
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
PrimitiveStatement<Boolean> p = (PrimitiveStatement<Boolean>) test.getTestCase().getStatement(statement);
ExecutionResult oldResult = test.getLastExecutionResult();
oldValue = p.getValue();
p.setValue(!oldValue);
if (!objective.hasImproved(test)) {
// Restore original
p.setValue(oldValue);
test.setLastExecutionResult(oldResult);
test.setChanged(false);
return false;
} else {
return true;
}
}
use of org.evosuite.testcase.statements.PrimitiveStatement in project evosuite by EvoSuite.
the class LegacyInsertion method selectVariableForCall.
/**
* Randomly select one of the variables in the test defined up to position
* to insert a call for
*
* @param test
* @param position
* @return
*/
private VariableReference selectVariableForCall(TestCase test, int position) {
if (test.isEmpty() || position == 0)
return null;
double sum = 0.0;
for (int i = 0; i < position; i++) {
// sum += 1d / (10 * test.getStatement(i).getReturnValue().getDistance() + 1d);
sum += 1d / (test.getStatement(i).getReturnValue().getDistance() + 1d);
if (logger.isDebugEnabled()) {
logger.debug(test.getStatement(i).getCode() + ": Distance = " + test.getStatement(i).getReturnValue().getDistance());
}
}
double rnd = Randomness.nextDouble() * sum;
for (int i = 0; i < position; i++) {
double dist = 1d / (test.getStatement(i).getReturnValue().getDistance() + 1d);
if (dist >= rnd && !(test.getStatement(i).getReturnValue() instanceof NullReference) && !(test.getStatement(i).getReturnValue().isPrimitive()) && !(test.getStatement(i).getReturnValue().isVoid()) && !(test.getStatement(i) instanceof PrimitiveStatement))
return test.getStatement(i).getReturnValue();
else
rnd = rnd - dist;
}
if (position > 0)
position = Randomness.nextInt(position);
VariableReference var = test.getStatement(position).getReturnValue();
if (!(var instanceof NullReference) && !var.isVoid() && !(test.getStatement(position) instanceof PrimitiveStatement) && !var.isPrimitive())
return var;
else
return null;
}
Aggregations