Search in sources :

Example 11 with ArrayReference

use of org.evosuite.testcase.variable.ArrayReference 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)

Example 12 with ArrayReference

use of org.evosuite.testcase.variable.ArrayReference in project evosuite by EvoSuite.

the class SymbolicObserverTest method build_test_input_9.

private static DefaultTestCase build_test_input_9() throws SecurityException, NoSuchMethodException, NoSuchFieldException {
    test_input9();
    Method checkEquals = Assertions.class.getMethod("checkEquals", int.class, int.class);
    TestCaseBuilder tc = new TestCaseBuilder();
    ArrayReference intArray0 = tc.appendArrayStmt(int[].class, 10);
    tc.appendArrayStmt(double[].class, 11);
    tc.appendArrayStmt(String[].class, 12);
    tc.appendArrayStmt(int[].class, 3, 3);
    VariableReference int0 = tc.appendIntPrimitive(Integer.MAX_VALUE);
    tc.appendAssignment(intArray0, 1, int0);
    VariableReference int1 = tc.appendIntPrimitive(Integer.MIN_VALUE);
    tc.appendAssignment(int1, intArray0, 1);
    tc.appendMethod(null, checkEquals, int0, int1);
    return tc.getDefaultTestCase();
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) Method(java.lang.reflect.Method)

Example 13 with ArrayReference

use of org.evosuite.testcase.variable.ArrayReference in project evosuite by EvoSuite.

the class TestCaseBuilder method appendArrayStmt.

public ArrayReference appendArrayStmt(Type type, int... length) {
    ArrayStatement arrayStmt = new ArrayStatement(tc, type, length);
    tc.addStatement(arrayStmt);
    return (ArrayReference) arrayStmt.getReturnValue();
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement)

Example 14 with ArrayReference

use of org.evosuite.testcase.variable.ArrayReference in project evosuite by EvoSuite.

the class TestDefaultValue method testShort.

@Test
public void testShort() throws SecurityException, NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    ArrayReference integerArray0 = builder.appendArrayStmt(Short[].class, 10);
    VariableReference integer0 = builder.appendNull(Short.class);
    builder.appendAssignment(integerArray0, 0, integer0);
    builder.appendAssignment(integer0, integerArray0, 0);
    builder.appendMethod(integer0, Short.class.getMethod("toString"));
    DefaultTestCase tc = builder.getDefaultTestCase();
    ExecutionResult ret_val = TestCaseExecutor.runTest(tc);
    assertNotNull(ret_val);
    assertFalse(ret_val.explicitExceptions.isEmpty());
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 15 with ArrayReference

use of org.evosuite.testcase.variable.ArrayReference in project evosuite by EvoSuite.

the class TestDefaultValue method testInteger.

@Test
public void testInteger() throws SecurityException, NoSuchMethodException {
    TestCaseBuilder builder = new TestCaseBuilder();
    ArrayReference integerArray0 = builder.appendArrayStmt(Integer[].class, 10);
    VariableReference integer0 = builder.appendNull(Integer.class);
    builder.appendAssignment(integerArray0, 0, integer0);
    builder.appendAssignment(integer0, integerArray0, 0);
    builder.appendMethod(integer0, Integer.class.getMethod("toString"));
    DefaultTestCase tc = builder.getDefaultTestCase();
    ExecutionResult ret_val = TestCaseExecutor.runTest(tc);
    assertNotNull(ret_val);
    assertFalse(ret_val.explicitExceptions.isEmpty());
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Aggregations

ArrayReference (org.evosuite.testcase.variable.ArrayReference)20 VariableReference (org.evosuite.testcase.variable.VariableReference)15 Test (org.junit.Test)10 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)8 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)7 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)7 ArrayIndex (org.evosuite.testcase.variable.ArrayIndex)6 ArrayList (java.util.ArrayList)3 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)3 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)3 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)3 GenericMethod (org.evosuite.utils.generic.GenericMethod)3 ReferenceExpression (org.evosuite.symbolic.expr.ref.ReferenceExpression)2 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)2 NullStatement (org.evosuite.testcase.statements.NullStatement)2 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)2 ObjectParameter (com.examples.with.different.packagename.ObjectParameter)1 TrivialInt (com.examples.with.different.packagename.TrivialInt)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1