Search in sources :

Example 16 with ConstructorStatement

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

the class MethodCoverageTestFitness method getFitness.

/**
 * {@inheritDoc}
 *
 * Calculate fitness
 *
 * @param individual
 *            a {@link org.evosuite.testcase.ExecutableChromosome} object.
 * @param result
 *            a {@link org.evosuite.testcase.execution.ExecutionResult} object.
 * @return a double.
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    double fitness = 1.0;
    List<Integer> exceptionPositions = asSortedList(result.getPositionsWhereExceptionsWereThrown());
    for (Statement stmt : result.test) {
        if (!isValidPosition(exceptionPositions, stmt.getPosition())) {
            break;
        }
        if ((stmt instanceof MethodStatement || stmt instanceof ConstructorStatement)) {
            EntityWithParametersStatement ps = (EntityWithParametersStatement) stmt;
            String className = ps.getDeclaringClassName();
            String methodDesc = ps.getDescriptor();
            String methodName = ps.getMethodName() + methodDesc;
            if (this.className.equals(className) && this.methodName.equals(methodName)) {
                fitness = 0.0;
                break;
            }
        }
    }
    updateIndividual(this, individual, fitness);
    if (fitness == 0.0) {
        individual.getTestCase().addCoveredGoal(this);
    }
    if (Properties.TEST_ARCHIVE) {
        Archive.getArchiveInstance().updateArchive(this, individual, fitness);
    }
    return fitness;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement)

Example 17 with ConstructorStatement

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

the class MethodNoExceptionCoverageTestFitness method getFitness.

/**
 * {@inheritDoc}
 *
 * Calculate fitness
 *
 * @param individual
 *            a {@link org.evosuite.testcase.ExecutableChromosome} object.
 * @param result
 *            a {@link org.evosuite.testcase.execution.ExecutionResult} object.
 * @return a double.
 */
@Override
public double getFitness(TestChromosome individual, ExecutionResult result) {
    double fitness = 1.0;
    List<Integer> exceptionPositions = new ArrayList<Integer>();
    if (Properties.BREAK_ON_EXCEPTION) {
        // we consider only the first thrown exception
        if (!result.getPositionsWhereExceptionsWereThrown().isEmpty()) {
            int firstPosition = Collections.min(result.getPositionsWhereExceptionsWereThrown());
            exceptionPositions.add(firstPosition);
        }
    } else {
        // we consider all thrown exceptions (if any)
        exceptionPositions.addAll(result.getPositionsWhereExceptionsWereThrown());
    }
    for (Statement stmt : result.test) {
        if (exceptionPositions.contains(stmt.getPosition())) {
            if (Properties.BREAK_ON_EXCEPTION)
                // if we look at the first exception, then no need to iterate over the remaining statements
                break;
            else
                // otherwise we simple skip statements throwing an exception
                continue;
        }
        if ((stmt instanceof MethodStatement || stmt instanceof ConstructorStatement)) {
            EntityWithParametersStatement ps = (EntityWithParametersStatement) stmt;
            String className = ps.getDeclaringClassName();
            String methodDesc = ps.getDescriptor();
            String methodName = ps.getMethodName() + methodDesc;
            if (this.className.equals(className) && this.methodName.equals(methodName)) {
                fitness = 0.0;
                break;
            }
        }
    }
    updateIndividual(this, individual, fitness);
    if (fitness == 0.0) {
        individual.getTestCase().addCoveredGoal(this);
    }
    if (Properties.TEST_ARCHIVE) {
        Archive.getArchiveInstance().updateArchive(this, individual, fitness);
    }
    return fitness;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) EntityWithParametersStatement(org.evosuite.testcase.statements.EntityWithParametersStatement) ArrayList(java.util.ArrayList)

Example 18 with ConstructorStatement

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

the class ParameterLocalSearch method doSearch.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
	 */
/**
 * {@inheritDoc}
 */
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
    Statement stmt = test.getTestCase().getStatement(statement);
    backup(test, stmt);
    if (stmt instanceof MethodStatement) {
        return doSearch(test, (MethodStatement) stmt, objective);
    } else if (stmt instanceof ConstructorStatement) {
        return doSearch(test, (ConstructorStatement) stmt, objective);
    } else if (stmt instanceof FieldStatement) {
        return doSearch(test, (FieldStatement) stmt, objective);
    } else {
        return false;
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement)

Example 19 with ConstructorStatement

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

the class CoverageGoalTestNameGenerationStrategy method chooseRepresentativeGoal.

/**
 * Out of a set of multiple goals, select one that is representative.
 * Assumes that goals is not empty, and all items in goals have the same type
 * @param test
 * @param goals
 * @return
 */
