use of org.evosuite.testcase.statements.StringPrimitiveStatement in project evosuite by EvoSuite.
the class AbstractMOSA method removeUnusedVariables.
/**
* When a test case is changed via crossover and/or mutation, it can contains some
* primitive variables that are not used as input (or to store the output) of method calls.
* Thus, this method removes all these "trash" statements.
* @param chromosome
* @return true or false depending on whether "unused variables" are removed
*/
public boolean removeUnusedVariables(T chromosome) {
int sizeBefore = chromosome.size();
TestCase t = ((TestChromosome) chromosome).getTestCase();
List<Integer> to_delete = new ArrayList<Integer>(chromosome.size());
boolean has_deleted = false;
int num = 0;
for (Statement s : t) {
VariableReference var = s.getReturnValue();
boolean delete = false;
delete = delete || s instanceof PrimitiveStatement;
delete = delete || s instanceof ArrayStatement;
delete = delete || s instanceof StringPrimitiveStatement;
if (!t.hasReferences(var) && delete) {
to_delete.add(num);
has_deleted = true;
}
num++;
}
Collections.sort(to_delete, Collections.reverseOrder());
for (Integer position : to_delete) {
t.remove(position);
}
int sizeAfter = chromosome.size();
if (has_deleted)
logger.debug("Removed {} unused statements", (sizeBefore - sizeAfter));
return has_deleted;
}
use of org.evosuite.testcase.statements.StringPrimitiveStatement in project evosuite by EvoSuite.
the class StringLocalSearch method doSearch.
/* (non-Javadoc)
* @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
*/
/**
* {@inheritDoc}
*/
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
StringPrimitiveStatement p = (StringPrimitiveStatement) test.getTestCase().getStatement(statement);
backup(test, p);
// TODO: First apply 10 random mutations to determine if string influences _uncovered_ branch
boolean affected = false;
String oldValue = p.getValue();
for (int i = 0; i < Properties.LOCAL_SEARCH_PROBES; i++) {
if (Randomness.nextDouble() > 0.5)
p.increment();
else
p.randomize();
logger.info("Probing string " + oldValue + " ->" + p.getCode());
int result = objective.hasChanged(test);
if (result < 0) {
backup(test, p);
} else {
restore(test, p);
}
if (result != 0) {
affected = true;
logger.info("String affects fitness");
break;
}
}
if (affected) {
boolean hasImproved = false;
logger.info("Applying local search to string " + p.getCode());
// First try to remove each of the characters
logger.info("Removing characters");
if (removeCharacters(objective, test, p, statement))
hasImproved = true;
logger.info("Statement: " + p.getCode());
// Second, try to replace each of the characters with each of the 64 possible characters
logger.info("Replacing characters");
if (replaceCharacters(objective, test, p, statement))
hasImproved = true;
logger.info("Statement: " + p.getCode());
// Third, try to add characters
logger.info("Adding characters");
if (addCharacters(objective, test, p, statement))
hasImproved = true;
logger.info("Statement: " + p.getCode());
logger.info("Resulting string: " + p.getValue());
return hasImproved;
// } else {
// logger.info("Not applying local search to string as it does not improve fitness");
}
return false;
}
use of org.evosuite.testcase.statements.StringPrimitiveStatement in project evosuite by EvoSuite.
the class EnvironmentDataSystemTest method testOnSpecificTest.
@Test
public void testOnSpecificTest() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
Properties.TARGET_CLASS = DseBar.class.getCanonicalName();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
Class<?> fooClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(DseFoo.class.getCanonicalName());
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
// String string0 = "baz5";
VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));
// DseFoo dseFoo0 = new DseFoo();
GenericConstructor fooConstructor = new GenericConstructor(fooClass.getConstructors()[0], fooClass);
ConstructorStatement fooConstructorStatement = new ConstructorStatement(test, fooConstructor, Arrays.asList(new VariableReference[] {}));
VariableReference fooVar = test.addStatement(fooConstructorStatement);
// String fileName = new String("/home/galeotti/README.txt")
String path = "/home/galeotti/README.txt";
EvoSuiteFile evosuiteFile = new EvoSuiteFile(path);
FileNamePrimitiveStatement fileNameStmt = new FileNamePrimitiveStatement(test, evosuiteFile);
test.addStatement(fileNameStmt);
Method fooIncMethod = fooClass.getMethod("inc", new Class<?>[] {});
GenericMethod incMethod = new GenericMethod(fooIncMethod, fooClass);
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
// DseBar dseBar0 = new DseBar(string0);
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
ConstructorStatement constructorStatement = new ConstructorStatement(test, gc, Arrays.asList(new VariableReference[] { stringVar }));
VariableReference callee = test.addStatement(constructorStatement);
// dseBar0.coverMe(dseFoo0);
Method m = clazz.getRawClass().getMethod("coverMe", new Class<?>[] { fooClass });
GenericMethod method = new GenericMethod(m, sut);
MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { fooVar }));
test.addStatement(ms);
System.out.println(test);
TestSuiteChromosome suite = new TestSuiteChromosome();
BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
BranchCoverageMap.getInstance().searchStarted(null);
assertEquals(4.0, fitness.getFitness(suite), 0.1F);
suite.addTest(test);
assertEquals(1.0, fitness.getFitness(suite), 0.1F);
System.out.println("Test suite: " + suite);
Properties.CONCOLIC_TIMEOUT = Integer.MAX_VALUE;
TestSuiteLocalSearch localSearch = TestSuiteLocalSearch.selectTestSuiteLocalSearch();
LocalSearchObjective<TestSuiteChromosome> localObjective = new DefaultLocalSearchObjective<TestSuiteChromosome>();
localObjective.addFitnessFunction(fitness);
localSearch.doSearch(suite, localObjective);
System.out.println("Fitness: " + fitness.getFitness(suite));
System.out.println("Test suite: " + suite);
assertEquals("Local search failed to cover class", 0.0, fitness.getFitness(suite), 0.1F);
BranchCoverageMap.getInstance().searchFinished(null);
}
use of org.evosuite.testcase.statements.StringPrimitiveStatement in project evosuite by EvoSuite.
the class ConstraintVerifierTest method testNoNullInputs_notNull.
@Test
public void testNoNullInputs_notNull() 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);
factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("initServlet", Servlet.class), EvoServletState.class), 1, 0);
StringPrimitiveStatement foo = new StringPrimitiveStatement(tc.getTestCase(), "foo");
tc.getTestCase().addStatement(foo);
VariableReference con = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getConfiguration"), EvoServletState.class), 3, 0);
factory.addMethodFor(tc.getTestCase(), con, new GenericMethod(EvoServletConfig.class.getDeclaredMethod("createDispatcher", String.class), EvoServletConfig.class), 4);
Assert.assertEquals(5, tc.size());
Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
}
use of org.evosuite.testcase.statements.StringPrimitiveStatement in project evosuite by EvoSuite.
the class ConstraintVerifierTest method testNoNullInputs_nullString.
@Test
public void testNoNullInputs_nullString() 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);
factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("initServlet", Servlet.class), EvoServletState.class), 1, 0);
// shouldn't be able to pass it to createDispatcher
StringPrimitiveStatement foo = new StringPrimitiveStatement(tc.getTestCase(), null);
tc.getTestCase().addStatement(foo);
VariableReference con = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getConfiguration"), EvoServletState.class), 3, 0);
factory.addMethodFor(tc.getTestCase(), con, new GenericMethod(EvoServletConfig.class.getDeclaredMethod("createDispatcher", String.class), EvoServletConfig.class), 4);
/*
even if "foo" is null and we have the P of object reuse to 1, still "foo"
will not be used, as "null" constraint is always enforced
*/
Assert.assertEquals(tc.getTestCase().toCode(), 6, tc.size());
Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
}
Aggregations