use of org.evosuite.coverage.mutation.Mutation in project evosuite by EvoSuite.
the class TestGenerationResultBuilder method fillInformationFromTestData.
private void fillInformationFromTestData(TestGenerationResultImpl result) {
Set<MutationInfo> exceptionMutants = new LinkedHashSet<MutationInfo>();
for (Mutation m : MutationPool.getMutants()) {
if (MutationTimeoutStoppingCondition.isDisabled(m)) {
MutationInfo info = new MutationInfo(m);
exceptionMutants.add(info);
uncoveredMutants.remove(info);
}
}
for (String test : testCode.keySet()) {
result.setTestCode(test, testCode.get(test));
result.setTestCase(test, testCases.get(test));
result.setContractViolations(test, contractViolations.get(test));
result.setCoveredLines(test, testLineCoverage.get(test));
result.setCoveredBranches(test, testBranchCoverage.get(test));
result.setCoveredMutants(test, testMutantCoverage.get(test));
result.setComment(test, testComments.get(test));
}
result.setUncoveredLines(uncoveredLines);
result.setUncoveredBranches(uncoveredBranches);
result.setUncoveredMutants(uncoveredMutants);
result.setExceptionMutants(exceptionMutants);
result.setTestSuiteCode(code);
result.setGeneticAlgorithm(ga);
for (Map.Entry<FitnessFunction<?>, Double> e : targetCoverages.entrySet()) {
result.setTargetCoverage(e.getKey(), e.getValue());
}
}
use of org.evosuite.coverage.mutation.Mutation in project evosuite by EvoSuite.
the class TestChromosome method copyCachedResults.
/* (non-Javadoc)
* @see org.evosuite.testcase.ExecutableChromosome#copyCachedResults(org.evosuite.testcase.ExecutableChromosome)
*/
/**
* {@inheritDoc}
*/
@Override
public void copyCachedResults(ExecutableChromosome other) {
if (test == null)
throw new RuntimeException("Test is null!");
if (other.lastExecutionResult != null) {
this.lastExecutionResult = other.lastExecutionResult.clone();
this.lastExecutionResult.setTest(this.test);
}
if (other.lastMutationResult != null) {
for (Mutation mutation : other.lastMutationResult.keySet()) {
// .clone();
MutationExecutionResult copy = other.lastMutationResult.get(mutation);
// copy.test = test;
this.lastMutationResult.put(mutation, copy);
}
}
}
use of org.evosuite.coverage.mutation.Mutation 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());
}
}
}
use of org.evosuite.coverage.mutation.Mutation in project evosuite by EvoSuite.
the class ReplaceComparisonOperator method apply.
/* (non-Javadoc)
* @see org.evosuite.cfg.instrumentation.MutationOperator#apply(org.objectweb.asm.tree.MethodNode, java.lang.String, java.lang.String, org.evosuite.cfg.BytecodeInstruction)
*/
/**
* {@inheritDoc}
*/
@Override
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction, Frame frame) {
JumpInsnNode node = (JumpInsnNode) instruction.getASMNode();
List<Mutation> mutations = new LinkedList<Mutation>();
LabelNode target = node.label;
boolean isBoolean = frame.getStack(frame.getStackSize() - 1) == BooleanValueInterpreter.BOOLEAN_VALUE;
for (Integer op : getOperators(node.getOpcode(), isBoolean)) {
logger.debug("Adding replacement " + op);
if (op >= 0) {
// insert mutation into bytecode with conditional
JumpInsnNode mutation = new JumpInsnNode(op, target);
// insert mutation into pool
Mutation mutationObject = MutationPool.addMutation(className, methodName, NAME + " " + getOp(node.getOpcode()) + " -> " + getOp(op), instruction, mutation, getInfectionDistance(node.getOpcode(), op));
mutations.add(mutationObject);
} else {
// Replace relational operator with TRUE/FALSE
InsnList mutation = new InsnList();
if (opcodesInt.contains(node.getOpcode()))
mutation.add(new InsnNode(Opcodes.POP));
else
mutation.add(new InsnNode(Opcodes.POP2));
if (op == TRUE) {
mutation.add(new LdcInsnNode(1));
} else {
mutation.add(new LdcInsnNode(0));
}
mutation.add(new JumpInsnNode(Opcodes.IFNE, target));
Mutation mutationObject = MutationPool.addMutation(className, methodName, NAME + " " + getOp(node.getOpcode()) + " -> " + op, instruction, mutation, getInfectionDistance(node.getOpcode(), op));
mutations.add(mutationObject);
}
}
return mutations;
}
use of org.evosuite.coverage.mutation.Mutation in project evosuite by EvoSuite.
the class ReplaceVariable method apply.
/* (non-Javadoc)
* @see org.evosuite.cfg.instrumentation.mutation.MutationOperator#apply(org.objectweb.asm.tree.MethodNode, java.lang.String, java.lang.String, org.evosuite.cfg.BytecodeInstruction)
*/
/**
* {@inheritDoc}
*/
@Override
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction, Frame frame) {
List<Mutation> mutations = new LinkedList<Mutation>();
if (mn.localVariables.isEmpty()) {
logger.debug("Have no information about local variables - recompile with full debug information");
return mutations;
}
logger.debug("Starting variable replacement in " + methodName);
try {
String origName = getName(mn, instruction.getASMNode());
int numReplacements = 0;
for (Entry<String, InsnList> mutation : getReplacements(mn, className, instruction.getASMNode(), frame).entrySet()) {
if (numReplacements++ > Properties.MAX_REPLACE_MUTANTS) {
logger.info("Reached maximum number of variable replacements");
break;
}
// insert mutation into pool
Mutation mutationObject = MutationPool.addMutation(className, methodName, NAME + " " + origName + " -> " + mutation.getKey(), instruction, mutation.getValue(), getInfectionDistance(getType(mn, instruction.getASMNode()), instruction.getASMNode(), mutation.getValue()));
mutations.add(mutationObject);
}
} catch (VariableNotFoundException e) {
logger.info("Variable not found: " + instruction);
}
logger.debug("Finished variable replacement in " + methodName);
return mutations;
}
Aggregations