All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.evosuite.coverage.ambiguity.AmbiguityCoverageSuiteFitness Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2010-2018 Gordon Fraser, Andrea Arcuri and EvoSuite
 * contributors
 *
 * This file is part of EvoSuite.
 *
 * EvoSuite is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3.0 of the License, or
 * (at your option) any later version.
 *
 * EvoSuite is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with EvoSuite. If not, see .
 */
package org.evosuite.coverage.ambiguity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.evosuite.coverage.line.LineCoverageTestFitness;
import org.evosuite.testcase.ExecutableChromosome;
import org.evosuite.testcase.TestFitnessFunction;
import org.evosuite.testcase.execution.ExecutionResult;
import org.evosuite.testsuite.AbstractTestSuiteChromosome;
import org.evosuite.testsuite.TestSuiteFitnessFunction;

/**
 * 
 * @author José Campos
 */
public class AmbiguityCoverageSuiteFitness extends TestSuiteFitnessFunction {

	private static final long serialVersionUID = -2721073655092419390L;

	/**
	 * 
	 */
	private final Set goals;

	/**
	 * 
	 */
	public AmbiguityCoverageSuiteFitness() {

		this.goals = new LinkedHashSet();
		for (LineCoverageTestFitness goal : AmbiguityCoverageFactory.getGoals()) {
			this.goals.add(goal.getLine());
		}
	}

	@Override
	public double getFitness(AbstractTestSuiteChromosome suite) {

		List transposedMatrix = new ArrayList(AmbiguityCoverageFactory.getTransposedMatrix());
		List> coveredLines = new ArrayList>();

		// Execute test cases and collect the covered lines
		List results = runTestSuite(suite);
		for (ExecutionResult result : results) {
			coveredLines.add(result.getTrace().getCoveredLines());
		}

		Map groups = new HashMap();
		int g_i = 0;

		for (Integer goal : this.goals) {
			StringBuffer str = null;

			if (transposedMatrix.size() > g_i) {
				str = new StringBuffer(transposedMatrix.get(g_i).length() + coveredLines.size());
				str.append(transposedMatrix.get(g_i));
			} else {
				str = new StringBuffer(coveredLines.size());
			}

			for (Set covered : coveredLines) {
				str.append( covered.contains(goal) ? "1" : "0" );
			}

			if (!groups.containsKey(str.toString())) {
				groups.put(str.toString(), 1); // in the beginning they are ambiguity, so they belong to the same group '1'
			} else {
				groups.put(str.toString(), groups.get(str.toString()) + 1);
			}

			g_i++;
		}

		//double fitness = AmbiguityCoverageFactory.getAmbiguity(this.goals.size(), groups) * 1.0 / AmbiguityCoverageFactory.getMaxAmbiguityScore();
		double fitness = TestFitnessFunction.normalize(AmbiguityCoverageFactory.getAmbiguity(this.goals.size(), groups));
		updateIndividual(this, suite, fitness);

		return fitness;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy