use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.
the class InputCoverageFitnessFunctionSystemTest method testInputCoverageClassWithField.
@Test
public void testInputCoverageClassWithField() throws NoSuchFieldException, NoSuchMethodException {
Class<?> sut = ClassWithField.class;
DefaultTestCase tc = new DefaultTestCase();
// ClassWithField classWithField0 = new ClassWithField();
GenericConstructor constructor = new GenericConstructor(sut.getConstructors()[0], sut);
ConstructorStatement constructorStatement = new ConstructorStatement(tc, constructor, Arrays.asList(new VariableReference[] {}));
VariableReference obj = tc.addStatement(constructorStatement);
// classWithField0.testFoo(classWithField0.BOOLEAN_FIELD);
FieldReference field = new FieldReference(tc, new GenericField(sut.getDeclaredField("BOOLEAN_FIELD"), sut), obj);
Method m = sut.getMethod("testFoo", new Class<?>[] { Boolean.TYPE });
GenericMethod gm = new GenericMethod(m, sut);
tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
// classWithField0.BOOLEAN_FIELD = false;
VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, false));
tc.addStatement(new AssignmentStatement(tc, field, boolRef));
tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
Properties.TARGET_CLASS = sut.getCanonicalName();
Properties.JUNIT_TESTS = true;
TestSuiteChromosome testSuite = new TestSuiteChromosome();
testSuite.addTest(tc);
FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(Properties.Criterion.INPUT);
assertEquals("Should be 0.0", 0.0, ffunction.getFitness(testSuite), 0.0);
assertEquals("Should be 1.0", 1.0, testSuite.getCoverage(ffunction), 0.0);
}
use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.
the class InputCoverageFitnessFunctionSystemTest method testInputCoverage.
@Test
public void testInputCoverage() {
EvoSuite evosuite = new EvoSuite();
String targetClass = MethodWithSeveralInputArguments.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.MAX_ARRAY = 2;
Properties.NULL_PROBABILITY = 0.2;
Properties.SEARCH_BUDGET = 20000;
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
List<?> goals = TestGenerationStrategy.getFitnessFactories().get(0).getCoverageGoals();
Assert.assertEquals(12, goals.size());
Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.
the class MethodCoverageFitnessFunctionSystemTest method testMethodFitnessSimpleExample.
public void testMethodFitnessSimpleExample() {
EvoSuite evosuite = new EvoSuite();
String targetClass = SingleMethod.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
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);
// assuming single fitness function
int goals = TestGenerationStrategy.getFitnessFactories().get(0).getCoverageGoals().size();
Assert.assertEquals(2, goals);
Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.
the class MethodCoverageFitnessFunctionSystemTest method systemTestMethodCoverageInnerClasses.
@Test
public void systemTestMethodCoverageInnerClasses() {
EvoSuite evosuite = new EvoSuite();
String targetClass = ClassWithInnerClass.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
int goals = TestGenerationStrategy.getFitnessFactories().get(0).getCoverageGoals().size();
Assert.assertEquals(4, goals);
System.out.println("EvolvedTestSuite:\n" + best);
Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
use of org.evosuite.testsuite.TestSuiteChromosome in project evosuite by EvoSuite.
the class MethodTraceCoverageFitnessFunctionSystemTest method testMethodFitnessCompositionalExample.
public void testMethodFitnessCompositionalExample() {
EvoSuite evosuite = new EvoSuite();
String targetClass = Compositional.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
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);
// assuming single fitness function
int goals = TestGenerationStrategy.getFitnessFactories().get(0).getCoverageGoals().size();
Assert.assertEquals(4, goals);
Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
Aggregations