Search in sources :

Example 61 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class SameAssertionSystemTest method testWrapperCopy.

// TODO: Same assertions are excluding wrapper classes for now, as there are issues
// when the values are inlined
@Ignore
@Test
public void testWrapperCopy() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = WrapperCreatingCopy.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.ASSERTION_STRATEGY = AssertionStrategy.ALL;
    // If we allow null in this test, then there is a way
    // to cover the branch without assertions but with
    // exception
    Properties.NULL_PROBABILITY = 0.0;
    // Ensure that a new Integer object is created rather than
    // just using an int, because there's no assertSame between
    // an int and an Integer
    Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    boolean hasSameAssertion = false;
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    for (TestChromosome testChromosome : best.getTestChromosomes()) {
        for (Assertion assertion : testChromosome.getTestCase().getAssertions()) {
            if (assertion instanceof SameAssertion) {
                hasSameAssertion = true;
                Assert.assertEquals(false, ((SameAssertion) assertion).value);
            }
        }
    }
    System.out.println("EvolvedTestSuite:\n" + best);
    Assert.assertTrue(hasSameAssertion);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 62 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class SameAssertionSystemTest method testPrimitiveArray.

/*
	 * SameAssertions on primitive/wrapper arrays are problematic,
	 * so we do not want to have them at all.
	 */
@Test
public void testPrimitiveArray() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = ArrayPrimitiveWrapper.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.ASSERTION_STRATEGY = AssertionStrategy.ALL;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    boolean hasSameAssertion = false;
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    for (TestChromosome testChromosome : best.getTestChromosomes()) {
        for (Assertion assertion : testChromosome.getTestCase().getAssertions()) {
            if (assertion instanceof SameAssertion) {
                hasSameAssertion = true;
            // Assert.assertEquals(true, ((SameAssertion)assertion).value);
            }
        }
    }
    System.out.println("EvolvedTestSuite:\n" + best);
    Assert.assertFalse(hasSameAssertion);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 63 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class SameAssertionSystemTest method testObjectArray.

@Test
public void testObjectArray() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = ArrayObjects.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.SEARCH_BUDGET = 100000;
    Properties.ASSERTION_STRATEGY = AssertionStrategy.ALL;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    boolean hasSameAssertion = false;
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    for (TestChromosome testChromosome : best.getTestChromosomes()) {
        for (Assertion assertion : testChromosome.getTestCase().getAssertions()) {
            if (assertion instanceof SameAssertion) {
                hasSameAssertion = true;
                Assert.assertEquals(false, ((SameAssertion) assertion).value);
            }
        }
    }
    System.out.println("EvolvedTestSuite:\n" + best);
    Assert.assertTrue(hasSameAssertion);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 64 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class SameAssertionSystemTest method testWrapper.

// TODO: Same assertions are excluding wrapper classes for now, as there are issues
// when the values are inlined
@Ignore
@Test
public void testWrapper() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = WrapperExample.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.ASSERTION_STRATEGY = AssertionStrategy.ALL;
    // If we allow null in this test, then there is a way
    // to cover the branch without assertions but with
    // exception
    Properties.NULL_PROBABILITY = 0.0;
    // Ensure that a new Integer object is created rather than
    // just using an int, because there's no assertSame between
    // an int and an Integer
    Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    boolean hasSameAssertion = false;
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    for (TestChromosome testChromosome : best.getTestChromosomes()) {
        for (Assertion assertion : testChromosome.getTestCase().getAssertions()) {
            if (assertion instanceof SameAssertion) {
                hasSameAssertion = true;
                Assert.assertEquals(true, ((SameAssertion) assertion).value);
            }
        }
    }
    Assert.assertTrue(hasSameAssertion);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 65 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class LCSAJCoverageSuiteFitness method getFitness.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.evosuite.ga.FitnessFunction#getFitness(org.
	 * evosuite.ga.Chromosome)
	 */
/**
 * {@inheritDoc}
 */
@Override
public double getFitness(AbstractTestSuiteChromosome<? extends ExecutableChromosome> suite) {
    List<ExecutionResult> results = runTestSuite(suite);
    double fitness = branchFitness.getFitness(suite);
    logger.debug("Branch fitness: {}", fitness);
    // Map<String, Integer> call_count = new HashMap<String, Integer>();
    HashMap<Integer, Integer> trueExecutions = new HashMap<Integer, Integer>();
    HashMap<Integer, Integer> falseExecutions = new HashMap<Integer, Integer>();
    HashMap<LCSAJ, Double> LCSAJFitnesses = new HashMap<LCSAJ, Double>();
    logger.debug("Checking " + LCSAJFitnessFunctions.size() + " LCSAJs against " + results.size() + " tests = " + LCSAJFitnessFunctions.size() * results.size() + " test/LCSAJ combinations");
    for (ExecutionResult result : results) {
        for (LCSAJCoverageTestFitness testFitness : LCSAJFitnessFunctions) {
            TestChromosome chromosome = new TestChromosome();
            chromosome.setTestCase(result.test);
            chromosome.setLastExecutionResult(result);
            double newFitness = testFitness.getFitness(chromosome, result);
            if (!LCSAJFitnesses.containsKey(testFitness.lcsaj))
                LCSAJFitnesses.put(testFitness.lcsaj, newFitness);
            else {
                double oldFitness = LCSAJFitnesses.get(testFitness.lcsaj);
                if (newFitness < oldFitness)
                    LCSAJFitnesses.put(testFitness.lcsaj, newFitness);
            }
        }
        for (Entry<Integer, Integer> entry : result.getTrace().getPredicateExecutionCount().entrySet()) {
            if (!trueExecutions.containsKey(entry.getKey()))
                trueExecutions.put(entry.getKey(), entry.getValue());
            else {
                trueExecutions.put(entry.getKey(), trueExecutions.get(entry.getKey()) + entry.getValue());
            }
            if (!falseExecutions.containsKey(entry.getKey()))
                falseExecutions.put(entry.getKey(), entry.getValue());
            else {
                falseExecutions.put(entry.getKey(), falseExecutions.get(entry.getKey()) + entry.getValue());
            }
        }
    }
    for (LCSAJ l : LCSAJFitnesses.keySet()) {
        fitness += normalize(LCSAJFitnesses.get(l));
    }
    fitness += branchFitness.getFitness(suite);
    logger.debug("Combined fitness: " + fitness);
    double missingBranches = 0.0;
    for (Integer executedID : expectedTrueExecutions.keySet()) {
        if (!trueExecutions.containsKey(executedID))
            missingBranches += expectedTrueExecutions.get(executedID);
        else {
            if (trueExecutions.get(executedID) < expectedTrueExecutions.get(executedID))
                missingBranches += expectedTrueExecutions.get(executedID) - trueExecutions.get(executedID);
        }
    }
    for (Integer executedID : expectedFalseExecutions.keySet()) {
        if (!falseExecutions.containsKey(executedID))
            missingBranches += expectedFalseExecutions.get(executedID);
        else {
            if (falseExecutions.get(executedID) < expectedFalseExecutions.get(executedID))
                missingBranches += expectedFalseExecutions.get(executedID) - falseExecutions.get(executedID);
        }
    }
    fitness += normalize(missingBranches);
    logger.info("Combined fitness with correction: " + fitness);
    updateIndividual(this, suite, fitness);
    double coverage = 0.0;
    for (LCSAJ l : LCSAJFitnesses.keySet()) {
        if (LCSAJFitnesses.get(l) == 0)
            coverage += 1;
    }
    suite.setCoverage(this, coverage / LCSAJFitnesses.size());
    return fitness;
}
Also used : HashMap(java.util.HashMap) ExecutionResult(org.evosuite.testcase.ExecutionResult) TestChromosome(org.evosuite.testcase.TestChromosome)

Aggregations

TestChromosome (org.evosuite.testcase.TestChromosome)128 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)47 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)33 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)22 ArrayList (java.util.ArrayList)17 TestCase (org.evosuite.testcase.TestCase)17 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)16 HashSet (java.util.HashSet)15 Properties (org.evosuite.Properties)15 Test (org.junit.Test)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)14 HashMap (java.util.HashMap)11 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)10 AbstractTestSuiteChromosome (org.evosuite.testsuite.AbstractTestSuiteChromosome)8 LinkedHashMap (java.util.LinkedHashMap)7 Foo (com.examples.with.different.packagename.symbolic.Foo)6 LinkedHashSet (java.util.LinkedHashSet)6 Set (java.util.Set)6 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 Map (java.util.Map)5