Search in sources :

Example 1 with BooleanPrimitiveStatement

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");
    }
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 2 with BooleanPrimitiveStatement

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;
    }
}
Also used : BytePrimitiveStatement(org.evosuite.testcase.statements.numeric.BytePrimitiveStatement) Solver(org.evosuite.symbolic.solver.Solver) Constraint(org.evosuite.symbolic.expr.Constraint) SolverResult(org.evosuite.symbolic.solver.SolverResult) SolverCache(org.evosuite.symbolic.solver.SolverCache) LinkedList(java.util.LinkedList) Constraint(org.evosuite.symbolic.expr.Constraint) CharPrimitiveStatement(org.evosuite.testcase.statements.numeric.CharPrimitiveStatement) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) TestCase(org.evosuite.testcase.TestCase) ShortPrimitiveStatement(org.evosuite.testcase.statements.numeric.ShortPrimitiveStatement) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) LongPrimitiveStatement(org.evosuite.testcase.statements.numeric.LongPrimitiveStatement)

Example 3 with BooleanPrimitiveStatement

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());
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Type(java.lang.reflect.Type) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) Test(org.junit.Test)

Example 4 with BooleanPrimitiveStatement

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);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) ClassWithField(com.examples.with.different.packagename.coverage.ClassWithField) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 5 with BooleanPrimitiveStatement

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();
}
Also used : BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement)

Aggregations

BooleanPrimitiveStatement (org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement)5 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)3 VariableReference (org.evosuite.testcase.variable.VariableReference)3 GenericMethod (org.evosuite.utils.generic.GenericMethod)3 Method (java.lang.reflect.Method)2 TestCase (org.evosuite.testcase.TestCase)2 MethodStatement (org.evosuite.testcase.statements.MethodStatement)2 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)2 Test (org.junit.Test)2 ClassWithField (com.examples.with.different.packagename.coverage.ClassWithField)1 Type (java.lang.reflect.Type)1 LinkedList (java.util.LinkedList)1 FitnessFunction (org.evosuite.ga.FitnessFunction)1 Constraint (org.evosuite.symbolic.expr.Constraint)1 Solver (org.evosuite.symbolic.solver.Solver)1 SolverCache (org.evosuite.symbolic.solver.SolverCache)1 SolverResult (org.evosuite.symbolic.solver.SolverResult)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)1 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)1