Search in sources :

Example 61 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class IntegerLocalSearch method doSearch.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
	 */
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
    boolean improved = false;
    TestCase slice = test.getTestCase().clone();
    int newPos = slice.sliceFor(slice.getStatement(statement).getReturnValue());
    TestCase oldTest = test.getTestCase();
    test.setTestCase(slice);
    test.setChanged(true);
    /**
     * Commenting the call to getCopyForTest(). It seems unusual that fitness
     * should be considered on a new test suite instead of the original objective
     */
    // objective = ((TestSuiteLocalSearchObjective)objective).getCopyForTest(test);
    int oldStatement = statement;
    statement = newPos;
    NumericalPrimitiveStatement<T> p = (NumericalPrimitiveStatement<T>) test.getTestCase().getStatement(statement);
    ExecutionResult oldResult = test.getLastExecutionResult();
    oldValue = p.getValue();
    logger.info("Applying search to: " + p.getCode());
    boolean done = false;
    while (!done) {
        done = true;
        // Try +1
        p.increment(1);
        logger.info("Trying increment of " + p.getCode());
        if (objective.hasImproved(test)) {
            done = false;
            improved = true;
            iterate(2, objective, test, p, statement);
            oldValue = p.getValue();
            oldResult = test.getLastExecutionResult();
        } else {
            // Restore original, try -1
            p.setValue(oldValue);
            test.setLastExecutionResult(oldResult);
            test.setChanged(false);
            p.increment(-1);
            logger.info("Trying decrement of " + p.getCode());
            if (objective.hasImproved(test)) {
                improved = true;
                done = false;
                iterate(-2, objective, test, p, statement);
                oldValue = p.getValue();
                oldResult = test.getLastExecutionResult();
            } else {
                p.setValue(oldValue);
                test.setLastExecutionResult(oldResult);
                test.setChanged(false);
            }
        }
    }
    if (improved) {
        NumericalPrimitiveStatement<T> ps = (NumericalPrimitiveStatement<T>) oldTest.getStatement(oldStatement);
        ps.setValue(p.getValue());
    }
    test.setChanged(true);
    test.setTestCase(oldTest);
    logger.info("Finished local search with result " + p.getCode());
    return improved;
}
Also used : TestCase(org.evosuite.testcase.TestCase) NumericalPrimitiveStatement(org.evosuite.testcase.statements.numeric.NumericalPrimitiveStatement) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 62 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class TestSuiteLocalSearch method ensureDoubleExecution.

/**
 * Ensure that all branches are executed twice For each branch such that
 * exists only one test case in the suite that covers that branch, it
 * creates a duplicate of that test case.
 *
 * By doing this, we avoid to incorrectly mark a new test case produced by
 * the local search as an improving test case because it simply executes
 * again a predicate.
 */
protected static void ensureDoubleExecution(TestSuiteChromosome individual, LocalSearchObjective<TestSuiteChromosome> objective) {
    logger.debug("Ensuring double execution");
    Set<TestChromosome> duplicates = new HashSet<TestChromosome>();
    TestSuiteFitnessFunction defaultFitness = (TestSuiteFitnessFunction) objective.getFitnessFunctions().get(0);
    Map<Integer, Integer> covered = new HashMap<Integer, Integer>();
    Map<Integer, TestChromosome> testMap = new HashMap<Integer, TestChromosome>();
    for (TestChromosome test : individual.getTestChromosomes()) {
        // Make sure we have an execution result
        if (test.getLastExecutionResult() == null || test.isChanged()) {
            ExecutionResult result = test.executeForFitnessFunction(defaultFitness);
            // .clone();
            test.setLastExecutionResult(result);
            test.setChanged(false);
        }
        for (Entry<Integer, Integer> entry : test.getLastExecutionResult().getTrace().getPredicateExecutionCount().entrySet()) {
            if (!covered.containsKey(entry.getKey())) {
                covered.put(entry.getKey(), 0);
            }
            covered.put(entry.getKey(), covered.get(entry.getKey()) + entry.getValue());
            testMap.put(entry.getKey(), test);
        }
    }
    for (Entry<Integer, Integer> entry : covered.entrySet()) {
        int branchId = entry.getKey();
        int count = entry.getValue();
        if (count == 1) {
            TestChromosome duplicate = (TestChromosome) testMap.get(branchId).clone();
            ExecutionResult result = duplicate.executeForFitnessFunction(defaultFitness);
            // .clone();
            duplicate.setLastExecutionResult(result);
            duplicate.setChanged(false);
            duplicates.add(duplicate);
        }
    }
    if (!duplicates.isEmpty()) {
        logger.info("Adding " + duplicates.size() + " tests to cover branches sufficiently");
        for (TestChromosome test : duplicates) {
            individual.addTest(test);
        }
        individual.setChanged(true);
        for (FitnessFunction<? extends Chromosome> ff : objective.getFitnessFunctions()) {
            ((TestSuiteFitnessFunction) ff).getFitness(individual);
        }
    }
}
Also used : HashMap(java.util.HashMap) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 63 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class TestSuiteFitnessFunction method runTest.

/**
 * Execute a test case
 *
 * @param test
 *            The test case to execute
 * @return Result of the execution
 */
@Deprecated
public ExecutionResult runTest(TestCase test) {
    ExecutionResult result = new ExecutionResult(test, null);
    try {
        result = TestCaseExecutor.getInstance().execute(test);
        MaxStatementsStoppingCondition.statementsExecuted(result.getExecutedStatements());
    } catch (Exception e) {
        logger.warn("TG: Exception caught: " + e.getMessage(), e);
        try {
            Thread.sleep(1000);
            result.setTrace(ExecutionTracer.getExecutionTracer().getTrace());
        } catch (Exception e1) {
            throw new Error(e1);
        }
    }
    // System.out.println("TG: Killed "+result.getNumKilled()+" out of "+mutants.size());
    return result;
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult)

Example 64 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class ConcolicExecution method executeConcolic.

public static List<BranchCondition> executeConcolic(DefaultTestCase defaultTestCase) {
    logger.debug("Preparing concolic execution");
    /**
     * Prepare DSC configuration
     */
    MainConfig.setInstance();
    /**
     * Path constraint and symbolic environment
     */
    SymbolicEnvironment env = new SymbolicEnvironment(classLoader);
    PathConditionCollector pc = new PathConditionCollector();
    /**
     * VM listeners
     */
    List<IVM> listeners = new ArrayList<IVM>();
    listeners.add(new CallVM(env, classLoader));
    listeners.add(new JumpVM(env, pc));
    listeners.add(new HeapVM(env, pc, classLoader));
    listeners.add(new LocalsVM(env));
    listeners.add(new ArithmeticVM(env, pc));
    listeners.add(new OtherVM(env));
    listeners.add(new SymbolicFunctionVM(env, pc));
    VM.getInstance().setListeners(listeners);
    VM.getInstance().prepareConcolicExecution();
    defaultTestCase.changeClassLoader(classLoader);
    SymbolicObserver symbolicExecObserver = new SymbolicObserver(env);
    Set<ExecutionObserver> originalExecutionObservers = TestCaseExecutor.getInstance().getExecutionObservers();
    TestCaseExecutor.getInstance().newObservers();
    TestCaseExecutor.getInstance().addObserver(symbolicExecObserver);
    logger.info("Starting concolic execution");
    ExecutionResult result = new ExecutionResult(defaultTestCase, null);
    try {
        logger.debug("Executing test");
        long startConcolicExecutionTime = System.currentTimeMillis();
        result = TestCaseExecutor.getInstance().execute(defaultTestCase, Properties.CONCOLIC_TIMEOUT);
        long estimatedConcolicExecutionTime = System.currentTimeMillis() - startConcolicExecutionTime;
        DSEStats.getInstance().reportNewConcolicExecutionTime(estimatedConcolicExecutionTime);
        MaxStatementsStoppingCondition.statementsExecuted(result.getExecutedStatements());
    } catch (Exception e) {
        logger.error("Exception during concolic execution {}", e);
        return new ArrayList<BranchCondition>();
    } finally {
        logger.debug("Cleaning concolic execution");
        TestCaseExecutor.getInstance().setExecutionObservers(originalExecutionObservers);
    }
    // ignore all callbacks from now on
    VM.disableCallBacks();
    List<BranchCondition> branches = pc.getPathCondition();
    logger.info("Concolic execution ended with " + branches.size() + " branches collected");
    if (!result.noThrownExceptions()) {
        int idx = result.getFirstPositionOfThrownException();
        logger.info("Exception thrown: " + result.getExceptionThrownAtPosition(idx));
    }
    logNrOfConstraints(branches);
    logger.debug("Cleaning concolic execution");
    TestCaseExecutor.getInstance().setExecutionObservers(originalExecutionObservers);
    return branches;
}
Also used : OtherVM(org.evosuite.symbolic.vm.OtherVM) ExecutionObserver(org.evosuite.testcase.execution.ExecutionObserver) IVM(org.evosuite.dse.IVM) ArithmeticVM(org.evosuite.symbolic.vm.ArithmeticVM) SymbolicFunctionVM(org.evosuite.symbolic.vm.SymbolicFunctionVM) ArrayList(java.util.ArrayList) SymbolicEnvironment(org.evosuite.symbolic.vm.SymbolicEnvironment) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) CallVM(org.evosuite.symbolic.vm.CallVM) Constraint(org.evosuite.symbolic.expr.Constraint) JumpVM(org.evosuite.symbolic.vm.JumpVM) PathConditionCollector(org.evosuite.symbolic.vm.PathConditionCollector) LocalsVM(org.evosuite.symbolic.vm.LocalsVM) HeapVM(org.evosuite.symbolic.vm.HeapVM)

