use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class SameAssertionSystemTest method testWrapperCopy.
// TODO: Same assertions are excluding wrapper classes for now, as there are issues
// when the values are inlined
@Ignore
@Test
public void testWrapperCopy() {
EvoSuite evosuite = new EvoSuite();
String targetClass = WrapperCreatingCopy.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.ASSERTION_STRATEGY = AssertionStrategy.ALL;
// If we allow null in this test, then there is a way
// to cover the branch without assertions but with
// exception
Properties.NULL_PROBABILITY = 0.0;
// Ensure that a new Integer object is created rather than
// just using an int, because there's no assertSame between
// an int and an Integer
Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0;
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
boolean hasSameAssertion = false;
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
for (TestChromosome testChromosome : best.getTestChromosomes()) {
for (Assertion assertion : testChromosome.getTestCase().getAssertions()) {
if (assertion instanceof SameAssertion) {
hasSameAssertion = true;
Assert.assertEquals(false, ((SameAssertion) assertion).value);
}
}
}
System.out.println("EvolvedTestSuite:\n" + best);
Assert.assertTrue(hasSameAssertion);
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class SameAssertionSystemTest method testPrimitiveArray.
/*
* SameAssertions on primitive/wrapper arrays are problematic,
* so we do not want to have them at all.
*/
@Test
public void testPrimitiveArray() {
EvoSuite evosuite = new EvoSuite();
String targetClass = ArrayPrimitiveWrapper.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.ASSERTION_STRATEGY = AssertionStrategy.ALL;
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
boolean hasSameAssertion = false;
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
for (TestChromosome testChromosome : best.getTestChromosomes()) {
for (Assertion assertion : testChromosome.getTestCase().getAssertions()) {
if (assertion instanceof SameAssertion) {
hasSameAssertion = true;
// Assert.assertEquals(true, ((SameAssertion)assertion).value);
}
}
}
System.out.println("EvolvedTestSuite:\n" + best);
Assert.assertFalse(hasSameAssertion);
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class SameAssertionSystemTest method testObjectArray.
@Test
public void testObjectArray() {
EvoSuite evosuite = new EvoSuite();
String targetClass = ArrayObjects.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.SEARCH_BUDGET = 100000;
Properties.ASSERTION_STRATEGY = AssertionStrategy.ALL;
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
boolean hasSameAssertion = false;
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
for (TestChromosome testChromosome : best.getTestChromosomes()) {
for (Assertion assertion : testChromosome.getTestCase().getAssertions()) {
if (assertion instanceof SameAssertion) {
hasSameAssertion = true;
Assert.assertEquals(false, ((SameAssertion) assertion).value);
}
}
}
System.out.println("EvolvedTestSuite:\n" + best);
Assert.assertTrue(hasSameAssertion);
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class SameAssertionSystemTest method testWrapper.
// TODO: Same assertions are excluding wrapper classes for now, as there are issues
// when the values are inlined
@Ignore
@Test
public void testWrapper() {
EvoSuite evosuite = new EvoSuite();
String targetClass = WrapperExample.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.ASSERTION_STRATEGY = AssertionStrategy.ALL;
// If we allow null in this test, then there is a way
// to cover the branch without assertions but with
// exception
Properties.NULL_PROBABILITY = 0.0;
// Ensure that a new Integer object is created rather than
// just using an int, because there's no assertSame between
// an int and an Integer
Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0;
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
boolean hasSameAssertion = false;
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
System.out.println("EvolvedTestSuite:\n" + best);
for (TestChromosome testChromosome : best.getTestChromosomes()) {
for (Assertion assertion : testChromosome.getTestCase().getAssertions()) {
if (assertion instanceof SameAssertion) {
hasSameAssertion = true;
Assert.assertEquals(true, ((SameAssertion) assertion).value);
}
}
}
Assert.assertTrue(hasSameAssertion);
}
use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.
the class LCSAJCoverageSuiteFitness method getFitness.
/*
* (non-Javadoc)
*
* @see
* org.evosuite.ga.FitnessFunction#getFitness(org.
* evosuite.ga.Chromosome)
*/
/**
* {@inheritDoc}
*/
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite) {
List<ExecutionResult> results = runTestSuite(suite);
double fitness = branchFitness.getFitness(suite);
logger.debug("Branch fitness: {}", fitness);
// Map<String, Integer> call_count = new HashMap<String, Integer>();
HashMap<Integer, Integer> trueExecutions = new HashMap<Integer, Integer>();
HashMap<Integer, Integer> falseExecutions = new HashMap<Integer, Integer>();
HashMap<LCSAJ, Double> LCSAJFitnesses = new HashMap<LCSAJ, Double>();
logger.debug("Checking " + LCSAJFitnessFunctions.size() + " LCSAJs against " + results.size() + " tests = " + LCSAJFitnessFunctions.size() * results.size() + " test/LCSAJ combinations");
for (ExecutionResult result : results) {
for (LCSAJCoverageTestFitness testFitness : LCSAJFitnessFunctions) {
TestChromosome chromosome = new TestChromosome();
chromosome.setTestCase(result.test);
chromosome.setLastExecutionResult(result);
double newFitness = testFitness.getFitness(chromosome, result);
if (!LCSAJFitnesses.containsKey(testFitness.lcsaj))
LCSAJFitnesses.put(testFitness.lcsaj, newFitness);
else {
double oldFitness = LCSAJFitnesses.get(testFitness.lcsaj);
if (newFitness < oldFitness)
LCSAJFitnesses.put(testFitness.lcsaj, newFitness);
}
}
for (Entry<Integer, Integer> entry : result.getTrace().getPredicateExecutionCount().entrySet()) {
if (!trueExecutions.containsKey(entry.getKey()))
trueExecutions.put(entry.getKey(), entry.getValue());
else {
trueExecutions.put(entry.getKey(), trueExecutions.get(entry.getKey()) + entry.getValue());
}
if (!falseExecutions.containsKey(entry.getKey()))
falseExecutions.put(entry.getKey(), entry.getValue());
else {
falseExecutions.put(entry.getKey(), falseExecutions.get(entry.getKey()) + entry.getValue());
}
}
}
for (LCSAJ l : LCSAJFitnesses.keySet()) {
fitness += normalize(LCSAJFitnesses.get(l));
}
fitness += branchFitness.getFitness(suite);
logger.debug("Combined fitness: " + fitness);
double missingBranches = 0.0;
for (Integer executedID : expectedTrueExecutions.keySet()) {
if (!trueExecutions.containsKey(executedID))
missingBranches += expectedTrueExecutions.get(executedID);
else {
if (trueExecutions.get(executedID) < expectedTrueExecutions.get(executedID))
missingBranches += expectedTrueExecutions.get(executedID) - trueExecutions.get(executedID);
}
}
for (Integer executedID : expectedFalseExecutions.keySet()) {
if (!falseExecutions.containsKey(executedID))
missingBranches += expectedFalseExecutions.get(executedID);
else {
if (falseExecutions.get(executedID) < expectedFalseExecutions.get(executedID))
missingBranches += expectedFalseExecutions.get(executedID) - falseExecutions.get(executedID);
}
}
fitness += normalize(missingBranches);
logger.info("Combined fitness with correction: " + fitness);
updateIndividual(this, suite, fitness);
double coverage = 0.0;
for (LCSAJ l : LCSAJFitnesses.keySet()) {
if (LCSAJFitnesses.get(l) == 0)
coverage += 1;
}
suite.setCoverage(this, coverage / LCSAJFitnesses.size());
return fitness;
}
Aggregations