testutil

package module
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2020 License: BSD-3-Clause Imports: 8 Imported by: 1

README


layout: default title: testutil

GoDoc Go Report Card Coverage Build Status

testutil

Package testutil provides generic functions for testing in Go. Please consult the GoDoc page for specific details and visit ScientificGo to see more scientific packages for Go.

Installation

go get scientificgo.org/testutil

Usage

import "scientificgo.org/testutil"

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 All

func All(f Func, xs ...interface{}) (ok bool, err error)

All returns true if the function f evaluates to true for all arguments in xs.

func Any

func Any(f Func, xs ...interface{}) (ok bool, err error)

Any returns true if the function f evaluates to true for any argument in xs.

func Test

func Test(t *testing.T, tolerance interface{}, cases Cases, funcs ...Func)

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.

type Func

type Func interface{}

Func represents an arbitrary function with a generic signature of N inputs and M outputs of any types, i.e.:

func Func(in1 In1, ..., inN InN) (out1 Out1, ..., outM OutM)

Jump to

Keyboard shortcuts

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