Search in sources :

Example 1 with TestFactory

use of org.evosuite.testcase.TestFactory 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 2 with TestFactory

use of org.evosuite.testcase.TestFactory in project evosuite by EvoSuite.

the class FixedLengthTestChromosomeFactory method getRandomTestCase.

/**
 * Create a random individual
 *
 * @param size
 */
private TestCase getRandomTestCase(int size) {
    TestCase test = new DefaultTestCase();
    int num = 0;
    TestFactory testFactory = TestFactory.getInstance();
    // Then add random stuff
    while (test.size() < size && num < Properties.MAX_ATTEMPTS) {
        testFactory.insertRandomStatement(test, test.size() - 1);
        num++;
    }
    return test;
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFactory(org.evosuite.testcase.TestFactory) DefaultTestCase(org.evosuite.testcase.DefaultTestCase)

Example 3 with TestFactory

use of org.evosuite.testcase.TestFactory in project evosuite by EvoSuite.

the class ReferenceLocalSearch method replace.

/**
 * Replace the call with a completely different call
 *
 * @param test
 * @param statement
 * @return
 */
private boolean replace(TestChromosome test, int statement) {
    logger.debug("Replacing call");
    TestFactory factory = TestFactory.getInstance();
    Statement theStatement = test.getTestCase().getStatement(statement);
    VariableReference var = theStatement.getReturnValue();
    int oldLength = test.size();
    try {
        VariableReference replacement = null;
        if (Randomness.nextDouble() < Properties.NULL_PROBABILITY) {
            NullStatement nullStatement = new NullStatement(test.getTestCase(), var.getType());
            replacement = test.getTestCase().addStatement(nullStatement, statement);
        } else if (!var.isPrimitive()) {
            // Test cluster does not keep track of generators for primitives
            replacement = factory.createObject(test.getTestCase(), var.getType(), statement, 0, null);
        }
        if (replacement != null) {
            int oldStatement = statement + (test.size() - oldLength);
            for (int i = oldStatement + 1; i < test.size(); i++) {
                test.getTestCase().getStatement(i).replace(var, replacement);
            }
            factory.deleteStatement(test.getTestCase(), oldStatement);
            test.setChanged(true);
        }
    } catch (ConstructionFailedException e) {
        if (test.size() < oldLength) {
            restore(test);
        }
        test.setChanged(test.size() != oldLength);
    }
    return false;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) NullStatement(org.evosuite.testcase.statements.NullStatement) TestFactory(org.evosuite.testcase.TestFactory) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 4 with TestFactory

use of org.evosuite.testcase.TestFactory in project evosuite by EvoSuite.

the class RandomLengthTestFactory method getRandomTestCase.

/**
 * Create a random individual
 *
 * @param size
 */
private TestCase getRandomTestCase(int size) {
    boolean tracerEnabled = ExecutionTracer.isEnabled();
    if (tracerEnabled)
        ExecutionTracer.disable();
    TestCase test = getNewTestCase();
    int num = 0;
    // Choose a random length in 0 - size
    int length = Randomness.nextInt(size);
    while (length == 0) length = Randomness.nextInt(size);
    TestFactory testFactory = TestFactory.getInstance();
    // Then add random stuff
    while (test.size() < length && num < Properties.MAX_ATTEMPTS) {
        testFactory.insertRandomStatement(test, test.size() - 1);
        num++;
    }
    if (logger.isDebugEnabled())
        logger.debug("Randomized test case:" + test.toCode());
    if (tracerEnabled)
        ExecutionTracer.enable();
    return test;
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFactory(org.evosuite.testcase.TestFactory)

Example 5 with TestFactory

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

Aggregations

TestFactory (org.evosuite.testcase.TestFactory)13 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)8 VariableReference (org.evosuite.testcase.variable.VariableReference)7 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)6 MethodStatement (org.evosuite.testcase.statements.MethodStatement)6 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)5 Method (java.lang.reflect.Method)4 TestCase (org.evosuite.testcase.TestCase)4 GenericClass (org.evosuite.utils.generic.GenericClass)4 GenericMethod (org.evosuite.utils.generic.GenericMethod)4 NullStatement (org.evosuite.testcase.statements.NullStatement)3 Statement (org.evosuite.testcase.statements.Statement)3 TestChromosome (org.evosuite.testcase.TestChromosome)2 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)2 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)2 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)2 FieldStatement (org.evosuite.testcase.statements.FieldStatement)2 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1