use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class PoolSystemTest method testPoolWithException.
@Test
public void testPoolWithException() throws IOException, NoSuchMethodException, SecurityException {
File f = File.createTempFile("EvoSuiteTestPool", null, FileUtils.getTempDirectory());
String filename = f.getAbsolutePath();
f.delete();
System.out.println(filename);
EvoSuite evosuite = new EvoSuite();
String targetClass = DependencyClassWithException.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.getTargetClassAndDontInitialise();
TestCase test = new DefaultTestCase();
VariableReference instance = test.addStatement(new ConstructorStatement(test, new GenericConstructor(DependencyClassWithException.class.getConstructors()[0], DependencyClassWithException.class), new ArrayList<VariableReference>()));
VariableReference int42 = test.addStatement(new IntPrimitiveStatement(test, 42));
GenericMethod foo = new GenericMethod(DependencyClassWithException.class.getMethod("foo", int.class), DependencyClassWithException.class);
test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
String[] command = new String[] { "-generateSuite", "-class", targetClass };
TestSuiteChromosome best = new TestSuiteChromosome();
best.addTest(test);
ObjectPool pool = new ObjectPool();
pool.addSequence(new GenericClass(DependencyClassWithException.class), test);
pool.writePool(filename);
System.out.println("EvolvedTestSuite:\n" + best);
resetStaticVariables();
setDefaultPropertiesForTestCases();
targetClass = ClassDependingOnExceptionClass.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.P_OBJECT_POOL = 0.8;
Properties.OBJECT_POOLS = filename;
ObjectPoolManager.getInstance().initialisePool();
// Properties.SEARCH_BUDGET = 50000;
command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
best = (TestSuiteChromosome) ga.getBestIndividual();
System.out.println("EvolvedTestSuite:\n" + best);
Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
f = new File(filename);
f.delete();
}
use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class TestConstantInliner method testArrayIndexInlining.
@Test
public void testArrayIndexInlining() throws NoSuchMethodException, SecurityException {
DefaultTestCase test = new DefaultTestCase();
ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(Object.class.getConstructor(), Object.class), new ArrayList<VariableReference>());
VariableReference objectVar = test.addStatement(cs);
ArrayStatement as = new ArrayStatement(test, Object[].class, 3);
test.addStatement(as);
ArrayReference arrayVar = as.getArrayReference();
ArrayIndex ai0 = new ArrayIndex(test, arrayVar, 0);
ArrayIndex ai1 = new ArrayIndex(test, arrayVar, 1);
ArrayIndex ai2 = new ArrayIndex(test, arrayVar, 2);
test.addStatement(new AssignmentStatement(test, ai0, objectVar));
test.addStatement(new AssignmentStatement(test, ai1, objectVar));
test.addStatement(new AssignmentStatement(test, ai2, objectVar));
ConstructorStatement sutCS = new ConstructorStatement(test, new GenericConstructor(ObjectParameter.class.getConstructor(), ObjectParameter.class), new ArrayList<VariableReference>());
VariableReference sut = test.addStatement(sutCS);
List<VariableReference> parameters = new ArrayList<VariableReference>();
parameters.add(ai0);
test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
parameters = new ArrayList<VariableReference>();
parameters.add(ai1);
test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
parameters = new ArrayList<VariableReference>();
parameters.add(ai2);
test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
System.out.println(test.toCode());
ConstantInliner inliner = new ConstantInliner();
inliner.inline(test);
String code = test.toCode();
assertFalse(code.contains("objectParameter0.testMe(objectArray0"));
}
use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class TestConstantInliner method testNumericArrayIndexInlining.
@Test
public void testNumericArrayIndexInlining() throws NoSuchMethodException, SecurityException {
DefaultTestCase test = new DefaultTestCase();
PrimitiveStatement<?> primitiveStatement = PrimitiveStatement.getPrimitiveStatement(test, int.class);
VariableReference intVar = test.addStatement(primitiveStatement);
ArrayStatement as = new ArrayStatement(test, int[].class, 3);
test.addStatement(as);
ArrayReference arrayVar = as.getArrayReference();
ArrayIndex ai0 = new ArrayIndex(test, arrayVar, 0);
ArrayIndex ai1 = new ArrayIndex(test, arrayVar, 1);
ArrayIndex ai2 = new ArrayIndex(test, arrayVar, 2);
test.addStatement(new AssignmentStatement(test, ai0, intVar));
test.addStatement(new AssignmentStatement(test, ai1, intVar));
test.addStatement(new AssignmentStatement(test, ai2, intVar));
ConstructorStatement sutCS = new ConstructorStatement(test, new GenericConstructor(TrivialInt.class.getConstructor(), TrivialInt.class), new ArrayList<>());
VariableReference sut = test.addStatement(sutCS);
List<VariableReference> parameters = new ArrayList<VariableReference>();
parameters.add(ai0);
test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
parameters = new ArrayList<>();
parameters.add(ai1);
test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
parameters = new ArrayList<>();
parameters.add(ai2);
test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
System.out.println(test.toCode());
ConstantInliner inliner = new ConstantInliner();
inliner.inline(test);
String code = test.toCode();
System.out.println(test.toCode());
assertFalse(code.contains("trivialInt0.testMe(int"));
}
use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class TestConstantInliner method testStringEndingWithClass.
@Test
public void testStringEndingWithClass() throws NoSuchMethodException, SecurityException {
DefaultTestCase test = new DefaultTestCase();
ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(StringConstantInliningExample.class.getConstructor(), StringConstantInliningExample.class), new ArrayList<VariableReference>());
VariableReference objectVar = test.addStatement(cs);
StringPrimitiveStatement stringStatement = new StringPrimitiveStatement(test, "test.class");
VariableReference stringParam = test.addStatement(stringStatement);
List<VariableReference> parameters = new ArrayList<VariableReference>();
parameters.add(stringParam);
test.addStatement(new MethodStatement(test, new GenericMethod(StringConstantInliningExample.class.getMethods()[0], StringConstantInliningExample.class), objectVar, parameters));
System.out.println(test.toCode());
ConstantInliner inliner = new ConstantInliner();
inliner.inline(test);
String code = test.toCode();
System.out.println(code);
assertFalse(code.contains("foo(test.class)"));
assertTrue(code.contains("foo(\"test.class\")"));
}
use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class ObjectFields method getObjectVariables.
public Map<Integer, Map<String, Object>> getObjectVariables(Collection<VariableReference> vrefs) {
List<Map<Integer, Map<String, Object>>> ov = new ArrayList<Map<Integer, Map<String, Object>>>();
Map<Integer, Map<String, Object>> variable_field = new HashMap<Integer, Map<String, Object>>();
Map<Integer, Map<String, Map<String, Object>>> variable_ref_field = new HashMap<Integer, Map<String, Map<String, Object>>>();
Map<String, Object> field_value = new HashMap<String, Object>();
// Collection<Field> fields = getAllFields(c);
List<Object> values = new ArrayList<Object>();
for (VariableReference vref : vrefs) {
Object scope_object = scope.getObject(vref);
if (scope_object == null)
continue;
String vref_class = vref.getClassName();
// logger.warn(x);
int vref_string = vref.getStPosition();
Map<String, Object> objectMap = getObjectMap(scope_object);
// Map<String,Map<String,Object>> vrefObjectMap = new
// HashMap<String,Map<String,Object>>();
// vrefObjectMap.put(vref_class, objectMap);
variable_field.put(vref_string, objectMap);
// variable_ref_field.put(vref_string, vrefObjectMap);
/*
*
* Class<?> so_class = scope_object.getClass();
*
* int vref_string = vref.getStPosition();
*
* if (ClassUtils.isPrimitiveOrWrapper(scope_object.getClass())) {
* Map<String, Object> all_vars = new HashMap<String, Object>();
* variable_field.put(vref_string, (Map<String, Object>) (all_vars
* .put("fake_var", scope_object))); } else { Map<String, Object>
* all_vars = getAllVars(scope_object, 0,"");
* variable_field.put(vref_string, all_vars); }
*/
// boolean is_prim = ClassUtils.isPrimitiveOrWrapper(so_class);
// assert is_prim != false : so_class.toString();
/*
* logger.warn("scope_ob: " + scope_object.toString() + ", so_class"
* + so_class.toString() + ", vref:" + vref_string +
* ",primitve? "+is_prim);
*/
}
return variable_field;
}
Aggregations