use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.
the class ComparisonTraceObserver 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) {
try {
Object object = var.getObject(scope);
if (object == null)
return;
if (statement instanceof AssignmentStatement)
return;
if (statement instanceof PrimitiveStatement<?>)
return;
ComparisonTraceEntry entry = new ComparisonTraceEntry(var);
int position = statement.getPosition();
for (VariableReference other : scope.getElements(var.getType())) {
Object otherObject = other.getObject(scope);
// TODO: Create a matrix of object comparisons?
if (otherObject == null)
// TODO: Don't do this?
continue;
if (object == otherObject)
// Don't compare with self?
continue;
int otherPos = other.getStPosition();
if (otherPos >= position)
// Don't compare with variables that are not defined - may happen with primitives?
continue;
Statement otherStatement = currentTest.getStatement(otherPos);
if (statement instanceof PrimitiveStatement && otherStatement instanceof PrimitiveStatement)
// Don't compare two primitives
continue;
if (otherStatement instanceof MethodStatement) {
if (((MethodStatement) otherStatement).getMethodName().equals("hashCode"))
// No comparison against hashCode, as the hashCode return value will not be in the test
continue;
}
if (Properties.PURE_EQUALS) {
String className = object.getClass().getCanonicalName();
String methodName = "equals";
String descriptor = Type.getMethodDescriptor(Type.BOOLEAN_TYPE, Type.getType(Object.class));
CheapPurityAnalyzer cheapPurityAnalyzer = CheapPurityAnalyzer.getInstance();
if (!cheapPurityAnalyzer.isPure(className, methodName, descriptor))
// Don't compare using impure equals(Object) methods
continue;
}
try {
logger.debug("Comparison of " + var + " with " + other + " is: " + object.equals(otherObject));
entry.addEntry(other, ComparisonTraceEntry.equals(object, otherObject));
} catch (Throwable t) {
logger.debug("Exception during equals: " + t);
// ignore?
}
if (object instanceof Comparable<?>) {
// TODO
}
}
trace.addEntry(statement.getPosition(), var, entry);
} catch (CodeUnderTestException e) {
logger.debug("", e);
}
}
use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.
the class ContainsTraceObserver 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) {
try {
Object object = var.getObject(scope);
if (object == null)
return;
if (statement instanceof AssignmentStatement)
return;
if (statement instanceof PrimitiveStatement<?>)
return;
// Only relevant for Collections
if (!(object instanceof Collection))
return;
Collection collectionObject = (Collection) object;
List<GenericClass> parameterClasses = var.getGenericClass().getParameterClasses();
// Need to know exact type
if (parameterClasses.size() != 1)
return;
java.lang.reflect.Type parameterType = parameterClasses.get(0).getType();
ContainsTraceEntry entry = new ContainsTraceEntry(var);
int position = statement.getPosition();
Set<VariableReference> otherVariables = new LinkedHashSet<>();
otherVariables.addAll(scope.getElements(parameterType));
for (int i = 0; i <= statement.getPosition(); i++) {
for (VariableReference candidateVar : currentTest.getStatement(i).getVariableReferences()) {
if (candidateVar instanceof ConstantValue && candidateVar.isAssignableTo(parameterType)) {
otherVariables.add(candidateVar);
}
}
}
for (VariableReference other : otherVariables) {
Object otherObject;
if (other instanceof ConstantValue)
otherObject = ((ConstantValue) other).getValue();
else
otherObject = other.getObject(scope);
if (otherObject == null)
// TODO: Don't do this?
continue;
int otherPos = other.getStPosition();
if (otherPos > position)
// Don't compare with variables that are not defined - may happen with primitives?
continue;
Statement otherStatement = currentTest.getStatement(otherPos);
if (otherStatement instanceof MethodStatement) {
if (((MethodStatement) otherStatement).getMethodName().equals("hashCode"))
// No comparison against hashCode, as the hashCode return value will not be in the test
continue;
}
try {
logger.debug("Checking whether {} contains {} is: {}", var, other, collectionObject.contains(otherObject));
entry.addEntry(other, collectionObject.contains(otherObject));
} catch (Throwable t) {
logger.debug("Exception during equals: " + t);
// ignore?
}
if (object instanceof Comparable<?>) {
// TODO
}
}
trace.addEntry(statement.getPosition(), var, entry);
} catch (CodeUnderTestException e) {
logger.debug("", e);
}
}
use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.
the class LocalSearchArraySystemTest method getArrayTest.
private TestCase getArrayTest(int length) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException {
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
TestFactory testFactory = TestFactory.getInstance();
VariableReference callee = testFactory.addConstructor(test, gc, 0, 0);
VariableReference arrayVar = test.addStatement(new ArrayStatement(test, int[].class, length));
for (int i = 0; i < length; i++) {
// Add value
VariableReference intVar = test.addStatement(new IntPrimitiveStatement(test, 0));
test.addStatement(new AssignmentStatement(test, new ArrayIndex(test, (ArrayReference) arrayVar, i), intVar));
}
Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int[].class });
GenericMethod method = new GenericMethod(m, sut);
MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { arrayVar }));
test.addStatement(ms);
return test;
}
use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.
the class TestCaseBuilder method appendAssignment.
public void appendAssignment(VariableReference receiver, Field field, VariableReference value) {
FieldReference fieldReference;
if (receiver == null) {
fieldReference = new FieldReference(tc, new GenericField(field, field.getDeclaringClass()));
} else {
fieldReference = new FieldReference(tc, new GenericField(field, receiver.getType()), receiver);
}
AssignmentStatement stmt = new AssignmentStatement(tc, fieldReference, value);
tc.addStatement(stmt);
}
use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.
the class TestCaseBuilder method appendAssignment.
/**
* var := array[index]
*
* @param var
* @param array
* @param index
*/
public void appendAssignment(VariableReference var, ArrayReference array, int index) {
ArrayIndex arrayIndex = new ArrayIndex(tc, array, index);
AssignmentStatement stmt = new AssignmentStatement(tc, var, arrayIndex);
tc.addStatement(stmt);
}
Aggregations