use of org.evosuite.utils.generic.GenericMethod 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.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class FactoryTestSystemTest method testMethodFor.
@Test
public void testMethodFor() throws ConstructionFailedException, NoSuchMethodException, SecurityException, ClassNotFoundException {
TestFactory testFactory = TestFactory.getInstance();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName());
GenericConstructor constructor = new GenericConstructor(sut.getConstructor(), sut);
GenericMethod method = new GenericMethod(sut.getMethod("testByte", byte.class, byte.class), sut);
DefaultTestCase test = new DefaultTestCase();
Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0;
Properties.OBJECT_REUSE_PROBABILITY = 0.0;
VariableReference var1 = testFactory.addConstructor(test, constructor, 0, 0);
testFactory.reset();
VariableReference var2 = testFactory.addConstructor(test, constructor, 1, 0);
testFactory.addMethodFor(test, var1, method, 2);
testFactory.reset();
testFactory.addMethodFor(test, var2, method, 3);
String code = test.toCode();
System.out.println(code);
assertEquals(8, test.size());
assertTrue(code.contains("factoryExample0.testByte"));
// byte2 is the first return value
assertTrue(code.contains("factoryExample1.testByte"));
}
use of org.evosuite.utils.generic.GenericMethod 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.utils.generic.GenericMethod in project evosuite by EvoSuite.
the class DeleteMutationSystemTest method getTwoIntTest.
private TestCase getTwoIntTest(int x, int y, int x1, int y1) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException {
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();
VariableReference callee = testFactory.addConstructor(test, gc, 0, 0);
test.addStatement(new IntPrimitiveStatement(test, x));
test.addStatement(new IntPrimitiveStatement(test, y));
VariableReference wrongIntVar1 = test.addStatement(new IntPrimitiveStatement(test, x1));
VariableReference wrongIntVar2 = test.addStatement(new IntPrimitiveStatement(test, y1));
Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class, int.class });
GenericMethod method = new GenericMethod(m, sut);
MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { wrongIntVar1, wrongIntVar2 }));
test.addStatement(ms);
return test;
}
use of org.evosuite.utils.generic.GenericMethod 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