Search in sources :

Example 1 with ConstantInliner

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

the class TestSuiteGenerator method postProcessTests.

/**
 * Apply any readability optimizations and other techniques that should use
 * or modify the generated tests
 *
 * @param testSuite
 */
protected void postProcessTests(TestSuiteChromosome testSuite) {
    // If overall time is short, the search might not have had enough time
    // to come up with a suite without timeouts. However, they will slow
    // down
    // the rest of the process, and may lead to invalid tests
    testSuite.getTestChromosomes().removeIf(t -> t.getLastExecutionResult() != null && (t.getLastExecutionResult().hasTimeout() || t.getLastExecutionResult().hasTestException()));
    if (Properties.CTG_SEEDS_FILE_OUT != null) {
        TestSuiteSerialization.saveTests(testSuite, new File(Properties.CTG_SEEDS_FILE_OUT));
    } else if (Properties.TEST_FACTORY == TestFactory.SERIALIZATION) {
        TestSuiteSerialization.saveTests(testSuite, new File(Properties.SEED_DIR + File.separator + Properties.TARGET_CLASS));
    }
    /*
		 * Remove covered goals that are not part of the minimization targets,
		 * as they might screw up coverage analysis when a minimization timeout
		 * occurs. This may happen e.g. when MutationSuiteFitness calls
		 * BranchCoverageSuiteFitness which adds branch goals.
		 */
    // TODO: This creates an inconsistency between
    // suite.getCoveredGoals().size() and suite.getNumCoveredGoals()
    // but it is not clear how to update numcoveredgoals
    List<TestFitnessFunction> goals = new ArrayList<>();
    for (TestFitnessFactory<?> ff : getFitnessFactories()) {
        goals.addAll(ff.getCoverageGoals());
    }
    for (TestFitnessFunction f : testSuite.getCoveredGoals()) {
        if (!goals.contains(f)) {
            testSuite.removeCoveredGoal(f);
        }
    }
    if (Properties.INLINE) {
        ClientServices.getInstance().getClientNode().changeState(ClientState.INLINING);
        ConstantInliner inliner = new ConstantInliner();
        // progressMonitor.setCurrentPhase("Inlining constants");
        // Map<FitnessFunction<? extends TestSuite<?>>, Double> fitnesses =
        // testSuite.getFitnesses();
        inliner.inline(testSuite);
    }
    if (Properties.MINIMIZE) {
        ClientServices.getInstance().getClientNode().changeState(ClientState.MINIMIZATION);
        // progressMonitor.setCurrentPhase("Minimizing test cases");
        if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
            LoggingUtils.getEvoLogger().info("* Skipping minimization because not enough time is left");
            ClientServices.track(RuntimeVariable.Result_Size, testSuite.size());
            ClientServices.track(RuntimeVariable.Minimized_Size, testSuite.size());
            ClientServices.track(RuntimeVariable.Result_Length, testSuite.totalLengthOfTestCases());
            ClientServices.track(RuntimeVariable.Minimized_Length, testSuite.totalLengthOfTestCases());
        } else if (Properties.isRegression()) {
            RegressionSuiteMinimizer minimizer = new RegressionSuiteMinimizer();
            minimizer.minimize(testSuite);
        } else {
            double before = testSuite.getFitness();
            TestSuiteMinimizer minimizer = new TestSuiteMinimizer(getFitnessFactories());
            LoggingUtils.getEvoLogger().info("* Minimizing test suite");
            minimizer.minimize(testSuite, true);
            double after = testSuite.getFitness();
            if (after > before + 0.01d) {
                // assume minimization
                throw new Error("EvoSuite bug: minimization lead fitness from " + before + " to " + after);
            }
        }
    } else {
        if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
            LoggingUtils.getEvoLogger().info("* Skipping minimization because not enough time is left");
        }
        ClientServices.track(RuntimeVariable.Result_Size, testSuite.size());
        ClientServices.track(RuntimeVariable.Minimized_Size, testSuite.size());
        ClientServices.track(RuntimeVariable.Result_Length, testSuite.totalLengthOfTestCases());
        ClientServices.track(RuntimeVariable.Minimized_Length, testSuite.totalLengthOfTestCases());
    }
    if (Properties.COVERAGE) {
        ClientServices.getInstance().getClientNode().changeState(ClientState.COVERAGE_ANALYSIS);
        CoverageCriteriaAnalyzer.analyzeCoverage(testSuite);
    }
    double coverage = testSuite.getCoverage();
    if (ArrayUtil.contains(Properties.CRITERION, Criterion.MUTATION) || ArrayUtil.contains(Properties.CRITERION, Criterion.STRONGMUTATION)) {
    // SearchStatistics.getInstance().mutationScore(coverage);
    }
    StatisticsSender.executedAndThenSendIndividualToMaster(testSuite);
    LoggingUtils.getEvoLogger().info("* Generated " + testSuite.size() + " tests with total length " + testSuite.totalLengthOfTestCases());
    // TODO: In the end we will only need one analysis technique
    if (!Properties.ANALYSIS_CRITERIA.isEmpty()) {
        // SearchStatistics.getInstance().addCoverage(Properties.CRITERION.toString(),
        // coverage);
        CoverageCriteriaAnalyzer.analyzeCriteria(testSuite, Properties.ANALYSIS_CRITERIA);
    // FIXME: can we send all bestSuites?
    }
    if (Properties.CRITERION.length > 1)
        LoggingUtils.getEvoLogger().info("* Resulting test suite's coverage: " + NumberFormat.getPercentInstance().format(coverage) + " (average coverage for all fitness functions)");
    else
        LoggingUtils.getEvoLogger().info("* Resulting test suite's coverage: " + NumberFormat.getPercentInstance().format(coverage));
    // printBudget(ga); // TODO - need to move this somewhere else
    if (ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE) && Properties.ANALYSIS_CRITERIA.isEmpty())
        DefUseCoverageSuiteFitness.printCoverage();
    DSEStats.getInstance().trackConstraintTypes();
    DSEStats.getInstance().trackSolverStatistics();
    if (Properties.DSE_PROBABILITY > 0.0 && Properties.LOCAL_SEARCH_RATE > 0 && Properties.LOCAL_SEARCH_PROBABILITY > 0.0) {
        DSEStats.getInstance().logStatistics();
    }
    if (Properties.FILTER_SANDBOX_TESTS) {
        for (TestChromosome test : testSuite.getTestChromosomes()) {
            // delete all statements leading to security exceptions
            ExecutionResult result = test.getLastExecutionResult();
            if (result == null) {
                result = TestCaseExecutor.runTest(test.getTestCase());
            }
            if (result.hasSecurityException()) {
                int position = result.getFirstPositionOfThrownException();
                if (position > 0) {
                    test.getTestCase().chop(position);
                    result = TestCaseExecutor.runTest(test.getTestCase());
                    test.setLastExecutionResult(result);
                }
            }
        }
    }
    if (Properties.ASSERTIONS && !Properties.isRegression()) {
        LoggingUtils.getEvoLogger().info("* Generating assertions");
        // progressMonitor.setCurrentPhase("Generating assertions");
        ClientServices.getInstance().getClientNode().changeState(ClientState.ASSERTION_GENERATION);
        if (!TimeController.getInstance().hasTimeToExecuteATestCase()) {
            LoggingUtils.getEvoLogger().info("* Skipping assertion generation because not enough time is left");
        } else {
            TestSuiteGeneratorHelper.addAssertions(testSuite);
        }
        // FIXME: can we
        StatisticsSender.sendIndividualToMaster(testSuite);
    // pass the list
    // of
    // testsuitechromosomes?
    }
    if (Properties.NO_RUNTIME_DEPENDENCY) {
        LoggingUtils.getEvoLogger().info("* Property NO_RUNTIME_DEPENDENCY is set to true - skipping JUnit compile check");
        LoggingUtils.getEvoLogger().info("* WARNING: Not including the runtime dependencies is likely to lead to flaky tests!");
    } else if (Properties.JUNIT_TESTS && Properties.JUNIT_CHECK) {
        compileAndCheckTests(testSuite);
    }
    if (Properties.SERIALIZE_REGRESSION_TEST_SUITE) {
        RegressionSuiteSerializer.appendToRegressionTestSuite(testSuite);
    }
    if (Properties.isRegression() && Properties.KEEP_REGRESSION_ARCHIVE) {
        RegressionSuiteSerializer.storeRegressionArchive();
    }
}
Also used : ConstantInliner(org.evosuite.testcase.ConstantInliner) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) RegressionSuiteMinimizer(org.evosuite.regression.RegressionSuiteMinimizer) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) File(java.io.File) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 2 with ConstantInliner

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