private TestFitnessFunction chooseRepresentativeGoal(TestCase test, Collection<TestFitnessFunction> goals) {
    Map<String, Integer> methodToPosition = new LinkedHashMap<>();
    for (Statement st : test) {
        if (st instanceof MethodStatement) {
            MethodStatement ms = (MethodStatement) st;
            String name = ms.getMethodName() + ms.getDescriptor();
            methodToPosition.put(name, st.getPosition());
        } else if (st instanceof ConstructorStatement) {
            ConstructorStatement cs = (ConstructorStatement) st;
            String name = "<init>" + cs.getDescriptor();
            methodToPosition.put(name, st.getPosition());
        }
    }
    // Randomness.choice(goals);
    TestFitnessFunction chosenGoal = goals.iterator().next();
    int chosenPosition = -1;
    for (TestFitnessFunction goal : goals) {
        if (methodToPosition.containsKey(goal.getTargetMethod())) {
            int position = methodToPosition.get(goal.getTargetMethod());
            if (position >= chosenPosition) {
                chosenPosition = position;
                chosenGoal = goal;
            }
        }
    }
    return chosenGoal;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 20 with ConstructorStatement

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

the class EnvironmentDataSystemTest method testOnSpecificTest.

@Test
public void testOnSpecificTest() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
    Properties.TARGET_CLASS = DseBar.class.getCanonicalName();
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    Class<?> fooClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(DseFoo.class.getCanonicalName());
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    // String string0 = "baz5";
    VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));
    // DseFoo dseFoo0 = new DseFoo();
    GenericConstructor fooConstructor = new GenericConstructor(fooClass.getConstructors()[0], fooClass);
    ConstructorStatement fooConstructorStatement = new ConstructorStatement(test, fooConstructor, Arrays.asList(new VariableReference[] {}));
    VariableReference fooVar = test.addStatement(fooConstructorStatement);
    // String fileName = new String("/home/galeotti/README.txt")
    String path = "/home/galeotti/README.txt";
    EvoSuiteFile evosuiteFile = new EvoSuiteFile(path);
    FileNamePrimitiveStatement fileNameStmt = new FileNamePrimitiveStatement(test, evosuiteFile);
    test.addStatement(fileNameStmt);
    Method fooIncMethod = fooClass.getMethod("inc", new Class<?>[] {});
    GenericMethod incMethod = new GenericMethod(fooIncMethod, fooClass);
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    // DseBar dseBar0 = new DseBar(string0);
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    ConstructorStatement constructorStatement = new ConstructorStatement(test, gc, Arrays.asList(new VariableReference[] { stringVar }));
    VariableReference callee = test.addStatement(constructorStatement);
    // dseBar0.coverMe(dseFoo0);
    Method m = clazz.getRawClass().getMethod("coverMe", new Class<?>[] { fooClass });
    GenericMethod method = new GenericMethod(m, sut);
    MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { fooVar }));
    test.addStatement(ms);
    System.out.println(test);
    TestSuiteChromosome suite = new TestSuiteChromosome();
    BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
    BranchCoverageMap.getInstance().searchStarted(null);
    assertEquals(4.0, fitness.getFitness(suite), 0.1F);
    suite.addTest(test);
    assertEquals(1.0, fitness.getFitness(suite), 0.1F);
    System.out.println("Test suite: " + suite);
    Properties.CONCOLIC_TIMEOUT = Integer.MAX_VALUE;
    TestSuiteLocalSearch localSearch = TestSuiteLocalSearch.selectTestSuiteLocalSearch();
    LocalSearchObjective<TestSuiteChromosome> localObjective = new DefaultLocalSearchObjective<TestSuiteChromosome>();
    localObjective.addFitnessFunction(fitness);
    localSearch.doSearch(suite, localObjective);
    System.out.println("Fitness: " + fitness.getFitness(suite));
    System.out.println("Test suite: " + suite);
    assertEquals("Local search failed to cover class", 0.0, fitness.getFitness(suite), 0.1F);
    BranchCoverageMap.getInstance().searchFinished(null);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) FileNamePrimitiveStatement(org.evosuite.testcase.statements.environment.FileNamePrimitiveStatement) 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) DseBar(com.examples.with.different.packagename.localsearch.DseBar) TestSuiteLocalSearch(org.evosuite.testsuite.localsearch.TestSuiteLocalSearch) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) GenericClass(org.evosuite.utils.generic.GenericClass) EvoSuiteFile(org.evosuite.runtime.testdata.EvoSuiteFile) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) DseFoo(com.examples.with.different.packagename.localsearch.DseFoo) Test(org.junit.Test)

Aggregations

ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)36 MethodStatement (org.evosuite.testcase.statements.MethodStatement)22 VariableReference (org.evosuite.testcase.variable.VariableReference)22 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)19 GenericMethod (org.evosuite.utils.generic.GenericMethod)15 Method (java.lang.reflect.Method)14 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)13 Statement (org.evosuite.testcase.statements.Statement)10 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)9 GenericClass (org.evosuite.utils.generic.GenericClass)9 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)7 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)6 FlagExample1 (com.examples.with.different.packagename.FlagExample1)5 BranchCoverageFactory (org.evosuite.coverage.branch.BranchCoverageFactory)5 TestCase (org.evosuite.testcase.TestCase)4 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)4 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)3 DefUseCoverageFactory (org.evosuite.coverage.dataflow.DefUseCoverageFactory)3 DefUseCoverageSuiteFitness (org.evosuite.coverage.dataflow.DefUseCoverageSuiteFitness)3