Search in sources :

Example 21 with TestFitnessFunction

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

the class TestMethodNamingComplexExamples method testConstructors.

@Test
public void testConstructors() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
    // method goal
    TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>(LFooClass;)V");
    // exception goal
    TestFitnessFunction goal2 = new ExceptionCoverageTestFitness("FooClass", "<init>(LFooClass;)V", ArrayIndexOutOfBoundsException.class, ExceptionCoverageTestFitness.ExceptionType.IMPLICIT);
    DefaultTestCase test1 = new DefaultTestCase();
    test1.addCoveredGoal(goal1);
    DefaultTestCase test2 = new DefaultTestCase();
    // Need to add statements to change hashCode
    test2.addStatement(new IntPrimitiveStatement(test2, 2));
    test2.addCoveredGoal(goal1);
    test2.addCoveredGoal(goal2);
    ArrayList<TestCase> testCases = new ArrayList<>();
    testCases.add(test1);
    testCases.add(test2);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
    String nameTest1 = naming.getName(test1);
    String nameTest2 = naming.getName(test2);
    assertEquals("Generated test name differs from expected", "testCreatesFooClass", nameTest1);
    assertEquals("Generated test name differs from expected", "testFailsToCreateFooClassThrowsArrayIndexOutOfBoundsException", nameTest2);
}
Also used : ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 22 with TestFitnessFunction

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

the class TestMethodNamingComplexExamples method testOverloadedMethods.

@Test
public void testOverloadedMethods() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
    // Method goals
    TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>(LFooClass;)V");
    TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "values([B)[B");
    TestFitnessFunction goal3 = new MethodCoverageTestFitness("FooClass", "values([I)[B");
    // Output goals for method values
    TestFitnessFunction goal4 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values([B)", Type.getType("[B"), ARRAY_EMPTY));
    TestFitnessFunction goal5 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values([I)", Type.getType("[B"), ARRAY_NONEMPTY));
    DefaultTestCase test1 = new DefaultTestCase();
    test1.addCoveredGoal(goal1);
    test1.addCoveredGoal(goal2);
    test1.addCoveredGoal(goal4);
    DefaultTestCase test2 = new DefaultTestCase();
    // Need to add statements to change hashCode
    test2.addStatement(new IntPrimitiveStatement(test2, 2));
    test2.addCoveredGoal(goal1);
    test2.addCoveredGoal(goal3);
    test2.addCoveredGoal(goal5);
    ArrayList<TestCase> testCases = new ArrayList<>();
    testCases.add(test1);
    testCases.add(test2);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
    String nameTest1 = naming.getName(test1);
    String nameTest2 = naming.getName(test2);
    assertEquals("Generated test name differs from expected", "testValuesTakingByteArray", nameTest1);
    assertEquals("Generated test name differs from expected", "testValuesTakingIntArray", nameTest2);
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) ArrayList(java.util.ArrayList) OutputCoverageTestFitness(org.evosuite.coverage.io.output.OutputCoverageTestFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) OutputCoverageGoal(org.evosuite.coverage.io.output.OutputCoverageGoal) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 23 with TestFitnessFunction

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

the class CoverageAnalysis method analyzeCoverageCriterion.

private static void analyzeCoverageCriterion(List<JUnitResult> results, Properties.Criterion criterion) {
    logger.info("analysing coverage of " + criterion);
    // Factory
    TestFitnessFactory<? extends TestFitnessFunction> factory = FitnessFunctions.getFitnessFactory(criterion);
    // Goals
    List<?> goals = null;
    if (criterion == Criterion.MUTATION || criterion == Criterion.STRONGMUTATION) {
        goals = MutationPool.getMutants();
    } else {
        goals = factory.getCoverageGoals();
    }
    totalGoals += goals.size();
    // A dummy Chromosome
    TestChromosome dummy = new TestChromosome();
    dummy.setChanged(false);
    // Execution result of a dummy Test Case
    ExecutionResult executionResult = new ExecutionResult(dummy.getTestCase());
    // coverage matrix (each row represents the coverage of each test case
    // and each column represents the coverage of each component (e.g., line)
    // this coverage matrix is useful for Rho fitness
    // +1 because we also want to include the test result
    boolean[][] coverage_matrix = new boolean[results.size()][goals.size() + 1];
    BitSet covered = new BitSet(goals.size());
    for (int index_test = 0; index_test < results.size(); index_test++) {
        JUnitResult tR = results.get(index_test);
        ExecutionTrace trace = tR.getExecutionTrace();
        executionResult.setTrace(trace);
        dummy.getTestCase().clearCoveredGoals();
        dummy.setLastExecutionResult(executionResult);
        if (criterion == Criterion.MUTATION || criterion == Criterion.STRONGMUTATION) {
            for (Integer mutationID : trace.getTouchedMutants()) {
                Mutation mutation = MutationPool.getMutant(mutationID);
                if (goals.contains(mutation)) {
                    MutationObserver.activateMutation(mutationID);
                    List<JUnitResult> mutationResults = executeTests(tR.getJUnitClass());
                    MutationObserver.deactivateMutation();
                    for (JUnitResult mR : mutationResults) {
                        if (mR.getFailureCount() != tR.getFailureCount()) {
                            logger.info("Mutation killed: " + mutationID);
                            covered.set(mutation.getId());
                            coverage_matrix[index_test][mutationID.intValue()] = true;
                            break;
                        }
                    }
                }
            }
        } else {
            for (int index_component = 0; index_component < goals.size(); index_component++) {
                TestFitnessFunction goal = (TestFitnessFunction) goals.get(index_component);
                if (goal.isCovered(dummy)) {
                    covered.set(index_component);
                    coverage_matrix[index_test][index_component] = true;
                } else {
                    coverage_matrix[index_test][index_component] = false;
                }
            }
        }
        coverage_matrix[index_test][goals.size()] = tR.wasSuccessful();
    }
    totalCoveredGoals += covered.cardinality();
    if (Properties.COVERAGE_MATRIX) {
        CoverageReportGenerator.writeCoverage(coverage_matrix, criterion);
    }
    StringBuilder str = new StringBuilder();
    for (int index_component = 0; index_component < goals.size(); index_component++) {
        str.append(covered.get(index_component) ? "1" : "0");
    }
    logger.info("* CoverageBitString " + str.toString());
    RuntimeVariable bitStringVariable = CoverageCriteriaAnalyzer.getBitStringVariable(criterion);
    if (goals.isEmpty()) {
        LoggingUtils.getEvoLogger().info("* Coverage of criterion " + criterion + ": 100% (no goals)");
        ClientServices.getInstance().getClientNode().trackOutputVariable(CoverageCriteriaAnalyzer.getCoverageVariable(criterion), 1.0);
        if (bitStringVariable != null) {
            ClientServices.getInstance().getClientNode().trackOutputVariable(bitStringVariable, "1");
        }
    } else {
        double coverage = ((double) covered.cardinality()) / ((double) goals.size());
        LoggingUtils.getEvoLogger().info("* Coverage of criterion " + criterion + ": " + NumberFormat.getPercentInstance().format(coverage));
        LoggingUtils.getEvoLogger().info("* Number of covered goals: " + covered.cardinality() + " / " + goals.size());
        ClientServices.getInstance().getClientNode().trackOutputVariable(CoverageCriteriaAnalyzer.getCoverageVariable(criterion), coverage);
        if (bitStringVariable != null) {
            ClientServices.getInstance().getClientNode().trackOutputVariable(bitStringVariable, str.toString());
        }
    }
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) ExecutionTrace(org.evosuite.testcase.execution.ExecutionTrace) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) RuntimeVariable(org.evosuite.statistics.RuntimeVariable) Mutation(org.evosuite.coverage.mutation.Mutation) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 24 with TestFitnessFunction

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

the class CoverageGoalTestNameGenerationStrategy method getTopGoals.

/**
 * Retrieve all goals at the highest level of priority
 *
 * @param coveredGoals
 * @return
 */
private List<TestFitnessFunction> getTopGoals(Set<TestFitnessFunction> coveredGoals) {
    List<TestFitnessFunction> goalList = new ArrayList<>(coveredGoals);
    Collections.sort(goalList, new GoalComparator());
    List<TestFitnessFunction> topGoals = new ArrayList<>();
    if (coveredGoals.isEmpty())
        return topGoals;
    Iterator<TestFitnessFunction> iterator = goalList.iterator();
    TestFitnessFunction lastGoal = iterator.next();
    topGoals.add(lastGoal);
    while (iterator.hasNext()) {
        TestFitnessFunction nextGoal = iterator.next();
        if (!nextGoal.getClass().equals(lastGoal.getClass()))
            break;
        topGoals.add(nextGoal);
        lastGoal = nextGoal;
    }
    return topGoals;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 25 with TestFitnessFunction

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

the class CoverageGoalTestNameGenerationStrategy method initializeMethodCoverageCount.

/**
 * Determine if we have overloaded methods, which requires the use of signatures
 *
 * @param testToGoals
 */
private void initializeMethodCoverageCount(Map<TestCase, Set<TestFitnessFunction>> testToGoals) {
    for (Set<TestFitnessFunction> goals : testToGoals.values()) {
        for (TestFitnessFunction goal : goals) {
            String methodName = getMethodNameWithoutDescriptor(goal.getTargetMethod());
            if (!methodCount.containsKey(methodName)) {
                methodCount.put(methodName, new LinkedHashSet<>());
            }
            methodCount.get(methodName).add(goal.getTargetMethod());
        }
    }
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Aggregations

TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)73 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)26 TestCase (org.evosuite.testcase.TestCase)24 Test (org.junit.Test)22 TestChromosome (org.evosuite.testcase.TestChromosome)21 ArrayList (java.util.ArrayList)20 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)12 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)11 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)11 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)9 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)9 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)8 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)8 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)8 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 HashSet (java.util.HashSet)5 LinkedHashSet (java.util.LinkedHashSet)4 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)4 Properties (org.evosuite.Properties)3