All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.evosuite.testcase.localsearch.StatementLocalSearch Maven / Gradle / Ivy

/**
 * Copyright (C) 2010-2018 Gordon Fraser, Andrea Arcuri and EvoSuite
 * contributors
 *
 * This file is part of EvoSuite.
 *
 * EvoSuite is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3.0 of the License, or
 * (at your option) any later version.
 *
 * EvoSuite is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with EvoSuite. If not, see .
 */
package org.evosuite.testcase.localsearch;

import java.util.Set;

import org.evosuite.Properties;
import org.evosuite.Properties.DSEType;
import org.evosuite.ga.localsearch.LocalSearchObjective;
import org.evosuite.testcase.statements.Statement;
import org.evosuite.testcase.TestChromosome;
import org.evosuite.testcase.statements.ArrayStatement;
import org.evosuite.testcase.statements.ConstructorStatement;
import org.evosuite.testcase.statements.EnumPrimitiveStatement;
import org.evosuite.testcase.statements.FieldStatement;
import org.evosuite.testcase.statements.MethodStatement;
import org.evosuite.testcase.statements.NullStatement;
import org.evosuite.testcase.statements.PrimitiveStatement;
import org.evosuite.utils.Randomness;

public abstract class StatementLocalSearch {

    private TestChromosome backup = null;

    protected void backup(TestChromosome test) {
        backup = (TestChromosome) test.clone();
    }

    protected void restore(TestChromosome test) {
        if (backup == null)
            return;

        test.setTestCase(backup.getTestCase().clone());
        test.copyCachedResults(backup);
        //test.setFitness(backup.getFitness());
        test.setFitnessValues(backup.getFitnessValues());
        test.setPreviousFitnessValues(backup.getPreviousFitnessValues());
        test.setChanged(backup.isChanged());
    }


    /**
     * 

* doSearch *

* * @param test a {@link org.evosuite.testcase.TestChromosome} object. * @param statement a int. * @param objective a {@link org.evosuite.ga.localsearch.LocalSearchObjective} object. */ public abstract boolean doSearch(TestChromosome test, int statement, LocalSearchObjective objective); public boolean doSearch(TestChromosome test, Set statements, LocalSearchObjective objective) { boolean success = false; for (Integer statement : statements) { if (doSearch(test, statement, objective)) success = true; } return success; } /** * If the position of the statement on which the local search was performed * has changed, then we need to tell this to the outside world * * @return */ public int getPositionDelta() { return 0; } public static StatementLocalSearch getLocalSearchFor(Statement statement) { StatementLocalSearch search = null; if (statement instanceof NullStatement) { if (Properties.LOCAL_SEARCH_REFERENCES == false) return null; search = new ReferenceLocalSearch(); } else if (statement instanceof PrimitiveStatement) { Class type = statement.getReturnValue().getVariableClass(); if (type.equals(String.class)) { if (Properties.LOCAL_SEARCH_STRINGS) search = new StringAVMLocalSearch(); } else { if (Properties.LOCAL_SEARCH_PRIMITIVES == false) return null; if (type.equals(Integer.class) || type.equals(int.class)) { search = new IntegerLocalSearch(); } else if (type.equals(Byte.class) || type.equals(byte.class)) { search = new IntegerLocalSearch(); } else if (type.equals(Short.class) || type.equals(short.class)) { search = new IntegerLocalSearch(); } else if (type.equals(Long.class) || type.equals(long.class)) { search = new IntegerLocalSearch(); } else if (type.equals(Character.class) || type.equals(char.class)) { search = new IntegerLocalSearch(); } else if (type.equals(Float.class) || type.equals(float.class)) { search = new FloatLocalSearch(); } else if (type.equals(Double.class) || type.equals(double.class)) { search = new FloatLocalSearch(); } else if (type.equals(Boolean.class)) { search = new BooleanLocalSearch(); } else if (statement instanceof EnumPrimitiveStatement) { search = new EnumLocalSearch(); } } } else if (statement instanceof ArrayStatement) { if (Properties.LOCAL_SEARCH_ARRAYS == false) return null; search = new ArrayLocalSearch(); } else if (statement instanceof MethodStatement) { if (Properties.LOCAL_SEARCH_REFERENCES == false) return null; search = new ReferenceLocalSearch(); } else if (statement instanceof ConstructorStatement) { if (Properties.LOCAL_SEARCH_REFERENCES == false) return null; search = new ReferenceLocalSearch(); } else if (statement instanceof FieldStatement) { if (Properties.LOCAL_SEARCH_REFERENCES == false) return null; search = new ReferenceLocalSearch(); } return search; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy