/** * {@inheritDoc} * * Create a deep copy of the chromosome */ @Override public Chromosome clone() { TestChromosome c = new TestChromosome(); c.test = test.clone(); c.setFitnessValues(getFitnessValues()); c.setPreviousFitnessValues(getPreviousFitnessValues()); c.copyCachedResults(this); c.setChanged(isChanged()); c.setLocalSearchApplied(hasLocalSearchBeenApplied()); if (Properties.LOCAL_SEARCH_SELECTIVE) { for (TestMutationHistoryEntry mutation : mutationHistory) { if(test.contains(mutation.getStatement())) c.mutationHistory.addMutationEntry(mutation.clone(c.getTestCase())); } } // c.mutationHistory.set(mutationHistory); c.setNumberOfMutations(this.getNumberOfMutations()); c.setNumberOfEvaluations(this.getNumberOfEvaluations()); c.setKineticEnergy(getKineticEnergy()); c.setNumCollisions(getNumCollisions()); return c; }
@Override public boolean isNotWorse() { ExecutionResult lastResult = individual.getLastExecutionResult(); individual.setChanged(true); individual.getTestCase().clearCoveredGoals(); double newFitness = fitness.getFitness(individual); boolean worse = false; if(fitness.isMaximizationFunction()) { if (newFitness < lastFitness) worse = true; } else { if (newFitness > lastFitness) worse = true; } if (!worse) { lastFitness = newFitness; individual.setFitness(fitness, lastFitness); return true; } else { individual.setFitness(fitness, lastFitness); individual.setLastExecutionResult(lastResult); return false; } } }
protected void restore(TestChromosome test) { if (backup == null) return; test.setTestCase(backup.getTestCase().clone()); test.copyCachedResults(backup); //test.setFitness(backup.getFitness()); test.setFitnessValues(backup.getFitnessValues()); test.setPreviousFitnessValues(backup.getPreviousFitnessValues()); test.setChanged(backup.isChanged()); }
private ExecutionResult getExecutionResult(TestChromosome individual) { ExecutionResult origResult = individual.getLastExecutionResult(); if(origResult == null||individual.isChanged()) { origResult = runTest(individual.getTestCase()); individual.setLastExecutionResult(origResult); individual.setChanged(false); } return individual.getLastExecutionResult(); }
/** * Helper function if this is used without a chromosome * * @param result * @return */ public boolean isCovered(ExecutionResult result) { TestChromosome chromosome = new TestChromosome(); chromosome.setTestCase(result.test); chromosome.setLastExecutionResult(result); chromosome.setChanged(false); return isCovered(chromosome, result); }
@Test public void testTwoIntsWeakMutation() 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(0, 0)); 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); 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);
@Test public void testHasAnyOnlyForAssertionMethod() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); VariableReference req = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getRequest"), EvoServletState.class), 0, 0); Assert.assertEquals(tc.getTestCase().toCode(), 1, tc.size()); Assert.assertFalse(ConstraintVerifier.hasAnyOnlyForAssertionMethod(tc.getTestCase())); //this method should be only for assertions factory.addMethodFor(tc.getTestCase(), req, new GenericMethod(EvoHttpServletRequest.class.getDeclaredMethod("isAsyncStarted"), EvoHttpServletRequest.class), 1); Assert.assertEquals(tc.getTestCase().toCode(), 2, tc.size()); Assert.assertTrue(ConstraintVerifier.hasAnyOnlyForAssertionMethod(tc.getTestCase())); }
@Test public void testCanDelete() throws Exception { TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); //initializing bounding variable method called directly after the new factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 1, 0); Assert.assertEquals(2, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 0)); //bounded variable can be deleted Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 1)); // method using bounded variable should not be deleted boolean mutated = tc.deleteStatement(factory, 1); Assert.assertFalse(mutated); //should fail Assert.assertEquals(2, tc.size()); mutated = tc.deleteStatement(factory, 0); Assert.assertTrue(mutated); Assert.assertEquals(0, tc.size());// deleting first statement should have had effect of removing the second as well }
@Test public void compareSmaller() { BranchCoverageGoal goal = Mockito.mock(BranchCoverageGoal.class); BranchCoverageTestFitness fitness = new BranchCoverageTestFitness(goal); TestChromosome tch1 = new TestChromosome(); tch1.setTestCase(new DefaultTestCase()); tch1.setFitness(fitness, 0); TestChromosome tch2 = new TestChromosome(); tch2.setTestCase(new DefaultTestCase()); tch2.setFitness(fitness, 1); PreferenceSortingComparator comparator = new PreferenceSortingComparator(fitness); double value = comparator.compare(tch1, tch2); assertEquals(-1, value, 0.0001); }
@Test public void compareHigherRank() { TestChromosome tch1 = new TestChromosome(); tch1.setTestCase(new DefaultTestCase()); tch1.setRank(1); TestChromosome tch2 = new TestChromosome(); tch2.setTestCase(new DefaultTestCase()); tch2.setRank(0); RankAndCrowdingDistanceComparator comparator = new RankAndCrowdingDistanceComparator(); double result = comparator.compare(tch1, tch2); assertEquals(+1, result, 0.00001); }
@Test public void testCrowdingDistanceAssignment() { // create test cases TestChromosome tch1 = new TestChromosome(); tch1.setFitness(ff.get(0), 3); tch1.setFitness(ff.get(1), 0.5); TestChromosome tch2 = new TestChromosome(); tch2.setFitness(ff.get(0), 1); tch2.setFitness(ff.get(1), 2); TestChromosome tch3 = new TestChromosome(); tch3.setFitness(ff.get(0), 0.5); tch3.setFitness(ff.get(1), 3); // create front List<TestChromosome> front = new LinkedList<>(); front.add(tch1); front.add(tch2); front.add(tch3); CrowdingDistance distance = new CrowdingDistance(); distance.crowdingDistanceAssignment(front, ff); assertEquals(Double.POSITIVE_INFINITY, tch1.getDistance(), 0.000001); assertEquals(2.0, tch2.getDistance(), 0.000001); assertEquals(Double.POSITIVE_INFINITY, tch3.getDistance(), 0.000001); }
@Test public void testSerializationNonEmptyTest() throws IOException, ClassNotFoundException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); double fitness = 0.9950513142057124d; TestChromosome chromosome = new TestChromosome(); TestCase test = new DefaultTestCase(); PrimitiveStatement<?> statement = PrimitiveStatement.getPrimitiveStatement(test, int.class); test.addStatement(statement); chromosome.setTestCase(test); chromosome.setFitness(null, fitness); oos.writeObject(chromosome); byte [] baSerialized = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(baSerialized); ObjectInputStream ois = new ObjectInputStream(bais); TestChromosome copy = (TestChromosome) ois.readObject(); Assert.assertEquals(chromosome.getFitness(), copy.getFitness(), 0.0); }
TestChromosome tch1 = new TestChromosome(); tch1.setTestCase(new DefaultTestCase()); tch1.getTestCase().addStatement(new BooleanPrimitiveStatement(tch1.getTestCase(),true)); tch1.setFitness(f1, 1); tch1.setFitness(f2, 0.5); TestChromosome tch2 = new TestChromosome(); tch2.setTestCase(new DefaultTestCase()); tch2.getTestCase().addStatement(new IntPrimitiveStatement(tch2.getTestCase(),2)); tch2.setFitness(f1, 0.6); tch2.setFitness(f2, 0.7); TestChromosome tch3 = new TestChromosome(); tch3.setTestCase(new DefaultTestCase()); tch3.getTestCase().addStatement(new IntPrimitiveStatement(tch3.getTestCase(),1)); tch3.setFitness(f1, 0.7); tch3.setFitness(f2, 0.6); TestChromosome tch4 = new TestChromosome(); tch4.setTestCase(new DefaultTestCase()); tch4.getTestCase().addStatement(new BooleanPrimitiveStatement(tch4.getTestCase(),false)); tch4.setFitness(f1, 0.5); tch4.setFitness(f2, 2);
/** * {@inheritDoc} * * Generate a random chromosome */ @Override public TestChromosome getChromosome() { TestChromosome c = new TestChromosome(); c.setTestCase(getRandomTestCase(Properties.CHROMOSOME_LENGTH)); return c; }
@Override public void searchFinished(GeneticAlgorithm<?> algorithm) { Chromosome individual = algorithm.getBestIndividual(); if(individual instanceof TestChromosome) { TestChromosome testChromosome = (TestChromosome)individual; testPool.add(testChromosome.getTestCase()); } else if(individual instanceof TestSuiteChromosome) { TestSuiteChromosome testSuiteChromosome = (TestSuiteChromosome) individual; testPool.addAll(testSuiteChromosome.getTests()); } }
/** * Execute a test case * * @param test * The test case to execute * @return Result of the execution */ protected ExecutionResult runTest(TestChromosome test) { if (!test.isChanged() && test.getLastExecutionResult() != null) return test.getLastExecutionResult(); try { ExecutionResult result = TestCaseExecutor.getInstance().execute(test.getTestCase()); return result; } catch (Exception e) { logger.error("TG: Exception caught: ", e); throw new EvosuiteError(e); } } }
/** * */ protected void updateClassloader() { if (theTest.isChanged()) { theSameTestForTheOtherClassLoader = (TestChromosome) theTest.clone(); ((DefaultTestCase) theSameTestForTheOtherClassLoader.getTestCase()) .changeClassLoader(TestGenerationContext.getInstance().getRegressionClassLoaderForSUT()); } }
/** * Add a method call on the return value of the object at position statement * * @param test * @param statement */ private boolean addCall(TestChromosome test, int statement) { logger.debug("Adding call"); TestFactory factory = TestFactory.getInstance(); Statement theStatement = test.getTestCase().getStatement(statement); VariableReference var = theStatement.getReturnValue(); int oldLength = test.size(); factory.insertRandomCallOnObjectAt(test.getTestCase(), var, statement + 1); test.setChanged(test.size() != oldLength); return false; }
@Test public void testSerializationEmptyTest() throws IOException, ClassNotFoundException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); double fitness = 3.14; TestChromosome chromosome = new TestChromosome(); chromosome.setFitness(null, fitness); oos.writeObject(chromosome); byte [] baSerialized = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(baSerialized); ObjectInputStream ois = new ObjectInputStream(bais); TestChromosome copy = (TestChromosome) ois.readObject(); Assert.assertEquals(chromosome.getFitness(), copy.getFitness(), 0.0); }
/** * Retrieve the path condition for a given test case * * @param test * a {@link org.evosuite.testcase.TestChromosome} object. * @return a {@link java.util.List} object. */ public static List<BranchCondition> getSymbolicPath(TestChromosome test) { TestChromosome dscCopy = (TestChromosome) test.clone(); DefaultTestCase defaultTestCase = (DefaultTestCase) dscCopy.getTestCase(); PathCondition pathCondition = executeConcolic(defaultTestCase); return pathCondition.getBranchConditions(); }