use of org.evosuite.testcase.statements.numeric.CharPrimitiveStatement 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.CharPrimitiveStatement in project evosuite by EvoSuite.
the class TestCaseBuilder method appendCharPrimitive.
public VariableReference appendCharPrimitive(char c) {
CharPrimitiveStatement primitiveStmt = new CharPrimitiveStatement(tc, c);
tc.addStatement(primitiveStmt);
return primitiveStmt.getReturnValue();
}
use of org.evosuite.testcase.statements.numeric.CharPrimitiveStatement in project evosuite by EvoSuite.
the class FactoryTestSystemTest method testGetCandidatesForReuse.
@Test
public void testGetCandidatesForReuse() throws ClassNotFoundException, NoSuchFieldException, ConstructionFailedException, NoSuchMethodException {
TestFactory testFactory = TestFactory.getInstance();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName());
Properties.PRIMITIVE_REUSE_PROBABILITY = 1.0;
Properties.OBJECT_REUSE_PROBABILITY = 1.0;
DefaultTestCase test = new DefaultTestCase();
GenericConstructor constructor = new GenericConstructor(sut.getConstructor(), sut);
VariableReference var1 = testFactory.addConstructor(test, constructor, 0, 0);
test.addStatement(new CharPrimitiveStatement(test, '-'));
GenericMethod method = new GenericMethod(sut.getMethod("testInt", int.class), sut);
testFactory.addMethodFor(test, var1, method, 2);
MethodStatement stmt = (MethodStatement) test.getStatement(test.size() - 1);
VariableReference var = stmt.getParameterReferences().get(0);
assertNotEquals("Char should not be passed as Integer", var.getType(), char.class);
assertEquals("Incorrect test size", 4, test.size());
}
Aggregations