the class MockJOptionPaneTest method testInlinerBug.

@Test
public void testInlinerBug() throws Exception {
    Properties.TIMEOUT = Integer.MAX_VALUE;
    InstrumentingClassLoader cl = new InstrumentingClassLoader();
    TestCase t0 = buildTestCase0(cl);
    TestCase t1 = buildTestCase1(cl);
    TestSuiteChromosome suite = new TestSuiteChromosome();
    suite.addTest(t0);
    suite.addTest(t1);
    System.out.println(suite.toString());
    BranchCoverageSuiteFitness ff = new BranchCoverageSuiteFitness(cl);
    ff.getFitness(suite);
    ConstantInliner inliner = new ConstantInliner();
    inliner.inline(suite);
    System.out.println(suite.toString());
    List<ExecutionResult> execResults = suite.getLastExecutionResults();
    assertEquals(2, execResults.size());
    ExecutionResult r1 = execResults.get(0);
    ExecutionResult r2 = execResults.get(1);
    r1.calledReflection();
    r2.calledReflection();
}
Also used : ConstantInliner(org.evosuite.testcase.ConstantInliner) TestCase(org.evosuite.testcase.TestCase) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) Test(org.junit.Test)

Example 3 with ConstantInliner

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

the class ContractViolation method minimizeTest.

