Search in sources :

Example 46 with SolverResult

use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.

the class TestIntegerSearch method testLTVariable.

@Test
public void testLTVariable() throws SolverEmptyQueryException {
    int var1 = 2;
    int var2 = 1;
    List<Constraint<?>> constraints = new ArrayList<Constraint<?>>();
    constraints.add(new IntegerConstraint(new IntegerVariable("test1", var1, -1000000, 1000000), Comparator.LT, new IntegerVariable("test2", var2, -1000000, 1000000)));
    try {
        EvoSuiteSolver solver = new EvoSuiteSolver();
        SolverResult solverResult = solver.solve(constraints);
        assertTrue(solverResult.isSAT());
        Map<String, Object> model = solverResult.getModel();
        if (model.containsKey("test1"))
            var1 = ((Number) model.get("test1")).intValue();
        if (model.containsKey("test2"))
            var2 = ((Number) model.get("test2")).intValue();
        assertTrue(var1 < var2);
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) ArrayList(java.util.ArrayList) SolverResult(org.evosuite.symbolic.solver.SolverResult) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) IntegerVariable(org.evosuite.symbolic.expr.bv.IntegerVariable) Test(org.junit.Test)

Example 47 with SolverResult

use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.

the class TestRealConstraint method test.

@Test
public void test() throws SolverEmptyQueryException {
    // 5000000000000L; TODO - ??
    Properties.LOCAL_SEARCH_BUDGET = 100;
    Properties.LOCAL_SEARCH_BUDGET_TYPE = LocalSearchBudgetType.FITNESS_EVALUATIONS;
    Collection<Constraint<?>> constraints = buildConstraintSystem();
    System.out.println("Constraints:");
    for (Constraint<?> c : constraints) {
        System.out.println(c.toString());
    }
    System.out.println("");
    System.out.println("Initial: " + String.valueOf(INIT_DOUBLE));
    EvoSuiteSolver seeker = new EvoSuiteSolver();
    try {
        SolverResult result = seeker.solve(constraints);
        if (result.isUNSAT()) {
            fail("search was unsuccessfull");
        } else {
            Map<String, Object> model = result.getModel();
            Object var0 = model.get("var0");
            System.out.println("Expected: " + EXPECTED_DOUBLE);
            System.out.println("Found: " + var0);
            assertEquals(EXPECTED_DOUBLE, var0);
        }
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) Constraint(org.evosuite.symbolic.expr.Constraint) RealConstraint(org.evosuite.symbolic.expr.RealConstraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) SolverResult(org.evosuite.symbolic.solver.SolverResult) Test(org.junit.Test)

Example 48 with SolverResult

use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.

the class TestConstraintSolver1 method test.

@Test
public void test() throws SolverEmptyQueryException {
    // 5000000000000L; TODO - ??
    Properties.LOCAL_SEARCH_BUDGET = 100;
    Properties.LOCAL_SEARCH_BUDGET_TYPE = LocalSearchBudgetType.FITNESS_EVALUATIONS;
    Collection<Constraint<?>> constraints = buildConstraintSystem();
    System.out.println("Constraints:");
    for (Constraint<?> c : constraints) {
        System.out.println(c.toString());
    }
    EvoSuiteSolver seeker = new EvoSuiteSolver();
    try {
        SolverResult solverResult = seeker.solve(constraints);
        assertTrue(solverResult.isSAT());
        Map<String, Object> model = solverResult.getModel();
        System.out.println(model);
        Object var0 = model.get("var0");
        System.out.println("Expected: " + EXPECTED_STRING);
        System.out.println("Found: " + var0);
        assertEquals(EXPECTED_STRING, var0);
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) SolverResult(org.evosuite.symbolic.solver.SolverResult) Test(org.junit.Test)

Example 49 with SolverResult

use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.

the class TestConstraintSolver3 method test.

@Test
public void test() throws SolverEmptyQueryException {
    // 5000000000000L; TODO - ??
    Properties.LOCAL_SEARCH_BUDGET = 100;
    Properties.LOCAL_SEARCH_BUDGET_TYPE = LocalSearchBudgetType.FITNESS_EVALUATIONS;
    Collection<Constraint<?>> constraints = buildConstraintSystem();
    System.out.println("Constraints:");
    for (Constraint<?> c : constraints) {
        System.out.println(c.toString());
    }
    System.out.println("");
    System.out.println("Initial: " + INIT_STRING);
    EvoSuiteSolver solver = new EvoSuiteSolver();
    try {
        SolverResult result = solver.solve(constraints);
        if (result.isUNSAT()) {
            fail("search was unsuccessfull");
        } else {
            Map<String, Object> model = result.getModel();
            Object var0 = model.get("var0");
            System.out.println("Expected: " + EXPECTED_INTEGER);
            System.out.println("Found: " + var0);
            assertEquals(String.valueOf(EXPECTED_INTEGER), var0);
        }
    } catch (SolverTimeoutException e) {
        fail();
    }
}
Also used : SolverTimeoutException(org.evosuite.symbolic.solver.SolverTimeoutException) IntegerConstraint(org.evosuite.symbolic.expr.IntegerConstraint) Constraint(org.evosuite.symbolic.expr.Constraint) EvoSuiteSolver(org.evosuite.symbolic.solver.avm.EvoSuiteSolver) SolverResult(org.evosuite.symbolic.solver.SolverResult) Test(org.junit.Test)

Example 50 with SolverResult

use of org.evosuite.symbolic.solver.SolverResult in project evosuite by EvoSuite.

the class DSETestGenerator method generateNewTest.

/**
 * Applies DSE to the passed test using as symbolic variables only those
 * that are declared in the set of statement indexes. The objective is used
 * to detect if the DSE has improved the fitness.
 *
 * @param test
 *            the test case to be used as parameterised unit test
 *
 * @param statementIndexes
 *            a set with statement indexes with primitive value declarations
 *            that can be used as symbolic variables. This set must be
 *            non-empty.
 *
 * @param objective
 *            the local search objective to measure fitness improvement.
 */
public TestChromosome generateNewTest(final TestChromosome test, Set<Integer> statementIndexes, LocalSearchObjective<TestChromosome> objective) {
    logger.info("APPLYING DSE EEEEEEEEEEEEEEEEEEEEEEE");
    logger.info(test.getTestCase().toCode());
    logger.info("Starting concolic execution");
    // Backup copy
    // test.getMutationHistory().clear();
    // I am not sure what is the purpose of this
    test.clone();
    DefaultTestCase clone_test_case = (DefaultTestCase) test.getTestCase().clone();
    List<BranchCondition> branchConditions = ConcolicExecution.executeConcolic(clone_test_case);
    final PathCondition collectedPathCondition = new PathCondition(branchConditions);
    logger.info("Done concolic execution");
    if (collectedPathCondition.isEmpty()) {
        return null;
    }
    for (BranchCondition c : collectedPathCondition.getBranchConditions()) {
        logger.info(" -> " + c.getConstraint());
    }
    Set<VariableReference> symbolicVariables = new HashSet<VariableReference>();
    for (Integer position : statementIndexes) {
        final VariableReference variableReference = test.getTestCase().getStatement(position).getReturnValue();
        symbolicVariables.add(variableReference);
    }
    logger.info("Checking {} conditions", collectedPathCondition.size());
    List<Integer> conditionIndexesNotCoveredTwoWays = computeConditionIndexesNotCoveredTwoWays(test, collectedPathCondition);
    // 
    for (int conditionIndex = 0; conditionIndex < collectedPathCondition.size(); conditionIndex++) {
        BranchCondition condition = collectedPathCondition.get(conditionIndex);
        if (LocalSearchBudget.getInstance().isFinished()) {
            logger.debug("Local search budget used up: " + Properties.LOCAL_SEARCH_BUDGET_TYPE);
            break;
        }
        logger.debug("Local search budget not yet used up");
        if (!conditionIndexesNotCoveredTwoWays.contains(conditionIndex)) {
            // skip branches covered two ways
            continue;
        }
        logger.info("Current condition: " + conditionIndex + "/" + collectedPathCondition.size() + ": " + condition.getConstraint());
        // Determine if this a branch condition depending on the target
        // statement
        Constraint<?> currentConstraint = condition.getConstraint();
        if (!isRelevant(currentConstraint, symbolicVariables)) {
            // if(!isRelevant(currentConstraint, test.getTestCase(),
            // statement)) {
            logger.info("Is not relevant for " + symbolicVariables);
            continue;
        }
        logger.info("Is relevant for " + symbolicVariables);
        List<Constraint<?>> query = buildQuery(collectedPathCondition, conditionIndex);
        logger.info("Trying to solve: ");
        for (Constraint<?> c : query) {
            logger.info("  " + c);
        }
        DSEStats.getInstance().reportNewConstraints(query);
        // Get solution
        Solver solver = SolverFactory.getInstance().buildNewSolver();
        long startSolvingTime = System.currentTimeMillis();
        SolverCache solverCache = SolverCache.getInstance();
        SolverResult solverResult = solverCache.solve(solver, query);
        long estimatedSolvingTime = System.currentTimeMillis() - startSolvingTime;
        DSEStats.getInstance().reportNewSolvingTime(estimatedSolvingTime);
        if (solverResult == null) {
            logger.info("Found no result");
        } else if (solverResult.isUNSAT()) {
            logger.info("Found UNSAT result");
            DSEStats.getInstance().reportNewUNSAT();
        } else {
            logger.info("Found SAT result");
            DSEStats.getInstance().reportNewSAT();
            Map<String, Object> model = solverResult.getModel();
            TestCase oldTest = test.getTestCase();
            ExecutionResult oldResult = test.getLastExecutionResult().clone();
            TestCase newTest = updateTest(oldTest, model);
            logger.info("New test: " + newTest.toCode());
            test.setTestCase(newTest);
            // test.clearCachedMutationResults(); // TODO Mutation
            test.clearCachedResults();
            if (objective.hasImproved(test)) {
                DSEStats.getInstance().reportNewTestUseful();
                logger.info("Solution improves fitness, finishing DSE");
                /* new test was created */
                return test;
            } else {
                DSEStats.getInstance().reportNewTestUnuseful();
                test.setTestCase(oldTest);
                // FIXXME: How can this be null?
                if (oldResult != null)
                    test.setLastExecutionResult(oldResult);
            // TODO Mutation
            }
        }
    }
    /* no new test was created */
    return null;
}
Also used : PathCondition(org.evosuite.symbolic.PathCondition) Solver(org.evosuite.symbolic.solver.Solver) VariableReference(org.evosuite.testcase.variable.VariableReference) Constraint(org.evosuite.symbolic.expr.Constraint) SolverResult(org.evosuite.symbolic.solver.SolverResult) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) BranchCondition(org.evosuite.symbolic.BranchCondition) SolverCache(org.evosuite.symbolic.solver.SolverCache) Constraint(org.evosuite.symbolic.expr.Constraint) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

SolverResult (org.evosuite.symbolic.solver.SolverResult)50 Test (org.junit.Test)37 Constraint (org.evosuite.symbolic.expr.Constraint)36 SolverTimeoutException (org.evosuite.symbolic.solver.SolverTimeoutException)34 EvoSuiteSolver (org.evosuite.symbolic.solver.avm.EvoSuiteSolver)31 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)27 IntegerVariable (org.evosuite.symbolic.expr.bv.IntegerVariable)26 ArrayList (java.util.ArrayList)24 IntegerConstant (org.evosuite.symbolic.expr.bv.IntegerConstant)14 IntegerBinaryExpression (org.evosuite.symbolic.expr.bv.IntegerBinaryExpression)7 HashMap (java.util.HashMap)6 Variable (org.evosuite.symbolic.expr.Variable)4 StringVariable (org.evosuite.symbolic.expr.str.StringVariable)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HashSet (java.util.HashSet)3 LinkedList (java.util.LinkedList)3 Solver (org.evosuite.symbolic.solver.Solver)3 SolverCache (org.evosuite.symbolic.solver.SolverCache)3 SolverEmptyQueryException (org.evosuite.symbolic.solver.SolverEmptyQueryException)3 SolverErrorException (org.evosuite.symbolic.solver.SolverErrorException)3