use of org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement in project evosuite by EvoSuite.
the class TestSuiteGenerator method buildLoadTargetClassTestCase.
/**
* Creates a single Test Case that only loads the target class.
* <code>
* Thread currentThread = Thread.currentThread();
* ClassLoader classLoader = currentThread.getClassLoader();
* classLoader.load(className);
* </code>
* @param className the class to be loaded
* @return
* @throws EvosuiteError if a reflection error happens while creating the test case
*/
private static DefaultTestCase buildLoadTargetClassTestCase(String className) throws EvosuiteError {
DefaultTestCase test = new DefaultTestCase();
StringPrimitiveStatement stmt0 = new StringPrimitiveStatement(test, className);
VariableReference string0 = test.addStatement(stmt0);
try {
Method currentThreadMethod = Thread.class.getMethod("currentThread");
Statement currentThreadStmt = new MethodStatement(test, new GenericMethod(currentThreadMethod, currentThreadMethod.getDeclaringClass()), null, Collections.emptyList());
VariableReference currentThreadVar = test.addStatement(currentThreadStmt);
Method getContextClassLoaderMethod = Thread.class.getMethod("getContextClassLoader");
Statement getContextClassLoaderStmt = new MethodStatement(test, new GenericMethod(getContextClassLoaderMethod, getContextClassLoaderMethod.getDeclaringClass()), currentThreadVar, Collections.emptyList());
VariableReference contextClassLoaderVar = test.addStatement(getContextClassLoaderStmt);
// Method loadClassMethod = ClassLoader.class.getMethod("loadClass", String.class);
// Statement loadClassStmt = new MethodStatement(test,
// new GenericMethod(loadClassMethod, loadClassMethod.getDeclaringClass()), contextClassLoaderVar,
// Collections.singletonList(string0));
// test.addStatement(loadClassStmt);
BooleanPrimitiveStatement stmt1 = new BooleanPrimitiveStatement(test, true);
VariableReference boolean0 = test.addStatement(stmt1);
Method forNameMethod = Class.class.getMethod("forName", String.class, boolean.class, ClassLoader.class);
Statement forNameStmt = new MethodStatement(test, new GenericMethod(forNameMethod, forNameMethod.getDeclaringClass()), null, Arrays.<VariableReference>asList(string0, boolean0, contextClassLoaderVar));
test.addStatement(forNameStmt);
return test;
} catch (NoSuchMethodException | SecurityException e) {
throw new EvosuiteError("Unexpected exception while creating Class Initializer Test Case");
}
}
use of org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement in project evosuite by EvoSuite.
the class ConcolicMutation method negateCondition.
/**
* Generate new constraint and ask solver for solution
*
* @param pathCondition
*
* @param targetCondition
* a {@link org.evosuite.symbolic.BranchCondition} object.
* @param test
* a {@link org.evosuite.testcase.TestCase} object.
* @return a {@link org.evosuite.testcase.TestCase} object.
*/
// @SuppressWarnings({ "rawtypes", "unchecked" })
public static TestCase negateCondition(List<BranchCondition> pathCondition, BranchCondition targetCondition, TestCase test) {
List<Constraint<?>> constraints = new LinkedList<Constraint<?>>();
for (BranchCondition b : pathCondition) {
constraints.addAll(b.getSupportingConstraints());
if (b == targetCondition) {
break;
} else {
constraints.add(b.getConstraint());
}
}
final Constraint<?> targetConstraint = targetCondition.getConstraint().negate();
constraints.add(targetConstraint);
if (!targetConstraint.isSolveable()) {
logger.info("Found unsolvable constraint: " + targetConstraint);
// Could we treat this as a special case?
return null;
}
int size = constraints.size();
if (size > 0) {
constraints = reduce(constraints);
// logger.info("Reduced constraints from " + size + " to " +
// constraints.size());
// logger.info("Now solving: " + constraints);
}
Solver solver = SolverFactory.getInstance().buildNewSolver();
SolverCache solverCache = SolverCache.getInstance();
SolverResult solverResult = solverCache.solve(solver, constraints);
if (solverResult != null) {
// logger.info(values.toString());
TestCase newTest = test.clone();
Map<String, Object> model = solverResult.getModel();
for (Object key : model.keySet()) {
Object val = model.get(key);
if (val != null) {
if (val instanceof Long) {
Long value = (Long) val;
String name = ((String) key).replace("__SYM", "");
logger.debug("New value for " + name + " is " + value);
PrimitiveStatement<?> p = getStatement(newTest, name);
assert (p != null);
if (p instanceof BooleanPrimitiveStatement) {
BooleanPrimitiveStatement bp = (BooleanPrimitiveStatement) p;
bp.setValue(value.intValue() > 0);
} else if (p instanceof CharPrimitiveStatement) {
CharPrimitiveStatement cp = (CharPrimitiveStatement) p;
cp.setValue((char) value.intValue());
} else if (p instanceof BytePrimitiveStatement) {
BytePrimitiveStatement bp = (BytePrimitiveStatement) p;
bp.setValue((byte) value.intValue());
} else if (p instanceof ShortPrimitiveStatement) {
ShortPrimitiveStatement sp = (ShortPrimitiveStatement) p;
sp.setValue((short) value.intValue());
} else if (p instanceof LongPrimitiveStatement) {
LongPrimitiveStatement lp = (LongPrimitiveStatement) p;
lp.setValue(value);
} else {
assert (p instanceof IntPrimitiveStatement);
IntPrimitiveStatement ip = (IntPrimitiveStatement) p;
ip.setValue(value.intValue());
}
} else {
logger.debug("New value is not long " + val);
}
} else {
logger.debug("New value is null");
}
}
return newTest;
} else {
logger.debug("Got null :-(");
return null;
}
}
use of org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement in project evosuite by EvoSuite.
the class FunctionalMockStatementTest method testLimit.
@Test
public void testLimit() throws Exception {
TestCase tc = new DefaultTestCase();
final int LIMIT_5 = 5;
Properties.FUNCTIONAL_MOCKING_INPUT_LIMIT = LIMIT_5;
final int LOOP_0 = 0, LOOP_3 = 3, LOOP_5 = 5, LOOP_7 = 7;
IntPrimitiveStatement x = new IntPrimitiveStatement(tc, LOOP_3);
VariableReference loop = tc.addStatement(x);
VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, true));
VariableReference ref = new VariableReferenceImpl(tc, Foo.class);
FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, Foo.class);
VariableReference mock = tc.addStatement(mockStmt);
tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("limit", Foo.class, int.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock, loop)));
// execute first time with default mock
execute(tc);
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
List<Type> types = mockStmt.updateMockedMethods();
Assert.assertEquals(LOOP_3, types.size());
for (Type t : types) {
Assert.assertEquals(boolean.class, t);
}
// add the 3 missing values
mockStmt.addMissingInputs(Arrays.asList(boolRef, boolRef, boolRef));
// before re-executing, change loops to the limit
x.setValue(LOOP_5);
execute(tc);
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
Assert.assertEquals(LOOP_5 - LOOP_3, types.size());
for (Type t : types) {
Assert.assertEquals(boolean.class, t);
}
// add the 2 missing values
mockStmt.addMissingInputs(Arrays.asList(boolRef, boolRef));
Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
// before re-executing 3rd time, change loops above the limit
x.setValue(LOOP_7);
execute(tc);
// no update should be required
Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
Assert.assertEquals(0, types.size());
Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
// decrease, but to the limit, so still no required change
x.setValue(LOOP_5);
execute(tc);
// no update should be required
Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
Assert.assertEquals(0, types.size());
Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
// further decrease, but now we need to remove parameters
x.setValue(LOOP_3);
execute(tc);
// do update
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
// but no new types to add
Assert.assertEquals(0, types.size());
Assert.assertEquals(LOOP_3, mockStmt.getNumParameters());
// remove all
x.setValue(LOOP_0);
execute(tc);
// do update
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
// but no new types to add
Assert.assertEquals(0, types.size());
Assert.assertEquals(LOOP_0, mockStmt.getNumParameters());
}
use of org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement in project evosuite by EvoSuite.
the class InputCoverageFitnessFunctionSystemTest method testInputCoverageClassWithField.
@Test
public void testInputCoverageClassWithField() throws NoSuchFieldException, NoSuchMethodException {
Class<?> sut = ClassWithField.class;
DefaultTestCase tc = new DefaultTestCase();
// ClassWithField classWithField0 = new ClassWithField();
GenericConstructor constructor = new GenericConstructor(sut.getConstructors()[0], sut);
ConstructorStatement constructorStatement = new ConstructorStatement(tc, constructor, Arrays.asList(new VariableReference[] {}));
VariableReference obj = tc.addStatement(constructorStatement);
// classWithField0.testFoo(classWithField0.BOOLEAN_FIELD);
FieldReference field = new FieldReference(tc, new GenericField(sut.getDeclaredField("BOOLEAN_FIELD"), sut), obj);
Method m = sut.getMethod("testFoo", new Class<?>[] { Boolean.TYPE });
GenericMethod gm = new GenericMethod(m, sut);
tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
// classWithField0.BOOLEAN_FIELD = false;
VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, false));
tc.addStatement(new AssignmentStatement(tc, field, boolRef));
tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
Properties.TARGET_CLASS = sut.getCanonicalName();
Properties.JUNIT_TESTS = true;
TestSuiteChromosome testSuite = new TestSuiteChromosome();
testSuite.addTest(tc);
FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(Properties.Criterion.INPUT);
assertEquals("Should be 0.0", 0.0, ffunction.getFitness(testSuite), 0.0);
assertEquals("Should be 1.0", 1.0, testSuite.getCoverage(ffunction), 0.0);
}
use of org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement in project evosuite by EvoSuite.
the class TestCaseBuilder method appendBooleanPrimitive.
public VariableReference appendBooleanPrimitive(boolean b) {
BooleanPrimitiveStatement primitiveStmt = new BooleanPrimitiveStatement(tc, b);
tc.addStatement(primitiveStmt);
return primitiveStmt.getReturnValue();
}
Aggregations