Search in sources :

Example 61 with IntPrimitiveStatement

use of org.evosuite.testcase.statements.numeric.IntPrimitiveStatement in project evosuite by EvoSuite.

the class ChangeMutationSystemTest method getIntTest.

private TestCase getIntTest(int x) 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);
    VariableReference intVar = test.addStatement(new IntPrimitiveStatement(test, x));
    Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
    GenericMethod method = new GenericMethod(m, sut);
    MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { intVar }));
    test.addStatement(ms);
    return test;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericClass(org.evosuite.utils.generic.GenericClass) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)

Example 62 with IntPrimitiveStatement

use of org.evosuite.testcase.statements.numeric.IntPrimitiveStatement in project evosuite by EvoSuite.

the class ChangeMutationSystemTest method testTwoInts.

@Test
public void testTwoInts() 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(-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);
    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 = 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;
                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);
}
Also used : IntExampleWithNoElse(com.examples.with.different.packagename.coverage.IntExampleWithNoElse) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 63 with IntPrimitiveStatement

use of org.evosuite.testcase.statements.numeric.IntPrimitiveStatement in project evosuite by EvoSuite.

the class ChangeMutationSystemTest method getTwoIntTest.

private TestCase getTwoIntTest(int x, int y) 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);
    VariableReference intVar1 = test.addStatement(new IntPrimitiveStatement(test, x));
    VariableReference intVar2 = test.addStatement(new IntPrimitiveStatement(test, y));
    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[] { intVar1, intVar2 }));
    test.addStatement(ms);
    return test;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericClass(org.evosuite.utils.generic.GenericClass) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)

Example 64 with IntPrimitiveStatement

use of org.evosuite.testcase.statements.numeric.IntPrimitiveStatement in project evosuite by EvoSuite.

the class LocalSearchNumericSystemTest method getIntTest.

private TestCase getIntTest(int x, int y) 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);
    VariableReference intVar1 = test.addStatement(new IntPrimitiveStatement(test, x));
    VariableReference intVar0 = test.addStatement(new IntPrimitiveStatement(test, y));
    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[] { intVar0, intVar1 }));
    test.addStatement(ms);
    return test;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericClass(org.evosuite.utils.generic.GenericClass) TestFactory(org.evosuite.testcase.TestFactory) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)

Aggregations

IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)64 Test (org.junit.Test)54 ArrayList (java.util.ArrayList)43 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)41 TestCase (org.evosuite.testcase.TestCase)39 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)35 CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)27 VariableReference (org.evosuite.testcase.variable.VariableReference)19 GenericMethod (org.evosuite.utils.generic.GenericMethod)19 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)17 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)17 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)17 Method (java.lang.reflect.Method)16 GenericClass (org.evosuite.utils.generic.GenericClass)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)10 MethodStatement (org.evosuite.testcase.statements.MethodStatement)10 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)9 InputCoverageGoal (org.evosuite.coverage.io.input.InputCoverageGoal)9 InputCoverageTestFitness (org.evosuite.coverage.io.input.InputCoverageTestFitness)9 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)9