use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.
the class ChangeMutationSystemTest method testTwoIntsLineCoverage.
@Test
public void testTwoIntsLineCoverage() throws NoSuchMethodException, SecurityException, ClassNotFoundException, ConstructionFailedException {
Properties.TARGET_CLASS = IntExampleWithNoElse.class.getCanonicalName();
TestChromosome test1 = new TestChromosome();
test1.setTestCase(getTwoIntTest(1000, 100));
TestChromosome test2 = new TestChromosome();
test2.setTestCase(getTwoIntTest(0, 0));
TestSuiteChromosome suite = new TestSuiteChromosome();
LineCoverageSuiteFitness fitness = new LineCoverageSuiteFitness();
suite.addTest(test1);
suite.addTest(test2);
Properties.P_TEST_CHANGE = 1.0;
Properties.P_TEST_DELETE = 0.0;
Properties.P_TEST_INSERT = 0.0;
Properties.PRIMITIVE_POOL = 0.0;
double oldFitness = fitness.getFitness(suite);
int notChanged = 0;
for (int i = 0; i < 10000; i++) {
TestChromosome testNew = (TestChromosome) test1.clone();
testNew.mutate();
if (testNew.isChanged()) {
suite.deleteTest(test1);
suite.addTest(testNew);
double newFitness = fitness.getFitness(suite);
if (newFitness < oldFitness) {
test1 = testNew;
oldFitness = newFitness;
System.out.println("" + i + ":" + ((IntPrimitiveStatement) test1.getTestCase().getStatement(1)).getValue());
System.out.println(" " + ((IntPrimitiveStatement) test1.getTestCase().getStatement(2)).getValue());
if (newFitness == 0.0) {
System.out.println("Iterations: " + i);
System.out.println("Not changed: " + notChanged);
break;
}
} else {
suite.deleteTest(testNew);
suite.addTest(test1);
fitness.getFitness(suite);
}
} else {
notChanged++;
}
}
System.out.println("Fitness: " + fitness.getFitness(suite));
System.out.println("Test suite: " + suite);
assertEquals(0.0, fitness.getFitness(suite), 0.1F);
}
use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactorySystemTest method testDifficultClassWithWrongTestFails.
// EvoSuite may also cover it without seeding now.
@Ignore
@Test
public void testDifficultClassWithWrongTestFails() {
EvoSuite evosuite = new EvoSuite();
String targetClass = DifficultClassWithoutCarving.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.TEST_FACTORY = TestFactory.JUNIT;
Properties.SELECTED_JUNIT = DifficultClassTest.class.getCanonicalName();
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
System.out.println("EvolvedTestSuite:\n" + best);
Assert.assertTrue("Did not expect optimal coverage: ", best.getCoverage() < 1d);
}
use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.
the class DefUseAnalysisSystemTest method testSimpleExample.
@Test
public void testSimpleExample() {
EvoSuite evosuite = new EvoSuite();
String targetClass = DefUseExample1.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
// Need to deactivate assertions, otherwise classloader is chanaged
// and DefUseCoverageFactory is reset
Properties.ASSERTIONS = false;
// Properties.ANALYSIS_CRITERIA = "Branch,DefUse";
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
System.out.println("EvolvedTestSuite:\n" + best);
Assert.assertEquals(0, DefUseCoverageFactory.getInterMethodGoalsCount());
Assert.assertEquals(0, DefUseCoverageFactory.getIntraClassGoalsCount());
Assert.assertEquals(1, DefUseCoverageFactory.getParamGoalsCount());
Assert.assertEquals(3, DefUseCoverageFactory.getIntraMethodGoalsCount());
Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.
the class DefUseAnalysisSystemTest method testGCDExample.
@Test
public void testGCDExample() {
EvoSuite evosuite = new EvoSuite();
String targetClass = GCD.class.getCanonicalName();
Properties.ASSERTIONS = false;
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
System.out.println("Def: " + DefUsePool.getDefCounter());
// DefUseCoverageFactory.computeGoals();
GeneticAlgorithm<?> ga = getGAFromResult(result);
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
System.out.println("EvolvedTestSuite:\n" + best);
Assert.assertEquals(0, DefUseCoverageFactory.getInterMethodGoalsCount());
Assert.assertEquals(0, DefUseCoverageFactory.getIntraClassGoalsCount());
// 3 or 4?
Assert.assertEquals(4, DefUseCoverageFactory.getParamGoalsCount());
Assert.assertEquals(6, DefUseCoverageFactory.getIntraMethodGoalsCount());
Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.
the class ImplicitExplicitExceptionsSystemTest method testExceptionFitness.
private void testExceptionFitness() {
EvoSuite evosuite = new EvoSuite();
String targetClass = ImplicitExplicitException.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.CRITERION = new Criterion[] { Properties.Criterion.EXCEPTION };
Properties.OUTPUT_VARIABLES = "" + RuntimeVariable.Explicit_MethodExceptions + "," + RuntimeVariable.Explicit_TypeExceptions + "," + RuntimeVariable.Implicit_MethodExceptions + "," + RuntimeVariable.Implicit_TypeExceptions;
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
System.out.println("EvolvedTestSuite:\n" + best);
double fitness = best.getFitness();
/*
* there are 2 undeclared exceptions (both implicit and explicit),
* and 3 declared: so fit = 1 / (1+5)
*/
Assert.assertEquals("Wrong fitness: ", 1d / 6d, fitness, 0.0000001);
Map<String, OutputVariable<?>> map = DebugStatisticsBackend.getLatestWritten();
Assert.assertNotNull(map);
Assert.assertEquals(1, map.get(RuntimeVariable.Explicit_MethodExceptions.toString()).getValue());
Assert.assertEquals(1, map.get(RuntimeVariable.Explicit_TypeExceptions.toString()).getValue());
Assert.assertEquals(1, map.get(RuntimeVariable.Implicit_MethodExceptions.toString()).getValue());
Assert.assertEquals(1, map.get(RuntimeVariable.Implicit_TypeExceptions.toString()).getValue());
}
Aggregations