Search in sources :

Example 6 with AssignmentStatement

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);
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement)

Example 7 with AssignmentStatement

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);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericClass(org.evosuite.utils.generic.GenericClass) Collection(java.util.Collection) ConstantValue(org.evosuite.testcase.variable.ConstantValue)

Example 8 with AssignmentStatement

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;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) GenericClass(org.evosuite.utils.generic.GenericClass) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestFactory(org.evosuite.testcase.TestFactory) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement)

Example 9 with AssignmentStatement

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);
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericField(org.evosuite.utils.generic.GenericField)

Example 10 with AssignmentStatement

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);
}
Also used : AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex)

Aggregations

AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)13 VariableReference (org.evosuite.testcase.variable.VariableReference)10 FieldReference (org.evosuite.testcase.variable.FieldReference)7 GenericField (org.evosuite.utils.generic.GenericField)6 MethodStatement (org.evosuite.testcase.statements.MethodStatement)5 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)3 NullStatement (org.evosuite.testcase.statements.NullStatement)3 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)3 Statement (org.evosuite.testcase.statements.Statement)3 ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)3 Test (org.junit.Test)3 DowncastExample (com.examples.with.different.packagename.test.DowncastExample)2 Method (java.lang.reflect.Method)2 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)2 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)2 TestFactory (org.evosuite.testcase.TestFactory)2 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)2 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)2 ArrayReference (org.evosuite.testcase.variable.ArrayReference)2 GenericClass (org.evosuite.utils.generic.GenericClass)2