Search in sources :

Example 41 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class DeleteMutationSystemTest method testTwoInts.

@Test
public void testTwoInts() throws NoSuchMethodException, SecurityException, ClassNotFoundException, ConstructionFailedException {
    Properties.TARGET_CLASS = IntExampleWithNoElse.class.getCanonicalName();
    TestChromosome test1 = new TestChromosome();
    test1.setTestCase(getTwoIntTest(1, 1, 22, 22));
    TestChromosome test2 = new TestChromosome();
    test2.setTestCase(getTwoIntTest(-23423423, 234234234, -23423423, 234234234));
    TestChromosome test3 = new TestChromosome();
    test3.setTestCase(getTwoIntTest(0, 0, 0, 0));
    TestSuiteChromosome suite = new TestSuiteChromosome();
    BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
    assertEquals(6.0, fitness.getFitness(suite), 0.0F);
    suite.addTest(test1);
    assertEquals(2.0, fitness.getFitness(suite), 0.0F);
    suite.addTest(test2);
    suite.addTest(test3);
    // This is the expected result
    // 
    TestSuiteChromosome checkSuite = suite.clone();
    TestChromosome test4 = new TestChromosome();
    test4.setTestCase(getTwoIntTest(1, 22, 22, 1));
    checkSuite.addTest(test4);
    assertEquals(0.0, fitness.getFitness(checkSuite), 0.0F);
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    Method m = sut.getMethod("testMe", new Class<?>[] { int.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 = fitness.getFitness(suite);
    int notChanged = 0;
    System.out.println("Original: " + test1);
    for (int i = 0; i < 100; i++) {
        TestChromosome testNew = (TestChromosome) test1.clone();
        testNew.mutate();
        if (testNew.isChanged()) {
            System.out.println("Trying: " + testNew);
            suite.deleteTest(test1);
            suite.addTest(testNew);
            double newFitness = fitness.getFitness(suite);
            if (newFitness < oldFitness) {
                test1 = testNew;
                oldFitness = newFitness;
                if (newFitness == 0.0) {
                    System.out.println("Iterations: " + i);
                    System.out.println("Not changed: " + notChanged);
                    break;
                }
            } else {
                // Can get stuck in a local optimum with just mutation
                if (testNew.size() == 1) {
                    test1.setTestCase(getTwoIntTest(1, 1, 22, 22));
                }
                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);
}
Also used : IntExampleWithNoElse(com.examples.with.different.packagename.coverage.IntExampleWithNoElse) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 42 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class InsertionMutationSystemTest method testSimpleInt.

@Test
public void testSimpleInt() throws NoSuchMethodException, SecurityException, ClassNotFoundException, ConstructionFailedException {
    Properties.TARGET_CLASS = TrivialInt.class.getCanonicalName();
    TestChromosome test1 = new TestChromosome();
    test1.setTestCase(getIntTest(-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 = 0.0;
    Properties.P_TEST_INSERT = 1.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);
}
Also used : TrivialInt(com.examples.with.different.packagename.TrivialInt) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 43 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class InsertionMutationSystemTest method testTwoInts.

@Test
public void testTwoInts() throws NoSuchMethodException, SecurityException, ClassNotFoundException, ConstructionFailedException {
    Properties.TARGET_CLASS = IntExampleWithNoElse.class.getCanonicalName();
    TestChromosome test1 = new TestChromosome();
    test1.setTestCase(getTwoIntTest(1, 1000));
    TestChromosome test2 = new TestChromosome();
    test2.setTestCase(getTwoIntTest(-23423423, 234234234));
    TestChromosome test3 = new TestChromosome();
    test3.setTestCase(getTwoIntTest(0, 0));
    TestSuiteChromosome suite = new TestSuiteChromosome();
    BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
    assertEquals(6.0, fitness.getFitness(suite), 0.0F);
    suite.addTest(test1);
    assertEquals(2.0, fitness.getFitness(suite), 0.0F);
    suite.addTest(test2);
    // 0.99...
    assertEquals(1.99, fitness.getFitness(suite), 0.01F);
    suite.addTest(test3);
    // 0.99...
    assertEquals(0.99, fitness.getFitness(suite), 0.01F);
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    Method m = sut.getMethod("testMe", new Class<?>[] { int.class, int.class });
    GenericMethod method = new GenericMethod(m, sut);
    TestCluster.getInstance().addTestCall(method);
    Properties.P_TEST_CHANGE = 0.0;
    Properties.P_TEST_DELETE = 0.0;
    Properties.P_TEST_INSERT = 1.0;
    double oldFitness = suite.getFitness();
    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;
                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);
}
Also used : IntExampleWithNoElse(com.examples.with.different.packagename.coverage.IntExampleWithNoElse) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 44 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class PoolSystemTest method testPoolWithException.

@Test
public void testPoolWithException() throws IOException, NoSuchMethodException, SecurityException {
    File f = File.createTempFile("EvoSuiteTestPool", null, FileUtils.getTempDirectory());
    String filename = f.getAbsolutePath();
    f.delete();
    System.out.println(filename);
    EvoSuite evosuite = new EvoSuite();
    String targetClass = DependencyClassWithException.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.getTargetClassAndDontInitialise();
    TestCase test = new DefaultTestCase();
    VariableReference instance = test.addStatement(new ConstructorStatement(test, new GenericConstructor(DependencyClassWithException.class.getConstructors()[0], DependencyClassWithException.class), new ArrayList<VariableReference>()));
    VariableReference int42 = test.addStatement(new IntPrimitiveStatement(test, 42));
    GenericMethod foo = new GenericMethod(DependencyClassWithException.class.getMethod("foo", int.class), DependencyClassWithException.class);
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    TestSuiteChromosome best = new TestSuiteChromosome();
    best.addTest(test);
    ObjectPool pool = new ObjectPool();
    pool.addSequence(new GenericClass(DependencyClassWithException.class), test);
    pool.writePool(filename);
    System.out.println("EvolvedTestSuite:\n" + best);
    resetStaticVariables();
    setDefaultPropertiesForTestCases();
    targetClass = ClassDependingOnExceptionClass.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.P_OBJECT_POOL = 0.8;
    Properties.OBJECT_POOLS = filename;
    ObjectPoolManager.getInstance().initialisePool();
    // Properties.SEARCH_BUDGET = 50000;
    command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
    f = new File(filename);
    f.delete();
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) DependencyClassWithException(com.examples.with.different.packagename.pool.DependencyClassWithException) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) GenericClass(org.evosuite.utils.generic.GenericClass) EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) File(java.io.File) ObjectPool(org.evosuite.seeding.ObjectPool) ClassDependingOnExceptionClass(com.examples.with.different.packagename.pool.ClassDependingOnExceptionClass) Test(org.junit.Test)

Example 45 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class TestConstantInliner method testArrayIndexInlining.

@Test
public void testArrayIndexInlining() throws NoSuchMethodException, SecurityException {
    DefaultTestCase test = new DefaultTestCase();
    ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(Object.class.getConstructor(), Object.class), new ArrayList<VariableReference>());
    VariableReference objectVar = test.addStatement(cs);
    ArrayStatement as = new ArrayStatement(test, Object[].class, 3);
    test.addStatement(as);
    ArrayReference arrayVar = as.getArrayReference();
    ArrayIndex ai0 = new ArrayIndex(test, arrayVar, 0);
    ArrayIndex ai1 = new ArrayIndex(test, arrayVar, 1);
    ArrayIndex ai2 = new ArrayIndex(test, arrayVar, 2);
    test.addStatement(new AssignmentStatement(test, ai0, objectVar));
    test.addStatement(new AssignmentStatement(test, ai1, objectVar));
    test.addStatement(new AssignmentStatement(test, ai2, objectVar));
    ConstructorStatement sutCS = new ConstructorStatement(test, new GenericConstructor(ObjectParameter.class.getConstructor(), ObjectParameter.class), new ArrayList<VariableReference>());
    VariableReference sut = test.addStatement(sutCS);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    parameters.add(ai0);
    test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
    parameters = new ArrayList<VariableReference>();
    parameters.add(ai1);
    test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
    parameters = new ArrayList<VariableReference>();
    parameters.add(ai2);
    test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
    System.out.println(test.toCode());
    ConstantInliner inliner = new ConstantInliner();
    inliner.inline(test);
    String code = test.toCode();
    assertFalse(code.contains("objectParameter0.testMe(objectArray0"));
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) ObjectParameter(com.examples.with.different.packagename.ObjectParameter) Test(org.junit.Test)

Aggregations

GenericMethod (org.evosuite.utils.generic.GenericMethod)110 Test (org.junit.Test)68 VariableReference (org.evosuite.testcase.variable.VariableReference)64 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)60 Method (java.lang.reflect.Method)57 GenericClass (org.evosuite.utils.generic.GenericClass)40 MethodStatement (org.evosuite.testcase.statements.MethodStatement)29 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 ArrayList (java.util.ArrayList)18 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)17 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)15 TestCase (org.evosuite.testcase.TestCase)12 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)11 Injector (org.evosuite.runtime.javaee.injection.Injector)10 Type (java.lang.reflect.Type)9 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)8 EvoServletState (org.evosuite.runtime.javaee.javax.servlet.EvoServletState)8 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)8 GenericField (org.evosuite.utils.generic.GenericField)8 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)7