Documentation ¶
Overview ¶
Package testutil provides generic functions for testing and benchmarking with go test, as well as type-agnostic implementations of Equal, Any and All.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Test ¶
Test is a generic case-driven testing function that accepts a slice of cases, a numerical tolerance and either 1 or 2 functions to be tested. A sub-test is run for each case.
If 1 function is provided, then its output is tested against the outputs provided in each case.
If 2 functions are provided, then their respective outputs are compared, using the inputs provided in each case.
Types ¶
type Cases ¶
type Cases interface{}
Cases represents a generic data structure for table-driven testing. It is an alias for a slice of cases with the following structure:
[]struct { Label string In1 TIn1 ⋮ ⋮ InN TInN Out1 TOut1 ⋮ ⋮ OutM TInM }
where N and M are arbitrary.
For the common case of testing a numerical function of a single argument, this would take the simple form of:
[]struct { Label string In float64 Out float64 }{ {"SomeLabel", x, y}, {"AnotherLabel", x1, y1}, ... }
type EqualResult ¶ added in v0.1.7
type EqualResult struct { // Ok is true if x equals y. Ok bool // Numerical is true if x and y are numerical. Numerical bool // RelativeError is the error in x relative to y if they are numerical. // It is a complex number if x and y are complex numbers. RelativeError reflect.Value // AbsoluteError is the difference between x and y if they are numerical. // It is a complex number if x and y are complex numbers. AbsoluteError reflect.Value // Position is the the first "location" that x does not equal y // if x and y are structured data types. // // For slices and arrays it is the index of first element from x that does not equal y. // For structs it is the index of the first field for which x does not equal y. // For maps it is the index of the first key for which x does not equal y. // // If MissingValue is true, Position gives the index in y of the missing field or key. Position int // LengthMismatch is true if the number of elements, fields or keys in x // differs from y for structured data types. LengthMismatch bool // MissingValue is true if x and y are maps or structs and x is missing one of the keys // or fields in y. MissingValue bool }
EqualResult represents the result of an Equal comparison between arbitrary x and y.
func Equal ¶
func Equal(x, y, tolerance interface{}) EqualResult
Equal reports whether x (actual) is equal to y (expected).
For numerical types, x is equal to y if:
|x - y| < tolerance * |y|, for y ≠ 0 (relative error) |x| < tolerance, for y = 0 (absolute error)
For structured types (slice, array, struct, map), x equals y if every element/field/key of x equals that in y.
For func types, x equals y if x(args) equals y(args) for randomly generated args.
For other types x equals y if reflect.DeepEqual(x, y) is true.