cockroach: Index | Files | Directories

package reduce

import ""

Package reduce implements a reducer core for reducing the size of test failure cases.



Package Files

datadriven.go pass.go reduce.go

func RunTest Uses

func RunTest(
    t *testing.T,
    path string,
    filter func([]byte) ([]byte, error),
    interesting func(contains string) InterestingFn,
    mode Mode,
    passes []Pass,

RunTest executes reducer commands. If the optional filter func is not nil, it is invoked on all "reduce" commands before the reduction algorithm is started. In verbose mode, output is written to stderr. Supported commands:

"contains": Sets the substring that must be contained by an error message for a test case to be marked as interesting. This variable is passed to the `interesting` func.

"reduce": Creates an InterestingFn based on the current value of contains and executes the reducer. Outputs the reduced case.

func Walk Uses

func Walk(
    t *testing.T,
    path string,
    filter func([]byte) ([]byte, error),
    interesting func(contains string) InterestingFn,
    mode Mode,
    passes []Pass,

Walk walks path for datadriven files and calls RunTest on them.

type File Uses

type File string

File contains the contents of a file.

func Reduce Uses

func Reduce(
    logger io.Writer,
    originalTestCase File,
    isInteresting InterestingFn,
    numGoroutines int,
    mode Mode,
    passList ...Pass,
) (File, error)

Reduce executes the test case reduction algorithm. logger, if not nil, will log progress output. numGoroutines is the number of parallel workers, or 0 for runtime.NumCPU().

func (File) Size Uses

func (f File) Size() int

Size returns the size of the file in bytes.

type InterestingFn Uses

type InterestingFn func(context.Context, File) bool

InterestingFn returns true if File triggers the target test failure. It should be context-aware and stop work if the context is canceled.

type Mode Uses

type Mode int

Mode is an enum specifying how to determine if forward progress was made.

const (
    // ModeSize instructs Reduce to use filesize as the progress indicator.
    ModeSize Mode = iota
    // ModeInteresting instructs Reduce to use the interestingness as the
    // progress indicator. That is, if any pass generates an interesting
    // result (even if the file size increases), that is considered
    // progress.

type Pass Uses

type Pass interface {
    // New creates a new opaque state object for the input File.
    New(File) State
    // Transform applies this transformation pass to the input File using
    // State to determine which occurrence to transform. It returns the
    // transformed File, a Result indicating whether to proceed or not, and
    // an error if the transformation could not be performed.
    Transform(File, State) (File, Result, error)
    // Advance moves State to the next occurrence of a transformation in
    // the given input File and returns the new State.
    Advance(File, State) State
    // Name returns the name of the Pass.
    Name() string

Pass defines a reduce pass.

func MakeIntPass Uses

func MakeIntPass(name string, f func(s string, i int) (out string, ok bool, err error)) Pass

MakeIntPass returns a Pass with a state that starts at 0 and increments by 1 each Advance. f is a transformation function that takes the input and an index (zero-based) determining which occurrence to transform. It returns the possibly transformed output and a boolean that is false if a transformation could not be done because i was exhausted.

For example, if f is a func that replaces spaces with underscores, invoking that function with an i value of 2 should return the input string with only the 3rd occurrence of a space replaced with an underscore.

This is a convenience wrapper since a large number of Pass implementations just need their state to increment a counter and don't have to keep track of other things like byte offsets.

type Result Uses

type Result int

Result is returned by a Transform func.

const (
    // OK indicates there are more transforms in the current Pass.
    OK  Result = iota
    // STOP indicates there are no more transforms.

type State Uses

type State interface{}

State is opaque state for a Pass.



Package reduce imports 10 packages (graph) and is imported by 3 packages. Updated 2019-09-22. Refresh now. Tools for package owners.