use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.
the class TestTestSuiteMinimizer method minimizeSuiteHalfCoverageWithTwoFitnessFunctions.
@Test
public void minimizeSuiteHalfCoverageWithTwoFitnessFunctions() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
Properties.TARGET_CLASS = FlagExample1.class.getCanonicalName();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
TestFactory testFactory = TestFactory.getInstance();
testFactory.addConstructor(test, gc, 0, 0);
List<VariableReference> parameters = new ArrayList<VariableReference>();
for (int i = 0; i < 10; i++) {
IntPrimitiveStatement ips = new IntPrimitiveStatement(test, 28234 + i);
VariableReference vr = test.addStatement(ips, i + 1);
}
ConstructorStatement ct = new ConstructorStatement(test, gc, parameters);
Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
GenericMethod method = new GenericMethod(m, sut);
testFactory.addMethod(test, method, 11, 0);
assertEquals(12, test.size());
TestSuiteChromosome tsc = new TestSuiteChromosome();
tsc.addTest(test);
TestSuiteFitnessFunction branch = new BranchCoverageSuiteFitness();
double previous_branch_fitness = branch.getFitness(tsc);
tsc.setFitness(branch, previous_branch_fitness);
assertEquals(previous_branch_fitness, 2.0, 0.0);
TestSuiteFitnessFunction defuse = new DefUseCoverageSuiteFitness();
double previous_defuse_fitness = defuse.getFitness(tsc);
tsc.setFitness(defuse, previous_defuse_fitness);
assertEquals(previous_defuse_fitness, 0.0, 0.0);
List<TestFitnessFactory<? extends TestFitnessFunction>> factories = new ArrayList<TestFitnessFactory<? extends TestFitnessFunction>>();
factories.add(new BranchCoverageFactory());
factories.add(new DefUseCoverageFactory());
TestSuiteMinimizer minimizer = new TestSuiteMinimizer(factories);
minimizer.minimize(tsc, false);
assertEquals(1, tsc.getTests().size());
assertEquals(3, tsc.getTests().get(0).size());
// assertTrue(tsc.getTests().get(0).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28234;\nflagExample1_0.testMe(int0);\n"));
double branch_fitness = branch.getFitness(tsc);
assertEquals(previous_branch_fitness, branch_fitness, 0.0);
double defuse_fitness = defuse.getFitness(tsc);
assertEquals(previous_defuse_fitness, defuse_fitness, 0.0);
}
use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.
the class TestTestSuiteMinimizer method minimizeSuiteOnlyWithVariables.
@Test
public void minimizeSuiteOnlyWithVariables() {
DefaultTestCase test = new DefaultTestCase();
for (int i = 0; i < 10; i++) {
IntPrimitiveStatement ips = new IntPrimitiveStatement(test, i);
test.addStatement(ips);
}
assertEquals(10, test.size());
TestSuiteChromosome tsc = new TestSuiteChromosome();
tsc.addTest(test);
TestSuiteFitnessFunction ff = new BranchCoverageSuiteFitness();
double previous_fitness = ff.getFitness(tsc);
tsc.setFitness(ff, previous_fitness);
assertEquals(previous_fitness, 0.0, 0.0);
TestSuiteMinimizer minimizer = new TestSuiteMinimizer(new BranchCoverageFactory());
minimizer.minimize(tsc, false);
assertEquals(0, tsc.getTests().size());
double fitness = ff.getFitness(tsc);
assertEquals(previous_fitness, fitness, 0.0);
}
use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.
the class TestDSETestSuiteFlag method testCVC4Solver.
@Test
public void testCVC4Solver() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
String cvc4_path = System.getenv("cvc4_path");
if (cvc4_path != null) {
Properties.CVC4_PATH = cvc4_path;
}
Assume.assumeTrue(Properties.CVC4_PATH != null);
Properties.DSE_SOLVER = Properties.SolverType.CVC4_SOLVER;
Properties.CRITERION = new Properties.Criterion[] { Criterion.BRANCH };
Properties.TARGET_CLASS = Flag.class.getName();
TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
BranchCoverageSuiteFitness branchCoverageSuiteFitness = new BranchCoverageSuiteFitness();
TestSuiteChromosome suite = new TestSuiteChromosome();
suite.addFitness(branchCoverageSuiteFitness);
branchCoverageSuiteFitness.getFitness(suite);
// no goals covered yet
int coveredGoals0 = suite.getNumOfCoveredGoals();
int notCoveredGoals0 = suite.getNumOfNotCoveredGoals();
assertEquals(0, coveredGoals0);
assertNotEquals(0, notCoveredGoals0);
DefaultTestCase testCase0 = buildTestCase0();
TestChromosome testChromosome0 = new TestChromosome();
testChromosome0.setTestCase(testCase0);
suite.addTest(testChromosome0);
branchCoverageSuiteFitness.getFitness(suite);
DefaultTestCase testCase1 = buildTestCase1();
TestChromosome testChromosome1 = new TestChromosome();
testChromosome1.setTestCase(testCase1);
suite.addTest(testChromosome1);
double fitnessBeforeLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
int coveredGoalsBeforeLocalSearch = suite.getNumOfCoveredGoals();
// some goal was covered
assertTrue(coveredGoalsBeforeLocalSearch > 0);
LocalSearchObjective<TestSuiteChromosome> localSearchObjective = new DefaultLocalSearchObjective<>();
localSearchObjective.addFitnessFunction(branchCoverageSuiteFitness);
boolean improved;
do {
TestSuiteLocalSearch localSearch = new TestSuiteLocalSearch();
improved = localSearch.doSearch(suite, localSearchObjective);
} while (improved);
double fitnessAfterLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
int coveredGoalsAfterLocalSearch = suite.getNumOfCoveredGoals();
assertTrue(fitnessAfterLocalSearch < fitnessBeforeLocalSearch);
assertTrue(coveredGoalsAfterLocalSearch > coveredGoalsBeforeLocalSearch);
int finalSuiteSize = suite.size();
/*
* Check at least 6 goals were covered
*/
assertTrue(coveredGoalsAfterLocalSearch >= 6);
assertTrue(finalSuiteSize > 1);
}
use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.
the class LocalSearchNumericSystemTest method runDoubleExample.
private void runDoubleExample(double x, double y) throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
TestCase test = getDoubleTest(x, y);
TestSuiteChromosome suite = new TestSuiteChromosome();
BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
BranchCoverageMap.getInstance().searchStarted(null);
assertEquals(4.0, fitness.getFitness(suite), 0.1F);
suite.addTest(test);
assertEquals(1.0, fitness.getFitness(suite), 0.1F);
TestSuiteLocalSearch localSearch = TestSuiteLocalSearch.selectTestSuiteLocalSearch();
LocalSearchObjective<TestSuiteChromosome> localObjective = new DefaultLocalSearchObjective<TestSuiteChromosome>();
localObjective.addFitnessFunction(fitness);
localSearch.doSearch(suite, localObjective);
System.out.println("Fitness: " + fitness.getFitness(suite));
System.out.println("Test suite: " + suite);
assertEquals(0.0, fitness.getFitness(suite), 0.1F);
BranchCoverageMap.getInstance().searchFinished(null);
}
use of org.evosuite.coverage.branch.BranchCoverageSuiteFitness in project evosuite by EvoSuite.
the class DeleteMutationSystemTest method testSimpleInt.
@Test
public void testSimpleInt() throws NoSuchMethodException, SecurityException, ClassNotFoundException, ConstructionFailedException {
Properties.TARGET_CLASS = TrivialInt.class.getCanonicalName();
TestChromosome test1 = new TestChromosome();
test1.setTestCase(getIntTest(2938, -1000000));
TestChromosome test2 = (TestChromosome) test1.clone();
TestSuiteChromosome suite = new TestSuiteChromosome();
BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
assertEquals(4.0, fitness.getFitness(suite), 0.0F);
suite.addTest(test1);
assertEquals(1.0, fitness.getFitness(suite), 0.0F);
suite.addTest(test2);
// 0.99...
assertEquals(1.0, fitness.getFitness(suite), 0.01F);
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
Method m = sut.getMethod("testMe", new Class<?>[] { int.class });
GenericMethod method = new GenericMethod(m, sut);
TestCluster.getInstance().addTestCall(method);
Properties.P_TEST_CHANGE = 0.0;
Properties.P_TEST_DELETE = 1.0;
Properties.P_TEST_INSERT = 0.0;
double oldFitness = suite.getFitness();
int notChanged = 0;
for (int i = 0; i < 100; i++) {
TestChromosome testNew = (TestChromosome) test1.clone();
testNew.mutate();
if (testNew.isChanged()) {
suite.deleteTest(test1);
suite.addTest(testNew);
System.out.println(testNew.getTestCase().toCode());
double newFitness = fitness.getFitness(suite);
if (newFitness < oldFitness) {
System.out.println("Improved: " + newFitness);
test1 = testNew;
oldFitness = newFitness;
System.out.println("" + i + ":" + ((IntPrimitiveStatement) test1.getTestCase().getStatement(1)).getValue());
if (newFitness == 0.0) {
System.out.println("Iterations: " + i);
System.out.println("Not changed: " + notChanged);
break;
}
} else {
System.out.println("Not improved: " + newFitness);
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);
}
Aggregations