Search in sources :

Example 1 with AssignmentStatement

use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.

the class EvoTestCaseCodeGenerator method createFieldWriteAccessStmt.

@Override
public void createFieldWriteAccessStmt(CaptureLog log, int logRecNo) {
    // assumption: all necessary statements are created and there is one variable for each referenced object
    final Object[] methodArgs = log.params.get(logRecNo);
    final int oid = log.objectIds.get(logRecNo);
    final int captureId = log.captureIds.get(logRecNo);
    final String fieldName = log.getNameOfAccessedFields(captureId);
    final String typeName = log.getTypeName(oid);
    try {
        final Class<?> type = getClassForName(typeName);
        final String fieldDesc = log.descList.get(logRecNo);
        final Class<?> fieldType = CaptureUtil.getClassFromDesc(fieldDesc);
        final FieldReference targetFieldRef = new FieldReference(testCase, new GenericField(this.getDeclaredField(type, fieldName), type), this.oidToVarRefMap.get(oid));
        final AssignmentStatement assignment;
        final Integer arg = (Integer) methodArgs[0];
        if (arg == null) {
            final NullStatement nullStmt = new NullStatement(testCase, fieldType);
            final VariableReference nullReference = testCase.addStatement(nullStmt);
            assignment = new AssignmentStatement(testCase, targetFieldRef, nullReference);
        } else {
            assignment = new AssignmentStatement(testCase, targetFieldRef, this.oidToVarRefMap.get(arg));
        }
        final VariableReference varRef = testCase.addStatement(assignment);
        logger.debug("Adding assignment statement: " + assignment.getCode());
        if (arg != null) {
            this.oidToVarRefMap.put(arg, varRef);
        }
    } catch (final Exception e) {
        CodeGeneratorException.propagateError(e, "[logRecNo = %s] - an unexpected error occurred while creating field write access stmt. Log: %s", logRecNo, log);
    }
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) NullStatement(org.evosuite.testcase.statements.NullStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericField(org.evosuite.utils.generic.GenericField)

Example 2 with AssignmentStatement

use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.

the class TestCarvingExecutionObserver method afterStatement.

/**
 * own comment..
 */
@Override
public void afterStatement(final Statement statement, final Scope scope, final Throwable exception) {
    if (statement instanceof AssignmentStatement) {
        final AssignmentStatement assign = (AssignmentStatement) statement;
        final VariableReference left = assign.getReturnValue();
        if (left instanceof FieldReference) {
            final FieldReference fieldRef = (FieldReference) left;
            final GenericField field = fieldRef.getField();
            FieldRegistry.notifyModification(field.isStatic() ? null : scope.getObject(fieldRef.getSource()), this.captureId, Type.getInternalName(field.getDeclaringClass()), field.getName(), Type.getDescriptor(field.getField().getType()));
            // PUTFIELDRegistry creates PUTXXX as well as corresponding GETXXX statements
            this.captureId -= 2;
        }
    }
}
Also used : FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericField(org.evosuite.utils.generic.GenericField)

Example 3 with AssignmentStatement

use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.

the class DowncastTest method testFieldReferenceNeedsDowncast.

@Test
public void testFieldReferenceNeedsDowncast() throws NoSuchMethodException, NoSuchFieldException {
    TestCaseBuilder builder = new TestCaseBuilder();
    VariableReference var = builder.appendConstructor(DowncastExample.class.getConstructor());
    VariableReference num0 = builder.appendMethod(var, DowncastExample.class.getMethod("getAbstractFoo"));
    // This would be set during execution
    num0.setType(ConcreteSubclass.class);
    VariableReference bool0 = builder.appendBooleanPrimitive(true);
    DefaultTestCase test = builder.getDefaultTestCase();
    FieldReference fr = new FieldReference(test, new GenericField(ConcreteSubclass.class.getField("fieldInConcreteClass"), ConcreteSubclass.class), num0);
    AssignmentStatement statement = new AssignmentStatement(test, fr, bool0);
    test.addStatement(statement);
    test.removeDownCasts();
    System.out.println(test);
    FieldReference fr2 = (FieldReference) test.getStatement(3).getReturnValue();
    assertEquals(ConcreteSubclass.class, fr2.getSource().getVariableClass());
}
Also used : DowncastExample(com.examples.with.different.packagename.test.DowncastExample) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) ConcreteSubclass(com.examples.with.different.packagename.test.ConcreteSubclass) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 4 with AssignmentStatement