Example 65 with ExecutionResult

use of org.evosuite.testcase.execution.ExecutionResult in project evosuite by EvoSuite.

the class StatisticsSender method sendExceptionInfo.

// -------- private methods ------------------------
private static void sendExceptionInfo(TestSuiteChromosome testSuite) {
    List<ExecutionResult> results = new ArrayList<>();
    for (TestChromosome testChromosome : testSuite.getTestChromosomes()) {
        results.add(testChromosome.getLastExecutionResult());
    }
    /*
		 * for each method name, check the class of thrown exceptions in those methods
		 */
    Map<String, Set<Class<?>>> implicitTypesOfExceptions = new HashMap<>();
    Map<String, Set<Class<?>>> explicitTypesOfExceptions = new HashMap<>();
    Map<String, Set<Class<?>>> declaredTypesOfExceptions = new HashMap<>();
    ExceptionCoverageSuiteFitness.calculateExceptionInfo(results, implicitTypesOfExceptions, explicitTypesOfExceptions, declaredTypesOfExceptions, null);
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Explicit_MethodExceptions, ExceptionCoverageSuiteFitness.getNumExceptions(explicitTypesOfExceptions));
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Explicit_TypeExceptions, ExceptionCoverageSuiteFitness.getNumClassExceptions(explicitTypesOfExceptions));
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Implicit_MethodExceptions, ExceptionCoverageSuiteFitness.getNumExceptions(implicitTypesOfExceptions));
    ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Implicit_TypeExceptions, ExceptionCoverageSuiteFitness.getNumClassExceptions(implicitTypesOfExceptions));
/*
		 * NOTE: in old report generator, we were using Properties.SAVE_ALL_DATA
		 * to check if writing the full explicitTypesOfExceptions and implicitTypesOfExceptions
		 */
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)93 TestChromosome (org.evosuite.testcase.TestChromosome)33 HashSet (java.util.HashSet)15 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)15 ArrayList (java.util.ArrayList)11 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)10 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)9 TestCase (org.evosuite.testcase.TestCase)9 VariableReference (org.evosuite.testcase.variable.VariableReference)9 Test (org.junit.Test)9 LinkedHashSet (java.util.LinkedHashSet)8 HashMap (java.util.HashMap)7 LinkedHashMap (java.util.LinkedHashMap)7 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)7 ArrayReference (org.evosuite.testcase.variable.ArrayReference)7 Set (java.util.Set)6 AbstractTestSuiteChromosome (org.evosuite.testsuite.AbstractTestSuiteChromosome)6 Map (java.util.Map)4 Entry (java.util.Map.Entry)3 ExecutionTrace (org.evosuite.testcase.execution.ExecutionTrace)3