reduce

package
v0.0.0-...-1b6ad0c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2020 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

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

See: https://blog.regehr.org/archives/1678.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunTest

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

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.

Types

type File

type File string

File contains the contents of a file.

func Reduce

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

func (f File) Size() int

Size returns the size of the file in bytes.

type InterestingFn

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

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.
	ModeInteresting
)

type Pass

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

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

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.
	STOP
)

type State

type State interface{}

State is opaque state for a Pass.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL