Search in sources :

Example 1 with SecondaryObjective

use of org.evosuite.Properties.SecondaryObjective in project evosuite by EvoSuite.

the class TestSuiteMinimizer method minimize.

/**
 * <p>
 * minimize
 * </p>
 *
 * @param suite             a {@link org.evosuite.testsuite.TestSuiteChromosome} object.
 * @param minimizePerTest true is to minimize tests, false is to minimize suites.
 */
public void minimize(TestSuiteChromosome suite, boolean minimizePerTest) {
    startTime = System.currentTimeMillis();
    SecondaryObjective strategy = Properties.SECONDARY_OBJECTIVE[0];
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Result_Size, suite.size());
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Result_Length, suite.totalLengthOfTestCases());
    logger.info("Minimization Strategy: " + strategy + ", " + suite.size() + " tests");
    suite.clearMutationHistory();
    if (minimizePerTest)
        minimizeTests(suite);
    else
        minimizeSuite(suite);
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Minimized_Size, suite.size());
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Minimized_Length, suite.totalLengthOfTestCases());
}
Also used : SecondaryObjective(org.evosuite.Properties.SecondaryObjective)

Example 2 with SecondaryObjective

use of org.evosuite.Properties.SecondaryObjective in project evosuite by EvoSuite.

the class TestSuiteMinimizer method minimizeSuite.

/**
 * Minimize test suite with respect to the isCovered Method of the goals
 * defined by the supplied TestFitnessFactory
 *
 * @param suite a {@link org.evosuite.testsuite.TestSuiteChromosome} object.
 */
private void minimizeSuite(TestSuiteChromosome suite) {
    // in the case of whole suite generation
    for (ExecutableChromosome test : suite.getTestChromosomes()) {
        test.setChanged(true);
        test.clearCachedResults();
    }
    SecondaryObjective strategy = Properties.SECONDARY_OBJECTIVE[0];
    boolean size = false;
    if (strategy.equals("size")) {
        size = true;
        // If we want to remove tests, start with shortest
        Collections.sort(suite.tests, new Comparator<TestChromosome>() {

            @Override
            public int compare(TestChromosome chromosome1, TestChromosome chromosome2) {
                return chromosome1.size() - chromosome2.size();
            }
        });
    } else if (strategy.equals("maxlength")) {
        // If we want to remove the longest test, start with longest
        Collections.sort(suite.tests, new Comparator<TestChromosome>() {

            @Override
            public int compare(TestChromosome chromosome1, TestChromosome chromosome2) {
                return chromosome2.size() - chromosome1.size();
            }
        });
    }
    List<TestFitnessFunction> goals = new ArrayList<TestFitnessFunction>();
    List<Double> fitness = new ArrayList<Double>();
    for (TestFitnessFactory<?> ff : testFitnessFactories) {
        goals.addAll(ff.getCoverageGoals());
        fitness.add(ff.getFitness(suite));
    }
    boolean changed = true;
    while (changed && !isTimeoutReached()) {
        changed = false;
        removeEmptyTestCases(suite);
        for (TestChromosome testChromosome : suite.tests) {
            if (isTimeoutReached())
                break;
            for (int i = testChromosome.size() - 1; i >= 0; i--) {
                if (isTimeoutReached())
                    break;
                logger.debug("Current size: " + suite.size() + "/" + suite.totalLengthOfTestCases());
                logger.debug("Deleting statement " + testChromosome.getTestCase().getStatement(i).getCode() + " from test");
                TestChromosome originalTestChromosome = (TestChromosome) testChromosome.clone();
                boolean modified = false;
                try {
                    TestFactory testFactory = TestFactory.getInstance();
                    modified = testFactory.deleteStatementGracefully(testChromosome.getTestCase(), i);
                } catch (ConstructionFailedException e) {
                    modified = false;
                }
                if (!modified) {
                    testChromosome.setChanged(false);
                    testChromosome.setTestCase(originalTestChromosome.getTestCase());
                    logger.debug("Deleting failed");
                    continue;
                }
                testChromosome.setChanged(true);
                testChromosome.getTestCase().clearCoveredGoals();
                List<Double> modifiedVerFitness = new ArrayList<Double>();
                for (TestFitnessFactory<?> ff : testFitnessFactories) modifiedVerFitness.add(ff.getFitness(suite));
                int compare_ff = 0;
                for (int i_fit = 0; i_fit < modifiedVerFitness.size(); i_fit++) {
                    if (Double.compare(modifiedVerFitness.get(i_fit), fitness.get(i_fit)) < 0) {
                        // new value is lower than previous one
                        compare_ff = -1;
                        break;
                    } else if (Double.compare(modifiedVerFitness.get(i_fit), fitness.get(i_fit)) > 0) {
                        // new value is greater than previous one
                        compare_ff = 1;
                        break;
                    }
                }
                // the value 0 if d1 (previous fitness) is numerically equal to d2 (new fitness)
                if (compare_ff == 0) {
                    // if we can guarantee that we have the same fitness value with less statements, better
                    continue;
                } else if (// a value less than 0 if d1 is numerically less than d2
                compare_ff < -1) {
                    fitness = modifiedVerFitness;
                    changed = true;
                    /**
                     * This means, that we try to delete statements equally
                     * from each test case (If size is 'false'.) The hope is
                     * that the median length of the test cases is shorter,
                     * as opposed to the average length.
                     */
                    if (!size)
                        break;
                } else // and a value greater than 0 if d1 is numerically greater than d2
                if (compare_ff == 1) {
                    // Restore previous state
                    logger.debug("Can't remove statement " + originalTestChromosome.getTestCase().getStatement(i).getCode());
                    logger.debug("Restoring fitness from " + modifiedVerFitness + " to " + fitness);
                    testChromosome.setTestCase(originalTestChromosome.getTestCase());
                    testChromosome.setLastExecutionResult(originalTestChromosome.getLastExecutionResult());
                    testChromosome.setChanged(false);
                }
            }
        }
    }
    this.removeEmptyTestCases(suite);
    this.removeRedundantTestCases(suite, goals);
}
Also used : ConstructionFailedException(org.evosuite.ga.ConstructionFailedException) SecondaryObjective(org.evosuite.Properties.SecondaryObjective)

Aggregations

SecondaryObjective (org.evosuite.Properties.SecondaryObjective)2 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)1