/**
 * Remove all statements that do not contribute to the contract violation
 */
public void minimizeTest() {
    if (isMinimized)
        return;
    /**
     * Factory method that handles statement deletion
     */
    TestFactory testFactory = TestFactory.getInstance();
    if (Properties.INLINE) {
        ConstantInliner inliner = new ConstantInliner();
        inliner.inline(test);
    }
    TestCase origTest = test.clone();
    List<Integer> positions = new ArrayList<Integer>();
    for (VariableReference var : variables) positions.add(var.getStPosition());
    int oldLength = test.size();
    boolean changed = true;
    while (changed) {
        changed = false;
        for (int i = test.size() - 1; i >= 0; i--) {
            // TODO - why??
            if (i >= test.size())
                continue;
            if (positions.contains(i))
                continue;
            try {
                boolean deleted = testFactory.deleteStatement(test, i);
                if (!deleted) {
                    continue;
                }
                if (!contract.fails(test)) {
                    test = origTest.clone();
                } else {
                    changed = true;
                    for (int j = 0; j < positions.size(); j++) {
                        if (positions.get(j) > i) {
                            positions.set(j, positions.get(j) - (oldLength - test.size()));
                        }
                    }
                    origTest = test.clone();
                    oldLength = test.size();
                }
            } catch (ConstructionFailedException e) {
                test = origTest.clone();
            }
        }
    }
    statement = test.getStatement(test.size() - 1);
    for (int i = 0; i < variables.size(); i++) {
        variables.set(i, test.getStatement(positions.get(i)).getReturnValue());
    }
    contract.addAssertionAndComments(statement, variables, exception);
    isMinimized = true;
}
Also used : ConstantInliner(org.evosuite.testcase.ConstantInliner) VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFactory(org.evosuite.testcase.TestFactory) ArrayList(java.util.ArrayList) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Aggregations

ConstantInliner (org.evosuite.testcase.ConstantInliner)3 TestCase (org.evosuite.testcase.TestCase)2 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)1 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)1 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)1 RegressionSuiteMinimizer (org.evosuite.regression.RegressionSuiteMinimizer)1 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)1 TestChromosome (org.evosuite.testcase.TestChromosome)1 TestFactory (org.evosuite.testcase.TestFactory)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)1 VariableReference (org.evosuite.testcase.variable.VariableReference)1 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)1 Test (org.junit.Test)1