use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.

the class InputCoverageFitnessFunctionSystemTest method testInputCoverageClassWithField.

@Test
public void testInputCoverageClassWithField() throws NoSuchFieldException, NoSuchMethodException {
    Class<?> sut = ClassWithField.class;
    DefaultTestCase tc = new DefaultTestCase();
    // ClassWithField classWithField0 = new ClassWithField();
    GenericConstructor constructor = new GenericConstructor(sut.getConstructors()[0], sut);
    ConstructorStatement constructorStatement = new ConstructorStatement(tc, constructor, Arrays.asList(new VariableReference[] {}));
    VariableReference obj = tc.addStatement(constructorStatement);
    // classWithField0.testFoo(classWithField0.BOOLEAN_FIELD);
    FieldReference field = new FieldReference(tc, new GenericField(sut.getDeclaredField("BOOLEAN_FIELD"), sut), obj);
    Method m = sut.getMethod("testFoo", new Class<?>[] { Boolean.TYPE });
    GenericMethod gm = new GenericMethod(m, sut);
    tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
    // classWithField0.BOOLEAN_FIELD = false;
    VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, false));
    tc.addStatement(new AssignmentStatement(tc, field, boolRef));
    tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
    Properties.TARGET_CLASS = sut.getCanonicalName();
    Properties.JUNIT_TESTS = true;
    TestSuiteChromosome testSuite = new TestSuiteChromosome();
    testSuite.addTest(tc);
    FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(Properties.Criterion.INPUT);
    assertEquals("Should be 0.0", 0.0, ffunction.getFitness(testSuite), 0.0);
    assertEquals("Should be 1.0", 1.0, testSuite.getCoverage(ffunction), 0.0);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) ClassWithField(com.examples.with.different.packagename.coverage.ClassWithField) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 5 with AssignmentStatement

use of org.evosuite.testcase.statements.AssignmentStatement in project evosuite by EvoSuite.

the class ArrayLocalSearch method stripAssignments.

private int stripAssignments(ArrayStatement statement, TestChromosome test, LocalSearchObjective<TestChromosome> objective) {
    int difference = 0;
    ArrayReference arrRef = (ArrayReference) statement.getReturnValue();
    TestFactory factory = TestFactory.getInstance();
    for (int position = test.size() - 1; position > statement.getPosition(); position--) {
        logger.debug("Current delete position: " + position);
        if (test.getTestCase().getStatement(position) instanceof AssignmentStatement) {
            logger.debug("Is assignment statement");
            AssignmentStatement assignment = (AssignmentStatement) test.getTestCase().getStatement(position);
            Statement valueStatement = test.getTestCase().getStatement(assignment.getValue().getStPosition());
            if (assignment.getReturnValue().getAdditionalVariableReference() == arrRef) {
                int currentDelta = 0;
                int differenceDelta = 0;
                logger.debug("Assigns to target array. Checking if we can remove it without worsening fitness");
                backup(test);
                try {
                    factory.deleteStatement(test.getTestCase(), position);
                    if (valueStatement instanceof PrimitiveStatement || valueStatement instanceof NullStatement) {
                        if (!test.getTestCase().hasReferences(valueStatement.getReturnValue())) {
                            if (valueStatement.getPosition() < statement.getPosition())
                                differenceDelta = 1;
                            currentDelta = 1;
                            logger.debug("Deleting primitive statement assigned to this array at " + valueStatement.getPosition());
                            factory.deleteStatement(test.getTestCase(), valueStatement.getPosition());
                        }
                    }
                    if (!objective.hasNotWorsened(test)) {
                        logger.debug("Fitness has decreased, so restoring test");
                        restore(test);
                        currentDelta = 0;
                        differenceDelta = 0;
                    }
                } catch (ConstructionFailedException e) {
                    currentDelta = 0;
                    differenceDelta = 0;
                    restore(test);
                }
                position -= currentDelta;
                difference += differenceDelta;
            }
        }
    }
    return difference;
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestFactory(org.evosuite.testcase.TestFactory) Statement(org.evosuite.testcase.statements.Statement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

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