Class ForwardGenerator


  • public class ForwardGenerator
    extends AbstractGenerator
    Randoop's forward, component-based generator.
    • Field Detail

      • sideEffectFreeMethods

        private final Set<TypedOperation> sideEffectFreeMethods
        The side-effect-free methods.
      • allsequencesAsCode

        private final List<String> allsequencesAsCode
        Set and used only if GenInputsAbstract.debug_checks==true. This contains the same components as allSequences, in the same order, but stores them as strings obtained via the toCodeString() method.
      • inputSequenceSelector

        private final InputSequenceSelector inputSequenceSelector
        How to select sequences as input for creating new sequences.
      • operationSelector

        private final TypedOperationSelector operationSelector
        How to select the method to use for creating a new sequence.
      • runtimePrimitivesSeen

        private Set<Object> runtimePrimitivesSeen
        The set of all primitive values seen during generation and execution of sequences. This set is used to tell if a new primitive value has been generated, to add the value to the components.

        Each value in the collection is a primitive wrapper or a String.

    • Constructor Detail

      • ForwardGenerator

        public ForwardGenerator​(List<TypedOperation> operations,
                                Set<TypedOperation> sideEffectFreeMethods,
                                GenInputsAbstract.Limits limits,
                                ComponentManager componentManager,
                                Set<ClassOrInterfaceType> classesUnderTest)
        Create a forward generator.
        Parameters:
        operations - list of operations under test
        sideEffectFreeMethods - side-effect-free methods
        limits - limits for generation, after which the generator will stop
        componentManager - stores previously-generated sequences
        classesUnderTest - set of classes under test
      • ForwardGenerator

        public ForwardGenerator​(List<TypedOperation> operations,
                                Set<TypedOperation> sideEffectFreeMethods,
                                GenInputsAbstract.Limits limits,
                                ComponentManager componentManager,
                                IStopper stopper,
                                Set<ClassOrInterfaceType> classesUnderTest)
        Create a forward generator.
        Parameters:
        operations - list of methods under test
        sideEffectFreeMethods - side-effect-free methods
        limits - limits for generation, after which the generator will stop
        componentManager - container for sequences that are used to generate new sequences
        stopper - determines when the test generation process should conclude. Can be null.
        classesUnderTest - the classes that are under test
    • Method Detail

      • newRegressionTestHook

        public void newRegressionTestHook​(Sequence sequence)
        Take action based on the given Sequence that was classified as a regression test.
        Specified by:
        newRegressionTestHook in class AbstractGenerator
        Parameters:
        sequence - the new sequence that was classified as a regression test
      • initializeRuntimePrimitivesSeen

        private void initializeRuntimePrimitivesSeen()
        The runtimePrimitivesSeen set contains primitive values seen during generation/execution and is used to determine new values that should be added to the component set. The component set initially contains a set of primitive sequences; this method puts those primitives in this set.
      • determineActiveIndices

        private void determineActiveIndices​(ExecutableSequence seq)
        Determines what indices in the given sequence are active. (Actually, sets some indices as not active, since the default is that every index is active.)

        An active index i means that the i-th method call creates an interesting/useful value that can be used as an input to a larger sequence; inactive indices are never used as inputs. The SequenceCollection to which the given sequences is added only considers the active indices when deciding whether the sequence creates values of a given type.

        In addition to determining active indices, this method determines if any primitive values created during execution of the sequence are new values not encountered before. Such values are added to the component manager so they can be used during subsequent generation attempts.

        Parameters:
        seq - the sequence, all of whose indices are initially marked as active
      • createNewUniqueSequence

        private ExecutableSequence createNewUniqueSequence()
        Tries to create a new sequence. If the sequence is new (not already in the specified component manager), then adds it to the manager's sequences.

        This method returns null if:

        • it selects an operation but cannot generate inputs for the operation
        • it creates too large a method
        • it creates a duplicate sequence
        This method modifies the list of operations that represent the set of methods under tests. Specifically, if the selected operation used for creating a new and unique sequence is a parameterless operation (a static constant method or no-argument constructor) it is removed from the list of operations. Such a method will return the same thing every time it is invoked (unless it's nondeterministic, but Randoop should not be run on nondeterministic methods). Once invoked, its result is in the pool and there is no need to call the operation again and so we will remove it from the list of operations.
        Returns:
        a new sequence, or null
      • repeat

        private Sequence repeat​(Sequence seq,
                                TypedOperation operation,
                                int times)
        Adds the given operation to a new Sequence with the statements of this object as a prefix, repeating the operation the given number of times. Used during generation.
        Parameters:
        seq - the sequence to extend
        operation - the TypedOperation to repeat
        times - the number of times to repeat the Operation
        Returns:
        a new Sequence
      • randoopConsistencyTest2

        private void randoopConsistencyTest2​(Sequence newSequence)
      • randoopConsistencyTests

        private void randoopConsistencyTests​(Sequence newSequence)
      • selectInputs

        private InputsAndSuccessFlag selectInputs​(TypedOperation operation)
        This method is responsible for doing two things:
        1. Selecting at random a collection of sequences that can be used to create input values for the given statement, and
        2. Selecting at random valid indices to the above sequence specifying the values to be used as input to the statement.

        The selected sequences and indices are wrapped in an InputsAndSuccessFlag object and returned. If an appropriate collection of sequences and indices was not found (e.g. because there are no sequences in the componentManager that create values of some type required by the statement), the success flag of the returned object is false.

        Parameters:
        operation - the statement to analyze
        Returns:
        the selected sequences and indices
      • randomVariable

        ForwardGenerator.VarAndSeq randomVariable​(SimpleList<Sequence> candidates,
                                                  Type inputType,
                                                  boolean isReceiver)
        Return a variable of the given type.
        Parameters:
        candidates - sequences, each of which produces a value of type inputType; that is, each would be a legal return value
        inputType - the type of the chosen variable/sequence
        isReceiver - whether the value will be used as a receiver
        Returns:
        a random variable of the given type, chosen from the candidates
      • numGeneratedSequences

        public int numGeneratedSequences()
        Description copied from class: AbstractGenerator
        Returns the count of sequences generated so far by the generator.
        Specified by:
        numGeneratedSequences in class AbstractGenerator
        Returns:
        the number of sequences generated